diff options
author | Shugo Maeda <shugo@ruby-lang.org> | 2022-12-15 15:12:33 +0900 |
---|---|---|
committer | Shugo Maeda <shugo.maeda@gmail.com> | 2022-12-15 18:56:24 +0900 |
commit | 2581de112c1957dc4b5852e54337551dc8972c99 (patch) | |
tree | d276a3734bf323ab95ff5abab293935a05714033 | |
parent | 613fca01486e47dee9364a2fd86b5f5e77fe23c8 (diff) |
Disallow mixed usage of ... and */**
[Feature #19134]
Notes
Notes:
Merged: https://github.com/ruby/ruby/pull/6934
-rw-r--r-- | compile.c | 1 | ||||
-rw-r--r-- | node.h | 1 | ||||
-rw-r--r-- | parse.y | 32 | ||||
-rw-r--r-- | test/ruby/test_ast.rb | 4 | ||||
-rw-r--r-- | test/ruby/test_syntax.rb | 32 |
5 files changed, 35 insertions, 35 deletions
@@ -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); @@ -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; @@ -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 667f8c0fd2..8acf4fe254 100644 --- a/test/ruby/test_ast.rb +++ b/test/ruby/test_ast.rb @@ -530,10 +530,10 @@ class TestAst < Test::Unit::TestCase 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 6695e46b92..f9416f8fcb 100644 --- a/test/ruby/test_syntax.rb +++ b/test/ruby/test_syntax.rb @@ -172,33 +172,11 @@ class TestSyntax < Test::Unit::TestCase 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 |