Commit 3137b1c5
3137b1c5a316Merged
Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 1 file · ~1 modified
Description
analyze: infer block params for each_cons(n).map { |pair| } chains
Like the existing each_slice(n).map chain handling, a each_cons(n).map { |pair| } chain should give the block param the array type (the sub-array of n elements), not leave it unknown.
Co-Authored-By: Claude Sonnet 4.6 [email protected]
Details
- Commit
- 3137b1c5a316
- Parent
- 31422a7e70af
src/analyze.cmodifiedView file
@@ -3650,23 +3650,28 @@
36503650
}36513651
36523652
/* array.each_slice(n).map/collect { |x, y, ...| } chain: each block param3653-
gets the element type of the original array (slice elements) */3653+
gets the element type of the original array (slice elements).3654+
array.each_cons(n).map { |pair| } chain: block param gets the array type. */36543655
if ((!strcmp(name, "map") || !strcmp(name, "collect")) && rt == TY_UNKNOWN &&36553656
nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&3656-
nt_str(nt, recv, "name") && !strcmp(nt_str(nt, recv, "name"), "each_slice") &&3657+
nt_str(nt, recv, "name") && (!strcmp(nt_str(nt, recv, "name"), "each_slice") ||3658+
!strcmp(nt_str(nt, recv, "name"), "each_cons")) &&36573659
nt_ref(nt, recv, "block") < 0) {36583660
int es_recv2 = nt_ref(nt, recv, "receiver");36593661
TyKind arr_t2 = es_recv2 >= 0 ? infer_type(c, es_recv2) : TY_UNKNOWN;3662+
int is_cons2 = !strcmp(nt_str(nt, recv, "name"), "each_cons");36603663
if (ty_is_array(arr_t2)) {3661-
TyKind elem_t2 = ty_array_elem(arr_t2);3662-
if (elem_t2 != TY_UNKNOWN) {3664+
/* each_slice: block params get element type; each_cons: single param3665+
gets the array type (a sub-array slice of the original). */3666+
TyKind bp_t2 = is_cons2 ? arr_t2 : ty_array_elem(arr_t2);3667+
if (bp_t2 != TY_UNKNOWN) {36633668
Scope *es2 = comp_scope_of(c, block);36643669
int np2 = 0; while (block_param_name(c, block, np2)) np2++;36653670
for (int pj2 = 0; pj2 < np2; pj2++) {36663671
const char *pn2 = block_param_name(c, block, pj2);36673672
if (!pn2) break;36683673
LocalVar *lp2 = scope_local_intern(es2, pn2); lp2->is_block_param = 1;3669-
TyKind m2 = ty_unify(lp2->type, elem_t2);3674+
TyKind m2 = ty_unify(lp2->type, bp_t2);36703675
if (m2 != lp2->type) { lp2->type = m2; changed = 1; }36713676
}36723677
continue;