Commit 84715d7b

84715d7bed78Merged

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

Description

fix(codegen): mixed-type ternary RHS into ivar slot widens to poly

Issue #131: a single-write ivar whose RHS is a ternary (or if-as- expression) with branches of different concrete types — the carve-out from #130 that the multi-write widening rule didn't catch. The slot typed from infer_ivar_init_type's default fallback ("int") and the emitted C ternary mixed both raw branches into one assignment, producing a -Wconditional-type-mismatch warning and a runtime segfault (@x = use_str ? "hello" : 42).

Three pieces:

(1) infer_ivar_init_type gains an IfNode case that recurses into both branches and unifies with strict comparison. Cannot delegate to unify_return_type — it has an "int is default/unresolved" escape (int + T → T) that's appropriate for method-return inference but silently picks the non-int side here, exactly the conflation that bit us in #130. Strict equality means int + string → poly.

Nullable widening (`T + nil → T?`) is still produced locally, so a
later infer_type-based pass at spinel_codegen.rb:7045 computing the
same `T?` doesn't widen us to poly via update_ivar_type's missing
`T + T? → T?` handler. (That helper has a real gap, but fixing it
sits outside #131 — bigger blast radius across all callers.)

(2) is_definite_ivar_init recognises IfNode as definite when both branches are themselves definite. Lets the #130 multi-write widening rule still fire when a later concrete write disagrees with an IfNode-typed slot.

(3) box_expr_to_poly per-branch boxes IfNode unconditionally in poly contexts (no at == "poly" gate). Same-type branches yield a redundant box that still produces a correct sp_RbVal; mixed-type branches need the per-branch box to avoid C's pointer/integer ternary type mismatch. Gating on infer_type's "poly" answer wouldn't work — unify_return_type's int + T → T rule lets a genuinely mixed-int-string ternary infer as "string", and the pre-#131 code path then sp_box_str'd the entire raw ternary (the same UB).

Test pins seven shapes: ternary mixing String/Integer, Integer/Float, the multi-write + ternary combination (interaction with #130's widening), same-type guards (int and string), if-as-expression form, and nullable ternary. Bootstrap (gen2 == gen3) clean; 199 tests + 56 benches pass.

Out of scope: ternary RHS into local-var declaration (x = cond ? a : b) and method-return position. Those have their own codegen paths and will need parallel boxing fixes if a real reproducer surfaces.

Closes #131

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

Details

Commit
84715d7bed78
Parent
e1ba19c804bb

spinel_codegen.rbmodifiedView file

@@ -5326,6 +5326,37 @@
53265326 if t == "SymbolNode" || t == "TrueNode" || t == "FalseNode"
53275327 return 1
53285328 end
5329+ # Issue #131: a ternary whose branches are themselves definite is
5330+ # definite. Lets the #130 multi-write widening rule still fire when
5331+ # a later concrete write disagrees with an IfNode-typed slot.
5332+ if t == "IfNode"
5333+ then_d = 0
5334+ body = @nd_body[nid]
5335+ if body >= 0
5336+ ts = get_stmts(body)
5337+ if ts.length > 0
5338+ then_d = is_definite_ivar_init(ts.last)
5339+ end
5340+ end
5341+ else_d = 0
5342+ sub = @nd_subsequent[nid]
5343+ if sub >= 0
5344+ if @nd_type[sub] == "ElseNode"
5345+ eb = @nd_body[sub]
5346+ if eb >= 0
5347+ es = get_stmts(eb)
5348+ if es.length > 0
5349+ else_d = is_definite_ivar_init(es.last)
5350+ end
5351+ end
5352+ else
5353+ else_d = is_definite_ivar_init(sub)
5354+ end
5355+ end
5356+ if then_d == 1 && else_d == 1
5357+ return 1
5358+ end
5359+ end
53295360 0
53305361 end
53315362
@@ -5540,6 +5571,62 @@
55405571 return vt
55415572 end
55425573 end
5574+ # Issue #131: ternary / if-as-expression RHS. Recurse into both
5575+ # branches' last statements and unify with strict comparison.
5576+ # Cannot delegate to unify_return_type here — that helper has an
5577+ # "int is default/unresolved" escape hatch (`int + T → T`) which
5578+ # is correct for method-return inference but exactly the
5579+ # conflation that bit us in #130: mixing concrete int and
5580+ # concrete non-int in a ternary needs to widen to poly, not
5581+ # silently pick the non-int side. nil branches still defer to
5582+ # the other type so existing nullable widening
5583+ # (string + nil → string?) flows through update_ivar_type.
5584+ if t == "IfNode"
5585+ then_t = "nil"
5586+ body = @nd_body[nid]
5587+ if body >= 0
5588+ ts = get_stmts(body)
5589+ if ts.length > 0
5590+ then_t = infer_ivar_init_type(ts.last)
5591+ end
5592+ end
5593+ else_t = "nil"
5594+ sub = @nd_subsequent[nid]
5595+ if sub >= 0
5596+ if @nd_type[sub] == "ElseNode"
5597+ eb = @nd_body[sub]
5598+ if eb >= 0
5599+ es = get_stmts(eb)
5600+ if es.length > 0
5601+ else_t = infer_ivar_init_type(es.last)
5602+ end
5603+ end
5604+ else
5605+ else_t = infer_ivar_init_type(sub)
5606+ end
5607+ end
5608+ if then_t == else_t
5609+ return then_t
5610+ end
5611+ # Nullable widening (`T + nil → T?`, `nil + T → T?`) — match
5612+ # unify_return_type's behavior locally so a later `infer_type`-
5613+ # based pass (spinel_codegen.rb:7045) computing the same "T?"
5614+ # doesn't widen us to poly via update_ivar_type's missing
5615+ # T + T? → T? handler.
5616+ if then_t == "nil"
5617+ if is_nullable_pointer_type(else_t) == 1 && is_nullable_type(else_t) == 0
5618+ return else_t + "?"
5619+ end
5620+ return else_t
5621+ end
5622+ if else_t == "nil"
5623+ if is_nullable_pointer_type(then_t) == 1 && is_nullable_type(then_t) == 0
5624+ return then_t + "?"
5625+ end
5626+ return then_t
5627+ end
5628+ return "poly"
5629+ end
55435630 "int"
55445631 end
55455632
@@ -18337,6 +18424,44 @@
1833718424 end
1833818425
1833918426 def box_expr_to_poly(nid)
18427+ # Issue #131: ternary / if-as-expression whose branches may have
18428+ # different concrete types. Per-branch box unconditionally — same-
18429+ # type branches yield a redundant box that still produces correct
18430+ # sp_RbVal, and mixed-type branches need the per-branch box to
18431+ # avoid C's pointer/integer ternary-type-mismatch (which lands as
18432+ # an unsafe cast in the poly slot and segfaults).
18433+ # Gating on infer_type's "poly" answer would not work: unify_return
18434+ # _type treats int as default/unresolved (`int + T → T`), so a
18435+ # genuinely mixed-int-string ternary infers as "string" and the
18436+ # gate misses it. The pre-#131 emit then `sp_box_str`'d the entire
18437+ # raw ternary, which is the same UB.
18438+ if nid >= 0 && @nd_type[nid] == "IfNode"
18439+ cond = compile_cond_expr(@nd_predicate[nid])
18440+ then_v = "sp_box_nil()"
18441+ body = @nd_body[nid]
18442+ if body >= 0
18443+ ts = get_stmts(body)
18444+ if ts.length > 0
18445+ then_v = box_expr_to_poly(ts.last)
18446+ end
18447+ end
18448+ else_v = "sp_box_nil()"
18449+ sub = @nd_subsequent[nid]
18450+ if sub >= 0
18451+ if @nd_type[sub] == "ElseNode"
18452+ eb = @nd_body[sub]
18453+ if eb >= 0
18454+ es = get_stmts(eb)
18455+ if es.length > 0
18456+ else_v = box_expr_to_poly(es.last)
18457+ end
18458+ end
18459+ else
18460+ else_v = box_expr_to_poly(sub)
18461+ end
18462+ end
18463+ return "(" + cond + " ? " + then_v + " : " + else_v + ")"
18464+ end
1834018465 at = infer_type(nid)
1834118466 val = compile_expr(nid)
1834218467 if at == "poly"