ripper: Check if anonymous parameters defined [Bug #18828]

This commit is contained in:
Nobuyoshi Nakada 2022-06-14 18:28:22 +09:00
Родитель 048f14221c
Коммит 1a70973f75
2 изменённых файлов: 49 добавлений и 13 удалений

24
parse.y
Просмотреть файл

@ -2856,14 +2856,13 @@ block_arg : tAMPER arg_value
}
| tAMPER
{
/*%%%*/
if (!local_id(p, ANON_BLOCK_ID)) {
compile_error(p, "no anonymous block parameter");
}
/*%%%*/
$$ = NEW_BLOCK_PASS(NEW_LVAR(ANON_BLOCK_ID, &@1), &@$);
/*%
$$ = Qnil;
%*/
/*% %*/
/*% ripper: Qnil %*/
}
;
@ -2894,10 +2893,10 @@ args : arg_value
}
| tSTAR
{
/*%%%*/
if (!local_id(p, ANON_REST_ID)) {
compile_error(p, "no anonymous rest parameter");
}
/*%%%*/
$$ = NEW_SPLAT(NEW_LVAR(ANON_REST_ID, &@1), &@$);
/*% %*/
/*% ripper: args_add_star!(args_new!, Qnil) %*/
@ -2918,10 +2917,10 @@ args : arg_value
}
| args ',' tSTAR
{
/*%%%*/
if (!local_id(p, ANON_REST_ID)) {
compile_error(p, "no anonymous rest parameter");
}
/*%%%*/
$$ = rest_arg_append(p, $1, NEW_LVAR(ANON_REST_ID, &@3), &@$);
/*% %*/
/*% ripper: args_add_star!($1, Qnil) %*/
@ -5489,8 +5488,8 @@ f_kwrest : kwrest_mark tIDENTIFIER
}
| kwrest_mark
{
arg_var(p, ANON_KEYWORD_REST_ID);
/*%%%*/
arg_var(p, shadowing_lvar(p, get_id(ANON_KEYWORD_REST_ID)));
/*% %*/
/*% ripper: kwrest_param!(Qnil) %*/
}
@ -5564,8 +5563,8 @@ f_rest_arg : restarg_mark tIDENTIFIER
}
| restarg_mark
{
arg_var(p, ANON_REST_ID);
/*%%%*/
arg_var(p, shadowing_lvar(p, get_id(ANON_REST_ID)));
/*% %*/
/*% ripper: rest_param!(Qnil) %*/
}
@ -5585,11 +5584,10 @@ f_block_arg : blkarg_mark tIDENTIFIER
}
| blkarg_mark
{
arg_var(p, ANON_BLOCK_ID);
/*%%%*/
arg_var(p, shadowing_lvar(p, get_id(ANON_BLOCK_ID)));
/*%
$$ = dispatch1(blockarg, Qnil);
%*/
/*% %*/
/*% ripper: blockarg!(Qnil) %*/
}
;
@ -5721,10 +5719,10 @@ assoc : arg_value tASSOC arg_value
}
| tDSTAR
{
/*%%%*/
if (!local_id(p, ANON_KEYWORD_REST_ID)) {
compile_error(p, "no anonymous keyword rest parameter");
}
/*%%%*/
$$ = list_append(p, NEW_LIST(0, &@$),
NEW_LVAR(ANON_KEYWORD_REST_ID, &@$));
/*% %*/

Просмотреть файл

@ -155,6 +155,44 @@ class TestRipper::ParserEvents < Test::Unit::TestCase
end
end
def test_anonymous_block_forwarding
thru_args_add_block = false
parse('def b(&); c(&); end', :on_args_add_block) {thru_args_add_block = true}
assert_equal true, thru_args_add_block
assert_match "no anonymous block parameter", compile_error('def b; c(&); end')
end
def test_anonymous_rest_forwarding
[
'c(*)',
'c(*, *)',
].each do |code|
thru_args_add_star = false
src = "def b(*); #{code} end"
parse(src, :on_args_add_star) {thru_args_add_star = true}
assert_equal true, thru_args_add_star, src
src = "def b; #{code} end"
assert_match "no anonymous rest parameter", compile_error(src), src
end
end
def test_anonymous_keyword_rest_forwarding
[
'c(**)',
'c(k: 1, **)',
'c(**, k: 1)',
].each do |code|
thru_assoc_splat = false
src = "def b(**); #{code} end"
parse(src, :on_assoc_splat) {thru_assoc_splat = true}
assert_equal true, thru_assoc_splat, src
src = "def b; #{code} end"
assert_match "no anonymous keyword rest parameter", compile_error(src), src
end
end
def test_arg_paren
# FIXME
end