Skip to content

Commit eb7a3a2

Browse files
eregonclaude
authored andcommitted
Handle the SCOPE node of a block like an ITER node
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>
1 parent ae50da6 commit eb7a3a2

1 file changed

Lines changed: 2 additions & 2 deletions

File tree

‎lib/error_highlight/base.rb‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ def spot
270270
return nil if @point_type != :name
271271
spot_lambda
272272

273-
when :ITER
273+
when :ITER, :SCOPE
274274
return nil if @point_type != :name
275275
spot_iter
276276

@@ -727,7 +727,7 @@ def spot_lambda
727727
# define_method :foo do
728728
# ^^
729729
def spot_iter
730-
_nd_fcall, nd_scope = @node.children
730+
nd_scope = @node.type == :SCOPE ? @node : @node.children[1]
731731
fetch_line(nd_scope.first_lineno)
732732
if @snippet.match(/\G(?:do\b|\{)/, nd_scope.first_column)
733733
@beg_column = $~.begin(0)

0 commit comments

Comments
 (0)