Commit e67eb243

e67eb2434d86Merged

Ryan Seys <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified

Description

feat(parse,codegen): pattern matching for ^var / ^(expr) pin patterns

Prism delivers case x in ^var as a PinnedVariableNode and case x in ^(expr) as a PinnedExpressionNode. spinel_parse did not emit either; the unknown-node passthrough routed them through compile_in_pattern's "always-match" fallback, so any pin pattern silently matched whatever scrutinee it saw.

Both pin shapes are "match the scrutinee by == against this computed value", so the parse layer routes PinnedVariableNode's variable slot into the same @nd_expression field as PinnedExpressionNode. The codegen treats them uniformly via a single arm.

For a typed scrutinee (int / string / sym / etc.) the emit is the direct comparison: tmp == pinned or strcmp(tmp, pinned) == 0. For a poly-tagged scrutinee the emit double-checks both the tag and the value so a pinned Integer doesn't spuriously match against a string with the same underlying byte pattern.

The scrutinee temp in compile_case_match_stmt was declared as mrb_int for everything non-poly/non-string/non-float/non-bigint -- that worked for the existing primitive patterns but degrades to a cc-rejected mrb_int x = sp_IntArray * when a typed container scrutinee reaches an array pattern arm in follow-up PRs. Widen the dispatch to use c_type(pred_type) for non-primitive types while keeping bigint's existing unbox arm untouched. Pin patterns don't hit this path themselves, but the fix is small and unblocks the remaining four pattern-node PRs from each rebasing the same typed-temp change.

Adding the pin arms surfaced two adjacent silent-match bugs in compile_in_pattern: SymbolNode (case sym in :foo) and FloatNode (case f in 3.14) literal patterns both fell through to the trailing "1" fallback and matched every scrutinee. Add the missing arms inline -- they share the same shape as the existing IntegerNode / StringNode arms.

Test test/pattern_pin.rb covers: int pin match + miss, string pin match + miss, expression pin ^(val * 3), method-call expression pin, symbol pin match + miss, pin alongside literal-symbol arms in the same case, ivar pin inside an instance method.

Bootstrap fixpoint green; 626 / 626 pass.

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
e67eb2434d86
Parent
c360ff731f28

spinel_codegen.rbmodifiedView file

@@ -27824,22 +27824,29 @@
2782427824 pred_type = infer_type(pred)
2782527825 pred_val = compile_expr(pred)
2782627826 tmp = new_temp
27827+ # For typed containers (int_array, sym_array, str_array, hash
27828+ # variants, range, time, user-class obj_*, ...) the temp must be
27829+ # declared with the receiver's actual C type so the pattern arms
27830+ # can dereference `tmp->len` / `tmp->data[i]` and per-variant hash
27831+ # lookups can pass the right pointer type. Without this the temp
27832+ # was declared as `mrb_int` for everything non-poly/string/float
27833+ # and any pattern shape touching the scrutinee structurally
27834+ # crashed at cc time. Primitive shapes (int/bool/symbol/nil) keep
27835+ # the historic `mrb_int` slot.
2782727836 if pred_type == "poly"
2782827837 emit(" sp_RbVal " + tmp + " = " + pred_val + ";")
27838+ elsif pred_type == "string"
27839+ emit(" const char *" + tmp + " = " + pred_val + ";")
27840+ elsif pred_type == "float"
27841+ emit(" mrb_float " + tmp + " = " + pred_val + ";")
27842+ elsif pred_type == "bigint"
27843+ @needs_bigint = 1
27844+ emit(" mrb_int " + tmp + " = sp_bigint_to_int((sp_Bigint *)" + pred_val + ");")
27845+ pred_type = "int"
27846+ elsif pred_type == "int" || pred_type == "bool" || pred_type == "symbol" || pred_type == "nil" || pred_type == ""
27847+ emit(" mrb_int " + tmp + " = " + pred_val + ";")
2782927848 else
27830- if pred_type == "string"
27831- emit(" const char *" + tmp + " = " + pred_val + ";")
27832- else
27833- if pred_type == "float"
27834- emit(" mrb_float " + tmp + " = " + pred_val + ";")
27835- elsif pred_type == "bigint"
27836- @needs_bigint = 1
27837- emit(" mrb_int " + tmp + " = sp_bigint_to_int((sp_Bigint *)" + pred_val + ");")
27838- pred_type = "int"
27839- else
27840- emit(" mrb_int " + tmp + " = " + pred_val + ";")
27841- end
27842- end
27849+ emit(" " + c_type(pred_type) + " " + tmp + " = " + pred_val + ";")
2784327850 end
2784427851 conds = parse_id_list(@nd_conditions[nid])
2784527852 k = 0
@@ -27931,11 +27938,66 @@
2793127938 end
2793227939 return tmp + " == 0"
2793327940 end
27941+ if pt == "SymbolNode"
27942+ # `case sym in :foo` -- literal symbol pattern. Without this arm
27943+ # the dispatch fell through to the trailing "1" fallback and any
27944+ # symbol pattern silently always matched.
27945+ sym_lit = compile_symbol_literal(@nd_content[pat_id])
27946+ if pred_type == "poly"
27947+ return "(" + tmp + ".tag == SP_TAG_SYM && (sp_sym)" + tmp + ".v.i == (" + sym_lit + "))"
27948+ end
27949+ return "(sp_sym)(" + tmp + ") == (" + sym_lit + ")"
27950+ end
27951+ if pt == "FloatNode"
27952+ # `case f in 3.14` -- literal float pattern. Same fallthrough
27953+ # silent-match bug as SymbolNode.
27954+ fval = @nd_value[pat_id]
27955+ if pred_type == "poly"
27956+ return "(" + tmp + ".tag == SP_TAG_FLT && " + tmp + ".v.f == " + fval + ")"
27957+ end
27958+ return tmp + " == " + fval
27959+ end
2793427960 if pt == "AlternationPatternNode"
2793527961 left = compile_in_pattern(@nd_left[pat_id], tmp, pred_type)
2793627962 right = compile_in_pattern(@nd_right[pat_id], tmp, pred_type)
2793727963 return "(" + left + " || " + right + ")"
2793827964 end
27965+ # PinnedVariableNode / PinnedExpressionNode: `case x in ^var` /
27966+ # `in ^(expr)`. Both share the `@nd_expression` slot -- the parse
27967+ # layer routes the variable into the expression field so codegen
27968+ # treats them uniformly. Match by `==` against the pinned value;
27969+ # poly-tagged scrutinees get both tag and value checks so a pinned
27970+ # Integer doesn't spuriously match against a string with the same
27971+ # underlying byte pattern.
27972+ if pt == "PinnedVariableNode" || pt == "PinnedExpressionNode"
27973+ pinned = compile_expr(@nd_expression[pat_id])
27974+ pinned_type = infer_type(@nd_expression[pat_id])
27975+ if pred_type == "poly"
27976+ if pinned_type == "int"
27977+ return "(" + tmp + ".tag == SP_TAG_INT && " + tmp + ".v.i == (" + pinned + "))"
27978+ end
27979+ if pinned_type == "string"
27980+ return "(" + tmp + ".tag == SP_TAG_STR && strcmp(" + tmp + ".v.s, " + pinned + ") == 0)"
27981+ end
27982+ if pinned_type == "symbol"
27983+ return "(" + tmp + ".tag == SP_TAG_SYM && (sp_sym)" + tmp + ".v.i == (" + pinned + "))"
27984+ end
27985+ if pinned_type == "float"
27986+ return "(" + tmp + ".tag == SP_TAG_FLT && " + tmp + ".v.f == (" + pinned + "))"
27987+ end
27988+ if pinned_type == "bool"
27989+ return "(" + tmp + ".tag == SP_TAG_BOOL && " + tmp + ".v.b == (" + pinned + "))"
27990+ end
27991+ if pinned_type == "nil"
27992+ return tmp + ".tag == SP_TAG_NIL"
27993+ end
27994+ return "0"
27995+ end
27996+ if pinned_type == "string" && pred_type == "string"
27997+ return "strcmp(" + tmp + ", " + pinned + ") == 0"
27998+ end
27999+ return "(" + tmp + ") == (" + pinned + ")"
28000+ end
2793928001 "1"
2794028002 end
2794128003