Commit 585bee04

585bee04368fMerged

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

Description

Merge pull request #534 from ryanseys/rs-fix-include-constant-path

fix(analyze): recognize include with ConstantPathNode argument

Details

Commit
585bee04368f
Parent
72b7af5f40e7
Merge parent
6a7836574202

spinel_analyze.rbmodifiedView file

@@ -6927,18 +6927,11 @@
69276927 inc_ids = get_args(inc_args)
69286928 ik = 0
69296929 while ik < inc_ids.length
6930- if @nd_type[inc_ids[ik]] == "ConstantReadNode"
6931- mod_name = @nd_name[inc_ids[ik]]
6932- # when this class is nested inside a
6933- # module (module_prefix != ""), the include arg is
6934- # a bare ConstantReadNode but the registered module
6935- # name is `<prefix>_<name>`. Try the qualified form
6936- # first; fall back to the bare name for top-level
6937- # modules.
6938- resolved_mod_name = resolve_include_module_name(mod_name, module_prefix)
6939- collect_module_methods_into_class(ci, resolved_mod_name)
6940- record_class_include(ci, resolved_mod_name)
6941- end
6930+ # `include Mod` / `A::B::C` / `::Top`. Shared helper
6931+ # flattens the path, distinguishes absolute from
6932+ # relative for prefix selection, and records the
6933+ # include on @cls_includes.
6934+ include_module_on_class(ci, inc_ids[ik], module_prefix)
69426935 ik = ik + 1
69436936 end
69446937 end
@@ -7162,21 +7155,13 @@
71627155 inc_ids = get_args(inc_args)
71637156 ik = 0
71647157 while ik < inc_ids.length
7165- if @nd_type[inc_ids[ik]] == "ConstantReadNode"
7166- mod_name = @nd_name[inc_ids[ik]]
7167- # same prefix resolution as the
7168- # class-reopening branch -- the include arg is a
7169- # bare name but the registered module's name is
7170- # `<prefix>_<name>` when nested in a module.
7171- resolved_mod_name = resolve_include_module_name(mod_name, module_prefix)
7172- collect_module_methods_into_class(ci, resolved_mod_name)
7173- # record the include
7174- # relationship for ancestors-table emission. The
7175- # methods are already merged into the class above;
7176- # this pass keeps the module link alive so the
7177- # codegen can weave it into the MRO.
7178- record_class_include(ci, resolved_mod_name)
7179- end
7158+ # Same resolution as the class-reopening branch:
7159+ # the shared helper accepts ConstantReadNode and
7160+ # qualified ConstantPathNode (`include Foo::Bar`,
7161+ # `include ::Top`), routes through the prefix-
7162+ # aware resolver for relative paths, and records
7163+ # the include for ancestors-table emission.
7164+ include_module_on_class(ci, inc_ids[ik], module_prefix)
71807165 ik = ik + 1
71817166 end
71827167 end
@@ -7337,6 +7322,32 @@
73377322 mod_name
73387323 end
73397324
7325+ # Resolve and record an `include` argument on a class. Shared between
7326+ # the class-reopening and fresh-class branches of
7327+ # collect_class_with_prefix. `inc_nid` is the AST node for the include
7328+ # argument; accepts bare ConstantReadNode (`include Helper`) and
7329+ # qualified ConstantPathNode (`include A::B::Foo`, `include ::Top`).
7330+ # Absolute paths (`::A::B`) bypass the lexical-scope prefix so a
7331+ # top-level `A_B` wins over a nested `Outer_A_B`; relative paths
7332+ # prefer the nested form via resolve_include_module_name.
7333+ def include_module_on_class(ci, inc_nid, module_prefix)
7334+ inc_t = @nd_type[inc_nid]
7335+ if inc_t != "ConstantReadNode" && inc_t != "ConstantPathNode"
7336+ return
7337+ end
7338+ mod_name = const_ref_flat_name(inc_nid)
7339+ if mod_name == ""
7340+ return
7341+ end
7342+ effective_prefix = module_prefix
7343+ if const_ref_is_relative(inc_nid) == 0
7344+ effective_prefix = ""
7345+ end
7346+ resolved_mod_name = resolve_include_module_name(mod_name, effective_prefix)
7347+ collect_module_methods_into_class(ci, resolved_mod_name)
7348+ record_class_include(ci, resolved_mod_name)
7349+ end
7350+
73407351 def collect_module_methods_into_class(ci, mod_name)
73417352 # Find the module and add its methods to the class
73427353 mi = 0