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
# instance instead of an `Article`. propagate_inherited_class_methods # Pass 1.7: reconcile `include` against module reopens that # registered after the include was first processed. 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 exist in @module_body_ids at # that moment. A subsequent `module Mod; def ...; end` reopen # appears later in the top-level statement order and pushes a # second entry that the earlier include never sees. Re-run the # method collection for every recorded (class, included-module) # pair now that all module entries are registered; # collect_module_methods_into_class is idempotent (adds only when # the class doesn't already have the method) and walks every # @module_names entry with the matching name, so this picks up # all reopens. Fixes #573. reconcile_class_includes # Pass 2: top-level methods, constants, define_method stmts.each { |sid| if @nd_type[sid] == "DefNode" end end # Re-run module-method collection for every recorded # (class, included-module) pair so reopens that registered after # the include site finally show up. See call-site comment for # why the original include-time collection misses them. Idempotent # by virtue of collect_module_methods_into_class' existing # "don't add if class already has this method" guard. def reconcile_class_includes ci = 0 while ci < @cls_includes.length includes_str = @cls_includes[ci] if includes_str != "" names = includes_str.split(";") ni = 0 while ni < names.length if names[ni] != "" collect_module_methods_into_class(ci, names[ni]) end ni = ni + 1 end end ci = ci + 1 end end def collect_class_method(ci, nid) mname = @nd_name[nid] body_id = @nd_body[nid]test/module_reopen_after_include_namespaced.rbaddedView file
Loading diff…
test/module_reopen_after_include_namespaced.rb.expectedaddedView file
Loading diff…