Commit ba8b1d5e
ba8b1d5e1b3eMerged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 4 files · +2 added · ~2 modified
Description
fix(analyze,codegen): multiple assignment as expression returns RHS array
T.Yamada's #554: (a, b = 10, 11)[1] lowered to bit
extraction on int 0 because neither analyze's infer_type
nor codegen's compile_expr had a MultiWriteNode arm; the
default int fallback hit, and the subscript on the int
became (0 >> 1) & 1. The locals never even received the
assignment.
Two pieces:
-
analyze: infer_type(MultiWriteNode) returns the rhs expression's type (ArrayNode's inferred array type for the common shape). The wrapping
[1]now lowers against an array recv rather than the int default. -
codegen: compile_expr(MultiWriteNode) emits the rhs elements into typed temps, builds a typed-array temp to be the expression value, and threads each temp through emit_multi_write_target for the per-target assignments. Homogeneous int / string rhs use IntArray / StrArray; mixed shapes fall through to PolyArray with boxed values.
The statement-form lowering (compile_multi_write) is
unchanged. Splat / nested-destructure shapes (*rest,
(a, (b, c)) = ...) still fall through to the catch-all;
the common 2-or-3-value tuple shape is what cielavenir's
reproducer needed.
Closes #554
Co-Authored-By: Claude Opus 4.7 (1M context) [email protected]
Details
- Commit
- ba8b1d5e1b3e
- Parent
- c6fec56e9af7
spinel_analyze.rbmodifiedView file
end return infer_type(@nd_expression[nid]) end if t == "MultiWriteNode" # `(a, b = 10, 11)` as an expression returns the RHS array. # The rhs ArrayNode's type infers normally; the wrapping # MWN takes the same type. Without this branch infer_type # falls back to int and downstream `[1]` lowers to bit # extraction on 0. Issue #554. val_id_mw = @nd_expression[nid] if val_id_mw >= 0 return infer_type(val_id_mw) end return "int" end if t == "IndexOrWriteNode" || t == "IndexAndWriteNode" || t == "IndexOperatorWriteNode" # `recv[k] ||= v` (etc.) as an expression value. The result type # is the recv's element type — same shape as Hash#[] / Array#[]spinel_codegen.rbmodifiedView file
Loading diff…
test/multi_write_as_expression.rbaddedView file
Loading diff…
test/multi_write_as_expression.rb.expectedaddedView file
Loading diff…