summaryrefslogtreecommitdiff
diff options
authorStan Lo <[email protected]>2025-07-01 17:10:22 +0100
committergit <[email protected]>2025-07-16 17:08:28 +0000
commit2591b935930aaeee652df3ffbbe1cb138e7f5346 (patch)
treea05aa124dffd1c3eee7378d6089558a2b2f779d8
parentacc317253043efc65e87b460de48dc4e50c87c59 (diff)
[ruby/prism] Fix crash when using arithmetic expressions in pattern matchingHEADmaster
When arithmetic expressions like `-1**2` are used in pattern matching contexts, Ruby crashes with "Unexpected node type in pattern matching expression: PM_CALL_NODE". This happens because the Prism parser creates `PM_CALL_NODE` for arithmetic operations, but Ruby's pattern matching compiler doesn't handle call nodes. This fix adds validation to reject `PM_CALL_NODE` in pattern contexts with a proper syntax error. https://github.com/ruby/prism/commit/365049a767
-rw-r--r--prism/prism.c8
-rw-r--r--test/prism/errors/pattern_arithmetic_expressions.txt3
2 files changed, 11 insertions, 0 deletions
diff --git a/prism/prism.c b/prism/prism.c
index 3d026f3009..18ab98bfa6 100644
--- a/prism/prism.c
+++ b/prism/prism.c
@@ -17412,6 +17412,14 @@ parse_pattern_primitive(pm_parser_t *parser, pm_constant_id_list_t *captures, pm
// If we found a label, we need to immediately return to the caller.
if (pm_symbol_node_label_p(node)) return node;
+ // Call nodes (arithmetic operations) are not allowed in patterns
+ if (PM_NODE_TYPE(node) == PM_CALL_NODE) {
+ pm_parser_err_node(parser, node, diag_id);
+ pm_missing_node_t *missing_node = pm_missing_node_create(parser, node->location.start, node->location.end);
+ pm_node_destroy(parser, node);
+ return (pm_node_t *) missing_node;
+ }
+
// Now that we have a primitive, we need to check if it's part of a range.
if (accept2(parser, PM_TOKEN_DOT_DOT, PM_TOKEN_DOT_DOT_DOT)) {
pm_token_t operator = parser->previous;
diff --git a/test/prism/errors/pattern_arithmetic_expressions.txt b/test/prism/errors/pattern_arithmetic_expressions.txt
new file mode 100644
index 0000000000..cfb3650531
--- /dev/null
+++ b/test/prism/errors/pattern_arithmetic_expressions.txt
@@ -0,0 +1,3 @@
+case 1; in -1**2; end
+ ^~~~~ expected a pattern expression after the `in` keyword
+
close