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
while ik < inc_ids.length if @nd_type[inc_ids[ik]] == "ConstantReadNode" mod_name = @nd_name[inc_ids[ik]] collect_module_methods_into_class(ci, mod_name) # Issue #425: when this class is nested inside a # module (module_prefix != ""), the include arg is # a bare ConstantReadNode but the registered module # name is `<prefix>_<name>`. Try the qualified form # first; fall back to the bare name for top-level # modules. collect_module_methods_into_class(ci, resolve_include_module_name(mod_name, module_prefix)) end ik = ik + 1 end while ik < inc_ids.length if @nd_type[inc_ids[ik]] == "ConstantReadNode" mod_name = @nd_name[inc_ids[ik]] collect_module_methods_into_class(ci, mod_name) # Issue #425: same prefix resolution as the # class-reopening branch -- the include arg is a # bare name but the registered module's name is # `<prefix>_<name>` when nested in a module. collect_module_methods_into_class(ci, resolve_include_module_name(mod_name, module_prefix)) end ik = ik + 1 end end # Issue #425: resolve a bare include arg against the enclosing # module's lexical scope. For `module Ns; class Base; include # Helper; end; end`, the include's ConstantReadNode is the # unqualified `Helper`, but the registered module's name (from # collect_module_with_prefix) is `Ns_Helper`. Try the qualified # form first; fall back to the bare name for top-level modules. def resolve_include_module_name(mod_name, module_prefix) if module_prefix != "" qualified = module_prefix + "_" + mod_name i = 0 while i < @module_names.length if @module_names[i] == qualified return qualified end i = i + 1 end end mod_name end def collect_module_methods_into_class(ci, mod_name) # Find the module and add its methods to the class mi = 0test/include_nested_module.rbaddedView file
Loading diff…
test/include_nested_module.rb.expectedaddedView file
Loading diff…