Commit 5694124e

5694124e4ab8Merged

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

Description

inject(&:set_op) / block fold over arrays of int arrays

[[1,2,3],[2,3,4]].inject(&:&) (and :| / :-) folds the inner int arrays with the IntArray set operations; the block form inject { |a, b| a & b } does the same. The inner arrays of a nested int-array literal are now typed as int arrays (comp_is_nested_int_array_literal): the symbol form emits an intersect/union/difference fold unboxing each poly-array element, and the block form types both fold params as int arrays and unboxes element access. The result is an int array.

Block-parameter renaming is extended to inject/reduce folds (their params are typed by the reduce param-inference pass, so a renamed slot still gets a type), and the collision trigger now also catches param-vs-param sharing -- two folds reusing |a, b| with different element types previously collapsed onto one poly LocalVar.

Fixes inject_symbol_to_proc.

Co-Authored-By: Claude Opus 4.8 (1M context) [email protected]

Details

Commit
5694124e4ab8
Parent
036ccc2f424f

src/analyze.cmodifiedView file

@@ -1595,6 +1595,17 @@
15951595 return ty_array_elem(rt);
15961596 }
15971597 if (!strcmp(name, "inject") || !strcmp(name, "reduce")) {
1598+ /* inject(&:&|:||:-) over a literal array of int arrays: set operation
1599+ folding the inner arrays -> an int array. */
1600+ if (rt == TY_POLY_ARRAY && comp_is_nested_int_array_literal(c, recv)) {
1601+ int blk = nt_ref(nt, id, "block");
1602+ const char *sop = NULL;
1603+ if (blk >= 0 && nt_type(nt, blk) && !strcmp(nt_type(nt, blk), "BlockArgumentNode")) {
1604+ int ex = nt_ref(nt, blk, "expression");
1605+ if (ex >= 0 && nt_type(nt, ex) && !strcmp(nt_type(nt, ex), "SymbolNode")) sop = nt_str(nt, ex, "value");
1606+ }
1607+ if (sop && (!strcmp(sop, "&") || !strcmp(sop, "|") || !strcmp(sop, "-"))) return TY_INT_ARRAY;
1608+ }
15981609 /* When an init argument is provided, the return type matches the init type.
15991610 inject(:op) is the no-init operator form — the sole symbol arg is the
16001611 operator, NOT an init value, so skip the "return argv[0] type" path. */
@@ -6478,6 +6489,10 @@
64786489 if (!p0) continue;
64796490 Scope *rs = comp_scope_of(c, block);
64806491 TyKind et2 = ty_array_elem(rt);
6492+ /* `[[ints],...].inject { |a, b| a & b }`: the inner arrays are int arrays,
6493+ so type both fold params as int arrays rather than poly. */
6494+ if (rt == TY_POLY_ARRAY && comp_is_nested_int_array_literal(c, nt_ref(nt, id, "receiver")))
6495+ et2 = TY_INT_ARRAY;
64816496 /* Determine accumulator type from initial value argument (if any) */
64826497 int rargs = nt_ref(nt, id, "arguments");
64836498 int rargc = 0;
@@ -7321,13 +7336,16 @@
73217336 const char *ty = nt_type(nt, L);
73227337 if (ty && !strcmp(ty, "LambdaNode")) return 1;
73237338 if (!ty || strcmp(ty, "BlockNode")) return 0;
7324- /* find the CallNode that owns this block; rename only instance_eval/exec
7325- splice bodies (runs pre-walk_scope, so no type info to identify
7326- trampolines -- those reuse param names rarely and are left to the inliner). */
7339+ /* Find the CallNode that owns this block. Rename for forms whose block
7340+ params are typed by a dedicated param-inference pass (so a renamed slot
7341+ still gets a type): instance_eval/exec splice bodies and inject/reduce
7342+ folds. Ordinary iteration blocks (each/map/...) keep the inliner's
7343+ save/restore over the shared slot and must not be renamed. */
73277344 for (int id = 0; id < nt->count; id++) {
73287345 if (nt_ref(nt, id, "block") != L) continue;
73297346 const char *cn = nt_str(nt, id, "name");
7330- if (cn && (!strcmp(cn, "instance_eval") || !strcmp(cn, "instance_exec"))) return 1;
7347+ if (cn && (!strcmp(cn, "instance_eval") || !strcmp(cn, "instance_exec") ||
7348+ !strcmp(cn, "inject") || !strcmp(cn, "reduce"))) return 1;
73317349 return 0;
73327350 }
73337351 return 0;
@@ -7355,11 +7373,22 @@
73557373 for (int i = 0; i < rn; i++) {
73567374 const char *p = nt_str(nt, reqs[i], "name");
73577375 if (!p) continue;
7358- /* collision: a write to `p` anywhere outside this block's body */
7376+ /* collision: the name is used outside this block's body -- as a local
7377+ write/read or as another block's parameter (param-vs-param, e.g. two
7378+ inject folds sharing `|a, b|` with different element types). Both pollute
7379+ the shared LocalVar's type. */
73597380 int collide = 0;
73607381 for (int w = 0; w < n && !collide; w++) {
73617382 if (inbody[w]) continue;
7362- if (!lv_node_is_write(nt_type(nt, w))) continue;
7383+ const char *wty = nt_type(nt, w);
7384+ int is_param_node = wty && !strcmp(wty, "RequiredParameterNode");
7385+ if (!lv_node_is_write(wty) && !is_param_node) continue;
7386+ /* don't let this block's own parameter nodes count as a collision */
7387+ if (is_param_node) {
7388+ int own = 0;
7389+ for (int q = 0; q < rn; q++) if (reqs[q] == w) { own = 1; break; }
7390+ if (own) continue;
7391+ }
73637392 const char *wn = nt_str(nt, w, "name");
73647393 if (wn && !strcmp(wn, p)) collide = 1;
73657394 }