Commit 0b35f2a6

0b35f2a667b9Merged

Yukihiro "Matz" Matsumoto <[email protected]> · 6 months ago · 3 files · +1 added · ~2 modified

Description

codegen,analyze: each_slice.map chain fusion + multi-param destructure

arr.each_slice(n).map { |x, y, ...| } used to emit "cannot resolve each_slice on int_array". Extend the each_cons.map fusion to handle each_slice (steps by n; drops short tails) and auto-destructure across multiple block params when their count matches the each_cons/each_slice n (+1 with with_index).

|pair| and |pair, i| still bind a sub-array.

Fixes #829

Co-Authored-By: Claude Opus 4.7 [email protected]

Details

Commit
0b35f2a667b9
Parent
9f9e5e9b446c

spinel_analyze.rbmodifiedView file

@@ -29986,6 +29986,61 @@
2998629986 return "poly" if pi == 0
2998729987 return "poly_array" if pi == 1
2998829988 end
29989+ # Issue #829: `arr.each_cons(n).map { |x, y, ...| }` and the
29990+ # each_slice variant fuse in codegen and auto-destructure the
29991+ # yielded sub-array across multiple block params. The block
29992+ # sees each param as a *scalar* element of the inner array,
29993+ # not as the sub-array itself. Without this the analyzer types
29994+ # `x` as int_array and `y - x` dispatches to set difference.
29995+ # Issue #829: arr.each_cons(n)[.with_index].map { |x, y, ...[, i]| }
29996+ # auto-destructures when block req-param count matches n (+1 for
29997+ # with_index). `|pair|` and `|pair, i|` stay as sub-arrays.
29998+ if mname == "map"
29999+ ec_arr_node_fz = -1
30000+ ec_wi_flag_fz = 0
30001+ if recv >= 0 && @nd_type[recv] == "CallNode" && (@nd_name[recv] == "each_cons" || @nd_name[recv] == "each_slice") && @nd_block[recv] < 0
30002+ ec_arr_node_fz = recv
30003+ elsif recv >= 0 && @nd_type[recv] == "CallNode" && @nd_name[recv] == "with_index" && @nd_block[recv] < 0
30004+ inner_fz = @nd_receiver[recv]
30005+ if inner_fz >= 0 && @nd_type[inner_fz] == "CallNode" && (@nd_name[inner_fz] == "each_cons" || @nd_name[inner_fz] == "each_slice") && @nd_block[inner_fz] < 0
30006+ ec_arr_node_fz = inner_fz
30007+ ec_wi_flag_fz = 1
30008+ end
30009+ end
30010+ if ec_arr_node_fz >= 0
30011+ ec_inner_recv_fz = @nd_receiver[ec_arr_node_fz]
30012+ ec_n_fz = -1
30013+ ec_args_fz = @nd_arguments[ec_arr_node_fz]
30014+ if ec_args_fz >= 0
30015+ ec_a_list_fz = get_args(ec_args_fz)
30016+ if ec_a_list_fz.length > 0 && @nd_type[ec_a_list_fz[0]] == "IntegerNode"
30017+ ec_n_fz = @nd_value[ec_a_list_fz[0]].to_i
30018+ end
30019+ end
30020+ if ec_inner_recv_fz >= 0 && ec_n_fz > 0
30021+ ec_inner_rt_fz = infer_type(ec_inner_recv_fz)
30022+ if is_array_type(ec_inner_rt_fz) == 1
30023+ blk_fz = @nd_block[call_nid]
30024+ if blk_fz >= 0
30025+ ec_bpn_fz = @nd_parameters[blk_fz]
30026+ if ec_bpn_fz >= 0
30027+ ec_inner_pn_fz = @nd_parameters[ec_bpn_fz]
30028+ if ec_inner_pn_fz >= 0
30029+ ec_reqs_fz = parse_id_list(@nd_requireds[ec_inner_pn_fz])
30030+ ec_expected_fz = ec_n_fz + ec_wi_flag_fz
30031+ if ec_reqs_fz.length == ec_expected_fz && ec_reqs_fz.length >= 2 && @nd_type[ec_reqs_fz[0]] != "MultiTargetNode"
30032+ if ec_wi_flag_fz == 1 && pi == ec_reqs_fz.length - 1
30033+ return "int"
30034+ end
30035+ return elem_type_of_array(ec_inner_rt_fz)
30036+ end
30037+ end
30038+ end
30039+ end
30040+ end
30041+ end
30042+ end
30043+ end
2998930044 # Hash#each yields |k, v|. elem_type_of_array on a hash type
2999030045 # falls back to "int" for both, which then types `puts k + ":"`
2999130046 # inside the block as int-arithmetic and lowers to printf("%lld")