Commit 0b501e4f
0b501e4f4758Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(codegen): self.class. dispatch with divergent return types (#431)
#422's switch lowering for self.class. was gated on all candidate owners returning the same type -- the canonical case in #422's test (Base.label and Child.label both return strings). The "abstract base class raises, subclass returns a concrete value" Rails pattern (and the milder "different concrete types per override") fell through to a direct static call to the base cmeth, so:
- Article.new.describe in Sam's repro raised, despite Article overriding the cmeth with a concrete string.
- Concrete-but-different return types silently monomorphized against the base's signature (Base.label returns 0, Article.label returns "articles" -- Article's body never ran).
Two-sided fix:
-
codegen: the divergent-types branch in compile_object_method_expr's recv_type == "class" arm boxes each arm's return into sp_RbVal so the unified result temp has a single C type. The base call also moves from the temp-initializer into a
default:arm, so an abstract-base raise stub only fires when the runtime cls_id genuinely is the base (or an unmodelled type), not on every invocation. The matching-types path gets the samedefault:restructure for symmetry -- pre-fix it ran the base call as the temp initializer, so a hypothetical raise stub on the base would have fired through the matching-types path too even though no existing test exercised it. -
analyze: infer_type for
<obj>.class.<m>chain enumerates the recv's descendants and widens to "poly" when any override's return type diverges from the base's. Downstream callers (puts, string concat, method args, etc.) consume the boxed sp_RbVal through the existing poly-dispatch path. A newcls_is_descendanthelper on the analyze side mirrors the codegen helper of the same name so the descendant walk stays in sync.
Coverage in test/self_class_divergent_returns.rb: Variant 1 (raise base + string override -- the headline shape), plus Variant 2 (int base + string override with both routes exercised) covers the divergent concrete-types case.
Bootstrap 4-way fixpoint OK; optcarrot checksum 59662 unchanged; 385 tests pass.
Closes #431 Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- 0b501e4f4758
- Parent
- fa16fa55d4e2
spinel_analyze.rbmodifiedView file
# `Base.all` (and emit `sp_Base_cls_all(...)`) when Leaf inherits # from Base without overriding `.all`. Mirrors cls_method_return's # parent walk for instance methods. # Walks @cls_parents from `child` looking for `ancestor`. # Mirrors codegen's `cls_is_descendant` so the analyze-side # divergence check at the `<obj>.class.<m>` inference site # can enumerate the recv's descendants without the helper # being out of sync. (Codegen has its own copy with the same # name; the two diverge only if one side is rebuilt while # the other isn't -- the bootstrap fixpoint catches that.) def cls_is_descendant(child, ancestor) ck = child while ck >= 0 pname = @cls_parents[ck] if pname == "" return 0 end pi = find_class_idx(pname) if pi < 0 return 0 end if pi == ancestor return 1 end ck = pi end 0 end def cls_cmethod_owner(ci, mname) if ci < 0 return -1 if owner_419 >= 0 cmnames_419 = @cls_cmeth_names[owner_419].split(";") cmreturns_419 = @cls_cmeth_returns[owner_419].split(";") base_rt_419 = "" cmidx_419 = 0 while cmidx_419 < cmnames_419.length if cmnames_419[cmidx_419] == mname && cmidx_419 < cmreturns_419.length return cmreturns_419[cmidx_419] base_rt_419 = cmreturns_419[cmidx_419] cmidx_419 = cmnames_419.length else cmidx_419 = cmidx_419 + 1 end end if base_rt_419 != "" # Issue #431: when descendants of inner_ci override # mname and any override's return type diverges from # the base owner's, the codegen-side chained dispatch # boxes each arm to sp_RbVal so the unified result # temp has a single C type. Widen the inferred type # to "poly" so consumers see the boxed value through # the poly-dispatch machinery. diverged_419 = 0 ck_419 = 0 while ck_419 < @cls_names.length if ck_419 != inner_ci_419 && cls_is_descendant(ck_419, inner_ci_419) == 1 own_419 = cls_cmethod_owner(ck_419, mname) if own_419 == ck_419 ck_cmnames = @cls_cmeth_names[ck_419].split(";") ck_cmreturns = @cls_cmeth_returns[ck_419].split(";") ki_419 = 0 while ki_419 < ck_cmnames.length if ck_cmnames[ki_419] == mname && ki_419 < ck_cmreturns.length if ck_cmreturns[ki_419] != base_rt_419 diverged_419 = 1 end ki_419 = ck_cmnames.length else ki_419 = ki_419 + 1 end end end end ck_419 = ck_419 + 1 end if diverged_419 == 1 return "poly" end cmidx_419 = cmidx_419 + 1 return base_rt_419 end end endspinel_codegen.rbmodifiedView file
Loading diff…
test/self_class_divergent_returns.rbaddedView file
Loading diff…
test/self_class_divergent_returns.rb.expectedaddedView file
Loading diff…