Commit 0d14102f
0d14102f82f0Merged
Ryan Seys <[email protected]> · 5 months ago · 3 files · +2 added · ~1 modified
Description
analyze: 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, even with several params, while the codegen bound each param to
a window ELEMENT (sp_IntArray_get(window, i + pj)). The mismatch made a + b
compile as array concat and lv_a = <element> a pointer-from-int error, so
non-trivial bodies were rejected or miscompiled.
each_cons and each_slice share the same param semantics: a single param |w|
binds the whole window/slice (an array); several params |a, b| (or a single
destructured |(a, b)|) split it into element-typed params. Drop the
each_cons-only branch that forced the array type so each_cons follows the same
rule, matching its codegen.
Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]
Details
- Commit
- 0d14102f82f0
- Parent
- 3d4efd9a07a7
src/analyze_pass.cmodifiedView file
nt_ref(nt, recv, "block") < 0) { int es_recv2 = nt_ref(nt, recv, "receiver"); TyKind arr_t2 = es_recv2 >= 0 ? infer_type(c, es_recv2) : TY_UNKNOWN; int is_cons2 = !strcmp(nt_str(nt, recv, "name"), "each_cons"); if (ty_is_array(arr_t2)) { Scope *es2 = comp_scope_of(c, block); int np2 = 0; while (block_param_name(c, block, np2)) np2++; /* each_cons binds the n-window (array); each_slice binds the slice (array) for a single param `|s|`, or destructures into elements when there are several params `|a, b|`. A single destructured param `|(a, b)|` over either splits the window/slice into its elements. */ TyKind bp_t2 = is_cons2 ? arr_t2 : (np2 == 1 ? arr_t2 : ty_array_elem(arr_t2)); /* each_cons and each_slice bind the n-window / slice (an array) for a single param `|w|`, or destructure it into elements for several params `|a, b|` (matching the codegen, which binds element pj when np > 1). A single destructured param `|(a, b)|` splits it likewise. */ TyKind bp_t2 = (np2 == 1 ? arr_t2 : ty_array_elem(arr_t2)); if (bp_t2 != TY_UNKNOWN) { if (np2 == 0 && block_param_is_multi(c, block, 0)) { /* |(a, b)| destructuring: each leaf gets element type */test/each_cons_map_types.rbaddedView file
Loading diff…
test/each_cons_map_types.rb.expectedaddedView file
Loading diff…