Skip to content

Commit

Permalink
Disallow mixed usage of ... and */**
Browse files Browse the repository at this point in the history
[Feature #19134]
  • Loading branch information
shugo committed Dec 15, 2022
1 parent 613fca0 commit 2581de1
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 35 deletions.
1 change: 1 addition & 0 deletions compile.c
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions node.h
Expand Up @@ -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;
Expand Down
32 changes: 26 additions & 6 deletions parse.y
Expand Up @@ -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
Expand Down Expand Up @@ -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");
}
/*%%%*/
Expand All @@ -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");
}
/*%%%*/
Expand Down Expand Up @@ -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! %*/
}
Expand Down Expand Up @@ -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");
}
/*%%%*/
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}
Expand All @@ -13267,19 +13280,26 @@ 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
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
Expand Down
4 changes: 2 additions & 2 deletions test/ruby/test_ast.rb
Expand Up @@ -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
Expand Down
32 changes: 5 additions & 27 deletions test/ruby/test_syntax.rb
Expand Up @@ -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
Expand Down

0 comments on commit 2581de1

Please sign in to comment.