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
compile_stmt(ws[i]) i = i + 1 end emit(" " + tmp + " = " + compile_expr(ws.last) + ";") emit(" " + tmp + " = " + box_when_arm_to_target(ws.last, rt) + ";") end end end compile_stmt(ws[i]) i = i + 1 end emit(" " + tmp + " = " + compile_expr(ws.last) + ";") emit(" " + tmp + " = " + box_when_arm_to_target(ws.last, rt) + ";") end end end compile_stmt(es[i]) i = i + 1 end emit(" " + tmp + " = " + compile_expr(es.last) + ";") emit(" " + tmp + " = " + box_when_arm_to_target(es.last, rt) + ";") end end end "(void *)(" + val + ").v.p" end # Compile a `when` / `else` arm's last expression and, when the # case-expression's unified target type is `poly` (sp_RbVal) but # the arm's static type is a typed pointer / scalar, route through # `box_value_to_poly` so the assignment lands in the tagged-union # slot. Without this, a `case ... when A.new; when B.new; end` # whose unified result is poly emits raw `sp_A *` / `sp_B *` # assignments into the sp_RbVal slot and fails C compile. # Issue #580. def box_when_arm_to_target(arm_nid, target_t) arm_t = infer_type(arm_nid) val = compile_expr(arm_nid) if target_t == "poly" && arm_t != "" && arm_t != "poly" && arm_t != "void" return box_value_to_poly(arm_t, val) end val end def box_value_to_poly(at, val) nullable = is_nullable_type(at) raw_at = attest/case_expr_sibling_classes_box.rbaddedView file
Loading diff…
test/case_expr_sibling_classes_box.rb.expectedaddedView file
Loading diff…