Commit 6a783657

6a7836574202Merged

Ryan Seys <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified

Description

fix(analyze): recognize include with ConstantPathNode argument

collect_class_with_prefix's two include handlers gated module extraction on @nd_type[arg] == "ConstantReadNode". A qualified path like Foo::Bar::Baz parses as ConstantPathNode and silently fell through, leaving the included module's methods off the class's method table and producing cannot resolve call to '<m>' on obj_<C> at every dispatch site.

Both sites now route the include argument through a shared helper include_module_on_class that:

  • accepts ConstantReadNode and ConstantPathNode,
  • flattens the path via const_ref_flat_name (A::B -> A_B),
  • gates the lexical-prefix probe on const_ref_is_relative so absolute paths (::A::B) skip the nesting prefix and resolve to the top-level module, while relative paths still pick <prefix>_<name> first via resolve_include_module_name,
  • records the include on @cls_includes for ancestors-table emission.

Extracting the helper deduplicates the class-reopen and fresh-class branches and keeps absolute-vs-relative resolution consistent between them.

Details

Commit
6a7836574202
Parent
992c82eada39

spinel_analyze.rbmodifiedView file

@@ -6900,18 +6900,11 @@
69006900 inc_ids = get_args(inc_args)
69016901 ik = 0
69026902 while ik < inc_ids.length
6903- if @nd_type[inc_ids[ik]] == "ConstantReadNode"
6904- mod_name = @nd_name[inc_ids[ik]]
6905- # when this class is nested inside a
6906- # module (module_prefix != ""), the include arg is
6907- # a bare ConstantReadNode but the registered module
6908- # name is `<prefix>_<name>`. Try the qualified form
6909- # first; fall back to the bare name for top-level
6910- # modules.
6911- resolved_mod_name = resolve_include_module_name(mod_name, module_prefix)
6912- collect_module_methods_into_class(ci, resolved_mod_name)
6913- record_class_include(ci, resolved_mod_name)
6914- end
6903+ # `include Mod` / `A::B::C` / `::Top`. Shared helper
6904+ # flattens the path, distinguishes absolute from
6905+ # relative for prefix selection, and records the
6906+ # include on @cls_includes.
6907+ include_module_on_class(ci, inc_ids[ik], module_prefix)
69156908 ik = ik + 1
69166909 end
69176910 end
@@ -7135,21 +7128,13 @@
71357128 inc_ids = get_args(inc_args)
71367129 ik = 0
71377130 while ik < inc_ids.length
7138- if @nd_type[inc_ids[ik]] == "ConstantReadNode"
7139- mod_name = @nd_name[inc_ids[ik]]
7140- # same prefix resolution as the
7141- # class-reopening branch -- the include arg is a
7142- # bare name but the registered module's name is
7143- # `<prefix>_<name>` when nested in a module.
7144- resolved_mod_name = resolve_include_module_name(mod_name, module_prefix)
7145- collect_module_methods_into_class(ci, resolved_mod_name)
7146- # record the include
7147- # relationship for ancestors-table emission. The
7148- # methods are already merged into the class above;
7149- # this pass keeps the module link alive so the
7150- # codegen can weave it into the MRO.
7151- record_class_include(ci, resolved_mod_name)
7152- end
7131+ # Same resolution as the class-reopening branch:
7132+ # the shared helper accepts ConstantReadNode and
7133+ # qualified ConstantPathNode (`include Foo::Bar`,
7134+ # `include ::Top`), routes through the prefix-
7135+ # aware resolver for relative paths, and records
7136+ # the include for ancestors-table emission.
7137+ include_module_on_class(ci, inc_ids[ik], module_prefix)
71537138 ik = ik + 1
71547139 end
71557140 end
@@ -7310,6 +7295,32 @@
73107295 mod_name
73117296 end
73127297
7298+ # Resolve and record an `include` argument on a class. Shared between
7299+ # the class-reopening and fresh-class branches of
7300+ # collect_class_with_prefix. `inc_nid` is the AST node for the include
7301+ # argument; accepts bare ConstantReadNode (`include Helper`) and
7302+ # qualified ConstantPathNode (`include A::B::Foo`, `include ::Top`).
7303+ # Absolute paths (`::A::B`) bypass the lexical-scope prefix so a
7304+ # top-level `A_B` wins over a nested `Outer_A_B`; relative paths
7305+ # prefer the nested form via resolve_include_module_name.
7306+ def include_module_on_class(ci, inc_nid, module_prefix)
7307+ inc_t = @nd_type[inc_nid]
7308+ if inc_t != "ConstantReadNode" && inc_t != "ConstantPathNode"
7309+ return
7310+ end
7311+ mod_name = const_ref_flat_name(inc_nid)
7312+ if mod_name == ""
7313+ return
7314+ end
7315+ effective_prefix = module_prefix
7316+ if const_ref_is_relative(inc_nid) == 0
7317+ effective_prefix = ""
7318+ end
7319+ resolved_mod_name = resolve_include_module_name(mod_name, effective_prefix)
7320+ collect_module_methods_into_class(ci, resolved_mod_name)
7321+ record_class_include(ci, resolved_mod_name)
7322+ end
7323+
73137324 def collect_module_methods_into_class(ci, mod_name)
73147325 # Find the module and add its methods to the class
73157326 mi = 0