Commit da7ca3b7

da7ca3b74f65Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 12 files · +10 added · ~2 modified

Description

analyze/codegen: super inside prepended method via shadow chain

prepend M was a no-op for the super-resolution piece even after 2fcc10c -- a prepended method calling super fell through to the unresolved-call fallback, printing empty instead of the original body's return.

Mechanism: at prepend time, snapshot the existing instance method (if any) as a synthetic shadow __prep_<n> registered as a normal method on the same class. Active method (M's body) overwrites the slot via existing last-def-wins. compile_super_expr consults a new per-class chain table @cls_meth_prep_chain to redirect super calls to the right shadow; super from the deepest shadow falls through to the parent class lookup using the original user mname.

Multi-prepend stacks: each prepend snapshots the current active into the chain, so M2's super -> M1's body -> C's original.

Subclasses inherit prepended methods naturally -- find_method_owner walks to C and dispatches to the M-prepended slot.

What's covered:

  • super (SuperNode) and bare super (ForwardingSuperNode)
  • multi-prepend chains (stacked shadows)
  • parent class in chain (C < P; super walks past final shadow)
  • subclass inheritance of prepended methods

What's deferred:

  • super { block } from a prepended method (warning + fallback)
  • prepend followed by def (spinel's last-def-wins limitation)
  • ivar references in M's body to slots not on C's struct

Refs #720

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
da7ca3b74f65
Parent
7d364cf24d53

spinel_analyze.rbmodifiedView file

@@ -261,6 +261,20 @@
261261 # Mirror of @meth_param_empty for class methods. Pipe-separated by
262262 # method, comma-separated by param. .
263263 @cls_meth_ptypes_empty = "".split(",", -1)
264+ # Per-class prepend chain table. Issue #720 follow-up: when
265+ # `prepend M` overrides C's existing method `m`, the original
266+ # body is registered as a synthetic shadow method on C named
267+ # `__prep_<sanitize(m)>_<idx>` (idx = insertion order, 0 = first
268+ # snapshot taken). The chain table stores per-class entries of
269+ # the form `<mname>:<idx0>,<idx1>,...;<mname2>:...` so that
270+ # super-from-prepended dispatch can locate the next shadow in
271+ # the chain. Higher idx = closer to the active method; super
272+ # from active calls highest idx, super from shadow at idx k
273+ # calls k-1, super from idx 0 falls to the parent chain.
274+ @cls_meth_prep_chain = "".split(",", -1)
275+ # When 1, append_cls_meth suppresses its redefinition warning
276+ # (the overwrite is the explicit semantics of `prepend M`).
277+ @prep_overwrite_in_progress = 0
264278 @cls_attr_readers = "".split(",", -1)
265279 @cls_attr_writers = "".split(",", -1)
266280 @cls_cmeth_names = "".split(",", -1)
@@ -1934,13 +1948,28 @@
19341948 # returns. Walk to the parent's `find_method_owner`-resolved
19351949 # method and read its return type.
19361950 if @current_class_idx >= 0 && @current_method_name != ""
1951+ # Prepend chain redirect (issue #720). When inside a prepended
1952+ # method on C, super calls the next shadow on C; when inside a
1953+ # shadow, super calls the next-older shadow (lower index) or
1954+ # falls to the parent walk at index 0.
1955+ prep_target = prep_super_target(@current_class_idx, @current_method_name)
1956+ if prep_target != ""
1957+ ret_pt = cls_method_return(@current_class_idx, prep_target)
1958+ if ret_pt != ""
1959+ return ret_pt
1960+ end
1961+ end
19371962 parent_name_st = @cls_parents[@current_class_idx]
19381963 if parent_name_st != ""
19391964 parent_ci_st = find_class_idx(parent_name_st)
19401965 if parent_ci_st >= 0
1941- owner_st = find_method_owner(parent_ci_st, @current_method_name)
1966+ # When inside a shadow whose chain index is 0 (deepest), the
1967+ # super walks past the synthetic and reads the parent's method
1968+ # under the *original* user mname, not the synthetic name.
1969+ super_mname = prep_super_parent_mname(@current_class_idx, @current_method_name)
1970+ owner_st = find_method_owner(parent_ci_st, super_mname)
19421971 if owner_st != ""
1943- ret_st = cls_method_return(find_class_idx(owner_st), @current_method_name)
1972+ ret_st = cls_method_return(find_class_idx(owner_st), super_mname)
19441973 if ret_st != ""
19451974 return ret_st
19461975 end
@@ -8168,6 +8197,7 @@
81688197 end
81698198 @cls_attr_readers.push(attr_readers)
81708199 @cls_attr_writers.push(attr_writers)
8200+ @cls_meth_prep_chain.push("")
81718201 @cls_cmeth_names.push("")
81728202 @cls_cmeth_params.push("")
81738203 @cls_cmeth_ptypes.push("")
@@ -8567,12 +8597,15 @@
85678597 end
85688598
85698599 # `prepend M` -- M's instance methods take precedence over the
8570- # class's own. spinel doesn't model the full MRO; the simplest
8571- # approximation is to copy M's methods over the class's via the
8572- # last-def-wins path in append_cls_meth. super resolution back
8573- # to the original is NOT implemented -- a prepended method that
8574- # calls super hits the unresolved-call fallback.
8575- # Issue #720.
8600+ # class's own. For each method M defines that C already has,
8601+ # snapshot C's existing slot into a synthetic shadow method
8602+ # (`__prep_<n>`) on the same class before letting M's body
8603+ # overwrite the active slot. The shadow is registered as a
8604+ # regular instance method on C so inference, ivar resolution,
8605+ # and codegen emission all pick it up for free. super dispatch
8606+ # (compile_super_expr / infer_type's SuperNode arm) consults
8607+ # @cls_meth_prep_chain to route super calls through the chain.
8608+ # Multi-prepend stacks: each call extends the chain. Issue #720.
85768609 def prepend_module_on_class(ci, inc_nid, module_prefix)
85778610 inc_t = @nd_type[inc_nid]
85788611 if inc_t != "ConstantReadNode" && inc_t != "ConstantPathNode"
@@ -8603,10 +8636,13 @@
86038636 is_cls_def_p = 1
86048637 end
86058638 if is_cls_def_p == 0
8606- # collect_class_method routes through append_cls_meth
8607- # which has last-def-wins on collision -- the prepended
8608- # method replaces the class's own.
8639+ # Snapshot the existing method (if any) as a shadow before
8640+ # overwrite. prep_snapshot_active is a no-op when there's
8641+ # nothing to snapshot.
8642+ prep_snapshot_active(ci, @nd_name[sid])
8643+ @prep_overwrite_in_progress = 1
86098644 collect_class_method(ci, sid)
8645+ @prep_overwrite_in_progress = 0
86108646 end
86118647 end
86128648 mk = mk + 1
@@ -8617,6 +8653,258 @@
86178653 end
86188654 end
86198655
8656+ # Snapshot class ci's existing method `mname` as a synthetic
8657+ # shadow `__prep_<n>` and record the (mname -> shadow) link in
8658+ # @cls_meth_prep_chain[ci]. No-op if ci has no existing method
8659+ # named `mname`. Synthetic names use a numeric suffix (no source
8660+ # mname encoded) so they don't need C-name sanitization;
8661+ # @cls_meth_prep_chain is the authoritative mapping. Issue #720.
8662+ def prep_snapshot_active(ci, mname)
8663+ existing_mi = cls_find_method_direct(ci, mname)
8664+ if existing_mi < 0
8665+ return
8666+ end
8667+ cur_names = @cls_meth_names[ci].split(";", -1)
8668+ cur_params = @cls_meth_params[ci].split("|", -1)
8669+ cur_ptypes = @cls_meth_ptypes[ci].split("|", -1)
8670+ cur_returns = @cls_meth_returns[ci].split(";", -1)
8671+ cur_bodies = @cls_meth_bodies[ci].split(";", -1)
8672+ cur_defaults = @cls_meth_defaults[ci].split("|", -1)
8673+ cur_hy = @cls_meth_has_yield[ci].split(";", -1)
8674+ snap_params = ""
8675+ snap_ptypes = ""
8676+ snap_ret = "int"
8677+ snap_body = -1
8678+ snap_defs = ""
8679+ snap_hy = "0"
8680+ if existing_mi < cur_params.length
8681+ snap_params = cur_params[existing_mi]
8682+ end
8683+ if existing_mi < cur_ptypes.length
8684+ snap_ptypes = cur_ptypes[existing_mi]
8685+ end
8686+ if existing_mi < cur_returns.length
8687+ snap_ret = cur_returns[existing_mi]
8688+ end
8689+ if existing_mi < cur_bodies.length
8690+ snap_body = cur_bodies[existing_mi].to_i
8691+ end
8692+ if existing_mi < cur_defaults.length
8693+ snap_defs = cur_defaults[existing_mi]
8694+ end
8695+ if existing_mi < cur_hy.length
8696+ snap_hy = cur_hy[existing_mi]
8697+ end
8698+ syn_name = prep_alloc_syn_name(ci)
8699+ # Register the shadow as a normal instance method on ci.
8700+ # Use raw append (not append_cls_meth) to avoid the redefinition
8701+ # path -- the synthetic name is guaranteed unique by allocator.
8702+ if @cls_meth_names[ci] != ""
8703+ @cls_meth_names[ci] = @cls_meth_names[ci] + ";" + syn_name
8704+ @cls_meth_params[ci] = @cls_meth_params[ci] + "|" + snap_params
8705+ @cls_meth_ptypes[ci] = @cls_meth_ptypes[ci] + "|" + snap_ptypes
8706+ @cls_meth_returns[ci] = @cls_meth_returns[ci] + ";" + snap_ret
8707+ @cls_meth_bodies[ci] = @cls_meth_bodies[ci] + ";" + snap_body.to_s
8708+ @cls_meth_defaults[ci] = @cls_meth_defaults[ci] + "|" + snap_defs
8709+ @cls_meth_ptypes_empty[ci] = @cls_meth_ptypes_empty[ci] + "|"
8710+ else
8711+ @cls_meth_names[ci] = syn_name
8712+ @cls_meth_params[ci] = snap_params
8713+ @cls_meth_ptypes[ci] = snap_ptypes
8714+ @cls_meth_returns[ci] = snap_ret
8715+ @cls_meth_bodies[ci] = snap_body.to_s
8716+ @cls_meth_defaults[ci] = snap_defs
8717+ @cls_meth_ptypes_empty[ci] = ""
8718+ end
8719+ if @cls_meth_has_yield[ci] != ""
8720+ @cls_meth_has_yield[ci] = @cls_meth_has_yield[ci] + ";" + snap_hy
8721+ else
8722+ @cls_meth_has_yield[ci] = snap_hy
8723+ end
8724+ @cls_meth_params_version = @cls_meth_params_version + 1
8725+ @cls_meth_ptypes_version = @cls_meth_ptypes_version + 1
8726+ @cls_meth_idx_cache = {}
8727+ @cls_meth_return_cache = {}
8728+ # Append (mname, syn_name) to the chain. Chain entry shape:
8729+ # `<mname>=<syn0>,<syn1>,...` joined by `;` across mnames.
8730+ chain_str = @cls_meth_prep_chain[ci]
8731+ if chain_str == ""
8732+ @cls_meth_prep_chain[ci] = mname + "=" + syn_name
8733+ else
8734+ entries = chain_str.split(";", -1)
8735+ found = 0
8736+ ei = 0
8737+ while ei < entries.length
8738+ eq_pos = entries[ei].index("=")
8739+ if eq_pos != nil
8740+ em = entries[ei][0, eq_pos]
8741+ if em == mname
8742+ entries[ei] = entries[ei] + "," + syn_name
8743+ found = 1
8744+ ei = entries.length
8745+ end
8746+ end
8747+ ei = ei + 1
8748+ end
8749+ if found == 0
8750+ entries.push(mname + "=" + syn_name)
8751+ end
8752+ @cls_meth_prep_chain[ci] = entries.join(";")
8753+ end
8754+ end
8755+
8756+ # Allocate a synthetic shadow name unique on class ci. Probes
8757+ # `__prep_0`, `__prep_1`, ... for the first free slot.
8758+ def prep_alloc_syn_name(ci)
8759+ seq = 0
8760+ while seq < 4096
8761+ cand = "__prep_" + seq.to_s
8762+ if cls_find_method_direct(ci, cand) < 0
8763+ return cand
8764+ end
8765+ seq = seq + 1
8766+ end
8767+ "__prep_overflow"
8768+ end
8769+
8770+ # Return the list of synthetic shadow names (in chain order:
8771+ # index 0 = oldest = closest to parent, last index = newest =
8772+ # next super target from active) for (ci, mname). [] if no
8773+ # prepends.
8774+ def prep_chain_syn_names(ci, mname)
8775+ result = "".split(",", -1)
8776+ if ci < 0 || ci >= @cls_meth_prep_chain.length
8777+ return result
8778+ end
8779+ chain_str = @cls_meth_prep_chain[ci]
8780+ if chain_str == ""
8781+ return result
8782+ end
8783+ entries = chain_str.split(";", -1)
8784+ ei = 0
8785+ while ei < entries.length
8786+ eq_pos = entries[ei].index("=")
8787+ if eq_pos != nil
8788+ em = entries[ei][0, eq_pos]
8789+ if em == mname
8790+ syns_str = entries[ei][eq_pos + 1, entries[ei].length - eq_pos - 1]
8791+ return syns_str.split(",", -1)
8792+ end
8793+ end
8794+ ei = ei + 1
8795+ end
8796+ result
8797+ end
8798+
8799+ # Determine the on-class super target for (ci, current_mname).
8800+ # Returns the synthetic name to call (always a method on ci) or
8801+ # "" if super should fall through to the parent class chain.
8802+ #
8803+ # - current is an active prepended method (chain non-empty):
8804+ # -> highest-index (newest) shadow
8805+ # - current is a shadow `__prep_<n>` at chain index k > 0:
8806+ # -> shadow at index k - 1
8807+ # - current is a shadow at chain index 0 or not prepended:
8808+ # -> "" (caller falls to parent)
8809+ def prep_super_target(ci, cur_mname)
8810+ if ci < 0 || ci >= @cls_meth_prep_chain.length
8811+ return ""
8812+ end
8813+ if @cls_meth_prep_chain[ci] == ""
8814+ return ""
8815+ end
8816+ pos = prep_syn_position(ci, cur_mname)
8817+ if pos >= 0
8818+ if pos > 0
8819+ src_mname = prep_syn_source_mname(ci, cur_mname)
8820+ syns = prep_chain_syn_names(ci, src_mname)
8821+ if pos - 1 < syns.length
8822+ return syns[pos - 1]
8823+ end
8824+ end
8825+ return ""
8826+ end
8827+ syns = prep_chain_syn_names(ci, cur_mname)
8828+ if syns.length > 0
8829+ return syns[syns.length - 1]
8830+ end
8831+ ""
8832+ end
8833+
8834+ # When super from a shadow falls past the synthetic to the
8835+ # parent class, the parent-side lookup must use the *original*
8836+ # user-visible mname, not the synthetic. Returns the user mname
8837+ # for a shadow, or cur_mname unchanged for non-shadows.
8838+ def prep_super_parent_mname(ci, cur_mname)
8839+ src = prep_syn_source_mname(ci, cur_mname)
8840+ if src != ""
8841+ return src
8842+ end
8843+ cur_mname
8844+ end
8845+
8846+ # Source user mname for a synthetic shadow on class ci. "" if
8847+ # syn_name is not a shadow. Scalar return -- callers can plug
8848+ # the result directly into string-typed contexts.
8849+ def prep_syn_source_mname(ci, syn_name)
8850+ if ci < 0 || ci >= @cls_meth_prep_chain.length
8851+ return ""
8852+ end
8853+ chain_str = @cls_meth_prep_chain[ci]
8854+ if chain_str == ""
8855+ return ""
8856+ end
8857+ entries = chain_str.split(";", -1)
8858+ ei = 0
8859+ while ei < entries.length
8860+ eq_pos = entries[ei].index("=")
8861+ if eq_pos != nil
8862+ em = entries[ei][0, eq_pos]
8863+ syns_str = entries[ei][eq_pos + 1, entries[ei].length - eq_pos - 1]
8864+ syns = syns_str.split(",", -1)
8865+ si = 0
8866+ while si < syns.length
8867+ if syns[si] == syn_name
8868+ return em
8869+ end
8870+ si = si + 1
8871+ end
8872+ end
8873+ ei = ei + 1
8874+ end
8875+ ""
8876+ end
8877+
8878+ # Chain position (0 = oldest, deepest from active) for a
8879+ # synthetic shadow on ci. -1 if syn_name is not a shadow.
8880+ def prep_syn_position(ci, syn_name)
8881+ if ci < 0 || ci >= @cls_meth_prep_chain.length
8882+ return -1
8883+ end
8884+ chain_str = @cls_meth_prep_chain[ci]
8885+ if chain_str == ""
8886+ return -1
8887+ end
8888+ entries = chain_str.split(";", -1)
8889+ ei = 0
8890+ while ei < entries.length
8891+ eq_pos = entries[ei].index("=")
8892+ if eq_pos != nil
8893+ syns_str = entries[ei][eq_pos + 1, entries[ei].length - eq_pos - 1]
8894+ syns = syns_str.split(",", -1)
8895+ si = 0
8896+ while si < syns.length
8897+ if syns[si] == syn_name
8898+ return si
8899+ end
8900+ si = si + 1
8901+ end
8902+ end
8903+ ei = ei + 1
8904+ end
8905+ -1
8906+ end
8907+
86208908 # `extend M` -- copy M's instance methods (`def X`) onto the
86218909 # extending class as class methods (`def self.X`). Module class
86228910 # methods (`def self.X` on the module) are skipped: those stay on
@@ -9087,7 +9375,9 @@
90879375 # (matches class reopen for adding new methods) and warn so the
90889376 # user can spot the divergence from CRuby's intermediate-version
90899377 # behaviour.
9090- $stderr.puts "warning: method '" + name + "' on " + @cls_names[ci] + " is being redefined; spinel uses last-def-wins semantics (calls before the redefinition won't see the original body — subset limitation)"
9378+ if @prep_overwrite_in_progress == 0
9379+ $stderr.puts "warning: method '" + name + "' on " + @cls_names[ci] + " is being redefined; spinel uses last-def-wins semantics (calls before the redefinition won't see the original body — subset limitation)"
9380+ end
90919381 cur_names = @cls_meth_names[ci].split(";", -1)
90929382 cur_params = @cls_meth_params[ci].split("|", -1)
90939383 cur_ptypes = @cls_meth_ptypes[ci].split("|", -1)
@@ -11763,6 +12053,7 @@
1176312053 @cls_meth_ptypes_empty.push("")
1176412054 @cls_attr_readers.push("")
1176512055 @cls_attr_writers.push("")
12056+ @cls_meth_prep_chain.push("")
1176612057 @cls_cmeth_names.push("")
1176712058 @cls_cmeth_params.push("")
1176812059 @cls_cmeth_ptypes.push("")
@@ -11797,6 +12088,7 @@
1179712088 @cls_meth_ptypes_empty.push("")
1179812089 @cls_attr_readers.push("")
1179912090 @cls_attr_writers.push("")
12091+ @cls_meth_prep_chain.push("")
1180012092 @cls_cmeth_names.push("")
1180112093 @cls_cmeth_params.push("")
1180212094 @cls_cmeth_ptypes.push("")
@@ -25436,6 +25728,36 @@
2543625728 end
2543725729 ui = ui + 1
2543825730 end
25731+ # Step 4 (issue #720): for every class with a prep chain, if the
25732+ # active user-visible method is live, mark every synthetic shadow
25733+ # in the chain live too. Shadows are only called from super
25734+ # dispatch (compile_super_expr) which doesn't appear as a
25735+ # CallNode in the AST, so the previous steps miss them.
25736+ cip = 0
25737+ while cip < @cls_meth_prep_chain.length
25738+ chain_str = @cls_meth_prep_chain[cip]
25739+ if chain_str != ""
25740+ entries = chain_str.split(";", -1)
25741+ ep = 0
25742+ while ep < entries.length
25743+ eq_pos = entries[ep].index("=")
25744+ if eq_pos != nil
25745+ em = entries[ep][0, eq_pos]
25746+ if cls_meth_is_live(cip, em) == 1
25747+ syns_str = entries[ep][eq_pos + 1, entries[ep].length - eq_pos - 1]
25748+ syns = syns_str.split(",", -1)
25749+ sp = 0
25750+ while sp < syns.length
25751+ cls_meth_mark_live(cip, syns[sp])
25752+ sp = sp + 1
25753+ end
25754+ end
25755+ end
25756+ ep = ep + 1
25757+ end
25758+ end
25759+ cip = cip + 1
25760+ end
2543925761 end
2544025762
2544125763 # Walk the AST collecting CallNode names + SymbolNode values into
@@ -28758,6 +29080,7 @@
2875829080 buf = ir_emit_sa(buf, "@cls_meth_bodies", @cls_meth_bodies)
2875929081 buf = ir_emit_sa(buf, "@cls_meth_defaults", @cls_meth_defaults)
2876029082 buf = ir_emit_sa(buf, "@cls_meth_ptypes_empty", @cls_meth_ptypes_empty)
29083+ buf = ir_emit_sa(buf, "@cls_meth_prep_chain", @cls_meth_prep_chain)
2876129084 buf = ir_emit_sa(buf, "@cls_attr_readers", @cls_attr_readers)
2876229085 buf = ir_emit_sa(buf, "@cls_attr_writers", @cls_attr_writers)
2876329086 buf = ir_emit_sa(buf, "@cls_cmeth_names", @cls_cmeth_names)
@@ -30685,11 +31008,20 @@
3068531008 saved_ci = @current_class_idx
3068631009 @current_class_idx = ci
3068731010 bodies = @cls_meth_bodies[ci].split(";", -1)
31011+ ann_mnames = @cls_meth_names[ci].split(";", -1)
3068831012 bj = 0
3068931013 while bj < bodies.length
3069031014 bid = bodies[bj].to_i
3069131015 if bid >= 0
3069231016 push_scope
31017+ # Issue #720: SuperNode infer_type consults @current_method_name
31018+ # to drive prep-chain redirect. Without setting it, super inside
31019+ # a prepended method or shadow caches as "int" and codegen reads
31020+ # the stale cache before its own redirect runs.
31021+ saved_meth_cls = @current_method_name
31022+ if bj < ann_mnames.length
31023+ @current_method_name = ann_mnames[bj]
31024+ end
3069331025 pnames2 = cls_meth_pnames_get(ci, bj)
3069431026 ptypes2 = cls_meth_ptypes_get(ci, bj)
3069531027 pk = 0
@@ -30705,6 +31037,7 @@
3070531037 mt2 = "".split(",", -1)
3070631038 refine_method_body_locals(bid, ml2, mt2, pnames2)
3070731039 walk_and_cache(bid)
31040+ @current_method_name = saved_meth_cls
3070831041 pop_scope
3070931042 end
3071031043 bj = bj + 1