Repository navigation
Handle the SCOPE node of a block like an ITER node - #87
Merged
Merged
Conversation
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>
mame
approved these changes
Nov 17, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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_highlightgiven thaterror_highlightusesThread::Backtrace::Location#syntax_treeand not{Proc,Method,UnboundMethod}#syntax_tree.However, what happens is
Thread::Backtrace::Location#syntax_treewas already inconsistent between Prism and parse.y on code like:So Prism returned the BlockNode, as expected, since in fact the
savecall is not on the execution stack (onlyProc#call+ the block is on the stack).And
parse.yreturns theITERnode, of thesavecall 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:
{Proc,Method,UnboundMethod}#syntax_treeThread::Backtrace::Location#syntax_tree(was already BlockNode in Prism, no change there)error_highlightwas 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 :)