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

@@ -7884,6 +7884,17 @@
78847884 end
78857885 end
78867886 end
7887+ if @nd_name[sid] == "extend"
7888+ ext_args = @nd_arguments[sid]
7889+ if ext_args >= 0
7890+ ext_ids = get_args(ext_args)
7891+ ek = 0
7892+ while ek < ext_ids.length
7893+ extend_module_on_class(ci, ext_ids[ek], module_prefix)
7894+ ek = ek + 1
7895+ end
7896+ end
7897+ end
78877898 end
78887899 }
78897900 # Pin lexical scope while collecting ivars. See longer comment
@@ -8126,6 +8137,19 @@
81268137 end
81278138 end
81288139 end
8140+ # `extend M` -- M's instance methods become class methods on
8141+ # this class. Issue #721.
8142+ if @nd_name[sid] == "extend"
8143+ ext_args = @nd_arguments[sid]
8144+ if ext_args >= 0
8145+ ext_ids = get_args(ext_args)
8146+ ek = 0
8147+ while ek < ext_ids.length
8148+ extend_module_on_class(ci, ext_ids[ek], module_prefix)
8149+ ek = ek + 1
8150+ end
8151+ end
8152+ end
81298153 end
81308154 }
81318155
@@ -8404,6 +8428,70 @@
84048428 end
84058429 end
84068430
8431+ # `extend M` -- copy M's instance methods (`def X`) onto the
8432+ # extending class as class methods (`def self.X`). Module class
8433+ # methods (`def self.X` on the module) are skipped: those stay on
8434+ # the module itself in CRuby's semantics. Issue #721.
8435+ def extend_module_on_class(ci, ext_nid, module_prefix)
8436+ ext_t = @nd_type[ext_nid]
8437+ if ext_t != "ConstantReadNode" && ext_t != "ConstantPathNode"
8438+ return
8439+ end
8440+ mod_name = const_ref_flat_name(ext_nid)
8441+ if mod_name == ""
8442+ return
8443+ end
8444+ effective_prefix = module_prefix
8445+ if const_ref_is_relative(ext_nid) == 0
8446+ effective_prefix = ""
8447+ end
8448+ resolved_mod_name = resolve_include_module_name(mod_name, effective_prefix)
8449+ mi = 0
8450+ while mi < @module_names.length
8451+ if @module_names[mi] == resolved_mod_name
8452+ mbody = @module_body_ids[mi]
8453+ if mbody >= 0
8454+ mstmts = get_stmts(mbody)
8455+ mk = 0
8456+ while mk < mstmts.length
8457+ sid = mstmts[mk]
8458+ if @nd_type[sid] == "DefNode"
8459+ is_cls_def_e = 0
8460+ if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"
8461+ is_cls_def_e = 1
8462+ end
8463+ if is_cls_def_e == 0
8464+ mname_e = @nd_name[sid]
8465+ # Only add if this class doesn't already have a class
8466+ # method with that name.
8467+ cm_names_e = @cls_cmeth_names[ci].split(";")
8468+ already_e = 0
8469+ ki_e = 0
8470+ while ki_e < cm_names_e.length
8471+ if cm_names_e[ki_e] == mname_e
8472+ already_e = 1
8473+ ki_e = cm_names_e.length
8474+ else
8475+ ki_e = ki_e + 1
8476+ end
8477+ end
8478+ if already_e == 0
8479+ params_e = collect_params_str(sid)
8480+ ptypes_e = collect_ptypes_str(sid, ci)
8481+ defs_e = collect_defaults_str(sid)
8482+ body_e = @nd_body[sid]
8483+ append_cls_cmeth(ci, mname_e, params_e, ptypes_e, "int", body_e, defs_e)
8484+ end
8485+ end
8486+ end
8487+ mk = mk + 1
8488+ end
8489+ end
8490+ end
8491+ mi = mi + 1
8492+ end
8493+ end
8494+
84078495 # Re-run module-method collection for every recorded
84088496 # (class, included-module) pair so reopens that registered after
84098497 # the include site finally show up. See call-site comment for