Commit c3adf401

c3adf401f22eMerged

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

Description

fix(analyze): unify override-family ptypes when any member is poly

Sibling of #563. When an override widens its value param to sp_RbVal because its callers pass mixed types (e.g. a[:id] = 42 and a[:title] = "Other"), but the parent's same param stays scalar (e.g. const char *), the codegen cls_imeth_override_ptypes_match gate rejects the family on signature mismatch. A self[k] = v inside a parent body then falls through to a static call on the parent's raise stub instead of dispatching to the override.

Post-fixpoint, mirror unify_imeth_override_returns for params: for each (base, descendants) family of imeths, if any slot has "poly" anywhere in the family, widen every member's slot to "poly" too. The codegen override-dispatch gate then accepts the family and the cls_id switch routes a self-receiver call to the concrete override.

Run the pass twice in the existing settle sequence (before each unify_imeth_override_returns call) so any return-type re-derivation that follows sees the widened param shape.

Fixes #567.

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

Details

Commit
c3adf401f22e
Parent
79f3d6040b94

spinel_analyze.rbmodifiedView file

@@ -16058,6 +16058,100 @@
1605816058 end
1605916059 end
1606016060
16061+ # Sibling of unify_imeth_override_returns for param types.
16062+ # When a base imeth and a descendant override disagree on a
16063+ # param slot because the override widened to "poly" (mixed
16064+ # call-site arg types), widen every member of the family at
16065+ # that slot to "poly" as well. Without this the codegen
16066+ # override-dispatch gate cls_imeth_override_ptypes_match
16067+ # rejects the family (signatures differ), and a `self[k]=v`
16068+ # in the base body lands on the static raise-stub instead
16069+ # of dispatching to the override. Issue #567.
16070+ def unify_imeth_override_ptypes
16071+ ci = 0
16072+ while ci < @cls_names.length
16073+ mnames = @cls_meth_names[ci].split(";")
16074+ mi = 0
16075+ while mi < mnames.length
16076+ mname = mnames[mi]
16077+ if mname != ""
16078+ base_pt = cls_meth_ptypes_get(ci, mi)
16079+ fam_owners = []
16080+ fam_midxs = []
16081+ fam_owners.push(ci)
16082+ fam_midxs.push(mi)
16083+ dci = 0
16084+ while dci < @cls_names.length
16085+ if dci != ci && cls_is_descendant(dci, ci) == 1
16086+ dmidx = cls_find_method_direct(dci, mname)
16087+ if dmidx >= 0
16088+ fam_owners.push(dci)
16089+ fam_midxs.push(dmidx)
16090+ end
16091+ end
16092+ dci = dci + 1
16093+ end
16094+ if fam_owners.length >= 2
16095+ n = base_pt.length
16096+ # Determine, per slot, whether at least one family member
16097+ # has "poly". If so, widen every member at that slot.
16098+ poly_slot = "".split(",")
16099+ k = 0
16100+ while k < n
16101+ poly_slot.push("0")
16102+ k = k + 1
16103+ end
16104+ fi = 0
16105+ while fi < fam_owners.length
16106+ pt = cls_meth_ptypes_get(fam_owners[fi], fam_midxs[fi])
16107+ if pt.length == n
16108+ kk = 0
16109+ while kk < n
16110+ if pt[kk] == "poly"
16111+ poly_slot[kk] = "1"
16112+ end
16113+ kk = kk + 1
16114+ end
16115+ end
16116+ fi = fi + 1
16117+ end
16118+ any_poly = 0
16119+ kk2 = 0
16120+ while kk2 < n
16121+ if poly_slot[kk2] == "1"
16122+ any_poly = 1
16123+ end
16124+ kk2 = kk2 + 1
16125+ end
16126+ if any_poly == 1
16127+ fi2 = 0
16128+ while fi2 < fam_owners.length
16129+ pt2 = cls_meth_ptypes_get(fam_owners[fi2], fam_midxs[fi2])
16130+ if pt2.length == n
16131+ changed = 0
16132+ kk3 = 0
16133+ while kk3 < n
16134+ if poly_slot[kk3] == "1" && pt2[kk3] != "poly"
16135+ pt2[kk3] = "poly"
16136+ changed = 1
16137+ end
16138+ kk3 = kk3 + 1
16139+ end
16140+ if changed == 1
16141+ cls_meth_ptypes_put(fam_owners[fi2], fam_midxs[fi2], pt2)
16142+ end
16143+ end
16144+ fi2 = fi2 + 1
16145+ end
16146+ end
16147+ end
16148+ end
16149+ mi = mi + 1
16150+ end
16151+ ci = ci + 1
16152+ end
16153+ end
16154+
1606116155 def infer_all_returns
1606216156 # Pre-pass: infer class method param types from body usage
1606316157 infer_cls_meth_param_from_body
@@ -18812,8 +18906,10 @@
1881218906 # Base#[]'s own body return as "int" (raise default),
1881318907 # clobbering step 1's write.
1881418908 # 3. unify again -- restore Base#[] to "string".
18909+ unify_imeth_override_ptypes
1881518910 unify_imeth_override_returns
1881618911 infer_all_returns
18912+ unify_imeth_override_ptypes
1881718913 unify_imeth_override_returns
1881818914 infer_all_returns
1881918915 unify_imeth_override_returns