Commit 0f6780d0

0f6780d05783Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 2 files · ~2 modified

Description

Pattern matching: case/in value, return type, and full condition set

case/in was emitted as a statement-only if/else chain, never inferred a value type, and its condition builder only handled typed scrutinees -- class patterns folded to a compile-time 0 and alternation/nil/true/false/pins emitted no condition (so an arm matched everything).

  • analyze: infer_type(CaseMatchNode) unifies each arm's body (+ else), so a method whose body is a case/in (e.g. describe) gets a return type.
  • codegen: emit_case_match takes a tail flag; in method-return position each arm's body is emitted via emit_stmts_tail (arms diverge, no fallthrough label). Wired into emit_stmt_tail_inner.
  • emit_pm_cond rewritten: literal patterns compare via emit_pm_eq (poly: sp_poly_eq with the value boxed; string: sp_str_eq; else ==); class patterns use emit_poly_class_when's runtime tag/class test for poly scrutinees; nil/true/false get tag checks; alternation ors its sides; pin patterns (^var / ^@ivar / ^(expr)) compare the scrutinee against the pinned value.

Fixes pattern and pattern_pin.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
0f6780d05783
Parent
5694124e4ab8

src/analyze.cmodifiedView file

@@ -2537,6 +2537,18 @@
25372537 else r = ty_unify(r, TY_NIL);
25382538 return r;
25392539 }
2540+ if (!strcmp(ty, "CaseMatchNode")) {
2541+ /* case X; in PATTERN; ... — value = unify of each arm's body (+ else). */
2542+ int nw = 0; const int *conds = nt_arr(nt, id, "conditions", &nw);
2543+ int else_c = nt_ref(nt, id, "else_clause");
2544+ TyKind r = TY_UNKNOWN;
2545+ for (int w = 0; w < nw; w++) {
2546+ int st = nt_ref(nt, conds[w], "statements");
2547+ r = ty_unify(r, st >= 0 ? infer_type(c, st) : TY_NIL);
2548+ }
2549+ if (else_c >= 0) { int st = nt_ref(nt, else_c, "statements"); r = ty_unify(r, st >= 0 ? infer_type(c, st) : TY_NIL); }
2550+ return r;
2551+ }
25402552 if (!strcmp(ty, "IfNode") || !strcmp(ty, "UnlessNode")) {
25412553 int is_unless = !strcmp(ty, "UnlessNode");
25422554 int then_b = nt_ref(nt, id, "statements");