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
a template method defined only in subclasses cannot be called directly as sp_<base>_<name>, so even a single descendant impl must dispatch virtually. */ int impl_n = dispatch_impl_count(c, cid, name); int virtual = is_scalar_ret(ret) && (impl_n > 1 || (!m && impl_n >= 1)); /* A void/nil-returning method that subclasses override must still dispatch on the runtime class -- an implicit-self call to it from a base method (e.g. `def run; validate; end` where each subclass overrides `validate`) would otherwise bind statically to the base impl and skip the override (#1443). The GCC statement-expression wrapper can't declare a `void` result, so a void dispatch uses a dummy int temp (its value is discarded). */ int ret_is_void = (ret == TY_VOID || ret == TY_NIL); TyKind disp_ret = ret_is_void ? TY_INT : ret; int virtual = (is_scalar_ret(ret) || ret_is_void) && (impl_n > 1 || (!m && impl_n >= 1)); if (!virtual) { /* a value-type receiver is passed by value (no pointer cast) */ /* runtime dispatch on cls_id (GCC statement-expression) */ int rtmp = ++g_tmp; buf_puts(b, "({ "); emit_ctype(c, ret, b); emit_ctype(c, disp_ret, b); buf_printf(b, " _t%d; switch ((%s)->cls_id) {", rtmp, selfptr); for (int k = 0; k < c->nclasses; k++) { if (!is_descendant(c, k, cid)) continue; buf_printf(b, " case %d: sp_%s_%s((sp_%s *)%s", k, c->classes[kd].name, kfn, c->classes[kd].name, selfptr); for (int a = 0; a < np; a++) buf_printf(b, ", _t%d", atmp[a]); buf_printf(b, "); _t%d = %s; break;", rtmp, default_value(ret)); buf_printf(b, "); _t%d = %s; break;", rtmp, default_value(disp_ret)); } else if (arm_ret != ret && ret == TY_POLY) { /* arm returns a concrete type but switch expects sp_RbVal: box it */ call to a nonexistent sp_<base>_<name>. */ if (!m) { buf_printf(b, " default: _t%d = %s; break; } _t%d; })", rtmp, ret == TY_POLY ? "sp_box_nil()" : default_value(ret), rtmp); ret == TY_POLY ? "sp_box_nil()" : default_value(disp_ret), rtmp); free(atmp); return; } buf_printf(b, " default: sp_%s_%s((sp_%s *)%s", c->classes[defcls].name, mc(mname), c->classes[defcls].name, selfptr); for (int a = 0; a < np; a++) buf_printf(b, ", _t%d", atmp[a]); buf_printf(b, "); _t%d = %s; break;", rtmp, default_value(ret)); buf_printf(b, "); _t%d = %s; break;", rtmp, default_value(disp_ret)); } else if (def_ret != ret && ret == TY_POLY) { buf_printf(b, " default: { ");test/virtual_dispatch_void_override.rbaddedView file
Loading diff…
test/virtual_dispatch_void_override.rb.expectedaddedView file
Loading diff…
test/virtual_dispatch_void_override_empty_base.rbaddedView file
Loading diff…
test/virtual_dispatch_void_override_empty_base.rb.expectedaddedView file
Loading diff…