Commit f8c852cd
f8c852cd0f2cMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(analyze): prepend M overrides class methods via last-def-wins (#720)
prepend M used to be silently ignored. Spinel doesn't model
the full MRO with proper super resolution, so the simplest
useful approximation is: copy M's instance methods onto the
class via collect_class_method (which routes through
append_cls_meth's last-def-wins). The prepended method
replaces the class's own at every call site.
What works:
- prepended methods are now reachable --
C.new.hicalls M#hi.
What's deferred:
- super inside a prepended method falls back to the unresolved- call path (no MRO chain to walk). The prepended method effectively replaces the original rather than wrapping it.
Closes #720
Co-Authored-By: Claude Opus 4.7 [email protected]
Details
- Commit
- f8c852cd0f2c
- Parent
- 554a4eb79996
spinel_analyze.rbmodifiedView file
@@ -7934,6 +7934,21 @@
79347934
end79357935
end79367936
end7937+
# `prepend M` -- spinel doesn't model the full MRO, so this7938+
# behaves like `include M` (M's instance methods become7939+
# callable on the class). super resolution to the original7940+
# method isn't implemented here. Issue #720.7941+
if @nd_name[sid] == "prepend"7942+
pre_args = @nd_arguments[sid]7943+
if pre_args >= 07944+
pre_ids = get_args(pre_args)7945+
pk = 07946+
while pk < pre_ids.length7947+
prepend_module_on_class(ci, pre_ids[pk], module_prefix)7948+
pk = pk + 17949+
end7950+
end7951+
end79377952
end79387953
}79397954
# Pin lexical scope while collecting ivars. See longer comment@@ -8212,6 +8227,19 @@
82128227
end82138228
end82148229
end8230+
# `prepend M` -- M's instance methods take precedence over the8231+
# class's own. Issue #720.8232+
if @nd_name[sid] == "prepend"8233+
pre_args2 = @nd_arguments[sid]8234+
if pre_args2 >= 08235+
pre_ids2 = get_args(pre_args2)8236+
pk2 = 08237+
while pk2 < pre_ids2.length8238+
prepend_module_on_class(ci, pre_ids2[pk2], module_prefix)8239+
pk2 = pk2 + 18240+
end8241+
end8242+
end82158243
end82168244
}82178245
@@ -8490,6 +8518,57 @@
84908518
end84918519
end84928520
8521+
# `prepend M` -- M's instance methods take precedence over the8522+
# class's own. spinel doesn't model the full MRO; the simplest8523+
# approximation is to copy M's methods over the class's via the8524+
# last-def-wins path in append_cls_meth. super resolution back8525+
# to the original is NOT implemented -- a prepended method that8526+
# calls super hits the unresolved-call fallback.8527+
# Issue #720.8528+
def prepend_module_on_class(ci, inc_nid, module_prefix)8529+
inc_t = @nd_type[inc_nid]8530+
if inc_t != "ConstantReadNode" && inc_t != "ConstantPathNode"8531+
return8532+
end8533+
mod_name = const_ref_flat_name(inc_nid)8534+
if mod_name == ""8535+
return8536+
end8537+
effective_prefix = module_prefix8538+
if const_ref_is_relative(inc_nid) == 08539+
effective_prefix = ""8540+
end8541+
resolved_mod_name = resolve_include_module_name(mod_name, effective_prefix)8542+
record_class_include(ci, resolved_mod_name)8543+
mi = 08544+
while mi < @module_names.length8545+
if @module_names[mi] == resolved_mod_name8546+
mbody = @module_body_ids[mi]8547+
if mbody >= 08548+
mstmts = get_stmts(mbody)8549+
mk = 08550+
while mk < mstmts.length8551+
sid = mstmts[mk]8552+
if @nd_type[sid] == "DefNode"8553+
is_cls_def_p = 08554+
if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"8555+
is_cls_def_p = 18556+
end8557+
if is_cls_def_p == 08558+
# collect_class_method routes through append_cls_meth8559+
# which has last-def-wins on collision -- the prepended8560+
# method replaces the class's own.8561+
collect_class_method(ci, sid)8562+
end8563+
end8564+
mk = mk + 18565+
end8566+
end8567+
end8568+
mi = mi + 18569+
end8570+
end8571+
84938572
# `extend M` -- copy M's instance methods (`def X`) onto the84948573
# extending class as class methods (`def self.X`). Module class84958574
# methods (`def self.X` on the module) are skipped: those stay on