Commit fcb39a79

fcb39a797bfeMerged

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

Description

feat(analyze,codegen): support top-level include <Mod> dispatch

Top-level include <Mod> at root scope previously fell through the analyzer with no effect — bare <m> calls following the include went unresolved with "cannot resolve call to '' on (no receiver)". CRuby installs the module's instance-side methods on Object so they resolve via normal method lookup; Spinel doesn't model the Object mixin chain.

Analyze gains a small pass after module-function registration: collect_toplevel_module_includes walks root statements, finds bare include <Mod> (ConstantReadNode or ConstantPathNode arg), and for each <Mod>_cls_<m> entry in @meth_* copies a parallel row keyed by the bare <m> name with the same body_id. The existing find_method_idx + sp_<name> dispatch path then picks the right body for an unprefixed call without any IR schema or dispatch changes.

Method-lookup precedence matches CRuby:

  • User-defined top-level def <m> wins over include (defs sit above the include chain in CRuby's method-lookup order).
  • For overlapping methods across multiple includes, the most recent include wins (last-include-wins, matching CRuby's linearised ancestor chain).

Both are implemented via @toplevel_include_alias, a presence hash that marks bare names registered by include. A collision with a marked name is overwritten in place; a collision with an unmarked name (user-def) is left alone. Lookups inside alias_module_methods_at_toplevel go through a one-shot bare-name → row-index map so each include is O(N) (vs the O(N²) the initial implementation incurred from calling find_method_idx inside the row-scan loop).

Other guards: unregistered modules (e.g. Comparable, which Spinel doesn't model) are silently skipped; Foo.include(M) class-side mixins are unaffected (still handled by collect_class_with_prefix).

Codegen treats the include call itself as a no-op returning "0" (CRuby returns nil from include) so the otherwise-unhandled bare call doesn't emit a spurious diagnostic.

Adds test/toplevel_include_module_function.rb covering four shapes: canonical include Greeter, include Outer::Inner (ConstantPathNode), multi-include with overlap (last wins, plus non-overlapping methods stay available), and user-def vs include collision (user-def wins). Full test suite (471 tests) passes; bootstrap fixpoint (IR + C, analyze + codegen) holds.

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

Details

Commit
fcb39a797bfe
Parent
2d24a16ed3c8

spinel_analyze.rbmodifiedView file

@@ -448,6 +448,13 @@
448448 @parse_id_cache = {}
449449 @parse_id_pool = [[0]]
450450
451+ # Tracks bare names whose @meth_* row was added by
452+ # collect_toplevel_module_includes. Entries marked here are
453+ # overwriteable by a later top-level `include` (last-include-
454+ # wins). User-defined `def <m>` rows aren't marked, so they
455+ # win over any subsequent include.
456+ @toplevel_include_alias = {}
457+
451458 @needs_stringio = 0
452459 @proc_counter = 0
453460 @proc_funcs = ""
@@ -6139,6 +6146,13 @@
61396146 end
61406147 }
61416148
6149+ # Top-level `include <Mod>` aliases the module's module_function
6150+ # methods into @meth_* under their bare names so unprefixed calls
6151+ # dispatch through the existing find_method_idx path. Later
6152+ # includes overwrite earlier aliases for the same bare name
6153+ # (last-include-wins); user-defined top-level `def`s are preserved.
6154+ # Must run after module-function entries are registered.
6155+ collect_toplevel_module_includes
61426156 # Pass 2.6: hoist `recv.instance_eval do ... end` blocks into
61436157 # file-scope static functions. Receiver-class flow analysis picks the
61446158 # receiver's class, the block body is later compiled as a function
@@ -8699,6 +8713,98 @@
86998713 "int"
87008714 end
87018715
8716+ # Walk root statements for bare `include <Mod>` and alias the
8717+ # module's `<Mod>_cls_<m>` entries into @meth_* under their
8718+ # bare names. Skips unregistered modules (e.g. Comparable);
8719+ # collisions with user-defined top-level defs are preserved
8720+ # while overlap with prior alias entries is overwritten (see
8721+ # alias_module_methods_at_toplevel). `Foo.include(M)` is
8722+ # handled separately by collect_class_with_prefix.
8723+ def collect_toplevel_module_includes
8724+ root = @root_id
8725+ if @nd_type[root] != "ProgramNode"
8726+ return
8727+ end
8728+ stmts = get_body_stmts(root)
8729+ si = 0
8730+ while si < stmts.length
8731+ sid = stmts[si]
8732+ if @nd_type[sid] == "CallNode" && @nd_receiver[sid] < 0 && @nd_name[sid] == "include"
8733+ inc_args = @nd_arguments[sid]
8734+ if inc_args >= 0
8735+ inc_ids = get_args(inc_args)
8736+ ik = 0
8737+ while ik < inc_ids.length
8738+ inc_t = @nd_type[inc_ids[ik]]
8739+ if inc_t == "ConstantReadNode" || inc_t == "ConstantPathNode"
8740+ mod_name = const_ref_flat_name(inc_ids[ik])
8741+ if mod_name != "" && module_name_exists(mod_name) == 1
8742+ alias_module_methods_at_toplevel(mod_name)
8743+ end
8744+ end
8745+ ik = ik + 1
8746+ end
8747+ end
8748+ end
8749+ si = si + 1
8750+ end
8751+ end
8752+
8753+ # Copy each `<mod_name>_cls_<m>` row in @meth_* to a parallel
8754+ # bare-name row sharing body_id. On collision: an entry already
8755+ # marked in @toplevel_include_alias is overwritten in place
8756+ # (last-include-wins); user-def rows are unmarked and preserved.
8757+ # A one-shot bare-name → row-index map keeps collision checks O(1).
8758+ def alias_module_methods_at_toplevel(mod_name)
8759+ prefix = mod_name + "_cls_"
8760+ plen = prefix.length
8761+
8762+ name_to_idx = {}
8763+ k = 0
8764+ while k < @meth_names.length
8765+ if !name_to_idx.key?(@meth_names[k])
8766+ name_to_idx[@meth_names[k]] = k
8767+ end
8768+ k = k + 1
8769+ end
8770+
8771+ src_count = @meth_names.length
8772+ si = 0
8773+ while si < src_count
8774+ full = @meth_names[si]
8775+ if full.length > plen && full[0, plen] == prefix
8776+ bare = full[plen, full.length - plen]
8777+ existing = -1
8778+ if name_to_idx.key?(bare)
8779+ existing = name_to_idx[bare]
8780+ end
8781+ if existing < 0
8782+ @meth_names.push(bare)
8783+ @meth_param_names.push(@meth_param_names[si])
8784+ @meth_param_types.push(@meth_param_types[si])
8785+ @meth_param_empty.push(@meth_param_empty[si])
8786+ @meth_return_types.push(@meth_return_types[si])
8787+ @meth_body_ids.push(@meth_body_ids[si])
8788+ @meth_has_defaults.push(@meth_has_defaults[si])
8789+ @meth_has_yield.push(@meth_has_yield[si])
8790+ @meth_rest_index.push(@meth_rest_index[si])
8791+ @toplevel_include_alias[bare] = 1
8792+ name_to_idx[bare] = @meth_names.length - 1
8793+ elsif @toplevel_include_alias.key?(bare)
8794+ @meth_param_names[existing] = @meth_param_names[si]
8795+ @meth_param_types[existing] = @meth_param_types[si]
8796+ @meth_param_empty[existing] = @meth_param_empty[si]
8797+ @meth_return_types[existing] = @meth_return_types[si]
8798+ @meth_body_ids[existing] = @meth_body_ids[si]
8799+ @meth_has_defaults[existing] = @meth_has_defaults[si]
8800+ @meth_has_yield[existing] = @meth_has_yield[si]
8801+ @meth_rest_index[existing] = @meth_rest_index[si]
8802+ end
8803+ end
8804+ si = si + 1
8805+ end
8806+ end
8807+
87028808 def collect_toplevel_method(nid)
87038809 mname = @nd_name[nid]
87048810 body_id = @nd_body[nid]