Commit 87a2ce25

87a2ce25d6b7Merged

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

Description

fix(analyze,codegen): nested-module constants resolved through include chain

When module B does include A and A has a nested module A::N, a reference to bare N from B's instance method failed with uninitialized constant 'N' (emitting 0). CRuby's include puts A on B's ancestor chain, and constant lookup walks ancestors, so A::N is reachable as bare N from any class that ends up with B in its ancestors.

The trace path was:

class Test; include RequestDispatch; end module RequestDispatch; include ActionView; def call; ...; end; end module ActionView; module ViewHelpers; ...; end; end

Test.new.call -> body of call -> bare ViewHelpers -> ???

Spinel walked Test's @cls_includes once, saw RequestDispatch, tried RequestDispatch_ViewHelpers (miss), then attempted to recurse via find_class_idx("RequestDispatch") — which returned -1 because RequestDispatch is a module, not a class, and the recursion bailed there.

The structural gap: include directives appearing inside a module body were never recorded anywhere. @cls_includes[ci] only carried includes from class bodies; modules had no parallel table.

This commit adds @module_includes, parallel to @module_names, populated in collect_module_with_prefix from each module body's include directives (deduping per-row so a re-opened module doesn't double-list a recurring include). The IR round-trip serializes the new array so codegen sees the same data.

Const lookup factors out a shared resolve_const_via_include_list that handles both kinds of parent (class via @cls_includes, module via @module_includes). The depth cap stays at 16 so a pathological include cycle bails instead of looping.

Test: test/include_chain_module_nested_const.rb covers the direct one-hop case from the issue plus a deeper Consumer -> OuterMixin -> MiddleMixin -> Outer::DeepHelpers chain. make bootstrap fixpoint clean; make test 580 pass / 0 fail (was 579, +1 new).

Closes #607

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

Details

Commit
87a2ce25d6b7
Parent
70e0dcbed3df

spinel_analyze.rbmodifiedView file

@@ -527,6 +527,11 @@
527527 # Module tracking: module_name -> body node id
528528 @module_names = "".split(",")
529529 @module_body_ids = []
530+ # `@module_includes[mi]`: `;`-separated list of names the module
531+ # introduced by row `mi` itself includes. Modules can re-open, so
532+ # each row tracks its own block's `include` calls; const lookup
533+ # walks every row matching the module name.
534+ @module_includes = "".split(",")
530535 # Module-level singleton accessors :
531536 # `class << self; attr_accessor :foo; end` inside `module M`.
532537 # `@module_acc_consts[i]` is a `;`-separated list of distinct
@@ -1155,23 +1160,89 @@
11551160 # body that was attached to the current class via `include M` should
11561161 # resolve M::CONST. spinel's method table doesn't retain the
11571162 # original-module lexical scope, so the chain walk above misses it.
1158- if @current_class_idx >= 0 && @current_class_idx < @cls_includes.length
1159- incs_str = @cls_includes[@current_class_idx]
1160- if incs_str != ""
1161- incs = incs_str.split(";")
1162- ii = 0
1163- while ii < incs.length
1164- if incs[ii] != ""
1165- cand_inc = incs[ii] + "_" + name
1166- if const_namespace_exists(cand_inc) == 1
1167- return cand_inc
1168- end
1163+ # Walk the chain transitively so `B include A; A include X; B body
1164+ # references bare N -> X::N` works even when B's directly-listed
1165+ # includes don't carry N themselves.
1166+ if @current_class_idx >= 0
1167+ found_inc = resolve_const_via_include_chain(@current_class_idx, name)
1168+ if found_inc != ""
1169+ return found_inc
1170+ end
1171+ end
1172+ name
1173+ end
1174+
1175+ # Recursively walk @cls_includes from `class_idx`, trying each
1176+ # included module's namespace for a constant named `name`. Caps at
1177+ # depth 16 so a pathological include cycle bails instead of looping.
1178+ # Returns the qualified `<Mod>_<name>` form on a hit, or "" on miss.
1179+ def resolve_const_via_include_chain(class_idx, name, depth = 16)
1180+ if class_idx < 0 || class_idx >= @cls_includes.length
1181+ return ""
1182+ end
1183+ resolve_const_via_include_list(@cls_includes[class_idx], name, depth)
1184+ end
1185+
1186+ # Shared walker: for each entry in `incs_str`, check the joined
1187+ # `<Mod>_<name>` namespace, then recurse into that entry's own
1188+ # include chain (class-or-module). Handles the `B include A;
1189+ # A include X` -> X::N path without conflating the two parent
1190+ # tables.
1191+ def resolve_const_via_include_list(incs_str, name, depth)
1192+ if depth <= 0
1193+ return ""
1194+ end
1195+ if incs_str == ""
1196+ return ""
1197+ end
1198+ incs = incs_str.split(";")
1199+ ii = 0
1200+ while ii < incs.length
1201+ inc = incs[ii]
1202+ if inc != ""
1203+ cand = inc + "_" + name
1204+ if const_namespace_exists(cand) == 1
1205+ return cand
1206+ end
1207+ inc_cls = find_class_idx(inc)
1208+ if inc_cls >= 0
1209+ sub = resolve_const_via_include_list(@cls_includes[inc_cls], name, depth - 1)
1210+ if sub != ""
1211+ return sub
1212+ end
1213+ end
1214+ if module_name_exists(inc) == 1
1215+ sub = resolve_const_via_module_chain(inc, name, depth - 1)
1216+ if sub != ""
1217+ return sub
11691218 end
1170- ii = ii + 1
11711219 end
11721220 end
1221+ ii = ii + 1
1222+ end
1223+ ""
1224+ end
1225+
1226+ # Walk every @module_names slot matching `mod_name` (a module can
1227+ # re-open and each open is a separate row, so the includes recorded
1228+ # on each have to be tried independently). Routes through
1229+ # resolve_const_via_include_list so module-include and class-include
1230+ # share the same lookup recursion.
1231+ def resolve_const_via_module_chain(mod_name, name, depth)
1232+ if depth <= 0
1233+ return ""
1234+ end
1235+ mi = 0
1236+ while mi < @module_names.length
1237+ if @module_names[mi] == mod_name && mi < @module_includes.length
1238+ sub = resolve_const_via_include_list(@module_includes[mi], name, depth - 1)
1239+ if sub != ""
1240+ return sub
1241+ end
1242+ end
1243+ mi = mi + 1
11731244 end
1174- name
1245+ ""
11751246 end
11761247
11771248 def resolve_const_ref_name(nid, depth = 32)
@@ -9699,11 +9770,57 @@
96999770 # Store module info for include
97009771 @module_names.push(mname)
97019772 @module_body_ids.push(body)
9773+ @module_includes.push("")
9774+ mi_for_includes = @module_names.length - 1
97029775 if body < 0
97039776 return
97049777 end
97059778 body_stmts = get_stmts(body)
97069779
9780+ # Record `include X` directives appearing in this module's body so
9781+ # const lookup from a method body attached via the include chain
9782+ # can walk into X's nested namespace. Class-body includes go through
9783+ # `include_module_on_class`; this is the module-body counterpart.
9784+ body_stmts.each { |sid|
9785+ if @nd_type[sid] == "CallNode" && @nd_receiver[sid] < 0 && @nd_name[sid] == "include"
9786+ inc_args = @nd_arguments[sid]
9787+ if inc_args >= 0
9788+ inc_ids = get_args(inc_args)
9789+ ik = 0
9790+ while ik < inc_ids.length
9791+ inc_t = @nd_type[inc_ids[ik]]
9792+ if inc_t == "ConstantReadNode" || inc_t == "ConstantPathNode"
9793+ inc_name = const_ref_flat_name(inc_ids[ik])
9794+ if inc_name != ""
9795+ cur_incs = @module_includes[mi_for_includes]
9796+ already = 0
9797+ if cur_incs != ""
9798+ parts = cur_incs.split(";")
9799+ pk = 0
9800+ while pk < parts.length
9801+ if parts[pk] == inc_name
9802+ already = 1
9803+ pk = parts.length
9804+ else
9805+ pk = pk + 1
9806+ end
9807+ end
9808+ end
9809+ if already == 0
9810+ if cur_incs == ""
9811+ @module_includes[mi_for_includes] = inc_name
9812+ else
9813+ @module_includes[mi_for_includes] = cur_incs + ";" + inc_name
9814+ end
9815+ end
9816+ end
9817+ end
9818+ ik = ik + 1
9819+ end
9820+ end
9821+ end
9822+ }
9823+
97079824 # Match top-level collection order: modules first, then classes.
97089825 body_stmts.each { |sid|
97099826 if @nd_type[sid] == "ModuleNode"
@@ -25225,6 +25342,7 @@
2522525342 # Modules
2522625343 buf = ir_emit_sa(buf, "@module_names", @module_names)
2522725344 buf = ir_emit_ia(buf, "@module_body_ids", @module_body_ids)
25345+ buf = ir_emit_sa(buf, "@module_includes", @module_includes)
2522825346 buf = ir_emit_sa(buf, "@module_acc_keys", @module_acc_keys)
2522925347 buf = ir_emit_sa(buf, "@module_acc_consts", @module_acc_consts)
2523025348