Commit a8639f57

a8639f578040Merged

Yusuke Endoh <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified

Description

fix(codegen): multi-write from array-returning RHS to ivar/const targets

compile_multi_write's int_array-returning RHS branch (where the RHS is a non-literal expression evaluating to an array — e.g. .map { ... }, a method call) only emitted assignments for LocalVariableTargetNode targets. InstanceVariableTargetNode and ConstantTargetNode targets were silently dropped: the C code compiled fine, but the ivars / constants stayed at their default values (typically 0 or nil).

Repro:

class C
  def initialize
    @a, @b = [10, 20].map { |x| x + 1 }
  end
  attr_reader :a, :b
end
c = C.new
puts c.a   # expected: 11; spinel emitted: 0
puts c.b   # expected: 21; spinel emitted: 0

Add ivar + const target handling alongside the existing local target handling in the int_array branch. Each target gets a sp_IntArray_get(<tmp>, k) indexed extraction; ivar writes go through self_arrow + sanitize_ivar(...), const writes through cst_<NAME> (only when the constant has been registered, so we don't emit assignments to undeclared symbols).

Test: test/multi_write_ivar_const_from_array_rhs.rb covers ivar targets with .map { ... } RHS, mixed local + ivar, and ivar targets with a top-level method call RHS.

(Constant-target coverage requires the multi-write-to-constants parser support, which lives in a sibling PR.)

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

Details

Commit
a8639f578040
Parent
e341e5e20470

spinel_codegen.rbmodifiedView file

@@ -22348,8 +22348,17 @@
2234822348 k = 0
2234922349 while k < targets.length
2235022350 tid = targets[k]
22351+ rhs = "sp_IntArray_get(" + tmp + ", " + k.to_s + ")"
2235122352 if @nd_type[tid] == "LocalVariableTargetNode"
22352- emit(" " + fiber_var_ref(@nd_name[tid]) + " = sp_IntArray_get(" + tmp + ", " + k.to_s + ");")
22353+ emit(" " + fiber_var_ref(@nd_name[tid]) + " = " + rhs + ";")
22354+ end
22355+ if @nd_type[tid] == "InstanceVariableTargetNode"
22356+ emit(" " + self_arrow + sanitize_ivar(@nd_name[tid]) + " = " + rhs + ";")
22357+ end
22358+ if @nd_type[tid] == "ConstantTargetNode"
22359+ if find_const_idx(@nd_name[tid]) >= 0
22360+ emit(" cst_" + @nd_name[tid] + " = " + rhs + ";")
22361+ end
2235322362 end
2235422363 k = k + 1
2235522364 end