Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions parse.y
Original file line number Diff line number Diff line change
Expand Up @@ -14298,16 +14298,12 @@ reduce_nodes(struct parser_params *p, NODE **body)
(reduce_nodes(p, &type(node)->n1), body = &type(node)->n2, 1))

while (node) {
int newline = (int)(nd_fl_newline(node));
int newline = (int)nd_fl_newline(node);
switch (nd_type(node)) {
end:
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);
Expand Down
6 changes: 3 additions & 3 deletions rubyparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -1197,9 +1197,9 @@ typedef struct RNode_ERROR {
#define NODE_TYPESHIFT 8
#define NODE_TYPEMASK (((VALUE)0x7f)<<NODE_TYPESHIFT)

#define nd_fl_newline(n) (n)->flags & NODE_FL_NEWLINE
#define nd_set_fl_newline(n) (n)->flags |= NODE_FL_NEWLINE
#define nd_unset_fl_newline(n) (n)->flags &= ~NODE_FL_NEWLINE
#define nd_fl_newline(n) ((n)->flags & NODE_FL_NEWLINE)
#define nd_set_fl_newline(n) ((n)->flags |= NODE_FL_NEWLINE)
#define nd_unset_fl_newline(n) ((n)->flags &= ~NODE_FL_NEWLINE)

#define nd_type(n) ((int) ((RNODE(n)->flags & NODE_TYPEMASK)>>NODE_TYPESHIFT))
#define nd_set_type(n,t) \
Expand Down
1 change: 1 addition & 0 deletions spec/ruby/core/tracepoint/inspect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def trace_point_spec_test_return
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
Expand Down
37 changes: 36 additions & 1 deletion test/ruby/test_ast.rb
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,37 @@ def test_args
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
Expand Down Expand Up @@ -1239,9 +1270,13 @@ def test_unused_block_local_variable
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
Expand Down
6 changes: 4 additions & 2 deletions test/ruby/test_settracefunc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ def test_return # [ruby-dev:38701]
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)
Expand Down Expand Up @@ -271,7 +273,7 @@ def test_return2 # [ruby-core:24463]
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)
Expand Down