Commit d48348e8

d48348e846f8Merged

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

Description

fix(analyze): widen ivar via poly-recv attr_accessor setter

When recv.attr = val has a statically-poly receiver (from a case-expression returning sibling-class instances etc.), codegen lowers the setter to a cls_id-switch dispatch:

if (_t.cls_id == 22) { ((sp_A *)_t.v.p)->iv_x = rhs; ... }
if (_t.cls_id == 23) { ((sp_B *)_t.v.p)->iv_x = rhs; ... }

Every candidate class's iv_x slot receives the RHS at runtime, but the analyzer's scan_writer_calls only observed setters when recv was a single obj_X (is_obj_type(rt) == 1). For poly recv, no class's ivar table widened against the RHS, so iv_x stayed at whatever its initialize-time default fixed it to (e.g. @data = {} → sp_StrIntHash *). The per-arm assignment then mismatched the wider RHS at the C boundary with -Wincompatible-pointer-types.

Add a poly-recv arm: iterate every class declaring the matching attr_writer and call update_ivar_type on each. Mirrors the codegen dispatch's behavior. The existing update_ivar_type already widens conflicting types to poly via its heterogeneity gate, so the codegen-side per-arm assignment lands in the sp_RbVal slot via sp_box_obj. Issue #579.

Fixes #579.

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

Details

Commit
d48348e846f8
Parent
4834315f8dd4

spinel_analyze.rbmodifiedView file

@@ -15499,6 +15499,41 @@
1549915499 end
1550015500 end
1550115501 end
15502+ # Poly-receiver setter (`receiver.data = merged` where
15503+ # `receiver` is statically poly because it came from
15504+ # `case ... when X then A.new; when Y then B.new`).
15505+ # Codegen lowers this to a cls_id-switch dispatch over
15506+ # every class that has the attr_writer, so each candidate
15507+ # class's ivar slot receives the RHS at runtime. The
15508+ # analyzer's per-class ivar table needs to widen those
15509+ # candidates too -- otherwise A's iv_data stays at the
15510+ # empty-hash default (str_int_hash) and the per-arm
15511+ # `((sp_A *)_t.v.p)->iv_data = lv_merged` write mismatches
15512+ # at the C boundary. Issue #579.
15513+ if rt == "poly"
15514+ args_id_p = @nd_arguments[nid]
15515+ if args_id_p >= 0
15516+ arg_ids_p = get_args(args_id_p)
15517+ if arg_ids_p.length > 0
15518+ at_p = infer_type(arg_ids_p[0])
15519+ if at_p != "int" && at_p != "nil"
15520+ iname_p = "@" + bname
15521+ ci_p = 0
15522+ while ci_p < @cls_names.length
15523+ writers_p = @cls_attr_writers[ci_p].split(";")
15524+ wk_p = 0
15525+ while wk_p < writers_p.length
15526+ if writers_p[wk_p] == bname
15527+ update_ivar_type(ci_p, iname_p, at_p)
15528+ end
15529+ wk_p = wk_p + 1
15530+ end
15531+ ci_p = ci_p + 1
15532+ end
15533+ end
15534+ end
15535+ end
15536+ end
1550215537 end
1550315538 end
1550415539 end