Commit 8811039f

8811039fc472Merged

Ryan Seys <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

fix(codegen): instance_eval splice routes ivar reads/writes through receiver

The instance_eval-trampoline splice in compile_instance_eval_inlined_stmt emits a typed self_var for the receiver but did not set @self_override, and ivar_lhs short- circuited on @current_class_idx < 0 into the toplevel-static-global slot. The combination silently mis-routed any direct ivar access inside the spliced block when the trampoline was called at toplevel: recv.with { @x = @x + 5 } wrote to a bare iv_x global instead of recv->iv_x. Undetected because the existing trampoline tests only call methods like add / bump inside the block -- never read or write @ivars directly.

Fix is two-part. compile_instance_eval_inlined_stmt now saves and restores @self_override around the splice, setting it to self_var so self_arrow() resolves to <self_var>-> for the rebound class. And ivar_lhs takes a new branch gated on @instance_eval_self_var (the canonical rebind signal from splice_block_with_self_rebound) that routes through self_arrow + sanitize_ivar before the toplevel fallback fires. The gate is @instance_eval_self_var, not @self_override, because @self_override is also set by compile_yield_method_call_stmt for inlined block params and default-arg substitution -- where ivars must still resolve against the enclosing class. Conflating them mis-routes ivars everywhere yield methods are inlined and breaks bootstrap.

Adds a section 4 regression to test/instance_eval_trampoline.rb that exercises @n = @n + 5 / @n + 3 inside the spliced block (Counter is reused so value-type promotion stays disabled).

A second latent bug surfaced by the new test: value-typed receivers crash compile_instance_eval_inlined_stmt's pointer cast. TODO comment documents the fix shape; deferred from this change because Counter sidesteps it.

504 tests pass; 4-way bootstrap fixpoint converges on a clean rebuild.

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

Details

Commit
8811039fc472
Parent
7780fff2c6e8

spinel_codegen.rbmodifiedView file

@@ -4974,6 +4974,26 @@
49744974 if mod_slot != ""
49754975 return mod_slot
49764976 end
4977+ # Rebound self (instance_eval / instance_exec splice): route ivar
4978+ # access through the receiver's struct via self_arrow(), not the
4979+ # toplevel static-global slot. Without this, a block body that
4980+ # writes `@x = ...` at a toplevel call site (where
4981+ # @current_class_idx == -1) falls into the toplevel-ivar fallback
4982+ # below and emits a bare global write -- never touching the
4983+ # receiver's iv_x. Detected by the section-4 regression added to
4984+ # test/instance_eval_trampoline.rb.
4985+ #
4986+ # Gate on @instance_eval_self_var (the rebind signal from
4987+ # splice_block_with_self_rebound), NOT @self_override --
4988+ # @self_override is also set by compile_yield_method_call_stmt
4989+ # for inlined-method-body locals + default-arg substitution, where
4990+ # ivars must still resolve against the enclosing class's struct,
4991+ # not the override expression. Conflating them silently mis-routes
4992+ # ivar reads everywhere yield methods are inlined (including
4993+ # spinel_codegen.rb's own methods during bootstrap).
4994+ if @instance_eval_self_var != nil && @instance_eval_self_var != ""
4995+ return self_arrow + sanitize_ivar(name)
4996+ end
49774997 if @current_class_idx < 0 && toplevel_ivar_type(name) != ""
49784998 return sanitize_ivar(name)
49794999 end
@@ -9289,8 +9309,6 @@
92899309
92909310
92919311
9292-
9293-
92949312 # Walk `nid`'s subtree and collect every `Cls.new(...)` class name
92959313 # into `out`. Used by detect_poly_returned_types to enumerate the
92969314 # classes returned (directly or via a temp) from a poly-returning
@@ -43935,11 +43953,24 @@
4393543953 end
4393643954 rc = compile_expr_gc_rooted(recv)
4393743955 self_var = new_temp
43956+ # Value-typed receivers (@cls_is_value_type[ci] == 1) are returned
43957+ # by-value from sp_<C>_new(), so (sp_<C> *)<rc> is an invalid cast
43958+ # of a struct value. Handling them requires the value-vs-pointer
43959+ # branch from emit_ieval_func and is tracked separately.
4393843960 emit(" sp_" + cname + " *" + self_var + " = (sp_" + cname + " *)" + rc + ";")
4393943961 if @in_gc_scope == 1
4394043962 emit(" SP_GC_ROOT(" + self_var + ");")
4394143963 end
43964+ # Route bare `@ivar` reads/writes inside the spliced block to the
43965+ # rebound receiver. Without this, ivar_lhs's toplevel-fallback
43966+ # branch returns the bare static-global slot when the splice
43967+ # happens at toplevel scope — broken silently because no prior
43968+ # trampoline test exercised direct ivar access inside the spliced
43969+ # block (existing tests only call methods like `add` / `bump`).
43970+ saved_self_override = @self_override
43971+ @self_override = self_var
4394243972 splice_block_with_self_rebound(@nd_body[blk], self_var, cname)
43973+ @self_override = saved_self_override
4394343974 end
4394443975
4394543976 def compile_yield_method_call_stmt(nid, cci, midx, mname)