Commit f5e97307

f5e973070dfeMerged

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

Description

fix(codegen): include attr_reader accessors in poly-receiver dispatch

A method call on a polymorphic receiver where the candidate classes declare the accessor as attr_reader :v (instead of def v; @v; end) silently emitted an empty cls_id-keyed if-cascade — the result temp stayed at its 0 default. The dispatcher only walked @cls_meth_names and never consulted @cls_attr_readers, so the auto-registered accessor was invisible to it. Discovered while landing #118.

Two fixes:

  • poly_dispatch_return_type now also unions ivar types from each class's attr_readers when no explicit method matches. The result stays a single concrete type when every class agrees on the ivar type, falls back to poly when they disagree.
  • compile_poly_method_call's user-class loop adds an else if cls_has_attr_reader(i, mname) arm that emits ((sp_<C> *)recv.v.p)->iv_<name> (or boxes that ivar value via box_val_to_poly when the result is poly).

test/poly_dispatch_attr_reader.rb covers two same-typed classes (C/D both attr_reader :v → string), plus a class mixing an explicit def v with another class's attr_reader so the dispatcher's two branches (method-walk and attr_reader-walk) coexist.

A more elaborate variant — different ivar types per class so the result is poly and the caller chains another method on it — sits on top of separate poly-inspect plumbing and isn't part of this fix.

Closes #119

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

Details

Commit
f5e973070dfe
Parent
ec0f76891c9b

spinel_codegen.rbmodifiedView file

@@ -17824,12 +17824,32 @@
1782417824 # that concrete type is used. If any two disagree, the call is
1782517825 # genuinely polymorphic and the caller must treat the result as
1782617826 # an sp_RbVal.
17827+ # Returns 1 if class `ci` declares `mname` as an attr_reader (in
17828+ # which case `obj.<mname>` reads `obj->iv_<mname>`).
17829+ def cls_has_attr_reader(ci, mname)
17830+ readers = @cls_attr_readers[ci].split(";")
17831+ j = 0
17832+ while j < readers.length
17833+ if readers[j] == mname
17834+ return 1
17835+ end
17836+ j = j + 1
17837+ end
17838+ 0
17839+ end
17840+
1782717841 def poly_dispatch_return_type(mname)
1782817842 common = ""
1782917843 ci = 0
1783017844 while ci < @cls_names.length
17845+ rt = ""
1783117846 if cls_find_method_direct(ci, mname) >= 0
1783217847 rt = cls_method_return(ci, mname)
17848+ elsif cls_has_attr_reader(ci, mname) == 1
17849+ # An attr_reader returns the ivar type. Issue #119.
17850+ rt = cls_ivar_type(ci, "@" + mname)
17851+ end
17852+ if rt != ""
1783317853 if common == ""
1783417854 common = rt
1783517855 elsif common != rt
@@ -17894,6 +17914,17 @@
1789417914 rhs = box_val_to_poly(call_expr, this_rt)
1789517915 end
1789617916 emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";")
17917+ elsif cls_has_attr_reader(i, mname) == 1
17918+ # An auto-registered attr_reader doesn't appear in
17919+ # @cls_meth_names, so the explicit-method walk above misses it.
17920+ # Read the ivar directly. Issue #119.
17921+ ivar_expr = "((sp_" + cname + " *)" + recv_tmp + ".v.p)->" + sanitize_ivar("@" + mname)
17922+ rhs = ivar_expr
17923+ if is_poly_ret == 1
17924+ this_rt = cls_ivar_type(i, "@" + mname)
17925+ rhs = box_val_to_poly(ivar_expr, this_rt)
17926+ end
17927+ emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";")
1789717928 end
1789817929 i = i + 1
1789917930 end