summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Newton <kddnewton@gmail.com>2025-11-29 14:14:48 -0500
committergit <svn-admin@ruby-lang.org>2025-11-29 19:47:45 +0000
commitf2a6cb2dc88ca12938b45f35bc6e64d72060a959 (patch)
tree1dae42b1f01284998b0bb68129dbb544a6c47b3b
parent48a73303e45b1dbaa3422e14e35c7834db98be4d (diff)
[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. https://github.com/ruby/prism/commit/f0b06d6269
-rw-r--r--prism/prism.c6
-rw-r--r--test/prism/errors/pattern_string_key.txt8
2 files changed, 13 insertions, 1 deletions
diff --git a/prism/prism.c b/prism/prism.c
index 186cdd354c..3485a58c28 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -17336,7 +17336,11 @@ parse_pattern_hash(pm_parser_t *parser, pm_constant_id_list_t *captures, pm_node
pm_node_t *value = NULL;
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)) {
- value = parse_pattern_hash_implicit_value(parser, captures, (pm_symbol_node_t *) key);
+ if (PM_NODE_TYPE_P(key, PM_SYMBOL_NODE)) {
+ value = parse_pattern_hash_implicit_value(parser, captures, (pm_symbol_node_t *) key);
+ } else {
+ value = (pm_node_t *) pm_missing_node_create(parser, key->location.end, key->location.end);
+ }
} else {
value = parse_pattern(parser, captures, PM_PARSE_PATTERN_SINGLE, PM_ERR_PATTERN_EXPRESSION_AFTER_KEY, (uint16_t) (depth + 1));
}
diff --git a/test/prism/errors/pattern_string_key.txt b/test/prism/errors/pattern_string_key.txt
new file mode 100644
index 0000000000..9f28feddb9
--- /dev/null
+++ b/test/prism/errors/pattern_string_key.txt
@@ -0,0 +1,8 @@
+case:a
+in b:"","#{}"
+ ^~~~~ expected a label after the `,` in the hash pattern
+ ^ expected a pattern expression after the key
+ ^ expected a delimiter after the patterns of an `in` clause
+ ^ unexpected end-of-input, assuming it is closing the parent top level context
+ ^ expected an `end` to close the `case` statement
+