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

@@ -1273,6 +1273,53 @@
12731273 end
12741274
12751275 # Get method return type from class
1276+ # Issue #208: walk the parent chain looking for a class method
1277+ # (`def self.<mname>`) named `mname`. Returns the class index that
1278+ # defines it, or -1 if not found. Used so `Leaf.all` resolves to
1279+ # `Base.all` (and emits `sp_Base_cls_all(...)`) when Leaf inherits
1280+ # from Base without overriding `.all`. Mirrors cls_method_return's
1281+ # parent walk for instance methods.
1282+ def cls_cmethod_owner(ci, mname)
1283+ if ci < 0
1284+ return -1
1285+ end
1286+ cmnames = @cls_cmeth_names[ci].split(";")
1287+ cj = 0
1288+ while cj < cmnames.length
1289+ if cmnames[cj] == mname
1290+ return ci
1291+ end
1292+ cj = cj + 1
1293+ end
1294+ if @cls_parents[ci] != ""
1295+ pi = find_class_idx(@cls_parents[ci])
1296+ if pi >= 0
1297+ return cls_cmethod_owner(pi, mname)
1298+ end
1299+ end
1300+ -1
1301+ end
1302+
1303+ def cls_cmethod_return_inherited(ci, mname)
1304+ owner = cls_cmethod_owner(ci, mname)
1305+ if owner < 0
1306+ return ""
1307+ end
1308+ cmnames = @cls_cmeth_names[owner].split(";")
1309+ cm_returns = @cls_cmeth_returns[owner].split(";")
1310+ cj = 0
1311+ while cj < cmnames.length
1312+ if cmnames[cj] == mname
1313+ if cj < cm_returns.length
1314+ return cm_returns[cj]
1315+ end
1316+ return ""
1317+ end
1318+ cj = cj + 1
1319+ end
1320+ ""
1321+ end
1322+
12761323 def cls_method_return(ci, mname)
12771324 names = @cls_meth_names[ci].split(";")
12781325 returns = @cls_meth_returns[ci].split(";")
@@ -3251,16 +3298,13 @@
32513298 if mname == "new"
32523299 return "obj_" + rcname
32533300 end
3254- cmnames = @cls_cmeth_names[ci2].split(";")
3255- cm_returns = @cls_cmeth_returns[ci2].split(";")
3256- cj = 0
3257- while cj < cmnames.length
3258- if cmnames[cj] == mname
3259- if cj < cm_returns.length && cm_returns[cj] != "" && cm_returns[cj] != "int"
3260- return cm_returns[cj]
3261- end
3262- end
3263- cj = cj + 1
3301+ # Issue #208: walk the parent chain so an inherited
3302+ # `def self.<mname>` on a base class resolves correctly
3303+ # when called on the subclass (e.g. `Leaf.all` →
3304+ # `Base.all`'s return type).
3305+ inherited_rt = cls_cmethod_return_inherited(ci2, mname)
3306+ if inherited_rt != "" && inherited_rt != "int"
3307+ return inherited_rt
32643308 end
32653309 end
32663310 # Issue #127: same lookup for module class methods. They live in
@@ -19696,21 +19740,21 @@
1969619740 end
1969719741 mi2 = mi2 + 1
1969819742 end
19699- # Class method dispatch (def self.xxx)
19743+ # Class method dispatch (def self.xxx). Issue #208: walk the
19744+ # parent chain so `Leaf.all` resolves to `Base.all`'s emitter
19745+ # (`sp_Base_cls_all`, not `sp_Leaf_cls_all` which is never
19746+ # generated when Leaf doesn't override).
1970019747 ci3 = find_class_idx(rcname)
1970119748 if ci3 >= 0
19702- cmnames = @cls_cmeth_names[ci3].split(";")
19703- cj = 0
19704- while cj < cmnames.length
19705- if cmnames[cj] == mname
19706- ca = compile_call_args(nid)
19707- if ca != ""
19708- return "sp_" + rcname + "_cls_" + sanitize_name(mname) + "(" + ca + ")"
19709- else
19710- return "sp_" + rcname + "_cls_" + sanitize_name(mname) + "()"
19711- end
19749+ owner_ci = cls_cmethod_owner(ci3, mname)
19750+ if owner_ci >= 0
19751+ owner_name = @cls_names[owner_ci]
19752+ ca = compile_call_args(nid)
19753+ if ca != ""
19754+ return "sp_" + owner_name + "_cls_" + sanitize_name(mname) + "(" + ca + ")"
19755+ else
19756+ return "sp_" + owner_name + "_cls_" + sanitize_name(mname) + "()"
1971219757 end
19713- cj = cj + 1
1971419758 end
1971519759 end
1971619760 end