Commit 8c52619d

8c52619d7597Merged

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

Description

Split sibling iteration-block params that share a name

Block/lambda parameters intern into the enclosing flat scope, so two sibling iteration blocks reusing a name -- e.g. arr.map{|x| x+0.5}.map{|x| x.floor} -- collapsed onto one LocalVar. Their element types (int, then float) unified to poly, and the poly-array map leg then wrote the shared poly slot, emitting sp_box_int(sp_box_nil()) and failing to compile.

The alpha-rename pass (rename_shadowing_block_params) already handled this for lambdas, proc/Fiber/Thread, and inject/reduce folds, but deliberately excluded ordinary iteration blocks on the assumption the inliner's element-typed C shadow made them safe. That holds for the shadow path but not when a divergent-typed sibling forces the shared slot to poly. blkp_needs_rename now accepts any call-owned block; the existing collision check (name also used outside the body) stays the real filter, so non-colliding blocks are untouched and optcarrot's checksum is unchanged.

Enabling iteration blocks unmasked a latent each_index typing bug: each_index binds the index (always int), not the element, but it sat in the element-type list and so typed |i| as the element type. Masked before because a sibling collision widened the slot to poly and the inliner's shadow forced int; now a clean rename leaves the wrong scalar type. Give each_index a dedicated int arm.

Adds test/block_param_sibling_collision.rb covering chained maps, sibling each_index/select, nested same-name shadowing, nested capture, and inject folds of divergent element types.

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

Details

Commit
8c52619d7597
Parent
f07dbb1869b5

src/analyze.cmodifiedView file

@@ -499,40 +499,29 @@
499499 for (int i = 0; i < na; i++) { int n = 0; const int *ids = nt_arr_at(nt, node, i, &n); for (int k = 0; k < n; k++) blkp_mark_subtree(nt, ids[k], marks); }
500500 }
501501
502-/* Iteration block parameters (each/map/select/...) are already shadow-safe via
503- a codegen save/restore around the inlined body. Renaming is only needed for
504- forms the inliner does not cover: lambdas lowered to standalone proc
505- functions, and instance_eval/exec (and trampoline) block bodies spliced at
506- the call site. Returns 1 if `L` is such a node. */
502+/* A block/lambda parameter is interned into the enclosing (flat) scope, so two
503+ blocks reusing a name -- or a block param sharing a name with an enclosing
504+ local -- collapse onto one LocalVar and one type. The rename pass below splits
505+ them, but only for nodes this predicate accepts. We accept any block owned by
506+ a call: the collision check in rename_shadowing_block_params is the real
507+ filter (it fires only when the name is actually shared), and codegen reads
508+ every param name through block_param_name + rename_local, so a renamed slot
509+ stays consistent in the inliner, the standalone-proc lowering, and the
510+ instance_eval/exec splice path alike. Returns 1 if `L` is such a node. */
507511 int blkp_needs_rename(Compiler *c, int L) {
508512 const NodeTable *nt = c->nt;
509513 const char *ty = nt_type(nt, L);
510514 if (ty && !strcmp(ty, "LambdaNode")) return 1;
511515 if (!ty || strcmp(ty, "BlockNode")) return 0;
512- /* Find the CallNode that owns this block. Rename for forms whose block
513- params are typed by a dedicated param-inference pass (so a renamed slot
514- still gets a type): instance_eval/exec splice bodies and inject/reduce
515- folds. Ordinary iteration blocks (each/map/...) keep the inliner's
516- save/restore over the shared slot and must not be renamed. */
516+ /* A block owned by a call is renameable. Ordinary iteration blocks
517+ (each/map/select/...) were once excluded on the assumption the inliner's
518+ save/restore made them shadow-safe; that holds for the element-typed shadow
519+ path but not when sibling blocks of divergent element types share a name
520+ (e.g. `arr.map{|x| x+0.5}.map{|x| x.floor}` -- the poly-array map leg writes
521+ the shared poly slot), so they go through the collision gate too. */
517522 for (int id = 0; id < nt->count; id++) {
518523 if (nt_ref(nt, id, "block") != L) continue;
519- const char *cn = nt_str(nt, id, "name");
520- if (!cn) return 0;
521- if (!strcmp(cn, "instance_eval") || !strcmp(cn, "instance_exec") ||
522- !strcmp(cn, "inject") || !strcmp(cn, "reduce")) return 1;
523- /* blocks lifted to standalone proc/fiber functions: proc {} / lambda {} /
524- Proc.new {} / Fiber.new {} / Thread.new {}. Their params are typed by a
525- dedicated pass, so renaming a shadowing param is safe. */
526- int rcv = nt_ref(nt, id, "receiver");
527- if (rcv < 0 && (!strcmp(cn, "proc") || !strcmp(cn, "lambda"))) return 1;
528- if (rcv >= 0 && !strcmp(cn, "new")) {
529- const char *rty = nt_type(nt, rcv);
530- int is_const = rty && (!strcmp(rty, "ConstantReadNode") ||
531- (!strcmp(rty, "ConstantPathNode") && nt_ref(nt, rcv, "parent") < 0));
532- const char *rn = is_const ? nt_str(nt, rcv, "name") : NULL;
533- if (rn && (!strcmp(rn, "Proc") || !strcmp(rn, "Fiber") || !strcmp(rn, "Thread"))) return 1;
534- }
535- return 0;
524+ return nt_str(nt, id, "name") != NULL;
536525 }
537526 return 0;
538527 }