diff options
| author | ydah <t.yudai92@gmail.com> | 2025-01-08 23:58:59 +0900 |
|---|---|---|
| committer | git <svn-admin@ruby-lang.org> | 2025-01-08 17:23:51 +0000 |
| commit | 500a87756f9873a320aa3a11ab2d1ac4e1b4afee (patch) | |
| tree | 76c0bea620aac49178544c7a70adce405b3cab5c | |
| parent | e0d600ec190c64aff76cfcbd6009cffb927da166 (diff) | |
[ruby/prism] Reject pattern match with unexpected double splat inside array
`a => [-2**b]` should be SyntaxError
Fixes: https://github.com/ruby/prism/issues/3381
https://github.com/ruby/prism/commit/ae8e83b389
| -rw-r--r-- | prism/prism.c | 3 | ||||
| -rw-r--r-- | test/prism/errors/pattern_match_with_unexpected_splat_inside_arraytxt | 14 |
2 files changed, 16 insertions, 1 deletions
diff --git a/prism/prism.c b/prism/prism.c index db1e475f95..19ff1d2fe6 100644 --- a/prism/prism.c +++ b/prism/prism.c @@ -20527,12 +20527,13 @@ parse_expression_prefix(pm_parser_t *parser, pm_binding_power_t binding_power, b return (pm_node_t *) node; } case PM_TOKEN_UMINUS_NUM: { + pm_token_t prev = parser->previous; parser_lex(parser); pm_token_t operator = parser->previous; pm_node_t *node = parse_expression(parser, pm_binding_powers[parser->previous.type].right, false, false, PM_ERR_UNARY_RECEIVER, (uint16_t) (depth + 1)); - if (accept1(parser, PM_TOKEN_STAR_STAR)) { + if ((prev.type != PM_TOKEN_BRACKET_LEFT_ARRAY) && (accept1(parser, PM_TOKEN_STAR_STAR))) { pm_token_t exponent_operator = parser->previous; pm_node_t *exponent = parse_expression(parser, pm_binding_powers[exponent_operator.type].right, false, false, PM_ERR_EXPECT_ARGUMENT, (uint16_t) (depth + 1)); node = (pm_node_t *) pm_call_node_binary_create(parser, node, &exponent_operator, exponent, 0); diff --git a/test/prism/errors/pattern_match_with_unexpected_splat_inside_arraytxt b/test/prism/errors/pattern_match_with_unexpected_splat_inside_arraytxt new file mode 100644 index 0000000000..d19491e6bf --- /dev/null +++ b/test/prism/errors/pattern_match_with_unexpected_splat_inside_arraytxt @@ -0,0 +1,14 @@ +a => [-2*b] + ^ expected a `]` to close the pattern expression + ^ unexpected '*', expecting end-of-input + ^ unexpected '*', ignoring it + ^ unexpected ']', expecting end-of-input + ^ unexpected ']', ignoring it + +a => [-2**b] + ^ expected a `]` to close the pattern expression + ^~ unexpected '**', expecting end-of-input + ^~ unexpected '**', ignoring it + ^ unexpected ']', expecting end-of-input + ^ unexpected ']', ignoring it + |
