Commit c179e9a3
c179e9a38c26Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 6 files · +4 added · ~2 modified
Description
codegen: fix instance-variable read in a default-argument value
An @ivar read used as a default-argument value (def m(r = @ivar)) is
expanded at the call site against the receiver, but two things went wrong.
-
Value-type receiver dereference. The default is emitted with
g_selfset to the receiver temp butg_self_derefleft at its "->" default. For a value- type object the receiver temp is a by-value struct (sp_C, notsp_C *), sodef get_it(r = @r)emittedselfval->iv_r, which doesn't compile. Set g_self_deref to "." for a value-type receiver in emit_dispatch's default-arg branch, mirroring the method-body and inline-call paths. -
Subclass missing the inherited field. When the method is mixed in via a module and called on a subclass (
module M; def m(r = @x); ..., included into a base, then subclassed), the subclass struct lacked the field, so the default's@xread referenced a non-existent member. inherit_members (which copies a parent's ivar NAMES into subclasses for the cast-compatible [parent..., own...] layout) ran once before the fixpoint -- before the base gained @x from the transplanted module method. Re-run it after the fixpoint, once every ivar (including module-transplanted ones) is registered.
Both reproduce ActionDispatch::IntegrationTest's
def assert_response(expected, response = @__response) shape that Roundhouse
hit. Verified surgical: emitted C is byte-identical across the whole test suite
except the two new regression tests, and optcarrot's emitted C is unchanged.
Gates: 925 tests pass, 57 benchmarks pass, optcarrot checksum 59662, clang clean.
Fixes #1442
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- c179e9a38c26
- Parent
- 71e2930379b7
src/analyze.cmodifiedView file
if (blv->type == TY_NIL && !blv->rbs_seeded) blv->type = TY_POLY; } /* Re-merge inherited ivar NAMES into subclasses now that the fixpoint has registered every ivar -- including ones a parent only gained from an included module's transplanted methods (`module M; def m; @x = ...; end` included into a base, then subclassed). inherit_members ran once before the fixpoint, when the parent had no such ivar yet, so the subclass struct would otherwise lack the field (a default-arg read `def g(r = @x)` on the subclass then references a non-existent member). Rebuilds parent-first, so the [parent ivars..., own ivars...] cast-compatible layout is preserved. */ inherit_members(c); /* Re-run ivar inference now that purely-nil params/locals became poly: an ivar fed by such a param (`@x = idx` where every `set` call passed nil) was skipped during the fixpoint (its value read as TY_NIL) and may havesrc/codegen_fold.cmodifiedView file
Loading diff…
test/default_arg_ivar_module_subclass.rbaddedView file
Loading diff…
test/default_arg_ivar_module_subclass.rb.expectedaddedView file
Loading diff…
test/default_arg_ivar_value_receiver.rbaddedView file
Loading diff…
test/default_arg_ivar_value_receiver.rb.expectedaddedView file
Loading diff…