Commit 2163485f
2163485fa3f9Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): include skips module class methods (#714)
class C; include M; end was dragging M's def self.X (class
methods on the module) into C as instance methods. Side effect:
M's @slots writes from those class methods got re-collected
against C's ivar table -- and the resulting cascade flipped
@const_types["M_slots"] from the correct hash variant back to
int. Subsequent @slots[k] = v writes became silent no-ops and
@slots.fetch(k, nil) failed to resolve on the int slot.
Ruby semantics: include only carries instance methods. Module
class methods (def self.X) and singleton methods stay on the
module itself.
Fix: collect_module_methods_into_class checks each DefNode's
receiver and skips the def when the receiver is a SelfNode (the
def self.X shape).
Test: test/include_skips_module_class_methods.rb verifies the hash-typed ivar survives the include and probes respond_to? to confirm the class methods didn't leak in.
459 tests pass; optcarrot checksum 59662.
Closes #714
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 2163485fa3f9
- Parent
- afd722eb9f54
spinel_analyze.rbmodifiedView file
while mk < mstmts.length sid = mstmts[mk] if @nd_type[sid] == "DefNode" mname = @nd_name[sid] # `def self.X` is a class-level method on the module and # does NOT flow through include. Copying it as an instance # method on the host class also drags the module's @ivar # writes into the class's ivar table and clobbers the # module's hoisted const slot. Issue #714. is_cls_def = 0 if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode" is_cls_def = 1 end if is_cls_def == 0 mname = @nd_name[sid] # Only add if class doesn't already have this method existing = cls_find_method_direct(ci, mname) if existing < 0 collect_class_method(ci, sid) existing = cls_find_method_direct(ci, mname) if existing < 0 collect_class_method(ci, sid) end end end mk = mk + 1test/include_skips_module_class_methods.rbaddedView file
Loading diff…
test/include_skips_module_class_methods.rb.expectedaddedView file
Loading diff…