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 same default: 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 new cls_is_descendant helper 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

@@ -1648,6 +1648,32 @@
16481648 # `Base.all` (and emit `sp_Base_cls_all(...)`) when Leaf inherits
16491649 # from Base without overriding `.all`. Mirrors cls_method_return's
16501650 # parent walk for instance methods.
1651+ # Walks @cls_parents from `child` looking for `ancestor`.
1652+ # Mirrors codegen's `cls_is_descendant` so the analyze-side
1653+ # divergence check at the `<obj>.class.<m>` inference site
1654+ # can enumerate the recv's descendants without the helper
1655+ # being out of sync. (Codegen has its own copy with the same
1656+ # name; the two diverge only if one side is rebuilt while
1657+ # the other isn't -- the bootstrap fixpoint catches that.)
1658+ def cls_is_descendant(child, ancestor)
1659+ ck = child
1660+ while ck >= 0
1661+ pname = @cls_parents[ck]
1662+ if pname == ""
1663+ return 0
1664+ end
1665+ pi = find_class_idx(pname)
1666+ if pi < 0
1667+ return 0
1668+ end
1669+ if pi == ancestor
1670+ return 1
1671+ end
1672+ ck = pi
1673+ end
1674+ 0
1675+ end
1676+
16511677 def cls_cmethod_owner(ci, mname)
16521678 if ci < 0
16531679 return -1
@@ -3346,12 +3372,51 @@
33463372 if owner_419 >= 0
33473373 cmnames_419 = @cls_cmeth_names[owner_419].split(";")
33483374 cmreturns_419 = @cls_cmeth_returns[owner_419].split(";")
3375+ base_rt_419 = ""
33493376 cmidx_419 = 0
33503377 while cmidx_419 < cmnames_419.length
33513378 if cmnames_419[cmidx_419] == mname && cmidx_419 < cmreturns_419.length
3352- return cmreturns_419[cmidx_419]
3379+ base_rt_419 = cmreturns_419[cmidx_419]
3380+ cmidx_419 = cmnames_419.length
3381+ else
3382+ cmidx_419 = cmidx_419 + 1
3383+ end
3384+ end
3385+ if base_rt_419 != ""
3386+ # Issue #431: when descendants of inner_ci override
3387+ # mname and any override's return type diverges from
3388+ # the base owner's, the codegen-side chained dispatch
3389+ # boxes each arm to sp_RbVal so the unified result
3390+ # temp has a single C type. Widen the inferred type
3391+ # to "poly" so consumers see the boxed value through
3392+ # the poly-dispatch machinery.
3393+ diverged_419 = 0
3394+ ck_419 = 0
3395+ while ck_419 < @cls_names.length
3396+ if ck_419 != inner_ci_419 && cls_is_descendant(ck_419, inner_ci_419) == 1
3397+ own_419 = cls_cmethod_owner(ck_419, mname)
3398+ if own_419 == ck_419
3399+ ck_cmnames = @cls_cmeth_names[ck_419].split(";")
3400+ ck_cmreturns = @cls_cmeth_returns[ck_419].split(";")
3401+ ki_419 = 0
3402+ while ki_419 < ck_cmnames.length
3403+ if ck_cmnames[ki_419] == mname && ki_419 < ck_cmreturns.length
3404+ if ck_cmreturns[ki_419] != base_rt_419
3405+ diverged_419 = 1
3406+ end
3407+ ki_419 = ck_cmnames.length
3408+ else
3409+ ki_419 = ki_419 + 1
3410+ end
3411+ end
3412+ end
3413+ end
3414+ ck_419 = ck_419 + 1
3415+ end
3416+ if diverged_419 == 1
3417+ return "poly"
33533418 end
3354- cmidx_419 = cmidx_419 + 1
3419+ return base_rt_419
33553420 end
33563421 end
33573422 end