[Bug #20457] Do not remove final `return` node

This was an optimization for versions prior to 1.9 that traverse the
AST at runtime.
This commit is contained in:
Nobuyoshi Nakada 2024-04-26 16:52:19 +09:00
Родитель 22f98bb7ca
Коммит 250fc1223c
4 изменённых файлов: 41 добавлений и 7 удалений

Просмотреть файл

@ -14045,10 +14045,6 @@ reduce_nodes(struct parser_params *p, NODE **body)
case NODE_NIL:
*body = 0;
return;
case NODE_RETURN:
*body = node = RNODE_RETURN(node)->nd_stts;
if (newline && node) nd_set_fl_newline(node);
continue;
case NODE_BEGIN:
*body = node = RNODE_BEGIN(node)->nd_body;
if (newline && node) nd_set_fl_newline(node);

Просмотреть файл

@ -67,6 +67,7 @@ describe 'TracePoint#inspect' do
end
trace_point_spec_test_return
end
ruby_version_is("3.4") { line -= 1 }
inspect.should =~ /\A#<TracePoint:return [`']trace_point_spec_test_return'#{@path_prefix}#{__FILE__}:#{line}>\z/
end

Просмотреть файл

@ -708,6 +708,37 @@ class TestAst < Test::Unit::TestCase
assert_equal(:a, args.children[rest])
end
def test_return
assert_ast_eqaul(<<~STR, <<~EXP)
def m(a)
return a
end
STR
(SCOPE@1:0-3:3
tbl: []
args: nil
body:
(DEFN@1:0-3:3
mid: :m
body:
(SCOPE@1:0-3:3
tbl: [:a]
args:
(ARGS@1:6-1:7
pre_num: 1
pre_init: nil
opt: nil
first_post: nil
post_num: 0
post_init: nil
rest: nil
kw: nil
kwrest: nil
block: nil)
body: (RETURN@2:2-2:10 (LVAR@2:9-2:10 :a)))))
EXP
end
def test_keep_script_lines_for_parse
node = RubyVM::AbstractSyntaxTree.parse(<<~END, keep_script_lines: true)
1.times do
@ -1271,9 +1302,13 @@ dummy
end
def assert_error_tolerant(src, expected, keep_tokens: false)
assert_ast_eqaul(src, expected, error_tolerant: true, keep_tokens: keep_tokens)
end
def assert_ast_eqaul(src, expected, **options)
begin
verbose_bak, $VERBOSE = $VERBOSE, false
node = RubyVM::AbstractSyntaxTree.parse(src, error_tolerant: true, keep_tokens: keep_tokens)
node = RubyVM::AbstractSyntaxTree.parse(src, **options)
ensure
$VERBOSE = verbose_bak
end

Просмотреть файл

@ -232,7 +232,9 @@ class TestSetTraceFunc < Test::Unit::TestCase
events.shift)
assert_equal(["line", 5, :meth_return, self.class],
events.shift)
assert_equal(["return", 7, :meth_return, self.class],
assert_equal(["line", 6, :meth_return, self.class],
events.shift)
assert_equal(["return", 6, :meth_return, self.class],
events.shift)
assert_equal(["line", 10, :test_return, self.class],
events.shift)
@ -271,7 +273,7 @@ class TestSetTraceFunc < Test::Unit::TestCase
events.shift)
assert_equal(["line", 6, :meth_return2, self.class],
events.shift)
assert_equal(["return", 7, :meth_return2, self.class],
assert_equal(["return", 6, :meth_return2, self.class],
events.shift)
assert_equal(["line", 9, :test_return2, self.class],
events.shift)