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 overinclude(defs sit above the include chain in CRuby's method-lookup order). - For overlapping methods across multiple includes, the most
recent
includewins (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
@parse_id_cache = {} @parse_id_pool = [[0]] # Tracks bare names whose @meth_* row was added by # collect_toplevel_module_includes. Entries marked here are # overwriteable by a later top-level `include` (last-include- # wins). User-defined `def <m>` rows aren't marked, so they # win over any subsequent include. @toplevel_include_alias = {} @needs_stringio = 0 @proc_counter = 0 @proc_funcs = "" end } # Top-level `include <Mod>` aliases the module's module_function # methods into @meth_* under their bare names so unprefixed calls # dispatch through the existing find_method_idx path. Later # includes overwrite earlier aliases for the same bare name # (last-include-wins); user-defined top-level `def`s are preserved. # Must run after module-function entries are registered. collect_toplevel_module_includes # Pass 2.6: hoist `recv.instance_eval do ... end` blocks into # file-scope static functions. Receiver-class flow analysis picks the # receiver's class, the block body is later compiled as a function "int" end # Walk root statements for bare `include <Mod>` and alias the # module's `<Mod>_cls_<m>` entries into @meth_* under their # bare names. Skips unregistered modules (e.g. Comparable); # collisions with user-defined top-level defs are preserved # while overlap with prior alias entries is overwritten (see # alias_module_methods_at_toplevel). `Foo.include(M)` is # handled separately by collect_class_with_prefix. def collect_toplevel_module_includes root = @root_id if @nd_type[root] != "ProgramNode" return end stmts = get_body_stmts(root) si = 0 while si < stmts.length sid = stmts[si] if @nd_type[sid] == "CallNode" && @nd_receiver[sid] < 0 && @nd_name[sid] == "include" inc_args = @nd_arguments[sid] if inc_args >= 0 inc_ids = get_args(inc_args) ik = 0 while ik < inc_ids.length inc_t = @nd_type[inc_ids[ik]] if inc_t == "ConstantReadNode" || inc_t == "ConstantPathNode" mod_name = const_ref_flat_name(inc_ids[ik]) if mod_name != "" && module_name_exists(mod_name) == 1 alias_module_methods_at_toplevel(mod_name) end end ik = ik + 1 end end end si = si + 1 end end # Copy each `<mod_name>_cls_<m>` row in @meth_* to a parallel # bare-name row sharing body_id. On collision: an entry already # marked in @toplevel_include_alias is overwritten in place # (last-include-wins); user-def rows are unmarked and preserved. # A one-shot bare-name → row-index map keeps collision checks O(1). def alias_module_methods_at_toplevel(mod_name) prefix = mod_name + "_cls_" plen = prefix.length name_to_idx = {} k = 0 while k < @meth_names.length if !name_to_idx.key?(@meth_names[k]) name_to_idx[@meth_names[k]] = k end k = k + 1 end src_count = @meth_names.length si = 0 while si < src_count full = @meth_names[si] if full.length > plen && full[0, plen] == prefix bare = full[plen, full.length - plen] existing = -1 if name_to_idx.key?(bare) existing = name_to_idx[bare] end if existing < 0 @meth_names.push(bare) @meth_param_names.push(@meth_param_names[si]) @meth_param_types.push(@meth_param_types[si]) @meth_param_empty.push(@meth_param_empty[si]) @meth_return_types.push(@meth_return_types[si]) @meth_body_ids.push(@meth_body_ids[si]) @meth_has_defaults.push(@meth_has_defaults[si]) @meth_has_yield.push(@meth_has_yield[si]) @meth_rest_index.push(@meth_rest_index[si]) @toplevel_include_alias[bare] = 1 name_to_idx[bare] = @meth_names.length - 1 elsif @toplevel_include_alias.key?(bare) @meth_param_names[existing] = @meth_param_names[si] @meth_param_types[existing] = @meth_param_types[si] @meth_param_empty[existing] = @meth_param_empty[si] @meth_return_types[existing] = @meth_return_types[si] @meth_body_ids[existing] = @meth_body_ids[si] @meth_has_defaults[existing] = @meth_has_defaults[si] @meth_has_yield[existing] = @meth_has_yield[si] @meth_rest_index[existing] = @meth_rest_index[si] end end si = si + 1 end end def collect_toplevel_method(nid) mname = @nd_name[nid] body_id = @nd_body[nid]spinel_codegen.rbmodifiedView file
Loading diff…
test/toplevel_include_module_function.rbaddedView file
Loading diff…
test/toplevel_include_module_function.rb.expectedaddedView file
Loading diff…