Commit 29e9e948

29e9e948c4ceMerged

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

Description

codegen: dispatch overridden void methods on the runtime class

An implicit-self call to a void/nil-returning method that subclasses override bound statically to the base implementation, skipping the override: def run; validate; end in a base class emitted sp_Base_validate((sp_Base *)self) with no class-id dispatch, so Sub.new.run ran Base#validate instead of Sub#validate. emit_dispatch only went virtual for is_scalar_ret(ret), which excludes TY_VOID/TY_NIL -- the one gap, since is_scalar_ret already covers every other type (objects, arrays, hashes, poly, ...).

Include void/nil in the virtual-dispatch gate. The GCC statement-expression wrapper can't declare a void result, so a void dispatch uses a dummy int temp (disp_ret) whose value is discarded; the per-arm calls stay void. The static path is still taken for a void method with a single implementation (no override), so the change only affects genuinely-overridden void methods.

This is ActiveRecord::Base#valid? -> validate (empty base body, each model overrides it): under --rbs the call bound to the empty base, so per-model validation never ran and invalid records saved. Both the non-empty-base and the empty-base+--rbs forms now dispatch dynamically.

Verified surgical: emitted C is byte-identical across the whole suite except the one new non-empty-base regression test, and optcarrot's emitted C is unchanged.

Gates: 927 tests pass, 57 benchmarks pass, optcarrot checksum 59662, clang clean.

Fixes #1443

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

Details

Commit
29e9e948c4ce
Parent
32524b7933d0

src/codegen_fold.cmodifiedView file

@@ -2774,7 +2774,15 @@
27742774 a template method defined only in subclasses cannot be called directly as
27752775 sp_<base>_<name>, so even a single descendant impl must dispatch virtually. */
27762776 int impl_n = dispatch_impl_count(c, cid, name);
2777- int virtual = is_scalar_ret(ret) && (impl_n > 1 || (!m && impl_n >= 1));
2777+ /* A void/nil-returning method that subclasses override must still dispatch on
2778+ the runtime class -- an implicit-self call to it from a base method (e.g.
2779+ `def run; validate; end` where each subclass overrides `validate`) would
2780+ otherwise bind statically to the base impl and skip the override (#1443).
2781+ The GCC statement-expression wrapper can't declare a `void` result, so a
2782+ void dispatch uses a dummy int temp (its value is discarded). */
2783+ int ret_is_void = (ret == TY_VOID || ret == TY_NIL);
2784+ TyKind disp_ret = ret_is_void ? TY_INT : ret;
2785+ int virtual = (is_scalar_ret(ret) || ret_is_void) && (impl_n > 1 || (!m && impl_n >= 1));
27782786
27792787 if (!virtual) {
27802788 /* a value-type receiver is passed by value (no pointer cast) */
@@ -2795,7 +2803,7 @@
27952803 /* runtime dispatch on cls_id (GCC statement-expression) */
27962804 int rtmp = ++g_tmp;
27972805 buf_puts(b, "({ ");
2798- emit_ctype(c, ret, b);
2806+ emit_ctype(c, disp_ret, b);
27992807 buf_printf(b, " _t%d; switch ((%s)->cls_id) {", rtmp, selfptr);
28002808 for (int k = 0; k < c->nclasses; k++) {
28012809 if (!is_descendant(c, k, cid)) continue;
@@ -2809,7 +2817,7 @@
28092817 buf_printf(b, " case %d: sp_%s_%s((sp_%s *)%s", k,
28102818 c->classes[kd].name, kfn, c->classes[kd].name, selfptr);
28112819 for (int a = 0; a < np; a++) buf_printf(b, ", _t%d", atmp[a]);
2812- buf_printf(b, "); _t%d = %s; break;", rtmp, default_value(ret));
2820+ buf_printf(b, "); _t%d = %s; break;", rtmp, default_value(disp_ret));
28132821 }
28142822 else if (arm_ret != ret && ret == TY_POLY) {
28152823 /* arm returns a concrete type but switch expects sp_RbVal: box it */
@@ -2837,7 +2845,7 @@
28372845 call to a nonexistent sp_<base>_<name>. */
28382846 if (!m) {
28392847 buf_printf(b, " default: _t%d = %s; break; } _t%d; })", rtmp,
2840- ret == TY_POLY ? "sp_box_nil()" : default_value(ret), rtmp);
2848+ ret == TY_POLY ? "sp_box_nil()" : default_value(disp_ret), rtmp);
28412849 free(atmp);
28422850 return;
28432851 }
@@ -2847,7 +2855,7 @@
28472855 buf_printf(b, " default: sp_%s_%s((sp_%s *)%s",
28482856 c->classes[defcls].name, mc(mname), c->classes[defcls].name, selfptr);
28492857 for (int a = 0; a < np; a++) buf_printf(b, ", _t%d", atmp[a]);
2850- buf_printf(b, "); _t%d = %s; break;", rtmp, default_value(ret));
2858+ buf_printf(b, "); _t%d = %s; break;", rtmp, default_value(disp_ret));
28512859 }
28522860 else if (def_ret != ret && ret == TY_POLY) {
28532861 buf_printf(b, " default: { ");