Commit 9638f6ec

9638f6ec14f2Merged

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

Description

fix(codegen): bare imeth call dispatches to subclass override (#436)

A bare-receiver instance method call (hook not self.hook) from inside a parent-defined method statically resolved against the method's owning class and routed every call to the parent's imeth. When the runtime instance was a subclass that overrode the imeth (the canonical abstract-base + override template), the dispatch landed on the parent's stub and the override never ran.

Imeth analog of the #422 fix: when descendants of @current_class_idx override mname as an instance method, lower the call site to a switch (self->cls_id) with the static base call moved into the default: arm so abstract stubs that raise don't fire eagerly before the subclass case is reached.

Gates mirror the cmeth path: non-value-type, no &block param, return types match across overrides (compared by c_type, so int/nil/void/bool unify), and param types match exactly. The param-type gate is conservative: when a base call site passes an arg whose type doesn't match an override's signature (the override wasn't widened because the bare-receiver path didn't reach it at inference time), the fix falls back to static dispatch rather than emitting a broken C call. The full multi-arg widening shape is deferred -- it's a separate concern from the dispatch lowering.

Closes #436

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

Details

Commit
9638f6ec14f2
Parent
ba1d1f038b3f

spinel_codegen.rbmodifiedView file

@@ -1958,6 +1958,80 @@
19581958 out
19591959 end
19601960
1961+ # Walk the parent chain looking for an instance method named
1962+ # `mname`. Returns the class index that defines it (the owner),
1963+ # or -1. Imeth analog of cls_cmethod_owner.
1964+ def cls_imethod_owner(ci, mname)
1965+ if ci < 0
1966+ return -1
1967+ end
1968+ mnames = @cls_meth_names[ci].split(";")
1969+ cj = 0
1970+ while cj < mnames.length
1971+ if mnames[cj] == mname
1972+ return ci
1973+ end
1974+ cj = cj + 1
1975+ end
1976+ if @cls_parents[ci] != ""
1977+ pi = find_class_idx(@cls_parents[ci])
1978+ if pi >= 0
1979+ return cls_imethod_owner(pi, mname)
1980+ end
1981+ end
1982+ -1
1983+ end
1984+
1985+ # Enumerate descendants of inner_ci (including inner_ci itself)
1986+ # whose imeth owner differs from base_owner. Imeth analog of
1987+ # cls_cmeth_override_descendants. Used by the bare-receiver imeth
1988+ # dispatch site to decide whether a cls_id switch is needed.
1989+ def cls_imeth_override_descendants(inner_ci, base_owner, mname)
1990+ out = ""
1991+ ck = 0
1992+ while ck < @cls_names.length
1993+ if ck == inner_ci || cls_is_descendant(ck, inner_ci) == 1
1994+ ow = cls_imethod_owner(ck, mname)
1995+ if ow >= 0 && ow != base_owner
1996+ if out != ""
1997+ out = out + ";"
1998+ end
1999+ out = out + ck.to_s + "," + ow.to_s
2000+ end
2001+ end
2002+ ck = ck + 1
2003+ end
2004+ out
2005+ end
2006+
2007+ # Whether all imeth overrides in `pairs_str` have a param-type
2008+ # signature compatible with `base_ptypes` (and same length).
2009+ # Used as a gate on the cls_id-switch dispatch — without this,
2010+ # an override declared with narrower param types than the base
2011+ # would emit a C call site that doesn't match its signature.
2012+ def cls_imeth_override_ptypes_match(pairs_str, mname, base_ptypes)
2013+ pairs = pairs_str.split(";")
2014+ pi = 0
2015+ while pi < pairs.length
2016+ p = pairs[pi].split(",")
2017+ cand_owner = p[1].to_i
2018+ cand_midx = cls_find_method_direct(cand_owner, mname)
2019+ cand_pt = cls_meth_ptypes_get(cand_owner, cand_midx)
2020+ if cand_pt.length != base_ptypes.length
2021+ return 0
2022+ end
2023+ k = 0
2024+ while k < base_ptypes.length
2025+ if cand_pt[k] != base_ptypes[k]
2026+ return 0
2027+ end
2028+ k = k + 1
2029+ end
2030+ pi = pi + 1
2031+ end
2032+ 1
2033+ end
2034+
19612035 # `<arr>.method(:op)` for built-in array types: which (recv_type,
19622036 # mname) pairs we can lower into a Method-dispatch adapter. Limited
19632037 # to int_array's bracket ops + push for now (the optcarrot CPU
@@ -12542,6 +12616,66 @@
1254212616 if owner != "" && owner != @cls_names[@current_class_idx]
1254312617 recv_arg = "(sp_" + owner + " *)" + self_expr
1254412618 end
12619+ # When descendants of @current_class_idx override `mname` as an
12620+ # instance method, the static call below routes everything to
12621+ # the resolved owner — wrong when the runtime instance is a
12622+ # subclass with its own override (the canonical "abstract base
12623+ # method overridden by concrete subclass" template). Lower to
12624+ # a switch on `self->cls_id` so the dispatch lands on the
12625+ # override. Imeth analog of the cmeth path's cls_id switch.
12626+ # Gated on: non-value-type, has_proc absent, return types
12627+ # match across overrides, and param types match (the bare
12628+ # call site doesn't propagate arg types to override defs, so
12629+ # mismatched signatures would emit a broken C call).
12630+ if owner_ci >= 0 && @cls_is_value_type[@current_class_idx] == 0 && has_proc == 0
12631+ ovr_pairs = cls_imeth_override_descendants(@current_class_idx, owner_ci, mname)
12632+ if ovr_pairs != ""
12633+ base_rt = cls_method_return(owner_ci, mname)
12634+ base_rt_c = c_type(base_rt)
12635+ rt_ok = 1
12636+ ovr_list = ovr_pairs.split(";")
12637+ ol = 0
12638+ while ol < ovr_list.length
12639+ p = ovr_list[ol].split(",")
12640+ cand_owner = p[1].to_i
12641+ cand_rt = cls_method_return(cand_owner, mname)
12642+ if c_type(cand_rt) != base_rt_c
12643+ rt_ok = 0
12644+ ol = ovr_list.length
12645+ else
12646+ ol = ol + 1
12647+ end
12648+ end
12649+ base_ptypes = cls_meth_ptypes_get(owner_ci, owner_midx)
12650+ pt_ok = cls_imeth_override_ptypes_match(ovr_pairs, mname, base_ptypes)
12651+ if rt_ok == 1 && pt_ok == 1
12652+ tmp = new_temp
12653+ rt_c = c_type(base_rt)
12654+ default_call = "sp_" + owner + "_" + sanitize_name(mname) + "(" + recv_arg + build_call_tail(ca, bp) + ")"
12655+ # Initialize tmp to a type-default so the base call only runs
12656+ # in the default arm — calling it eagerly before the switch
12657+ # would raise from an abstract stub before any subclass arm
12658+ # gets a chance to dispatch.
12659+ emit(" " + rt_c + " " + tmp + " = " + c_return_default(base_rt) + ";")
12660+ emit(" switch (" + self_expr + "->cls_id) {")
12661+ ol2 = 0
12662+ while ol2 < ovr_list.length
12663+ p2 = ovr_list[ol2].split(",")
12664+ cand_internal_ci = p2[0].to_i
12665+ cand_cid = cls_id_for_user_internal(cand_internal_ci)
12666+ cand_owner2 = p2[1].to_i
12667+ cand_name = @cls_names[cand_owner2]
12668+ cand_recv = "(sp_" + cand_name + " *)" + self_expr
12669+ cand_call = "sp_" + cand_name + "_" + sanitize_name(mname) + "(" + cand_recv + build_call_tail(ca, bp) + ")"
12670+ emit(" case " + cand_cid.to_s + "LL: " + tmp + " = " + cand_call + "; break;")
12671+ ol2 = ol2 + 1
12672+ end
12673+ emit(" default: " + tmp + " = " + default_call + "; break;")
12674+ emit(" }")
12675+ return tmp
12676+ end
12677+ end
12678+ end
1254512679 return "sp_" + owner + "_" + sanitize_name(mname) + "(" + recv_arg + build_call_tail(ca, bp) + ")"
1254612680 end
1254712681 # Check attr_readers (bare method call like `x` meaning self.x)