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
if mod_slot != "" return mod_slot end # Rebound self (instance_eval / instance_exec splice): route ivar # access through the receiver's struct via self_arrow(), not the # toplevel static-global slot. Without this, a block body that # writes `@x = ...` at a toplevel call site (where # @current_class_idx == -1) falls into the toplevel-ivar fallback # below and emits a bare global write -- never touching the # receiver's iv_x. Detected by the section-4 regression added to # test/instance_eval_trampoline.rb. # # Gate on @instance_eval_self_var (the rebind signal from # splice_block_with_self_rebound), NOT @self_override -- # @self_override is also set by compile_yield_method_call_stmt # for inlined-method-body locals + default-arg substitution, where # ivars must still resolve against the enclosing class's struct, # not the override expression. Conflating them silently mis-routes # ivar reads everywhere yield methods are inlined (including # spinel_codegen.rb's own methods during bootstrap). if @instance_eval_self_var != nil && @instance_eval_self_var != "" return self_arrow + sanitize_ivar(name) end if @current_class_idx < 0 && toplevel_ivar_type(name) != "" return sanitize_ivar(name) end # Walk `nid`'s subtree and collect every `Cls.new(...)` class name # into `out`. Used by detect_poly_returned_types to enumerate the # classes returned (directly or via a temp) from a poly-returning end rc = compile_expr_gc_rooted(recv) self_var = new_temp # Value-typed receivers (@cls_is_value_type[ci] == 1) are returned # by-value from sp_<C>_new(), so (sp_<C> *)<rc> is an invalid cast # of a struct value. Handling them requires the value-vs-pointer # branch from emit_ieval_func and is tracked separately. emit(" sp_" + cname + " *" + self_var + " = (sp_" + cname + " *)" + rc + ";") if @in_gc_scope == 1 emit(" SP_GC_ROOT(" + self_var + ");") end # Route bare `@ivar` reads/writes inside the spliced block to the # rebound receiver. Without this, ivar_lhs's toplevel-fallback # branch returns the bare static-global slot when the splice # happens at toplevel scope — broken silently because no prior # trampoline test exercised direct ivar access inside the spliced # block (existing tests only call methods like `add` / `bump`). saved_self_override = @self_override @self_override = self_var splice_block_with_self_rebound(@nd_body[blk], self_var, cname) @self_override = saved_self_override end def compile_yield_method_call_stmt(nid, cci, midx, mname)test/instance_eval_trampoline.rbmodifiedView file
Loading diff…
test/instance_eval_trampoline.rb.expectedmodifiedView file
Loading diff…