Commit b8f582b3

b8f582b3d120Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 3 files · ~3 modified

Description

Merge pull request #1023 from ryanseys/rs-instance-eval-splice-ivar-routing

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

Details

Commit
b8f582b3d120
Parent
ffefc0d06bfa
Merge parent
8811039fc472

spinel_codegen.rbmodifiedView file

@@ -5019,6 +5019,26 @@
50195019 if mod_slot != ""
50205020 return mod_slot
50215021 end
5022+ # Rebound self (instance_eval / instance_exec splice): route ivar
5023+ # access through the receiver's struct via self_arrow(), not the
5024+ # toplevel static-global slot. Without this, a block body that
5025+ # writes `@x = ...` at a toplevel call site (where
5026+ # @current_class_idx == -1) falls into the toplevel-ivar fallback
5027+ # below and emits a bare global write -- never touching the
5028+ # receiver's iv_x. Detected by the section-4 regression added to
5029+ # test/instance_eval_trampoline.rb.
5030+ #
5031+ # Gate on @instance_eval_self_var (the rebind signal from
5032+ # splice_block_with_self_rebound), NOT @self_override --
5033+ # @self_override is also set by compile_yield_method_call_stmt
5034+ # for inlined-method-body locals + default-arg substitution, where
5035+ # ivars must still resolve against the enclosing class's struct,
5036+ # not the override expression. Conflating them silently mis-routes
5037+ # ivar reads everywhere yield methods are inlined (including
5038+ # spinel_codegen.rb's own methods during bootstrap).
5039+ if @instance_eval_self_var != nil && @instance_eval_self_var != ""
5040+ return self_arrow + sanitize_ivar(name)
5041+ end
50225042 if @current_class_idx < 0 && toplevel_ivar_type(name) != ""
50235043 return sanitize_ivar(name)
50245044 end
@@ -9336,8 +9356,6 @@
93369356
93379357
93389358
9339-
9340-
93419359 # Walk `nid`'s subtree and collect every `Cls.new(...)` class name
93429360 # into `out`. Used by detect_poly_returned_types to enumerate the
93439361 # classes returned (directly or via a temp) from a poly-returning
@@ -44945,11 +44963,24 @@
4494544963 end
4494644964 rc = compile_expr_gc_rooted(recv)
4494744965 self_var = new_temp
44966+ # Value-typed receivers (@cls_is_value_type[ci] == 1) are returned
44967+ # by-value from sp_<C>_new(), so (sp_<C> *)<rc> is an invalid cast
44968+ # of a struct value. Handling them requires the value-vs-pointer
44969+ # branch from emit_ieval_func and is tracked separately.
4494844970 emit(" sp_" + cname + " *" + self_var + " = (sp_" + cname + " *)" + rc + ";")
4494944971 if @in_gc_scope == 1
4495044972 emit(" SP_GC_ROOT(" + self_var + ");")
4495144973 end
44974+ # Route bare `@ivar` reads/writes inside the spliced block to the
44975+ # rebound receiver. Without this, ivar_lhs's toplevel-fallback
44976+ # branch returns the bare static-global slot when the splice
44977+ # happens at toplevel scope — broken silently because no prior
44978+ # trampoline test exercised direct ivar access inside the spliced
44979+ # block (existing tests only call methods like `add` / `bump`).
44980+ saved_self_override = @self_override
44981+ @self_override = self_var
4495244982 splice_block_with_self_rebound(@nd_body[blk], self_var, cname)
44983+ @self_override = saved_self_override
4495344984 end
4495444985
4495544986 def compile_yield_method_call_stmt(nid, cci, midx, mname)