Skip to content

Handle the SCOPE node of a block like an ITER node - #87

Merged
mame merged 1 commit into
ruby:masterfrom
eregon:scope-node
Nov 17, 2026
Merged

mame merged 1 commit into
ruby:masterfrom
eregon:scope-node

Conversation

@eregon

@eregon eregon commented Nov 16, 2026 •

Copy link
Copy Markdown
Member

For https://bugs.ruby-lang.org/issues/22520 I need this small change in error_highlight (otherwise the CI fails in ruby/ruby in ruby/ruby#19800).

I actually didn't expect I would need a change in error_highlight given that error_highlight uses Thread::Backtrace::Location#syntax_tree and not {Proc,Method,UnboundMethod}#syntax_tree.
However, what happens is Thread::Backtrace::Location#syntax_tree was already inconsistent between Prism and parse.y on code like:

def save(&b)
  $b = b
end

save { |k:| k }

begin
  $b.call
rescue => e
  p e.backtrace_locations.first.syntax_tree
end
$ ruby -v
ruby 4.1.0dev (2026-11-05T20:37:41Z master 2a3b7678ff) +PRISM [x86_64-linux]
$ ruby --parser=parse.y inconsistent.rb                                                                                                             
#<RubyVM::AbstractSyntaxTree::Node:ITER@5:0-5:15>
$ ruby inconsistent.rb                 
@ BlockNode (location: (5,5)-(5,15))
...

So Prism returned the BlockNode, as expected, since in fact the save call is not on the execution stack (only Proc#call + the block is on the stack).
And parse.y returns the ITER node, of the save call not on the execution stack, which at least I find unexpected and is inconsistent.
So the fix for https://bugs.ruby-lang.org/issues/22520 naturally fixes both cases at once:

  • BlockNode/SCOPE for {Proc,Method,UnboundMethod}#syntax_tree
  • SCOPE for Thread::Backtrace::Location#syntax_tree (was already BlockNode in Prism, no change there)

error_highlight was anyway already getting the SCOPE node out of the ITER node, so the fix is very small and just needs to allow receiving a SCOPE node directly.

Claude has the same explanation in the commit message but I hope this PR description is easier to read and understand :)

For an ArgumentError raised when calling a lambda or a method defined
with define_method, the backtrace location of the callee is the block
frame itself, so the node of that location is the node of the block iseq.

Ruby 4.1 changes RubyVM::InstructionSequence#syntax_tree for a block iseq
to return the node that is compiled into the iseq: the Ruby::Prism::BlockNode
with prism, or the SCOPE node with parse.y, instead of the method call the
block is passed to [Feature #21795]. Thread::Backtrace::Location#syntax_tree
builds on that method, so for such a location it now returns the SCOPE
node under parse.y, where it returned the ITER node before (with prism it
already returned the BlockNode). Without this change, the callee snippet
pointing at the opening `{` or `do` is not displayed anymore under parse.y:

    wrong number of arguments (given 1, expected 0) (ArgumentError)

        caller: test.rb:3
        |       v.call(1)
                 ^^^^^
        callee: test.rb:1
        |     v = lambda { }
                         ^

The ITER node is the method call with its block, and its second child is
the SCOPE node of the block, so a SCOPE node can be spotted exactly like
an ITER node, by looking at the SCOPE node directly. The ITER case is
kept for older Rubies, where RubyVM::AbstractSyntaxTree.of is used.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
@eregon
eregon requested a review from mame November 16, 2026 20:22
@mame
mame merged commit eb7a3a2 into ruby:master Nov 17, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

⌂ Proxy