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.hi calls 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 end
79357935 end
79367936 end
7937+ # `prepend M` -- spinel doesn't model the full MRO, so this
7938+ # behaves like `include M` (M's instance methods become
7939+ # callable on the class). super resolution to the original
7940+ # method isn't implemented here. Issue #720.
7941+ if @nd_name[sid] == "prepend"
7942+ pre_args = @nd_arguments[sid]
7943+ if pre_args >= 0
7944+ pre_ids = get_args(pre_args)
7945+ pk = 0
7946+ while pk < pre_ids.length
7947+ prepend_module_on_class(ci, pre_ids[pk], module_prefix)
7948+ pk = pk + 1
7949+ end
7950+ end
7951+ end
79377952 end
79387953 }
79397954 # Pin lexical scope while collecting ivars. See longer comment
@@ -8212,6 +8227,19 @@
82128227 end
82138228 end
82148229 end
8230+ # `prepend M` -- M's instance methods take precedence over the
8231+ # class's own. Issue #720.
8232+ if @nd_name[sid] == "prepend"
8233+ pre_args2 = @nd_arguments[sid]
8234+ if pre_args2 >= 0
8235+ pre_ids2 = get_args(pre_args2)
8236+ pk2 = 0
8237+ while pk2 < pre_ids2.length
8238+ prepend_module_on_class(ci, pre_ids2[pk2], module_prefix)
8239+ pk2 = pk2 + 1
8240+ end
8241+ end
8242+ end
82158243 end
82168244 }
82178245
@@ -8490,6 +8518,57 @@
84908518 end
84918519 end
84928520
8521+ # `prepend M` -- M's instance methods take precedence over the
8522+ # class's own. spinel doesn't model the full MRO; the simplest
8523+ # approximation is to copy M's methods over the class's via the
8524+ # last-def-wins path in append_cls_meth. super resolution back
8525+ # to the original is NOT implemented -- a prepended method that
8526+ # 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+ return
8532+ end
8533+ mod_name = const_ref_flat_name(inc_nid)
8534+ if mod_name == ""
8535+ return
8536+ end
8537+ effective_prefix = module_prefix
8538+ if const_ref_is_relative(inc_nid) == 0
8539+ effective_prefix = ""
8540+ end
8541+ resolved_mod_name = resolve_include_module_name(mod_name, effective_prefix)
8542+ record_class_include(ci, resolved_mod_name)
8543+ mi = 0
8544+ while mi < @module_names.length
8545+ if @module_names[mi] == resolved_mod_name
8546+ mbody = @module_body_ids[mi]
8547+ if mbody >= 0
8548+ mstmts = get_stmts(mbody)
8549+ mk = 0
8550+ while mk < mstmts.length
8551+ sid = mstmts[mk]
8552+ if @nd_type[sid] == "DefNode"
8553+ is_cls_def_p = 0
8554+ if @nd_receiver[sid] >= 0 && @nd_type[@nd_receiver[sid]] == "SelfNode"
8555+ is_cls_def_p = 1
8556+ end
8557+ if is_cls_def_p == 0
8558+ # collect_class_method routes through append_cls_meth
8559+ # which has last-def-wins on collision -- the prepended
8560+ # method replaces the class's own.
8561+ collect_class_method(ci, sid)
8562+ end
8563+ end
8564+ mk = mk + 1
8565+ end
8566+ end
8567+ end
8568+ mi = mi + 1
8569+ end
8570+ end
8571+
84938572 # `extend M` -- copy M's instance methods (`def X`) onto the
84948573 # extending class as class methods (`def self.X`). Module class
84958574 # methods (`def self.X` on the module) are skipped: those stay on