Skip to content

Commit 365049a

Browse files
committed
Fix crash when using arithmetic expressions in pattern matching
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.
1 parent 09d9ac3 commit 365049a

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

src/prism.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17393,6 +17393,14 @@ parse_pattern_primitive(pm_parser_t *parser, pm_constant_id_list_t *captures, pm
1739317393
// If we found a label, we need to immediately return to the caller.
1739417394
if (pm_symbol_node_label_p(node)) return node;
1739517395

17396+
// Call nodes (arithmetic operations) are not allowed in patterns
17397+
if (PM_NODE_TYPE(node) == PM_CALL_NODE) {
17398+
pm_parser_err_node(parser, node, diag_id);
17399+
pm_missing_node_t *missing_node = pm_missing_node_create(parser, node->location.start, node->location.end);
17400+
pm_node_destroy(parser, node);
17401+
return (pm_node_t *) missing_node;
17402+
}
17403+
1739617404
// Now that we have a primitive, we need to check if it's part of a range.
1739717405
if (accept2(parser, PM_TOKEN_DOT_DOT, PM_TOKEN_DOT_DOT_DOT)) {
1739817406
pm_token_t operator = parser->previous;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
case 1; in -1**2; end
2+
^~~~~ expected a pattern expression after the `in` keyword
3+

0 commit comments

Comments
 (0)