Commit 712d6114

712d6114c584Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(codegen): box per-arm value when case-expr unifies to poly

When a case expression's when arms return instances of unrelated sibling classes (or any mix of types that unifies at the expression level to sp_RbVal), each arm's last expression was emitted as a raw assignment into the sp_RbVal result slot:

sp_RbVal _t1 = sp_box_nil();
if (_t2 == 0LL) {
  _t1 = sp_A_new();   // error: incompatible types
} else if (_t2 == 1LL) {
  _t1 = sp_B_new();   // error: incompatible types
}

The unified target is poly but the arm's static type is sp_A * / sp_B *; C rejects the assignment.

Route each arm's last expression through a new helper box_when_arm_to_target that calls box_value_to_poly when the case-expression's unified type is poly and the arm's type is something narrower. Applies to predicate'd case (case x when 1), bare case (case when cond), and the else clause.

Fixes #580.

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

Details

Commit
712d6114c584
Parent
7747ae4af0b2

spinel_codegen.rbmodifiedView file

@@ -11394,7 +11394,7 @@
1139411394 compile_stmt(ws[i])
1139511395 i = i + 1
1139611396 end
11397- emit(" " + tmp + " = " + compile_expr(ws.last) + ";")
11397+ emit(" " + tmp + " = " + box_when_arm_to_target(ws.last, rt) + ";")
1139811398 end
1139911399 end
1140011400 end
@@ -11426,7 +11426,7 @@
1142611426 compile_stmt(ws[i])
1142711427 i = i + 1
1142811428 end
11429- emit(" " + tmp + " = " + compile_expr(ws.last) + ";")
11429+ emit(" " + tmp + " = " + box_when_arm_to_target(ws.last, rt) + ";")
1143011430 end
1143111431 end
1143211432 end
@@ -11445,7 +11445,7 @@
1144511445 compile_stmt(es[i])
1144611446 i = i + 1
1144711447 end
11448- emit(" " + tmp + " = " + compile_expr(es.last) + ";")
11448+ emit(" " + tmp + " = " + box_when_arm_to_target(es.last, rt) + ";")
1144911449 end
1145011450 end
1145111451 end
@@ -21105,6 +21105,23 @@
2110521105 "(void *)(" + val + ").v.p"
2110621106 end
2110721107
21108+ # Compile a `when` / `else` arm's last expression and, when the
21109+ # case-expression's unified target type is `poly` (sp_RbVal) but
21110+ # the arm's static type is a typed pointer / scalar, route through
21111+ # `box_value_to_poly` so the assignment lands in the tagged-union
21112+ # slot. Without this, a `case ... when A.new; when B.new; end`
21113+ # whose unified result is poly emits raw `sp_A *` / `sp_B *`
21114+ # assignments into the sp_RbVal slot and fails C compile.
21115+ # Issue #580.
21116+ def box_when_arm_to_target(arm_nid, target_t)
21117+ arm_t = infer_type(arm_nid)
21118+ val = compile_expr(arm_nid)
21119+ if target_t == "poly" && arm_t != "" && arm_t != "poly" && arm_t != "void"
21120+ return box_value_to_poly(arm_t, val)
21121+ end
21122+ val
21123+ end
21124+
2110821125 def box_value_to_poly(at, val)
2110921126 nullable = is_nullable_type(at)
2111021127 raw_at = at