Commit 06be9d56

06be9d56b1d2Merged

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

Description

fix(analyze,codegen): self[k] / self[k]=v from parent body dispatch to subclass override

Sam Ruby's #563. self[k] and self[k] = v inside a parent- defined method statically resolved to the parent's operator stub, ignoring subclass overrides. Roundhouse uncovered the gap after #558 closed the cols.include? precursor: ActiveRecord::Base#fill_timestamps does self[:updated_at] = now if cols.include?(:updated_at) and the dispatch must land on the model subclass's []= routing surface, not raise NotImplementedError from the base stub.

Three pieces:

  1. Self-recv override dispatch for self[k] in compile_call_expr's obj_-recv arm (compile_user_class_method_expr). When recv is a SelfNode inside @current_class_idx and descendants override mname, emit a switch (rc->cls_id) mirroring the bare-imeth path. Reads return / param types via the existing cls_method_override_descendants helper.

  2. Same dispatch for the statement form self[k] = v in compile_bracket_assign's user-class __aset arm. The stmt path is separate from the expr path; both needed the switch.

  3. unify_imeth_override_returns + @unified_imeth_returns tracking (analyze). The cls_id-switch's rt_ok gate in codegen rejects override families with disagreeing return types; raise-only stubs default to "int" while concrete subclass overrides return e.g. "string", so analyze must unify them first. The pass runs post-fixpoint, and a flag marks each unified slot so a subsequent infer_all_returns doesn't re-derive the raise-only body's int and clobber the unification. Cross-override descendants get the unified return too so each override's C signature agrees on the return-slot type.

Same machinery covers the broader operator family the issue notes (==, <=>, +, <<, etc.) since they all route through the same obj-recv dispatch path.

Verified: 532 tests pass, optcarrot checksum 59662 preserved.

Closes #563

Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]

Details

Commit
06be9d56b1d2
Parent
447f469047b7

spinel_analyze.rbmodifiedView file

@@ -15908,6 +15908,99 @@
1590815908 end
1590915909 end
1591015910
15911+ # For each (class, imeth) where descendants override the
15912+ # imeth, unify the return types across the family and write
15913+ # back. Without this, a base whose only definition is
15914+ # `raise NotImplementedError` keeps the mrb_int default
15915+ # while subclass overrides return concrete types; the
15916+ # codegen-side override-dispatch (compile_call_expr's
15917+ # self-recv arm at compile_user_class_method_expr) gates
15918+ # on `c_type(base_rt) == c_type(cand_rt)` and skips the
15919+ # dispatch when they differ, falling back to the static
15920+ # call that runs the abstract stub. Issue #563.
15921+ # Tracks (ci, mi) pairs whose return type was promoted by
15922+ # unify_imeth_override_returns. propagate_unified_returns_to_callers
15923+ # uses this to skip re-deriving these methods' return types from
15924+ # their bodies during the post-unify infer_all_returns pass
15925+ # (raise-only bodies would otherwise re-derive as "int").
15926+ def unify_imeth_override_returns
15927+ if @unified_imeth_returns == nil
15928+ @unified_imeth_returns = "".split(",")
15929+ end
15930+ ci = 0
15931+ while ci < @cls_names.length
15932+ mnames = @cls_meth_names[ci].split(";")
15933+ returns = @cls_meth_returns[ci].split(";")
15934+ changed = 0
15935+ mi = 0
15936+ while mi < mnames.length
15937+ mname = mnames[mi]
15938+ if mname != ""
15939+ types = "".split(",")
15940+ base_ret = ""
15941+ if mi < returns.length
15942+ base_ret = returns[mi]
15943+ types.push(base_ret)
15944+ end
15945+ have_descendant_override = 0
15946+ dci = 0
15947+ while dci < @cls_names.length
15948+ if dci != ci && cls_is_descendant(dci, ci) == 1
15949+ dmidx = cls_find_method_direct(dci, mname)
15950+ if dmidx >= 0
15951+ d_returns = @cls_meth_returns[dci].split(";")
15952+ if dmidx < d_returns.length
15953+ types.push(d_returns[dmidx])
15954+ have_descendant_override = 1
15955+ end
15956+ end
15957+ end
15958+ dci = dci + 1
15959+ end
15960+ if have_descendant_override == 1
15961+ unified = unify_return_type(types)
15962+ if base_ret != unified && unified != ""
15963+ returns[mi] = unified
15964+ changed = 1
15965+ # Record the (ci, mi) so callers that re-derive return
15966+ # types skip this slot rather than reverting to the
15967+ # raise-only "int" default.
15968+ tag = ci.to_s + ":" + mi.to_s
15969+ if @unified_imeth_returns.index(tag) == nil
15970+ @unified_imeth_returns.push(tag)
15971+ end
15972+ dci2 = 0
15973+ while dci2 < @cls_names.length
15974+ if dci2 != ci && cls_is_descendant(dci2, ci) == 1
15975+ dmidx2 = cls_find_method_direct(dci2, mname)
15976+ if dmidx2 >= 0
15977+ d_returns2 = @cls_meth_returns[dci2].split(";")
15978+ if dmidx2 < d_returns2.length && d_returns2[dmidx2] != unified
15979+ d_returns2[dmidx2] = unified
15980+ @cls_meth_returns[dci2] = d_returns2.join(";")
15981+ @cls_meth_return_cache = {}
15982+ dtag = dci2.to_s + ":" + dmidx2.to_s
15983+ if @unified_imeth_returns.index(dtag) == nil
15984+ @unified_imeth_returns.push(dtag)
15985+ end
15986+ end
15987+ end
15988+ end
15989+ dci2 = dci2 + 1
15990+ end
15991+ end
15992+ end
15993+ end
15994+ mi = mi + 1
15995+ end
15996+ if changed == 1
15997+ @cls_meth_returns[ci] = returns.join(";")
15998+ @cls_meth_return_cache = {}
15999+ end
16000+ ci = ci + 1
16001+ end
16002+ end
16003+
1591116004 def infer_all_returns
1591216005 # Pre-pass: infer class method param types from body usage
1591316006 infer_cls_meth_param_from_body
@@ -16135,6 +16228,15 @@
1613516228 rt = infer_body_return(bid)
1613616229 end
1613716230 end
16231+ # Override-unified imeth: keep the unified return rather
16232+ # than the body-derived (often "int" for raise-only stubs).
16233+ # Issue #563.
16234+ if @unified_imeth_returns != nil && j < returns.length
16235+ tag_iru = i.to_s + ":" + j.to_s
16236+ if @unified_imeth_returns.index(tag_iru) != nil && returns[j] != "" && returns[j] != "int"
16237+ rt = returns[j]
16238+ end
16239+ end
1613816240 if j < returns.length
1613916241 returns[j] = rt
1614016242 end
@@ -18321,8 +18423,15 @@
1832118423 end
1832218424 if @nd_type[nid] == "CallNode"
1832318425 mname = @nd_name[nid]
18324- # Handle implicit self calls (no receiver) to same-class methods
18325- if @nd_receiver[nid] < 0
18426+ # Handle implicit self calls (no receiver) and explicit
18427+ # self.X / self[k] calls to same-class methods. The same
18428+ # cls_id-switch dispatch covers both shapes at codegen, so
18429+ # the analyze-side widening must too. Issue #563 (the
18430+ # operator-form `self[k]` / `self[k] = v` shape uncovered
18431+ # this gap, which the existing #516 fix only handled for
18432+ # bare-recv calls).
18433+ recv_scm = @nd_receiver[nid]
18434+ if recv_scm < 0 || @nd_type[recv_scm] == "SelfNode"
1832618435 midx = cls_find_method_direct(ci, mname)
1832718436 if midx >= 0
1832818437 args_id = @nd_arguments[nid]
@@ -18630,6 +18739,27 @@
1863018739 infer_function_body_call_types
1863118740 infer_class_body_call_types
1863218741 infer_all_returns
18742+ # Unify imeth return types across override families AFTER
18743+ # the fixpoint converges. Running inside the loop is
18744+ # ineffective because the next iteration's infer_all_returns
18745+ # re-derives raise-only methods to int and clobbers the
18746+ # unified value. Post-fixpoint this is the final pass that
18747+ # touches @cls_meth_returns so the codegen-side
18748+ # override-dispatch rt_ok gate accepts the family. Issue
18749+ # #563.
18750+ #
18751+ # Three-step settle:
18752+ # 1. unify -- Base#[] gets "string" (from Article).
18753+ # 2. infer_returns -- callers of Base#[] (Base#fetch) pick
18754+ # up the new return type. But this also re-derives
18755+ # Base#[]'s own body return as "int" (raise default),
18756+ # clobbering step 1's write.
18757+ # 3. unify again -- restore Base#[] to "string".
18758+ unify_imeth_override_returns
18759+ infer_all_returns
18760+ unify_imeth_override_returns
18761+ infer_all_returns
18762+ unify_imeth_override_returns
1863318763 # Fix lambda return types based on call-site usage
1863418764 fix_lambda_return_types
1863518765 # Pre-detect bigint variables before feature detection