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
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); }}/* Iteration block parameters (each/map/select/...) are already shadow-safe via a codegen save/restore around the inlined body. Renaming is only needed for forms the inliner does not cover: lambdas lowered to standalone proc functions, and instance_eval/exec (and trampoline) block bodies spliced at the call site. Returns 1 if `L` is such a node. *//* A block/lambda parameter is interned into the enclosing (flat) scope, so two blocks reusing a name -- or a block param sharing a name with an enclosing local -- collapse onto one LocalVar and one type. The rename pass below splits them, but only for nodes this predicate accepts. We accept any block owned by a call: the collision check in rename_shadowing_block_params is the real filter (it fires only when the name is actually shared), and codegen reads every param name through block_param_name + rename_local, so a renamed slot stays consistent in the inliner, the standalone-proc lowering, and the instance_eval/exec splice path alike. Returns 1 if `L` is such a node. */int blkp_needs_rename(Compiler *c, int L) { const NodeTable *nt = c->nt; const char *ty = nt_type(nt, L); if (ty && !strcmp(ty, "LambdaNode")) return 1; if (!ty || strcmp(ty, "BlockNode")) return 0; /* Find the CallNode that owns this block. Rename for forms whose block params are typed by a dedicated param-inference pass (so a renamed slot still gets a type): instance_eval/exec splice bodies and inject/reduce folds. Ordinary iteration blocks (each/map/...) keep the inliner's save/restore over the shared slot and must not be renamed. */ /* A block owned by a call is renameable. Ordinary iteration blocks (each/map/select/...) were once excluded on the assumption the inliner's save/restore made them shadow-safe; that holds for the element-typed shadow path but not when sibling blocks of divergent element types share a name (e.g. `arr.map{|x| x+0.5}.map{|x| x.floor}` -- the poly-array map leg writes the shared poly slot), so they go through the collision gate too. */ for (int id = 0; id < nt->count; id++) { if (nt_ref(nt, id, "block") != L) continue; const char *cn = nt_str(nt, id, "name"); if (!cn) return 0; if (!strcmp(cn, "instance_eval") || !strcmp(cn, "instance_exec") || !strcmp(cn, "inject") || !strcmp(cn, "reduce")) return 1; /* blocks lifted to standalone proc/fiber functions: proc {} / lambda {} / Proc.new {} / Fiber.new {} / Thread.new {}. Their params are typed by a dedicated pass, so renaming a shadowing param is safe. */ int rcv = nt_ref(nt, id, "receiver"); if (rcv < 0 && (!strcmp(cn, "proc") || !strcmp(cn, "lambda"))) return 1; if (rcv >= 0 && !strcmp(cn, "new")) { const char *rty = nt_type(nt, rcv); int is_const = rty && (!strcmp(rty, "ConstantReadNode") || (!strcmp(rty, "ConstantPathNode") && nt_ref(nt, rcv, "parent") < 0)); const char *rn = is_const ? nt_str(nt, rcv, "name") : NULL; if (rn && (!strcmp(rn, "Proc") || !strcmp(rn, "Fiber") || !strcmp(rn, "Thread"))) return 1; } return 0; return nt_str(nt, id, "name") != NULL; } return 0;}src/analyze_pass.cmodifiedView file
Loading diff…
test/block_param_sibling_collision.rbaddedView file
Loading diff…