Commit 4d039962

4d039962b6ffMerged

Yukihiro "Matz" Matsumoto <[email protected]> · 5 months ago · 7 files · +2 added · ~5 modified

Description

Support map/to_a/select/count/any? over an each.with_index enumerator

arr.each.with_index.<terminal> (with a chained map/to_a/select/reject/ count/any?/all?/none?) compiled cleanly but silently yielded empty results ([], nil/false, 0) instead of iterating -- a silent miscompile, worse than a rejection.

each.with_index / each_with_index yields [element, index] pairs. emit_each_with_index_terminal recognises the exact chain shape and lowers each terminal as an indexed loop: map/collect collect the block value; select/reject/to_a/entries collect the [element, index] pair; count and any?/all?/none? reduce to a scalar. analyze types the |v, i| block params (element + int) and infers each terminal's result type.

Scoped to the unambiguous |v, i| two-param block form (and blockless to_a/entries); a single |x| param has method-dependent yield semantics in MRI (map binds the element, select binds the pair) and a |(v,i)| destructure is an MRI corner, so both are left to their existing rules. The match is gated tightly on the chain shape, so no other call path is affected.

Verified vs MRI (incl. string-element pairs, offset, each_with_index, and all listed terminals). 957 test (gcc + clang) / 57 bench / optcarrot 59662 (744 fps); clean under clang -fsanitize=address,undefined.

Closes #1483

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

Details

Commit
4d039962b6ff
Parent
7db5be948f7b

src/analyze_infer.cmodifiedView file

@@ -1468,7 +1468,7 @@
14681468 /* arr.each.with_index(off).<terminal> / arr.each_with_index.<terminal>:
14691469 a blockless [elem, index]-pair enumerator consumed by the terminal.
14701470 (matz/spinel#1481 inject/reduce result; #1483 others.) */
1471- if (recv >= 0 && rt == TY_UNKNOWN &&
1471+ if (recv >= 0 &&
14721472 nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") &&
14731473 nt_ref(nt, recv, "block") < 0) {
14741474 const char *rn = nt_str(nt, recv, "name");
@@ -1499,6 +1499,21 @@
14991499 }
15001500 return acc;
15011501 }
1502+ int blk = nt_ref(nt, id, "block");
1503+ int body = blk >= 0 ? nt_ref(nt, blk, "body") : -1;
1504+ int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL;
1505+ /* The codegen path only handles the |v, i| two-param block form; gate the
1506+ result type on it so single-param forms fall to their normal rules. */
1507+ int two_param = blk >= 0 && !block_param_is_multi(c, blk, 0) &&
1508+ block_param_name(c, blk, 0) && block_param_name(c, blk, 1);
1509+ if (two_param && (!strcmp(name, "map") || !strcmp(name, "collect")))
1510+ return ty_array_of(bn > 0 ? infer_type(c, bb[bn - 1]) : TY_UNKNOWN);
1511+ if (!strcmp(name, "to_a") || !strcmp(name, "entries") ||
1512+ (two_param && (!strcmp(name, "select") || !strcmp(name, "filter") || !strcmp(name, "reject"))))
1513+ return TY_POLY_ARRAY; /* an array of [element, index] pairs */
1514+ if (two_param && !strcmp(name, "count")) return TY_INT;
1515+ if (two_param && (!strcmp(name, "any?") || !strcmp(name, "all?") || !strcmp(name, "none?")))
1516+ return TY_BOOL;
15021517 }
15031518 }
15041519