Skip to content

Commit ed8fe53

Browse files
committed
Allow to get a NODE_SCOPE node of dummy stack frame of ArgumentError
Previously, it was not possible to obtain a node of the callee's `Thread::Backtrace::Location` for cases like "wrong number of arguments" by using `RubyVM::AST.of`. This change allows that retrieval. This is preparation for [Feature #21543].
1 parent 2ccb2de commit ed8fe53

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

test/ruby/test_ast.rb

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,50 @@ def test_node_id_for_location
365365
assert_equal node.node_id, node_id
366366
end
367367

368+
def add(x, y)
369+
end
370+
371+
def test_node_id_for_backtrace_location_of_method_definition
372+
omit if ParserSupport.prism_enabled?
373+
374+
begin
375+
add(1)
376+
rescue ArgumentError => exc
377+
loc = exc.backtrace_locations.first
378+
node_id = RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(loc)
379+
node = RubyVM::AbstractSyntaxTree.of(method(:add))
380+
assert_equal node.node_id, node_id
381+
end
382+
end
383+
384+
def test_node_id_for_backtrace_location_of_lambda
385+
omit if ParserSupport.prism_enabled?
386+
387+
v = -> {}
388+
begin
389+
v.call(1)
390+
rescue ArgumentError => exc
391+
loc = exc.backtrace_locations.first
392+
node_id = RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(loc)
393+
node = RubyVM::AbstractSyntaxTree.of(v)
394+
assert_equal node.node_id, node_id
395+
end
396+
end
397+
398+
def test_node_id_for_backtrace_location_of_lambda_method
399+
omit if ParserSupport.prism_enabled?
400+
401+
v = lambda {}
402+
begin
403+
v.call(1)
404+
rescue ArgumentError => exc
405+
loc = exc.backtrace_locations.first
406+
node_id = RubyVM::AbstractSyntaxTree.node_id_for_backtrace_location(loc)
407+
node = RubyVM::AbstractSyntaxTree.of(v)
408+
assert_equal node.node_id, node_id
409+
end
410+
end
411+
368412
def test_node_id_for_backtrace_location_raises_argument_error
369413
bug19262 = '[ruby-core:111435]'
370414

vm_backtrace.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ calc_pos(const rb_iseq_t *iseq, const VALUE *pc, int *lineno, int *node_id)
4444
}
4545
if (lineno) *lineno = ISEQ_BODY(iseq)->location.first_lineno;
4646
#ifdef USE_ISEQ_NODE_ID
47-
if (node_id) *node_id = -1;
47+
if (node_id) *node_id = ISEQ_BODY(iseq)->location.node_id;
4848
#endif
4949
return 1;
5050
}
@@ -400,7 +400,7 @@ location_path_m(VALUE self)
400400
static int
401401
location_node_id(rb_backtrace_location_t *loc)
402402
{
403-
if (loc->iseq && loc->pc) {
403+
if (loc->iseq) {
404404
return calc_node_id(loc->iseq, loc->pc);
405405
}
406406
return -1;

0 commit comments

Comments
 (0)