Skip to content

Commit f2a6cb2

Browse files
kddnewtonmatzbot
authored andcommitted
[ruby/prism] Handle invalid string pattern key
When a pattern match is using a string as a hash pattern key and is using it incorrectly, we were previously assuming it was a symbol. In the case of an error, that's not the case. So we need to add a missing node in this case. ruby/prism@f0b06d6269
1 parent 48a7330 commit f2a6cb2

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

prism/prism.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17336,7 +17336,11 @@ parse_pattern_hash(pm_parser_t *parser, pm_constant_id_list_t *captures, pm_node
1733617336
pm_node_t *value = NULL;
1733717337

1733817338
if (match7(parser, PM_TOKEN_COMMA, PM_TOKEN_KEYWORD_THEN, PM_TOKEN_BRACE_RIGHT, PM_TOKEN_BRACKET_RIGHT, PM_TOKEN_PARENTHESIS_RIGHT, PM_TOKEN_NEWLINE, PM_TOKEN_SEMICOLON)) {
17339-
value = parse_pattern_hash_implicit_value(parser, captures, (pm_symbol_node_t *) key);
17339+
if (PM_NODE_TYPE_P(key, PM_SYMBOL_NODE)) {
17340+
value = parse_pattern_hash_implicit_value(parser, captures, (pm_symbol_node_t *) key);
17341+
} else {
17342+
value = (pm_node_t *) pm_missing_node_create(parser, key->location.end, key->location.end);
17343+
}
1734017344
} else {
1734117345
value = parse_pattern(parser, captures, PM_PARSE_PATTERN_SINGLE, PM_ERR_PATTERN_EXPRESSION_AFTER_KEY, (uint16_t) (depth + 1));
1734217346
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
case:a
2+
in b:"","#{}"
3+
^~~~~ expected a label after the `,` in the hash pattern
4+
^ expected a pattern expression after the key
5+
^ expected a delimiter after the patterns of an `in` clause
6+
^ unexpected end-of-input, assuming it is closing the parent top level context
7+
^ expected an `end` to close the `case` statement
8+

0 commit comments

Comments
 (0)