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_typenow 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 topolywhen they disagree.compile_poly_method_call's user-class loop adds anelse if cls_has_attr_reader(i, mname)arm that emits((sp_<C> *)recv.v.p)->iv_<name>(or boxes that ivar value viabox_val_to_polywhen the result ispoly).
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
# that concrete type is used. If any two disagree, the call is # genuinely polymorphic and the caller must treat the result as # an sp_RbVal. # Returns 1 if class `ci` declares `mname` as an attr_reader (in # which case `obj.<mname>` reads `obj->iv_<mname>`). def cls_has_attr_reader(ci, mname) readers = @cls_attr_readers[ci].split(";") j = 0 while j < readers.length if readers[j] == mname return 1 end j = j + 1 end 0 end def poly_dispatch_return_type(mname) common = "" ci = 0 while ci < @cls_names.length rt = "" if cls_find_method_direct(ci, mname) >= 0 rt = cls_method_return(ci, mname) elsif cls_has_attr_reader(ci, mname) == 1 # An attr_reader returns the ivar type. Issue #119. rt = cls_ivar_type(ci, "@" + mname) end if rt != "" if common == "" common = rt elsif common != rt rhs = box_val_to_poly(call_expr, this_rt) end emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";") elsif cls_has_attr_reader(i, mname) == 1 # An auto-registered attr_reader doesn't appear in # @cls_meth_names, so the explicit-method walk above misses it. # Read the ivar directly. Issue #119. ivar_expr = "((sp_" + cname + " *)" + recv_tmp + ".v.p)->" + sanitize_ivar("@" + mname) rhs = ivar_expr if is_poly_ret == 1 this_rt = cls_ivar_type(i, "@" + mname) rhs = box_val_to_poly(ivar_expr, this_rt) end emit(" if (" + recv_tmp + ".cls_id == " + i.to_s + ") " + tmp + " = " + rhs + ";") end i = i + 1 endtest/poly_dispatch_attr_reader.rbaddedView file
Loading diff…