Commit 229ae8ad
229ae8ad9d58Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +2 added · ~1 modified
Description
fix(analyze): extend M registers M's instance methods as class methods (#721)
class C; extend M; end used to be a silent no-op: the analyze
walker only treated include specially; extend calls just fell
through to the generic-no-op path. Subsequent C.<m> dispatch
emitted the cannot-resolve warning and returned 0.
Fix: walk class body / class-reopen body for CallNodes named
"extend" (alongside the existing "include" pass). For each
ConstantReadNode / ConstantPathNode argument that resolves to a
known module, copy that module's instance method definitions
(def X) onto the extending class as class methods (def self.X)
via append_cls_cmeth. def self.X defs on the module are skipped
-- those stay on the module itself in CRuby semantics.
The body_id is re-used as-is, so the same AST runs in both the
module's M.X context (via the existing module path) and the
extending class's C.X context. For module bodies with no self
references or @ivar reads this is correct; bodies that read @ivars
hit the same scan_ivars-driven const slot setup as include does.
Test: test/extend_module.rb covers the issue example plus a method
with an argument and verifies that def self.X on the module is
NOT pulled into the extending class.
465 tests pass; optcarrot checksum 59662.
Closes #721
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- 229ae8ad9d58
- Parent
- b84ace118500
spinel_analyze.rbmodifiedView file
end end end if @nd_name[sid] == "extend" ext_args = @nd_arguments[sid] if ext_args >= 0 ext_ids = get_args(ext_args) ek = 0 while ek < ext_ids.length extend_module_on_class(ci, ext_ids[ek], module_prefix) ek = ek + 1 end end end end } # Pin lexical scope while collecting ivars. See longer comment end end end # `extend M` -- M's instance methods become class methods on # this class. Issue #721. if @nd_name[sid] == "extend" ext_args = @nd_arguments[sid] if ext_args >= 0 ext_ids = get_args(ext_args) ek = 0 while ek < ext_ids.length extend_module_on_class(ci, ext_ids[ek], module_prefix) ek = ek + 1 end end end end } end end # `extend M` -- copy M's instance methods (`def X`) onto the # extending class as class methods (`def self.X`). Module class # methods (`def self.X` on the module) are skipped: those stay on # the module itself in CRuby's semantics. Issue #721. def extend_module_on_class(ci, ext_nid, module_prefix) ext_t = @nd_type[ext_nid] if ext_t != "ConstantReadNode" && ext_t != "ConstantPathNode" return end mod_name = const_ref_flat_name(ext_nid) if mod_name == "" return end effective_prefix = module_prefix if const_ref_is_relative(ext_nid) == 0 effective_prefix = "" end resolved_mod_name = resolve_include_module_name(mod_name, effective_prefix) mi = 0 while mi < @module_names.length if @module_names[mi] == resolved_mod_name mbody = @module_body_ids[mi] if mbody >= 0 mstmts = get_stmts(mbody) mk = 0 while mk < mstmts.length sid = mstmts[mk] if @nd_type[sid] == "DefNode" is_cls_def_e = 0 if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode" is_cls_def_e = 1 end if is_cls_def_e == 0 mname_e = @nd_name[sid] # Only add if this class doesn't already have a class # method with that name. cm_names_e = @cls_cmeth_names[ci].split(";") already_e = 0 ki_e = 0 while ki_e < cm_names_e.length if cm_names_e[ki_e] == mname_e already_e = 1 ki_e = cm_names_e.length else ki_e = ki_e + 1 end end if already_e == 0 params_e = collect_params_str(sid) ptypes_e = collect_ptypes_str(sid, ci) defs_e = collect_defaults_str(sid) body_e = @nd_body[sid] append_cls_cmeth(ci, mname_e, params_e, ptypes_e, "int", body_e, defs_e) end end end mk = mk + 1 end end end mi = mi + 1 end end # Re-run module-method collection for every recorded # (class, included-module) pair so reopens that registered after # the include site finally show up. See call-site comment fortest/extend_module.rbaddedView file
Loading diff…
test/extend_module.rb.expectedaddedView file
Loading diff…