From a07bac07317e90e2552696d5be61c7c3ffce1597 Mon Sep 17 00:00:00 2001 From: Benoit Daloze Date: Sat, 14 Nov 2026 21:28:38 +0100 Subject: [PATCH] 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 --- lib/error_highlight/base.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/error_highlight/base.rb b/lib/error_highlight/base.rb index 3ad4530..8f1b549 100644 --- a/lib/error_highlight/base.rb +++ b/lib/error_highlight/base.rb @@ -270,7 +270,7 @@ def spot return nil if @point_type != :name spot_lambda - when :ITER + when :ITER, :SCOPE return nil if @point_type != :name spot_iter @@ -727,7 +727,7 @@ def spot_lambda # define_method :foo do # ^^ def spot_iter - _nd_fcall, nd_scope = @node.children + nd_scope = @node.type == :SCOPE ? @node : @node.children[1] fetch_line(nd_scope.first_lineno) if @snippet.match(/\G(?:do\b|\{)/, nd_scope.first_column) @beg_column = $~.begin(0)