diff --git a/compile.c b/compile.c index 8f53955e74dbe5..f252339ee5709c 100644 --- a/compile.c +++ b/compile.c @@ -1832,6 +1832,7 @@ iseq_set_arguments(rb_iseq_t *iseq, LINK_ANCHOR *const optargs, const NODE *cons EXPECT_NODE("iseq_set_arguments", node_args, NODE_ARGS, COMPILE_NG); + body->param.flags.ruby2_keywords = args->ruby2_keywords; body->param.lead_num = arg_size = (int)args->pre_args_num; if (body->param.lead_num > 0) body->param.flags.has_lead = TRUE; debugs(" - argc: %d\n", body->param.lead_num); diff --git a/node.h b/node.h index 885d55b7a1baf4..befb1328fb3ef5 100644 --- a/node.h +++ b/node.h @@ -462,6 +462,7 @@ struct rb_args_info { NODE *opt_args; unsigned int no_kwarg: 1; + unsigned int ruby2_keywords: 1; unsigned int forwarding: 1; VALUE imemo; diff --git a/parse.y b/parse.y index 2caea0f138a5c3..bd3251e4e1a33a 100644 --- a/parse.y +++ b/parse.y @@ -943,6 +943,8 @@ static void numparam_pop(struct parser_params *p, NODE *prev_inner); #define idFWD_REST '*' #define idFWD_KWREST idPow /* Use simple "**", as tDSTAR is "**arg" */ #define idFWD_BLOCK '&' +#define idFWD_ALL idDot3 +#define FORWARD_ARGS_WITH_RUBY2_KEYWORDS #define RE_OPTION_ONCE (1<<16) #define RE_OPTION_ENCODING_SHIFT 8 @@ -3150,7 +3152,8 @@ args : arg_value } | tSTAR { - if (!local_id(p, idFWD_REST)) { + if (!local_id(p, idFWD_REST) || + local_id(p, idFWD_ALL)) { compile_error(p, "no anonymous rest parameter"); } /*%%%*/ @@ -3174,7 +3177,8 @@ args : arg_value } | args ',' tSTAR { - if (!local_id(p, idFWD_REST)) { + if (!local_id(p, idFWD_REST) || + local_id(p, idFWD_ALL)) { compile_error(p, "no anonymous rest parameter"); } /*%%%*/ @@ -5601,7 +5605,11 @@ f_args : f_arg ',' f_optarg ',' f_rest_arg opt_args_tail args_forward : tBDOT3 { /*%%%*/ +#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS + $$ = 0; +#else $$ = idFWD_KWREST; +#endif /*% %*/ /*% ripper: args_forward! %*/ } @@ -6044,7 +6052,8 @@ assoc : arg_value tASSOC arg_value } | tDSTAR { - if (!local_id(p, idFWD_KWREST)) { + if (!local_id(p, idFWD_KWREST) || + local_id(p, idFWD_ALL)) { compile_error(p, "no anonymous keyword rest parameter"); } /*%%%*/ @@ -12574,6 +12583,12 @@ new_args(struct parser_params *p, NODE *pre_args, NODE *opt_args, ID rest_arg, N args->opt_args = opt_args; +#ifdef FORWARD_ARGS_WITH_RUBY2_KEYWORDS + args->ruby2_keywords = args->forwarding; +#else + args->ruby2_keywords = 0; +#endif + p->ruby_sourceline = saved_line; nd_set_loc(tail, loc); @@ -13256,9 +13271,7 @@ local_id(struct parser_params *p, ID id) static int check_forwarding_args(struct parser_params *p) { - if (local_id(p, idFWD_REST) && - local_id(p, idFWD_KWREST) && - local_id(p, idFWD_BLOCK)) return TRUE; + if (local_id(p, idFWD_ALL)) return TRUE; compile_error(p, "unexpected ..."); return FALSE; } @@ -13267,8 +13280,11 @@ static void add_forwarding_args(struct parser_params *p) { arg_var(p, idFWD_REST); +#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS arg_var(p, idFWD_KWREST); +#endif arg_var(p, idFWD_BLOCK); + arg_var(p, idFWD_ALL); } #ifndef RIPPER @@ -13276,10 +13292,14 @@ static NODE * new_args_forward_call(struct parser_params *p, NODE *leading, const YYLTYPE *loc, const YYLTYPE *argsloc) { NODE *rest = NEW_LVAR(idFWD_REST, loc); +#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS NODE *kwrest = list_append(p, NEW_LIST(0, loc), NEW_LVAR(idFWD_KWREST, loc)); +#endif NODE *block = NEW_BLOCK_PASS(NEW_LVAR(idFWD_BLOCK, loc), loc); NODE *args = leading ? rest_arg_append(p, leading, rest, argsloc) : NEW_SPLAT(rest, loc); +#ifndef FORWARD_ARGS_WITH_RUBY2_KEYWORDS args = arg_append(p, args, new_hash(p, kwrest, loc), loc); +#endif return arg_blk_pass(args, block); } #endif diff --git a/test/ruby/test_ast.rb b/test/ruby/test_ast.rb index 667f8c0fd22d86..8acf4fe254c395 100644 --- a/test/ruby/test_ast.rb +++ b/test/ruby/test_ast.rb @@ -530,10 +530,10 @@ def test_argument_forwarding forwarding = lambda do |arg_str| node = RubyVM::AbstractSyntaxTree.parse("def a(#{arg_str}) end") node = node.children.last.children.last.children[1] - node ? [node.children[-4], node.children[-2].children, node.children[-1]] : [] + node ? [node.children[-4], node.children[-2]&.children, node.children[-1]] : [] end - assert_equal([:*, [:**], :&], forwarding.call('...')) + assert_equal([:*, nil, :&], forwarding.call('...')) end def test_ranges_numbered_parameter diff --git a/test/ruby/test_syntax.rb b/test/ruby/test_syntax.rb index 6695e46b92f83f..f9416f8fcba6fa 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -172,33 +172,11 @@ def f(a: nil, **); b(**) end end def test_argument_forwarding_with_anon_rest_kwrest_and_block - assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}") - begin; - def args(*args); args end - def kw(**kw); kw end - def block(&block); block end - def deconstruct(...); [args(*), kw(**), block(&)&.call] end - assert_equal([[], {}, nil], deconstruct) - assert_equal([[1], {}, nil], deconstruct(1)) - assert_equal([[1, 2], {}, nil], deconstruct(1, 2)) - assert_equal([[], {x: 1}, nil], deconstruct(x: 1)) - assert_equal([[], {x: 1, y: 2}, nil], deconstruct(x: 1, y: 2)) - assert_equal([[], {}, 1], deconstruct { 1 }) - assert_equal([[1, 2], {x: 3, y: 4}, 5], deconstruct(1, 2, x: 3, y: 4) { 5 }) - end; - - assert_separately([], "#{<<-"begin;"}\n#{<<-'end;'}") - begin; - def deconstruct(*args, **kw, &block); [args, kw, block&.call] end - def deconstruct2(*, **, &); deconstruct(...); end - assert_equal([[], {}, nil], deconstruct2) - assert_equal([[1], {}, nil], deconstruct2(1)) - assert_equal([[1, 2], {}, nil], deconstruct2(1, 2)) - assert_equal([[], {x: 1}, nil], deconstruct2(x: 1)) - assert_equal([[], {x: 1, y: 2}, nil], deconstruct2(x: 1, y: 2)) - assert_equal([[], {}, 1], deconstruct2 { 1 }) - assert_equal([[1, 2], {x: 3, y: 4}, 5], deconstruct2(1, 2, x: 3, y: 4) { 5 }) - end; + assert_syntax_error("def f(*, **, &); g(...); end", /unexpected \.\.\./) + assert_syntax_error("def f(...); g(*); end", /no anonymous rest parameter/) + assert_syntax_error("def f(...); g(0, *); end", /no anonymous rest parameter/) + assert_syntax_error("def f(...); g(**); end", /no anonymous keyword rest parameter/) + assert_syntax_error("def f(...); g(x: 1, **); end", /no anonymous keyword rest parameter/) end def test_newline_in_block_parameters