Commit 7fa74417

7fa74417171fMerged

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

Description

fix(codegen): collect ivars from MultiWriteNode (@a, @b = ...)

scan_ivars only handled InstanceVariableWriteNode and InstanceVariableOperatorWriteNode, so @a, @b = expr1, expr2 (or @a, @b = [expr1, expr2]) — the multi-write destructuring shape — never reached add_ivar / update_ivar_type. The ivars referenced only via destructuring were dropped from the class struct, so reads later compiled to undeclared-field errors.

Add a MultiWriteNode branch to scan_ivars. Walk the targets list, pick out InstanceVariableTargetNode entries, and register each with the matching position from the RHS — ArrayNode literal split per index, otherwise the i-th element of a tuple-typed call return.

The element-type lookup is factored into a new scan_ivars_multi_target_type(val_id, ti) helper. It mirrors the existing multi_write_target_type (used at emit time) but also handles ArrayNode RHS, since ivar collection runs before the tuple inference pass that emit-time relies on.

Encountered while compiling optcarrot — APU's @pulse_0, @pulse_1 = Pulse.new(self), Pulse.new(self) left both ivars unregistered.

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

Details

Commit
7fa74417171f
Parent
d920667f4cc7

spinel_codegen.rbmodifiedView file

@@ -5679,10 +5679,56 @@
56795679 add_ivar(ci, iname, "int")
56805680 end
56815681 end
5682+ # Multi-write to ivars: `@a, @b = expr1, expr2` (or `[expr1, expr2]`).
5683+ # Without this branch, ivars assigned only via destructuring never get
5684+ # registered and the struct comes out missing them.
5685+ if @nd_type[nid] == "MultiWriteNode"
5686+ targets = parse_id_list(@nd_targets[nid])
5687+ val_id = @nd_expression[nid]
5688+ ti = 0
5689+ while ti < targets.length
5690+ tid = targets[ti]
5691+ if @nd_type[tid] == "InstanceVariableTargetNode"
5692+ iname = @nd_name[tid]
5693+ vtype = scan_ivars_multi_target_type(val_id, ti)
5694+ if ivar_exists(ci, iname) == 0
5695+ add_ivar(ci, iname, vtype)
5696+ else
5697+ if vtype != "int" && vtype != "nil"
5698+ update_ivar_type(ci, iname, vtype)
5699+ end
5700+ end
5701+ end
5702+ ti = ti + 1
5703+ end
5704+ end
56825705 # Recurse into children
56835706 scan_ivars_children(ci, nid)
56845707 end
56855708
5709+ # i-th element type when scanning ivar destructuring writes. Mirrors
5710+ # multi_write_target_type but also handles array literals on the RHS
5711+ # (ivar collection runs before tuple inference, so `@a, @b = 1, 2`
5712+ # gets its types from positional ArrayNode elements rather than a
5713+ # tuple return).
5714+ def scan_ivars_multi_target_type(val_id, ti)
5715+ if val_id < 0
5716+ return "int"
5717+ end
5718+ if @nd_type[val_id] == "ArrayNode"
5719+ elems = parse_id_list(@nd_elements[val_id])
5720+ if ti < elems.length
5721+ return infer_ivar_init_type(elems[ti])
5722+ end
5723+ return "int"
5724+ end
5725+ rt = infer_type(val_id)
5726+ if is_tuple_type(rt) == 1
5727+ return tuple_elem_type_at(rt, ti)
5728+ end
5729+ "int"
5730+ end
5731+
56865732 def scan_ivars_children(ci, nid)
56875733 if @nd_body[nid] >= 0
56885734 scan_ivars(ci, @nd_body[nid])