Commit 30d08ace

30d08ace359bMerged

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

Description

fix(analyze): reconcile class includes against module reopens

When a class nested inside a module gets collected during Pass 0's recursion into ModuleNode bodies, its include Mod statement resolves against whatever module entries are already pushed onto @module_body_ids at that moment. A module Mod; def ... end reopen that appears later in the top-level statement order pushes a second entry the earlier include never sees, so methods defined in the reopen body are missing from the including class's dispatch table.

The top-level case worked because top-level classes are collected in Pass 1 -- by then Pass 0 has finished iterating every top-level ModuleNode (reopens included). Only the nested-in-module shape loses the race.

Add Pass 1.7 reconcile_class_includes: iterate every recorded (class, included-module) pair from @cls_includes and re-run collect_module_methods_into_class for each. The helper already skips methods the class has, so this is idempotent and harmless when no reopen changed anything; when a reopen DID add methods, the class now picks them up.

Fixes #573.

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

Details

Commit
30d08ace359b
Parent
540115eddca2

spinel_analyze.rbmodifiedView file

@@ -6548,6 +6548,22 @@
65486548 # instance instead of an `Article`.
65496549 propagate_inherited_class_methods
65506550
6551+ # Pass 1.7: reconcile `include` against module reopens that
6552+ # registered after the include was first processed. When a class
6553+ # nested inside a module gets collected (during Pass 0's recursion
6554+ # into ModuleNode bodies), its `include Mod` statement resolves
6555+ # against whatever module entries exist in @module_body_ids at
6556+ # that moment. A subsequent `module Mod; def ...; end` reopen
6557+ # appears later in the top-level statement order and pushes a
6558+ # second entry that the earlier include never sees. Re-run the
6559+ # method collection for every recorded (class, included-module)
6560+ # pair now that all module entries are registered;
6561+ # collect_module_methods_into_class is idempotent (adds only when
6562+ # the class doesn't already have the method) and walks every
6563+ # @module_names entry with the matching name, so this picks up
6564+ # all reopens. Fixes #573.
6565+ reconcile_class_includes
6566+
65516567 # Pass 2: top-level methods, constants, define_method
65526568 stmts.each { |sid|
65536569 if @nd_type[sid] == "DefNode"
@@ -7660,6 +7676,30 @@
76607676 end
76617677 end
76627678
7679+ # Re-run module-method collection for every recorded
7680+ # (class, included-module) pair so reopens that registered after
7681+ # the include site finally show up. See call-site comment for
7682+ # why the original include-time collection misses them. Idempotent
7683+ # by virtue of collect_module_methods_into_class' existing
7684+ # "don't add if class already has this method" guard.
7685+ def reconcile_class_includes
7686+ ci = 0
7687+ while ci < @cls_includes.length
7688+ includes_str = @cls_includes[ci]
7689+ if includes_str != ""
7690+ names = includes_str.split(";")
7691+ ni = 0
7692+ while ni < names.length
7693+ if names[ni] != ""
7694+ collect_module_methods_into_class(ci, names[ni])
7695+ end
7696+ ni = ni + 1
7697+ end
7698+ end
7699+ ci = ci + 1
7700+ end
7701+ end
7702+
76637703 def collect_class_method(ci, nid)
76647704 mname = @nd_name[nid]
76657705 body_id = @nd_body[nid]