Commit e33e3309

e33e3309f65bMerged

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

Description

fix(analyze): nested-module class include resolves qualified module (#425)

A bare-receiver call to a method defined on an included module failed when the including class was nested inside a module namespace:

module Ns module Helper def helped; "helped"; end end class Base include Helper def call; helped; end # warning: cannot resolve end end

Top-level usage (no module Ns wrapper) worked. The gap was just the include arg's name lookup at class-collection time.

Root cause: collect_class_with_prefix's include handlers (both the class-reopening branch and the fresh-class branch) passed the unqualified ConstantReadNode name (Helper) to collect_module_methods_into_class, but the module's registered name post-collect_module_with_prefix is <prefix>_<name> -- Ns_Helper in this case. The bare-name lookup in collect_module_methods_into_class walked @module_names and missed every qualified entry.

Fix: new helper resolve_include_module_name(mod_name, module_prefix) tries the <prefix>_<name> form against @module_names first; falls back to the bare name for top-level modules. Both include-handler call sites in collect_class_with_prefix route through it.

Test include_nested_module.rb covers:

  • Canonical Rails-style shape (module namespace + included module also in the namespace).
  • Cross-namespace fallback (top-level module included into a nested class -- qualified lookup misses, bare-name fallback resolves).
  • Top-level baseline (regression check that the no-nesting path still works).

Closes #425

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

Details

Commit
e33e3309f65b
Parent
dcc77dcb65ac

spinel_analyze.rbmodifiedView file

@@ -6286,7 +6286,13 @@
62866286 while ik < inc_ids.length
62876287 if @nd_type[inc_ids[ik]] == "ConstantReadNode"
62886288 mod_name = @nd_name[inc_ids[ik]]
6289- collect_module_methods_into_class(ci, mod_name)
6289+ # Issue #425: when this class is nested inside a
6290+ # module (module_prefix != ""), the include arg is
6291+ # a bare ConstantReadNode but the registered module
6292+ # name is `<prefix>_<name>`. Try the qualified form
6293+ # first; fall back to the bare name for top-level
6294+ # modules.
6295+ collect_module_methods_into_class(ci, resolve_include_module_name(mod_name, module_prefix))
62906296 end
62916297 ik = ik + 1
62926298 end
@@ -6509,7 +6515,11 @@
65096515 while ik < inc_ids.length
65106516 if @nd_type[inc_ids[ik]] == "ConstantReadNode"
65116517 mod_name = @nd_name[inc_ids[ik]]
6512- collect_module_methods_into_class(ci, mod_name)
6518+ # Issue #425: same prefix resolution as the
6519+ # class-reopening branch -- the include arg is a
6520+ # bare name but the registered module's name is
6521+ # `<prefix>_<name>` when nested in a module.
6522+ collect_module_methods_into_class(ci, resolve_include_module_name(mod_name, module_prefix))
65136523 end
65146524 ik = ik + 1
65156525 end
@@ -6615,6 +6625,26 @@
66156625 end
66166626
66176627
6628+ # Issue #425: resolve a bare include arg against the enclosing
6629+ # module's lexical scope. For `module Ns; class Base; include
6630+ # Helper; end; end`, the include's ConstantReadNode is the
6631+ # unqualified `Helper`, but the registered module's name (from
6632+ # collect_module_with_prefix) is `Ns_Helper`. Try the qualified
6633+ # form first; fall back to the bare name for top-level modules.
6634+ def resolve_include_module_name(mod_name, module_prefix)
6635+ if module_prefix != ""
6636+ qualified = module_prefix + "_" + mod_name
6637+ i = 0
6638+ while i < @module_names.length
6639+ if @module_names[i] == qualified
6640+ return qualified
6641+ end
6642+ i = i + 1
6643+ end
6644+ end
6645+ mod_name
6646+ end
6647+
66186648 def collect_module_methods_into_class(ci, mod_name)
66196649 # Find the module and add its methods to the class
66206650 mi = 0