Skip to content

Commit 0aa2363

Browse files
Earlopainkddnewton
authored andcommitted
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 f741032 commit 0aa2363

File tree

5 files changed

+51
-5
lines changed

5 files changed

+51
-5
lines changed

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
@@ -13957,6 +13957,7 @@ parse_parameters(
1395713957
bool allows_forwarding_parameters,
1395813958
bool accepts_blocks_in_defaults,
1395913959
bool in_block,
13960+
pm_diagnostic_id_t diag_id_forwarding,
1396013961
uint16_t depth
1396113962
) {
1396213963
pm_do_loop_stack_push(parser, false);
@@ -14018,7 +14019,7 @@ parse_parameters(
1401814019
}
1401914020
case PM_TOKEN_UDOT_DOT_DOT: {
1402014021
if (!allows_forwarding_parameters) {
14021-
pm_parser_err_current(parser, PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES);
14022+
pm_parser_err_current(parser, diag_id_forwarding);
1402214023
}
1402314024

1402414025
bool succeeded = update_parameter_state(parser, &parser->current, &order);
@@ -14682,6 +14683,7 @@ parse_block_parameters(
1468214683
false,
1468314684
accepts_blocks_in_defaults,
1468414685
true,
14686+
is_lambda_literal ? PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES_LAMBDA : PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES_BLOCK,
1468514687
(uint16_t) (depth + 1)
1468614688
);
1468714689
if (!is_lambda_literal) {
@@ -18904,7 +18906,17 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1890418906
} else {
1890518907
// https://bugs.ruby-lang.org/issues/19107
1890618908
bool allow_trailing_comma = parser->version >= PM_OPTIONS_VERSION_CRUBY_4_1;
18907-
params = parse_parameters(parser, PM_BINDING_POWER_DEFINED, true, allow_trailing_comma, true, true, false, (uint16_t) (depth + 1));
18909+
params = parse_parameters(
18910+
parser,
18911+
PM_BINDING_POWER_DEFINED,
18912+
true,
18913+
allow_trailing_comma,
18914+
true,
18915+
true,
18916+
false,
18917+
PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES,
18918+
(uint16_t) (depth + 1)
18919+
);
1890818920
}
1890918921

1891018922
lex_state_set(parser, PM_LEX_STATE_BEG);
@@ -18927,7 +18939,17 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b
1892718939
lex_state_set(parser, parser->lex_state | PM_LEX_STATE_LABEL);
1892818940
}
1892918941

18930-
params = parse_parameters(parser, PM_BINDING_POWER_DEFINED, false, false, true, true, false, (uint16_t) (depth + 1));
18942+
params = parse_parameters(
18943+
parser,
18944+
PM_BINDING_POWER_DEFINED,
18945+
false,
18946+
false,
18947+
true,
18948+
true,
18949+
false,
18950+
PM_ERR_ARGUMENT_NO_FORWARDING_ELLIPSES,
18951+
(uint16_t) (depth + 1)
18952+
);
1893118953

1893218954
// Reject `def * = 1` and similar. We have to specifically check
1893318955
// 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)