Skip to content

Commit f86bff6

Browse files
committed
Fix assertion failure for fwd params after rest
1 parent 24cab4c commit f86bff6

File tree

4 files changed

+17
-0
lines changed

4 files changed

+17
-0
lines changed

include/prism/diagnostic.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ typedef enum {
168168
PM_ERR_PARAMETER_ORDER,
169169
PM_ERR_PARAMETER_SPLAT_MULTI,
170170
PM_ERR_PARAMETER_STAR,
171+
PM_ERR_PARAMETER_UNEXPECTED_FWD,
171172
PM_ERR_PARAMETER_WILD_LOOSE_COMMA,
172173
PM_ERR_PATTERN_EXPRESSION_AFTER_BRACKET,
173174
PM_ERR_PATTERN_EXPRESSION_AFTER_HROCKET,

src/diagnostic.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ static const char* const diagnostic_messages[PM_DIAGNOSTIC_ID_LEN] = {
202202
[PM_ERR_PARAMETER_ORDER] = "Unexpected parameter order",
203203
[PM_ERR_PARAMETER_SPLAT_MULTI] = "Unexpected multiple `*` splat parameters",
204204
[PM_ERR_PARAMETER_STAR] = "Unexpected parameter `*`",
205+
[PM_ERR_PARAMETER_UNEXPECTED_FWD] = "Unexpected `...` in parameters",
205206
[PM_ERR_PARAMETER_WILD_LOOSE_COMMA] = "Unexpected `,` in parameters",
206207
[PM_ERR_PATTERN_EXPRESSION_AFTER_BRACKET] = "Expected a pattern expression after the `[` operator",
207208
[PM_ERR_PATTERN_EXPRESSION_AFTER_COMMA] = "Expected a pattern expression after `,`",

src/prism.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9292,6 +9292,14 @@ parse_parameters(
92929292

92939293
pm_parser_local_add_token(parser, &parser->previous);
92949294
pm_forwarding_parameter_node_t *param = pm_forwarding_parameter_node_create(parser, &parser->previous);
9295+
if (params->keyword_rest != NULL) {
9296+
// If we already have a keyword rest parameter, then we replace it with the
9297+
// forwarding parameter and move the keyword rest parameter to the posts list.
9298+
pm_node_t *keyword_rest = params->keyword_rest;
9299+
pm_parameters_node_posts_append(params, keyword_rest);
9300+
pm_diagnostic_list_append(&parser->error_list, parser->previous.start, parser->previous.end, PM_ERR_PARAMETER_UNEXPECTED_FWD);
9301+
params->keyword_rest = NULL;
9302+
}
92959303
pm_parameters_node_keyword_rest_set(params, (pm_node_t *)param);
92969304
} else {
92979305
update_parameter_state(parser, &parser->current, &order);

test/prism/errors_test.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,6 +1348,13 @@ def test_loop_conditional_is_closed
13481348
]
13491349
end
13501350

1351+
def test_forwarding_arg_after_keyword_rest
1352+
source = "def f(**,...);end"
1353+
assert_errors expression(source), source, [
1354+
["Unexpected `...` in parameters", 9..12],
1355+
]
1356+
end
1357+
13511358
private
13521359

13531360
def assert_errors(expected, source, errors, compare_ripper: RUBY_ENGINE == "ruby")

0 commit comments

Comments
 (0)