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 == 02998729987
return "poly_array" if pi == 12998829988
end29989+
# Issue #829: `arr.each_cons(n).map { |x, y, ...| }` and the29990+
# each_slice variant fuse in codegen and auto-destructure the29991+
# yielded sub-array across multiple block params. The block29992+
# sees each param as a *scalar* element of the inner array,29993+
# not as the sub-array itself. Without this the analyzer types29994+
# `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 for29997+
# with_index). `|pair|` and `|pair, i|` stay as sub-arrays.29998+
if mname == "map"29999+
ec_arr_node_fz = -130000+
ec_wi_flag_fz = 030001+
if recv >= 0 && @nd_type[recv] == "CallNode" && (@nd_name[recv] == "each_cons" || @nd_name[recv] == "each_slice") && @nd_block[recv] < 030002+
ec_arr_node_fz = recv30003+
elsif recv >= 0 && @nd_type[recv] == "CallNode" && @nd_name[recv] == "with_index" && @nd_block[recv] < 030004+
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] < 030006+
ec_arr_node_fz = inner_fz30007+
ec_wi_flag_fz = 130008+
end30009+
end30010+
if ec_arr_node_fz >= 030011+
ec_inner_recv_fz = @nd_receiver[ec_arr_node_fz]30012+
ec_n_fz = -130013+
ec_args_fz = @nd_arguments[ec_arr_node_fz]30014+
if ec_args_fz >= 030015+
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_i30018+
end30019+
end30020+
if ec_inner_recv_fz >= 0 && ec_n_fz > 030021+
ec_inner_rt_fz = infer_type(ec_inner_recv_fz)30022+
if is_array_type(ec_inner_rt_fz) == 130023+
blk_fz = @nd_block[call_nid]30024+
if blk_fz >= 030025+
ec_bpn_fz = @nd_parameters[blk_fz]30026+
if ec_bpn_fz >= 030027+
ec_inner_pn_fz = @nd_parameters[ec_bpn_fz]30028+
if ec_inner_pn_fz >= 030029+
ec_reqs_fz = parse_id_list(@nd_requireds[ec_inner_pn_fz])30030+
ec_expected_fz = ec_n_fz + ec_wi_flag_fz30031+
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 - 130033+
return "int"30034+
end30035+
return elem_type_of_array(ec_inner_rt_fz)30036+
end30037+
end30038+
end30039+
end30040+
end30041+
end30042+
end30043+
end2998930044
# Hash#each yields |k, v|. elem_type_of_array on a hash type2999030045
# falls back to "int" for both, which then types `puts k + ":"`2999130046
# inside the block as int-arithmetic and lowers to printf("%lld")spinel_codegen.rbmodifiedView file
Loading diff…
test/each_slice_map.rbaddedView file
Loading diff…