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) == 1
64556455 return tuple_elem_type_at(rt, ti)
64566456 end
6457+ # `A, B = arr.map { block }` — each target is one element of the
6458+ # mapped array, so its type is the block's return type. Spinel
6459+ # collapses array-of-array to a placeholder (int_array) at the
6460+ # outer infer_type level, so we have to peek through the call
6461+ # 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 >= 0
6465+ bbody = @nd_body[blk]
6466+ if bbody >= 0
6467+ bbs = get_stmts(bbody)
6468+ if bbs.length > 0
6469+ bret = infer_type(bbs.last)
6470+ if bret != "int" && bret != "" && bret != "void"
6471+ return bret
6472+ end
6473+ end
6474+ end
6475+ end
6476+ end
6477+ # Array-typed RHS (e.g. `A, B = [1, 6].map { ... }`): each target
6478+ # 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+ end
6482+ if is_ptr_array_type(rt) == 1
6483+ return ptr_array_elem_type(rt)
6484+ end
6485+ if rt == "str_array"
6486+ return "string"
6487+ end
6488+ if rt == "float_array"
6489+ return "float"
6490+ end
64576491 "int"
64586492 end
64596493