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:

  1. 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.

  2. 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

@@ -2180,6 +2180,18 @@
21802180 end
21812181 return infer_type(@nd_expression[nid])
21822182 end
2183+ if t == "MultiWriteNode"
2184+ # `(a, b = 10, 11)` as an expression returns the RHS array.
2185+ # The rhs ArrayNode's type infers normally; the wrapping
2186+ # MWN takes the same type. Without this branch infer_type
2187+ # falls back to int and downstream `[1]` lowers to bit
2188+ # extraction on 0. Issue #554.
2189+ val_id_mw = @nd_expression[nid]
2190+ if val_id_mw >= 0
2191+ return infer_type(val_id_mw)
2192+ end
2193+ return "int"
2194+ end
21832195 if t == "IndexOrWriteNode" || t == "IndexAndWriteNode" || t == "IndexOperatorWriteNode"
21842196 # `recv[k] ||= v` (etc.) as an expression value. The result type
21852197 # is the recv's element type — same shape as Hash#[] / Array#[]