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
end end # Sibling of unify_imeth_override_returns for param types. # When a base imeth and a descendant override disagree on a # param slot because the override widened to "poly" (mixed # call-site arg types), widen every member of the family at # that slot to "poly" as well. Without this the codegen # override-dispatch gate cls_imeth_override_ptypes_match # rejects the family (signatures differ), and a `self[k]=v` # in the base body lands on the static raise-stub instead # of dispatching to the override. Issue #567. def unify_imeth_override_ptypes ci = 0 while ci < @cls_names.length mnames = @cls_meth_names[ci].split(";") mi = 0 while mi < mnames.length mname = mnames[mi] if mname != "" base_pt = cls_meth_ptypes_get(ci, mi) fam_owners = [] fam_midxs = [] fam_owners.push(ci) fam_midxs.push(mi) dci = 0 while dci < @cls_names.length if dci != ci && cls_is_descendant(dci, ci) == 1 dmidx = cls_find_method_direct(dci, mname) if dmidx >= 0 fam_owners.push(dci) fam_midxs.push(dmidx) end end dci = dci + 1 end if fam_owners.length >= 2 n = base_pt.length # Determine, per slot, whether at least one family member # has "poly". If so, widen every member at that slot. poly_slot = "".split(",") k = 0 while k < n poly_slot.push("0") k = k + 1 end fi = 0 while fi < fam_owners.length pt = cls_meth_ptypes_get(fam_owners[fi], fam_midxs[fi]) if pt.length == n kk = 0 while kk < n if pt[kk] == "poly" poly_slot[kk] = "1" end kk = kk + 1 end end fi = fi + 1 end any_poly = 0 kk2 = 0 while kk2 < n if poly_slot[kk2] == "1" any_poly = 1 end kk2 = kk2 + 1 end if any_poly == 1 fi2 = 0 while fi2 < fam_owners.length pt2 = cls_meth_ptypes_get(fam_owners[fi2], fam_midxs[fi2]) if pt2.length == n changed = 0 kk3 = 0 while kk3 < n if poly_slot[kk3] == "1" && pt2[kk3] != "poly" pt2[kk3] = "poly" changed = 1 end kk3 = kk3 + 1 end if changed == 1 cls_meth_ptypes_put(fam_owners[fi2], fam_midxs[fi2], pt2) end end fi2 = fi2 + 1 end end end end mi = mi + 1 end ci = ci + 1 end end def infer_all_returns # Pre-pass: infer class method param types from body usage infer_cls_meth_param_from_body # Base#[]'s own body return as "int" (raise default), # clobbering step 1's write. # 3. unify again -- restore Base#[] to "string". unify_imeth_override_ptypes unify_imeth_override_returns infer_all_returns unify_imeth_override_ptypes unify_imeth_override_returns infer_all_returns unify_imeth_override_returnstest/imeth_override_widen_to_poly.rbaddedView file
Loading diff…
test/imeth_override_widen_to_poly.rb.expectedaddedView file
Loading diff…