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
/* arr.each.with_index(off).<terminal> / arr.each_with_index.<terminal>: a blockless [elem, index]-pair enumerator consumed by the terminal. (matz/spinel#1481 inject/reduce result; #1483 others.) */ if (recv >= 0 && rt == TY_UNKNOWN && if (recv >= 0 && nt_type(nt, recv) && !strcmp(nt_type(nt, recv), "CallNode") && nt_ref(nt, recv, "block") < 0) { const char *rn = nt_str(nt, recv, "name"); } return acc; } int blk = nt_ref(nt, id, "block"); int body = blk >= 0 ? nt_ref(nt, blk, "body") : -1; int bn = 0; const int *bb = body >= 0 ? nt_arr(nt, body, "body", &bn) : NULL; /* The codegen path only handles the |v, i| two-param block form; gate the result type on it so single-param forms fall to their normal rules. */ int two_param = blk >= 0 && !block_param_is_multi(c, blk, 0) && block_param_name(c, blk, 0) && block_param_name(c, blk, 1); if (two_param && (!strcmp(name, "map") || !strcmp(name, "collect"))) return ty_array_of(bn > 0 ? infer_type(c, bb[bn - 1]) : TY_UNKNOWN); if (!strcmp(name, "to_a") || !strcmp(name, "entries") || (two_param && (!strcmp(name, "select") || !strcmp(name, "filter") || !strcmp(name, "reject")))) return TY_POLY_ARRAY; /* an array of [element, index] pairs */ if (two_param && !strcmp(name, "count")) return TY_INT; if (two_param && (!strcmp(name, "any?") || !strcmp(name, "all?") || !strcmp(name, "none?"))) return TY_BOOL; } }src/analyze_pass.cmodifiedView file
Loading diff…
src/codegen_call.cmodifiedView file
Loading diff…
src/codegen_fold.cmodifiedView file
Loading diff…
src/codegen_internal.hmodifiedView file
Loading diff…
test/each_with_index_terminals.rbaddedView file
Loading diff…
test/each_with_index_terminals.rb.expectedaddedView file
Loading diff…