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
end end end # Poly-receiver setter (`receiver.data = merged` where # `receiver` is statically poly because it came from # `case ... when X then A.new; when Y then B.new`). # Codegen lowers this to a cls_id-switch dispatch over # every class that has the attr_writer, so each candidate # class's ivar slot receives the RHS at runtime. The # analyzer's per-class ivar table needs to widen those # candidates too -- otherwise A's iv_data stays at the # empty-hash default (str_int_hash) and the per-arm # `((sp_A *)_t.v.p)->iv_data = lv_merged` write mismatches # at the C boundary. Issue #579. if rt == "poly" args_id_p = @nd_arguments[nid] if args_id_p >= 0 arg_ids_p = get_args(args_id_p) if arg_ids_p.length > 0 at_p = infer_type(arg_ids_p[0]) if at_p != "int" && at_p != "nil" iname_p = "@" + bname ci_p = 0 while ci_p < @cls_names.length writers_p = @cls_attr_writers[ci_p].split(";") wk_p = 0 while wk_p < writers_p.length if writers_p[wk_p] == bname update_ivar_type(ci_p, iname_p, at_p) end wk_p = wk_p + 1 end ci_p = ci_p + 1 end end end end end end end endtest/poly_recv_setter_widens_ivar.rbaddedView file
Loading diff…
test/poly_recv_setter_widens_ivar.rb.expectedaddedView file
Loading diff…