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

@@ -8296,11 +8296,22 @@
82968296 while mk < mstmts.length
82978297 sid = mstmts[mk]
82988298 if @nd_type[sid] == "DefNode"
8299- mname = @nd_name[sid]
8299+ # `def self.X` is a class-level method on the module and
8300+ # does NOT flow through include. Copying it as an instance
8301+ # method on the host class also drags the module's @ivar
8302+ # writes into the class's ivar table and clobbers the
8303+ # module's hoisted const slot. Issue #714.
8304+ is_cls_def = 0
8305+ if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"
8306+ is_cls_def = 1
8307+ end
8308+ if is_cls_def == 0
8309+ mname = @nd_name[sid]
83008310 # Only add if class doesn't already have this method
8301- existing = cls_find_method_direct(ci, mname)
8302- if existing < 0
8303- collect_class_method(ci, sid)
8311+ existing = cls_find_method_direct(ci, mname)
8312+ if existing < 0
8313+ collect_class_method(ci, sid)
8314+ end
83048315 end
83058316 end
83068317 mk = mk + 1