Commit 3bae3b7b

3bae3b7b87c2Merged

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

Description

fix(analyze,codegen): subclass instance method dispatches to inherited attr_reader

Closes #508. Bare fmt (or self.fmt) inside a subclass instance method failed to resolve when attr_accessor :fmt was declared on the parent class. The attr_reader lookups in both analyze and codegen consulted @cls_attr_readers[current_class] only and didn't walk @cls_parents.

Fix is symmetric across three lookup sites:

  • analyze cls_has_attr_reader: was current-class-only; now recurses into the parent chain (cls_ivar_type already walked parents, so the type token resolves correctly).

  • analyze bare-call inference (`recv < 0 && @current_class_idx

    = 0): consult cls_has_attr_reader before falling through to cls_method_return, so a bare fmt` returns the inherited ivar's type instead of the int default.

  • analyze method-name inference for obj.attr (recv typed obj_): same swap from per-class loop to cls_has_attr_reader.

  • codegen compile_call_expr's bare-call attr_reader probe and compile_object_method_expr's obj-receiver path both swap to cls_has_attr_reader (which already walked parents in codegen -- the codegen helper had the recursion; the analyze one didn't).

Roundhouse hit this everywhere ActionController-style attr_accessors fan out to controller subclasses (params, session, flash, request_format, ...).

Local: 480 tests pass.

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

Details

Commit
3bae3b7b87c2
Parent
1d1ce513cda5

spinel_analyze.rbmodifiedView file

@@ -2909,8 +2909,17 @@
29092909 # class's method table. Only for `recv < 0` — without that guard,
29102910 # a `Fiber.yield ...` (which has a receiver) inside a class body
29112911 # would short-circuit here returning the int default and never
2912- # reach the Fiber.yield → poly branch further down.
2912+ # reach the Fiber.yield → poly branch further down. Also checks
2913+ # the inherited attr_reader chain so a bare `fmt` in a subclass
2914+ # method body returns the inherited ivar's type rather than the
2915+ # int default (issue #508).
29132916 if recv < 0 && @current_class_idx >= 0
2917+ if cls_has_attr_reader(@current_class_idx, mname) == 1
2918+ ivt_attr = cls_ivar_type(@current_class_idx, "@" + mname)
2919+ if ivt_attr != "int"
2920+ return ivt_attr
2921+ end
2922+ end
29142923 mr = cls_method_return(@current_class_idx, mname)
29152924 return mr
29162925 end
@@ -5182,17 +5191,12 @@
51825191 end
51835192 mi2 = mi2 + 1
51845193 end
5185- # Check attr_readers
5186- readers2 = @cls_attr_readers[ci].split(";")
5187- j2 = 0
5188- while j2 < readers2.length
5189- if readers2[j2] == mname
5190- ivt = cls_ivar_type(ci, "@" + mname)
5191- if ivt != "int"
5192- return ivt
5193- end
5194+ # Check attr_readers (walks parent chain — issue #508).
5195+ if cls_has_attr_reader(ci, mname) == 1
5196+ ivt = cls_ivar_type(ci, "@" + mname)
5197+ if ivt != "int"
5198+ return ivt
51945199 end
5195- j2 = j2 + 1
51965200 end
51975201 # Check methods with args
51985202 midx = cls_find_method_direct(ci, mname)
@@ -5210,14 +5214,12 @@
52105214 cname = bt_rt[4, bt_rt.length - 4]
52115215 ci = find_class_idx(cname)
52125216 if ci >= 0
5213- # Check attr_reader
5214- readers = @cls_attr_readers[ci].split(";")
5215- j = 0
5216- while j < readers.length
5217- if readers[j] == mname
5218- return cls_ivar_type(ci, "@" + mname)
5219- end
5220- j = j + 1
5217+ # Check attr_reader (walks parent chain via cls_has_attr_reader;
5218+ # cls_ivar_type also walks parents so an inherited
5219+ # attr_accessor's typed ivar resolves to the right token).
5220+ # Issue #508 (analyze side).
5221+ if cls_has_attr_reader(ci, mname) == 1
5222+ return cls_ivar_type(ci, "@" + mname)
52215223 end
52225224 # Check method
52235225 mr = cls_method_return(ci, mname)
@@ -21314,7 +21316,9 @@
2131421316 # genuinely polymorphic and the caller must treat the result as
2131521317 # an sp_RbVal.
2131621318 # Returns 1 if class `ci` declares `mname` as an attr_reader (in
21317- # which case `obj.<mname>` reads `obj->iv_<mname>`).
21319+ # which case `obj.<mname>` reads `obj->iv_<mname>`). Walks the
21320+ # parent chain so a subclass call to an inherited
21321+ # attr_accessor reader is recognised (issue #508 analyze side).
2131821322 def cls_has_attr_reader(ci, mname)
2131921323 readers = @cls_attr_readers[ci].split(";")
2132021324 j = 0
@@ -21324,6 +21328,12 @@
2132421328 end
2132521329 j = j + 1
2132621330 end
21331+ if @cls_parents[ci] != ""
21332+ pi = find_class_idx(@cls_parents[ci])
21333+ if pi >= 0
21334+ return cls_has_attr_reader(pi, mname)
21335+ end
21336+ end
2132721337 0
2132821338 end
2132921339