Skip to content

Commit 7ae0d9b

Browse files
committed
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]
1 parent 34e5539 commit 7ae0d9b

5 files changed

Lines changed: 51 additions & 5 deletions

File tree

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

src/prism.c

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13859,6 +13859,7 @@ parse_parameters(
1385913859
bool allows_forwarding_parameters,
1386013860
bool accepts_blocks_in_defaults,
1386113861
bool in_block,
13862+
pm_diagnostic_id_t diag_id_forwarding,
1386213863
uint16_t depth
1386313864
) {
1386413865
pm_do_loop_stack_push(parser, false);
@@ -13914,7 +13915,7 @@ parse_parameters(
1391413915
}
1391513916
case PM_TOKEN_UDOT_DOT_DOT: {
1391613917
if (!allows_forwarding_parameters) {
13917-
pm_parser_err_current(parser, PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES);
13918+
pm_parser_err_current(parser, diag_id_forwarding);
1391813919
}
1391913920

1392013921
bool succeeded = update_parameter_state(parser, &parser->current, &order);
@@ -14594,6 +14595,7 @@ parse_block_parameters(
1459414595
false,
1459514596
accepts_blocks_in_defaults,
1459614597
true,
14598+
is_lambda_literal ? PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES_LAMBDA : PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES_BLOCK,
1459714599
(uint16_t) (depth + 1)
1459814600
);
1459914601
if (!is_lambda_literal) {
@@ -18836,7 +18838,17 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1883618838
if (match1(parser, PM_TOKEN_PARENTHESIS_RIGHT)) {
1883718839
params = NULL;
1883818840
} else {
18839-
params = parse_parameters(parser, PM_BINDING_POWER_DEFINED, true, false, true, true, false, (uint16_t) (depth + 1));
18841+
params = parse_parameters(
18842+
parser,
18843+
PM_BINDING_POWER_DEFINED,
18844+
true,
18845+
false,
18846+
true,
18847+
true,
18848+
false,
18849+
PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES,
18850+
(uint16_t) (depth + 1)
18851+
);
1884018852
}
1884118853

1884218854
lex_state_set(parser, PM_LEX_STATE_BEG);
@@ -18861,7 +18873,17 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1886118873

1886218874
lparen = not_provided(parser);
1886318875
rparen = not_provided(parser);
18864-
params = parse_parameters(parser, PM_BINDING_POWER_DEFINED, false, false, true, true, false, (uint16_t) (depth + 1));
18876+
params = parse_parameters(
18877+
parser,
18878+
PM_BINDING_POWER_DEFINED,
18879+
false,
18880+
false,
18881+
true,
18882+
true,
18883+
false,
18884+
PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES,
18885+
(uint16_t) (depth + 1)
18886+
);
1886518887

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

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)