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 = 069296929
while ik < inc_ids.length6930-
if @nd_type[inc_ids[ik]] == "ConstantReadNode"6931-
mod_name = @nd_name[inc_ids[ik]]6932-
# when this class is nested inside a6933-
# module (module_prefix != ""), the include arg is6934-
# a bare ConstantReadNode but the registered module6935-
# name is `<prefix>_<name>`. Try the qualified form6936-
# first; fall back to the bare name for top-level6937-
# 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-
end6930+
# `include Mod` / `A::B::C` / `::Top`. Shared helper6931+
# flattens the path, distinguishes absolute from6932+
# relative for prefix selection, and records the6933+
# include on @cls_includes.6934+
include_module_on_class(ci, inc_ids[ik], module_prefix)69426935
ik = ik + 169436936
end69446937
end@@ -7162,21 +7155,13 @@
71627155
inc_ids = get_args(inc_args)71637156
ik = 071647157
while ik < inc_ids.length7165-
if @nd_type[inc_ids[ik]] == "ConstantReadNode"7166-
mod_name = @nd_name[inc_ids[ik]]7167-
# same prefix resolution as the7168-
# class-reopening branch -- the include arg is a7169-
# bare name but the registered module's name is7170-
# `<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 include7174-
# relationship for ancestors-table emission. The7175-
# methods are already merged into the class above;7176-
# this pass keeps the module link alive so the7177-
# codegen can weave it into the MRO.7178-
record_class_include(ci, resolved_mod_name)7179-
end7158+
# Same resolution as the class-reopening branch:7159+
# the shared helper accepts ConstantReadNode and7160+
# qualified ConstantPathNode (`include Foo::Bar`,7161+
# `include ::Top`), routes through the prefix-7162+
# aware resolver for relative paths, and records7163+
# the include for ancestors-table emission.7164+
include_module_on_class(ci, inc_ids[ik], module_prefix)71807165
ik = ik + 171817166
end71827167
end@@ -7337,6 +7322,32 @@
73377322
mod_name73387323
end73397324
7325+
# Resolve and record an `include` argument on a class. Shared between7326+
# the class-reopening and fresh-class branches of7327+
# collect_class_with_prefix. `inc_nid` is the AST node for the include7328+
# argument; accepts bare ConstantReadNode (`include Helper`) and7329+
# qualified ConstantPathNode (`include A::B::Foo`, `include ::Top`).7330+
# Absolute paths (`::A::B`) bypass the lexical-scope prefix so a7331+
# top-level `A_B` wins over a nested `Outer_A_B`; relative paths7332+
# 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+
return7337+
end7338+
mod_name = const_ref_flat_name(inc_nid)7339+
if mod_name == ""7340+
return7341+
end7342+
effective_prefix = module_prefix7343+
if const_ref_is_relative(inc_nid) == 07344+
effective_prefix = ""7345+
end7346+
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+
end7350+
73407351
def collect_module_methods_into_class(ci, mod_name)73417352
# Find the module and add its methods to the class73427353
mi = 0test/include_constant_path.rbaddedView file
Loading diff…
test/include_constant_path.rb.expectedaddedView file
Loading diff…