Commit e341e5e2
e341e5e20470Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(codegen): inherited class methods dispatch through parent chain
Leaf.all where def self.all lives on Base (class Leaf < Base) silently emitted 0 with the warning "cannot resolve call
to 'all' on class_Leaf". The def self.<m> lookup only walked
@cls_cmeth_names[ci] for the immediate class — no parent walk
like the instance-method side already had.
Add cls_cmethod_owner / cls_cmethod_return_inherited helpers that walk @cls_parents in the same shape as cls_method_return for instance methods. Wire them into both spots:
- infer_constant_recv_type: returns the parent's cm_returns entry so chained calls on the result type-check.
- compile_constant_recv_method (the call dispatcher): emits
sp_<OwnerClass>_cls_<m>(...)so the call binds to the actually-defined function, not a never-emitted Leaf-prefixed one.
Single-level (Leaf < Base) and multi-level (Deep < Mid < Base) inheritance both verified.
Out of scope (separate pre-existing gap visible with
Base.greet("hello") direct calls): class method parameter
type widening from call-site argument types — unrelated to the
inheritance dispatch fixed here.
Closes #208
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- e341e5e20470
- Parent
- c2520d01c6ca
spinel_codegen.rbmodifiedView file
end # Get method return type from class # Issue #208: walk the parent chain looking for a class method # (`def self.<mname>`) named `mname`. Returns the class index that # defines it, or -1 if not found. Used so `Leaf.all` resolves to # `Base.all` (and emits `sp_Base_cls_all(...)`) when Leaf inherits # from Base without overriding `.all`. Mirrors cls_method_return's # parent walk for instance methods. def cls_cmethod_owner(ci, mname) if ci < 0 return -1 end cmnames = @cls_cmeth_names[ci].split(";") cj = 0 while cj < cmnames.length if cmnames[cj] == mname return ci end cj = cj + 1 end if @cls_parents[ci] != "" pi = find_class_idx(@cls_parents[ci]) if pi >= 0 return cls_cmethod_owner(pi, mname) end end -1 end def cls_cmethod_return_inherited(ci, mname) owner = cls_cmethod_owner(ci, mname) if owner < 0 return "" end cmnames = @cls_cmeth_names[owner].split(";") cm_returns = @cls_cmeth_returns[owner].split(";") cj = 0 while cj < cmnames.length if cmnames[cj] == mname if cj < cm_returns.length return cm_returns[cj] end return "" end cj = cj + 1 end "" end def cls_method_return(ci, mname) names = @cls_meth_names[ci].split(";") returns = @cls_meth_returns[ci].split(";") if mname == "new" return "obj_" + rcname end cmnames = @cls_cmeth_names[ci2].split(";") cm_returns = @cls_cmeth_returns[ci2].split(";") cj = 0 while cj < cmnames.length if cmnames[cj] == mname if cj < cm_returns.length && cm_returns[cj] != "" && cm_returns[cj] != "int" return cm_returns[cj] end end cj = cj + 1 # Issue #208: walk the parent chain so an inherited # `def self.<mname>` on a base class resolves correctly # when called on the subclass (e.g. `Leaf.all` → # `Base.all`'s return type). inherited_rt = cls_cmethod_return_inherited(ci2, mname) if inherited_rt != "" && inherited_rt != "int" return inherited_rt end end # Issue #127: same lookup for module class methods. They live in end mi2 = mi2 + 1 end # Class method dispatch (def self.xxx) # Class method dispatch (def self.xxx). Issue #208: walk the # parent chain so `Leaf.all` resolves to `Base.all`'s emitter # (`sp_Base_cls_all`, not `sp_Leaf_cls_all` which is never # generated when Leaf doesn't override). ci3 = find_class_idx(rcname) if ci3 >= 0 cmnames = @cls_cmeth_names[ci3].split(";") cj = 0 while cj < cmnames.length if cmnames[cj] == mname ca = compile_call_args(nid) if ca != "" return "sp_" + rcname + "_cls_" + sanitize_name(mname) + "(" + ca + ")" else return "sp_" + rcname + "_cls_" + sanitize_name(mname) + "()" end owner_ci = cls_cmethod_owner(ci3, mname) if owner_ci >= 0 owner_name = @cls_names[owner_ci] ca = compile_call_args(nid) if ca != "" return "sp_" + owner_name + "_cls_" + sanitize_name(mname) + "(" + ca + ")" else return "sp_" + owner_name + "_cls_" + sanitize_name(mname) + "()" end cj = cj + 1 end end endtest/issue208_inherited_class_method.rbaddedView file
Loading diff…
test/issue208_inherited_class_method.rb.expectedaddedView file
Loading diff…