Commit 676669bd
676669bd66beMerged
Yusuke Endoh <[email protected]> · 6 months ago · 1 file · ~1 modified
Description
fix(codegen): peek through arr.map { block } for multi-write target type
In A, B = arr.map { block }, each target gets one element of
the mapped array — its type should be the block's return type.
scan_ivars_multi_target_type was reading the call's overall
infer_type, which collapses array-of-array to int_array as a
placeholder, so the targets came out int.
Peek through the CallNode's block body directly to recover the block's return type. Falls back to the existing recv-element logic for non-map RHS.
Details
- Commit
- 676669bd66be
- Parent
- 15f7e5f356d7
spinel_codegen.rbmodifiedView file
@@ -6454,6 +6454,40 @@
64546454
if is_tuple_type(rt) == 164556455
return tuple_elem_type_at(rt, ti)64566456
end6457+
# `A, B = arr.map { block }` — each target is one element of the6458+
# mapped array, so its type is the block's return type. Spinel6459+
# collapses array-of-array to a placeholder (int_array) at the6460+
# outer infer_type level, so we have to peek through the call6461+
# node directly to recover the element type.6462+
if @nd_type[val_id] == "CallNode" && @nd_name[val_id] == "map"6463+
blk = @nd_block[val_id]6464+
if blk >= 06465+
bbody = @nd_body[blk]6466+
if bbody >= 06467+
bbs = get_stmts(bbody)6468+
if bbs.length > 06469+
bret = infer_type(bbs.last)6470+
if bret != "int" && bret != "" && bret != "void"6471+
return bret6472+
end6473+
end6474+
end6475+
end6476+
end6477+
# Array-typed RHS (e.g. `A, B = [1, 6].map { ... }`): each target6478+
# gets one element of the recv array. Use the recv's element type.6479+
if rt == "int_array" || rt == "sym_array"6480+
return "int"6481+
end6482+
if is_ptr_array_type(rt) == 16483+
return ptr_array_elem_type(rt)6484+
end6485+
if rt == "str_array"6486+
return "string"6487+
end6488+
if rt == "float_array"6489+
return "float"6490+
end64576491
"int"64586492
end64596493