Commit d0104eb8

d0104eb864c8Merged

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

Description

Add each_slice multi-param block, poly bitwise ops, and infer each_slice param types

each_slice: add multi-param block destructuring (|x, y| picks elements from the slice) mirroring the existing each_cons multi-param path. Also add each_slice to the analyze.c param-type inference block alongside each_cons.

Poly bitwise ops: poly_val >> int / & / | / ^ now lowers to sp_poly_to_i(recv) OP arg, matching how poly shift/bitops work in the runtime. Add TY_POLY recv to the infer_type bitwise table so calls return TY_INT rather than TY_UNKNOWN, fixing "unsupported puts argument" for expressions like arr[1][1] >> 1.

Co-Authored-By: Claude Sonnet 4.6 [email protected]

Details

Commit
d0104eb864c8
Parent
f4869a2e1761

src/analyze.cmodifiedView file

@@ -1270,6 +1270,10 @@
12701270 (!strcmp(name, "&") || !strcmp(name, "|") || !strcmp(name, "^") ||
12711271 !strcmp(name, "<<") || !strcmp(name, ">>")))
12721272 return TY_INT;
1273+ /* poly recv bitwise shift: result is int (sp_poly_to_i applied) */
1274+ if (recv >= 0 && argc == 1 && rt == TY_POLY &&
1275+ (!strcmp(name, ">>") || !strcmp(name, "&") || !strcmp(name, "|") || !strcmp(name, "^")))
1276+ return TY_INT;
12731277 /* boolean &/|/^ */
12741278 if (recv >= 0 && argc == 1 && rt == TY_BOOL &&
12751279 (!strcmp(name, "&") || !strcmp(name, "|") || !strcmp(name, "^")))
@@ -3594,9 +3598,9 @@
35943598 !strcmp(name, "detect") || !strcmp(name, "any?") || !strcmp(name, "all?")))
35953599 pt = TY_POLY;
35963600
3597- /* array.each_cons(n) { |a, b, ...| } -- a single param binds the n-element
3598- sub-array; multiple params destructure consecutive elements */
3599- if (!strcmp(name, "each_cons") && ty_is_array(rt)) {
3601+ /* array.each_cons(n) / each_slice(n) { |a, b, ...| } -- a single param
3602+ binds the n-element sub-array; multiple params destructure elements */
3603+ if ((!strcmp(name, "each_cons") || !strcmp(name, "each_slice")) && ty_is_array(rt)) {
36003604 Scope *es = comp_scope_of(c, block);
36013605 int np = 0; while (block_param_name(c, block, np)) np++;
36023606 for (int pj = 0; pj < np; pj++) {