Commit c7d1d151
c7d1d1512affMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): class << self def registers as a class method (#719)
class C; class << self; def X; ...; end; end; end used to be a
no-op for the analyzer's class-body walk: SingletonClassNode was
only handled at module scope, and only for attr_accessor. The
def's body got walked for completeness but no method entry landed
on @cls_cmeth_*, so C.X dispatched to the unresolved-call
fallback (emit 0).
Fix: in collect_class_with_prefix's body walk, when a stmt is a
SingletonClassNode, iterate its body for DefNodes with no
explicit receiver and call append_cls_cmeth on the enclosing
class -- exactly the same path def self.X takes. def self.X
inside class << self is excluded (its receiver is non-nil),
matching the wider DefNode arm at the same site.
Test: test/class_singleton_def.rb covers a bare def + a def with
an argument + a mixed class with both def self.X and class << self; def Y; end so both styles can coexist.
466 tests pass; optcarrot checksum 59662.
Closes #719
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- c7d1d1512aff
- Parent
- 229ae8ad9d58
spinel_analyze.rbmodifiedView file
if @nd_type[sid] == "DefNode" collect_class_method(ci, sid) end # `class << self; def X; ...; end; end` inside a class body -- # the def is a singleton method on the enclosing class, # equivalent to `def self.X`. Register as a class method on # this class. Issue #719. if @nd_type[sid] == "SingletonClassNode" sbody_id = @nd_body[sid] if sbody_id >= 0 sbody_stmts = get_stmts(sbody_id) sbi = 0 while sbi < sbody_stmts.length sb = sbody_stmts[sbi] if @nd_type[sb] == "DefNode" && @nd_receiver[sb] < 0 sb_name = @nd_name[sb] sb_params = collect_params_str(sb) sb_ptypes = collect_ptypes_str(sb, ci) sb_defs = collect_defaults_str(sb) sb_body_id = @nd_body[sb] append_cls_cmeth(ci, sb_name, sb_params, sb_ptypes, "int", sb_body_id, sb_defs) end sbi = sbi + 1 end end end if @nd_type[sid] == "ConstantWriteNode" collect_scoped_constant(cname, sid) endtest/class_singleton_def.rbaddedView file
Loading diff…
test/class_singleton_def.rb.expectedaddedView file
Loading diff…