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
add_ivar(ci, iname, "int") end end # Multi-write to ivars: `@a, @b = expr1, expr2` (or `[expr1, expr2]`). # Without this branch, ivars assigned only via destructuring never get # registered and the struct comes out missing them. if @nd_type[nid] == "MultiWriteNode" targets = parse_id_list(@nd_targets[nid]) val_id = @nd_expression[nid] ti = 0 while ti < targets.length tid = targets[ti] if @nd_type[tid] == "InstanceVariableTargetNode" iname = @nd_name[tid] vtype = scan_ivars_multi_target_type(val_id, ti) if ivar_exists(ci, iname) == 0 add_ivar(ci, iname, vtype) else if vtype != "int" && vtype != "nil" update_ivar_type(ci, iname, vtype) end end end ti = ti + 1 end end # Recurse into children scan_ivars_children(ci, nid) end # i-th element type when scanning ivar destructuring writes. Mirrors # multi_write_target_type but also handles array literals on the RHS # (ivar collection runs before tuple inference, so `@a, @b = 1, 2` # gets its types from positional ArrayNode elements rather than a # tuple return). def scan_ivars_multi_target_type(val_id, ti) if val_id < 0 return "int" end if @nd_type[val_id] == "ArrayNode" elems = parse_id_list(@nd_elements[val_id]) if ti < elems.length return infer_ivar_init_type(elems[ti]) end return "int" end rt = infer_type(val_id) if is_tuple_type(rt) == 1 return tuple_elem_type_at(rt, ti) end "int" end def scan_ivars_children(ci, nid) if @nd_body[nid] >= 0 scan_ivars(ci, @nd_body[nid])test/multi_write_ivar.rbaddedView file
Loading diff…