Commit 013dff7b
013dff7b0140Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 2 files · +1 added · ~1 modified
Description
Merge pull request #186 from mame/pr/multi-write-ivar
fix(codegen): collect ivars from MultiWriteNode (@a, @b = ...)
Details
- Commit
- 013dff7b0140
- Parent
- d1af4f4285c1
- Merge parent
- 7fa74417171f
spinel_codegen.rbmodifiedView file
@@ -5824,10 +5824,56 @@
58245824
add_ivar(ci, iname, "int")58255825
end58265826
end5827+
# Multi-write to ivars: `@a, @b = expr1, expr2` (or `[expr1, expr2]`).5828+
# Without this branch, ivars assigned only via destructuring never get5829+
# registered and the struct comes out missing them.5830+
if @nd_type[nid] == "MultiWriteNode"5831+
targets = parse_id_list(@nd_targets[nid])5832+
val_id = @nd_expression[nid]5833+
ti = 05834+
while ti < targets.length5835+
tid = targets[ti]5836+
if @nd_type[tid] == "InstanceVariableTargetNode"5837+
iname = @nd_name[tid]5838+
vtype = scan_ivars_multi_target_type(val_id, ti)5839+
if ivar_exists(ci, iname) == 05840+
add_ivar(ci, iname, vtype)5841+
else5842+
if vtype != "int" && vtype != "nil"5843+
update_ivar_type(ci, iname, vtype)5844+
end5845+
end5846+
end5847+
ti = ti + 15848+
end5849+
end58275850
# Recurse into children58285851
scan_ivars_children(ci, nid)58295852
end58305853
5854+
# i-th element type when scanning ivar destructuring writes. Mirrors5855+
# multi_write_target_type but also handles array literals on the RHS5856+
# (ivar collection runs before tuple inference, so `@a, @b = 1, 2`5857+
# gets its types from positional ArrayNode elements rather than a5858+
# tuple return).5859+
def scan_ivars_multi_target_type(val_id, ti)5860+
if val_id < 05861+
return "int"5862+
end5863+
if @nd_type[val_id] == "ArrayNode"5864+
elems = parse_id_list(@nd_elements[val_id])5865+
if ti < elems.length5866+
return infer_ivar_init_type(elems[ti])5867+
end5868+
return "int"5869+
end5870+
rt = infer_type(val_id)5871+
if is_tuple_type(rt) == 15872+
return tuple_elem_type_at(rt, ti)5873+
end5874+
"int"5875+
end5876+
58315877
def scan_ivars_children(ci, nid)58325878
if @nd_body[nid] >= 058335879
scan_ivars(ci, @nd_body[nid])test/multi_write_ivar.rbaddedView file
Loading diff…