Skip to content

Commit 48ac726

Browse files
nobuk0kubun
authored andcommitted
[Bug #22124] Add block to the proper node
1 parent e4c52b4 commit 48ac726

2 files changed

Lines changed: 39 additions & 7 deletions

File tree

parse.y

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1427,6 +1427,7 @@ static NODE *call_uni_op(struct parser_params*,NODE*,ID,const YYLTYPE*,const YYL
14271427
static NODE *new_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, const YYLTYPE *op_loc, const YYLTYPE *loc);
14281428
static NODE *new_command_qcall(struct parser_params* p, ID atype, NODE *recv, ID mid, NODE *args, NODE *block, const YYLTYPE *op_loc, const YYLTYPE *loc);
14291429
static NODE *method_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc) {RNODE_ITER(b)->nd_iter = m; b->nd_loc = *loc; return b;}
1430+
static NODE *command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc);
14301431

14311432
static bool args_info_empty_p(struct rb_args_info *args);
14321433
static rb_node_args_t *new_args(struct parser_params*,rb_node_args_aux_t*,rb_node_opt_arg_t*,ID,rb_node_args_aux_t*,rb_node_args_t*,const YYLTYPE*);
@@ -5205,13 +5206,7 @@ do_block : k_do_block do_body k_end
52055206

52065207
block_call : command do_block
52075208
{
5208-
if (nd_type_p($1, NODE_YIELD)) {
5209-
compile_error(p, "block given to yield");
5210-
}
5211-
else {
5212-
block_dup_check(p, get_nd_args(p, $1), $2);
5213-
}
5214-
$$ = method_add_block(p, $1, $2, &@$);
5209+
$$ = command_add_block(p, $1, $2, &@$);
52155210
fixpos($$, $1);
52165211
/*% ripper: method_add_block!($:1, $:2) %*/
52175212
}
@@ -12869,6 +12864,39 @@ new_locations_lambda_body(struct parser_params* p, NODE *node, const YYLTYPE *lo
1286912864
return body;
1287012865
}
1287112866

12867+
static NODE *
12868+
command_add_block(struct parser_params*p, NODE *m, NODE *b, const YYLTYPE *loc)
12869+
{
12870+
NODE **body;
12871+
enum node_type type = nd_type(m);
12872+
switch (type) {
12873+
case NODE_YIELD:
12874+
compile_error(p, "block given to yield");
12875+
return m;
12876+
case NODE_RETURN:
12877+
body = &RNODE_RETURN(m)->nd_stts;
12878+
break;
12879+
case NODE_BREAK:
12880+
case NODE_NEXT:
12881+
body = &RNODE_EXITS(m)->nd_stts;
12882+
break;
12883+
case NODE_REDO: /* `redo` has no argument */
12884+
compile_error(p, "command_add_block: unexpected node: NODE_REDO");
12885+
return m;
12886+
case NODE_RETRY: /* `retry` has no argument */
12887+
compile_error(p, "command_add_block: unexpected node: NODE_RETRY");
12888+
return m;
12889+
default:
12890+
block_dup_check(p, get_nd_args(p, m), b);
12891+
return method_add_block(p, m, b, loc);
12892+
}
12893+
RUBY_ASSERT(*body, "no argument %s with do_block", parser_node_name(type));
12894+
YYLTYPE b_loc = code_loc_gen(&(*body)->nd_loc, loc);
12895+
*body = command_add_block(p, *body, b, &b_loc);
12896+
m->nd_loc.end_pos = loc->end_pos;
12897+
return m;
12898+
}
12899+
1287212900
#define nd_once_body(node) (nd_type_p((node), NODE_ONCE) ? RNODE_ONCE(node)->nd_body : node)
1287312901

1287412902
static NODE*

test/ruby/test_parse.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1520,6 +1520,10 @@ def test_void_value_in_rhs
15201520
"x = begin return ensure return end",
15211521
"x = begin return; rescue; return end",
15221522
"x = begin return; rescue; return; else return end",
1523+
"x = return",
1524+
"x = return a",
1525+
"x = return a do end",
1526+
"x = return a b do end",
15231527
].each do |code|
15241528
ex = assert_syntax_error(code, w)
15251529
assert_equal(1, ex.message.scan(w).size, ->{"same #{w.inspect} warning should be just once\n#{w.message}"})

0 commit comments

Comments
 (0)