Skip to content

Commit e7d2828

Browse files
Earlopaink0kubun
authored andcommitted
[ruby/prism] Fix error message for block/lambda with ... argument
They currently complain that the parent method is not forwarding. But the actual problem is that these types of arguments simply don't accept `...` Fixes [Bug #21927] ruby/prism@0aa2363331
1 parent 83c261f commit e7d2828

5 files changed

Lines changed: 51 additions & 5 deletions

File tree

prism/config.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ errors:
1717
- ARGUMENT_FORWARDING_UNBOUND
1818
- ARGUMENT_NO_FORWARDING_AMPERSAND
1919
- ARGUMENT_NO_FORWARDING_ELLIPSES
20+
- ARGUMENT_NO_FORWARDING_ELLIPSES_LAMBDA
21+
- ARGUMENT_NO_FORWARDING_ELLIPSES_BLOCK
2022
- ARGUMENT_NO_FORWARDING_STAR
2123
- ARGUMENT_NO_FORWARDING_STAR_STAR
2224
- ARGUMENT_SPLAT_AFTER_ASSOC_SPLAT

prism/prism.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13876,6 +13876,7 @@ parse_parameters(
1387613876
bool allows_forwarding_parameters,
1387713877
bool accepts_blocks_in_defaults,
1387813878
bool in_block,
13879+
pm_diagnostic_id_t diag_id_forwarding,
1387913880
uint16_t depth
1388013881
) {
1388113882
pm_do_loop_stack_push(parser, false);
@@ -13931,7 +13932,7 @@ parse_parameters(
1393113932
}
1393213933
case PM_TOKEN_UDOT_DOT_DOT: {
1393313934
if (!allows_forwarding_parameters) {
13934-
pm_parser_err_current(parser, PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES);
13935+
pm_parser_err_current(parser, diag_id_forwarding);
1393513936
}
1393613937

1393713938
bool succeeded = update_parameter_state(parser, &parser->current, &order);
@@ -14611,6 +14612,7 @@ parse_block_parameters(
1461114612
false,
1461214613
accepts_blocks_in_defaults,
1461314614
true,
14615+
is_lambda_literal ? PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES_LAMBDA : PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES_BLOCK,
1461414616
(uint16_t) (depth + 1)
1461514617
);
1461614618
if (!is_lambda_literal) {
@@ -18853,7 +18855,17 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1885318855
if (match1(parser, PM_TOKEN_PARENTHESIS_RIGHT)) {
1885418856
params = NULL;
1885518857
} else {
18856-
params = parse_parameters(parser, PM_BINDING_POWER_DEFINED, true, false, true, true, false, (uint16_t) (depth + 1));
18858+
params = parse_parameters(
18859+
parser,
18860+
PM_BINDING_POWER_DEFINED,
18861+
true,
18862+
false,
18863+
true,
18864+
true,
18865+
false,
18866+
PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES,
18867+
(uint16_t) (depth + 1)
18868+
);
1885718869
}
1885818870

1885918871
lex_state_set(parser, PM_LEX_STATE_BEG);
@@ -18878,7 +18890,17 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1887818890

1887918891
lparen = not_provided(parser);
1888018892
rparen = not_provided(parser);
18881-
params = parse_parameters(parser, PM_BINDING_POWER_DEFINED, false, false, true, true, false, (uint16_t) (depth + 1));
18893+
params = parse_parameters(
18894+
parser,
18895+
PM_BINDING_POWER_DEFINED,
18896+
false,
18897+
false,
18898+
true,
18899+
true,
18900+
false,
18901+
PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES,
18902+
(uint16_t) (depth + 1)
18903+
);
1888218904

1888318905
// Reject `def * = 1` and similar. We have to specifically check
1888418906
// for them because they create ambiguity with optional arguments.

prism/templates/src/diagnostic.c.erb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,8 @@ static const pm_diagnostic_data_t diagnostic_messages[PM_DIAGNOSTIC_ID_MAX] = {
102102
[PM_ERR_ARGUMENT_FORWARDING_UNBOUND] = { "unexpected `...` in an non-parenthesized call", PM_ERROR_LEVEL_SYNTAX },
103103
[PM_ERR_ARGUMENT_NO_FORWARDING_AMPERSAND] = { "unexpected `&`; no anonymous block parameter", PM_ERROR_LEVEL_SYNTAX },
104104
[PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES] = { "unexpected ... when the parent method is not forwarding", PM_ERROR_LEVEL_SYNTAX },
105+
[PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES_LAMBDA] = { "unexpected ... in lambda argument", PM_ERROR_LEVEL_SYNTAX },
106+
[PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES_BLOCK] = { "unexpected ... in block argument", PM_ERROR_LEVEL_SYNTAX },
105107
[PM_ERR_ARGUMENT_NO_FORWARDING_STAR] = { "unexpected `*`; no anonymous rest parameter", PM_ERROR_LEVEL_SYNTAX },
106108
[PM_ERR_ARGUMENT_NO_FORWARDING_STAR_STAR] = { "unexpected `**`; no anonymous keyword rest parameter", PM_ERROR_LEVEL_SYNTAX },
107109
[PM_ERR_ARGUMENT_SPLAT_AFTER_ASSOC_SPLAT] = { "unexpected `*` splat argument after a `**` keyword splat argument", PM_ERROR_LEVEL_SYNTAX },
Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
a {|...|}
2-
^~~ unexpected ... when the parent method is not forwarding
2+
^~~ unexpected ... in block argument
3+
4+
def foo(...)
5+
a {|...|}
6+
^~~ unexpected ... in block argument
7+
end
8+
9+
def foo
10+
a {|...|}
11+
^~~ unexpected ... in block argument
12+
end
313

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
11
->(...) {}
2-
^~~ unexpected ... when the parent method is not forwarding
2+
^~~ unexpected ... in lambda argument
3+
4+
def foo(...)
5+
->(...) {}
6+
^~~ unexpected ... in lambda argument
7+
end
8+
9+
def foo
10+
->(...) {}
11+
^~~ unexpected ... in lambda argument
12+
end
313

0 commit comments

Comments
 (0)