Commit ab3631c9

ab3631c9b24bMerged

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

Description

Merge PR #1520: type each_cons block params as elements for multi-param blocks

arr.each_cons(n).map { |a, b| ... } typed every block param as the whole n-window array while codegen bound each param to a window element, so non-trivial bodies were rejected (unsupported equality) or miscompiled. each_cons now shares each_slice's param semantics: a single param |w| binds the whole window (array); several params |a, b| (or a destructured |(a, b)|) split it into element-typed params.

Combined-state gates: test 1007/0/0, bench 57/57, optcarrot checksum 59662.

Closes #1520

Co-authored-by: Ryan Seys [email protected] Co-Authored-By: Claude Opus 4.8 [email protected]

Details

Commit
ab3631c9b24b
Parent
6749983efd8a
Merge parent
0d14102f82f0

src/analyze_pass.cmodifiedView file

@@ -3143,15 +3143,14 @@
31433143 nt_ref(nt, recv, "block") < 0) {
31443144 int es_recv2 = nt_ref(nt, recv, "receiver");
31453145 TyKind arr_t2 = es_recv2 >= 0 ? infer_type(c, es_recv2) : TY_UNKNOWN;
3146- int is_cons2 = !strcmp(nt_str(nt, recv, "name"), "each_cons");
31473146 if (ty_is_array(arr_t2)) {
31483147 Scope *es2 = comp_scope_of(c, block);
31493148 int np2 = 0; while (block_param_name(c, block, np2)) np2++;
3150- /* each_cons binds the n-window (array); each_slice binds the slice
3151- (array) for a single param `|s|`, or destructures into elements when
3152- there are several params `|a, b|`. A single destructured param
3153- `|(a, b)|` over either splits the window/slice into its elements. */
3154- TyKind bp_t2 = is_cons2 ? arr_t2 : (np2 == 1 ? arr_t2 : ty_array_elem(arr_t2));
3149+ /* each_cons and each_slice bind the n-window / slice (an array) for a
3150+ single param `|w|`, or destructure it into elements for several
3151+ params `|a, b|` (matching the codegen, which binds element pj when
3152+ np > 1). A single destructured param `|(a, b)|` splits it likewise. */
3153+ TyKind bp_t2 = (np2 == 1 ? arr_t2 : ty_array_elem(arr_t2));
31553154 if (bp_t2 != TY_UNKNOWN) {
31563155 if (np2 == 0 && block_param_is_multi(c, block, 0)) {
31573156 /* |(a, b)| destructuring: each leaf gets element type */