From 64ac984129a7a4645efe5ac57c168ef880b479b2 Mon Sep 17 00:00:00 2001 From: Jeremy Evans Date: Fri, 21 May 2021 11:01:06 -0700 Subject: Make RubyVM::AbstractSyntaxTree.of raise for method/proc created in eval This changes Thread::Location::Backtrace#absolute_path to return nil for methods/procs defined in eval. If the realpath of an iseq is nil, that indicates it was defined in eval, in which case you cannot use RubyVM::AbstractSyntaxTree.of. Fixes [Bug #16983] Co-authored-by: Koichi Sasada --- test/ruby/test_ast.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'test/ruby') diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb index 5a229eabd4..3b4cbc7dee 100644 --- a/test/ruby/test_ast.rb +++ b/test/ruby/test_ast.rb @@ -211,6 +211,26 @@ class TestAst < Test::Unit::TestCase end end + def test_of_eval + method = self.method(eval("def example_method_#{$$}; end")) + assert_raise(ArgumentError) { RubyVM::AbstractSyntaxTree.of(method) } + + method = self.method(eval("def self.example_singleton_method_#{$$}; end")) + assert_raise(ArgumentError) { RubyVM::AbstractSyntaxTree.of(method) } + + method = eval("proc{}") + assert_raise(ArgumentError) { RubyVM::AbstractSyntaxTree.of(method) } + + method = self.method(eval("singleton_class.define_method(:example_define_method_#{$$}){}")) + assert_raise(ArgumentError) { RubyVM::AbstractSyntaxTree.of(method) } + + method = self.method(eval("define_singleton_method(:example_dsm_#{$$}){}")) + assert_raise(ArgumentError) { RubyVM::AbstractSyntaxTree.of(method) } + + method = eval("Class.new{def example_method; end}.instance_method(:example_method)") + assert_raise(ArgumentError) { RubyVM::AbstractSyntaxTree.of(method) } + end + def test_scope_local_variables node = RubyVM::AbstractSyntaxTree.parse("_x = 0") lv, _, body = *node.children -- cgit v1.2.3