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.

  1. Value-type receiver dereference. The default is emitted with g_self set to the receiver temp but g_self_deref left at its "->" default. For a value- type object the receiver temp is a by-value struct (sp_C, not sp_C *), so def get_it(r = @r) emitted selfval->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.

  2. 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 @x read 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

@@ -2015,6 +2015,16 @@
20152015 if (blv->type == TY_NIL && !blv->rbs_seeded) blv->type = TY_POLY;
20162016 }
20172017
2018+ /* Re-merge inherited ivar NAMES into subclasses now that the fixpoint has
2019+ registered every ivar -- including ones a parent only gained from an
2020+ included module's transplanted methods (`module M; def m; @x = ...; end`
2021+ included into a base, then subclassed). inherit_members ran once before
2022+ the fixpoint, when the parent had no such ivar yet, so the subclass struct
2023+ would otherwise lack the field (a default-arg read `def g(r = @x)` on the
2024+ subclass then references a non-existent member). Rebuilds parent-first, so
2025+ the [parent ivars..., own ivars...] cast-compatible layout is preserved. */
2026+ inherit_members(c);
2027+
20182028 /* Re-run ivar inference now that purely-nil params/locals became poly: an
20192029 ivar fed by such a param (`@x = idx` where every `set` call passed nil)
20202030 was skipped during the fixpoint (its value read as TY_NIL) and may have