Commit 036ccc2f

036ccc2f424fMerged

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

Description

Alpha-rename shadowing block/lambda parameters

Block and lambda parameters are interned into the enclosing scope, so a parameter sharing a name with an enclosing local collapsed onto one LocalVar (and one type), corrupting both -- e.g. i919's c = add.curry (a curry local) and ->(a, b, c) (a lambda parameter) became a single conflicting variable.

A pre-walk_scope pass now renames a colliding parameter and its in-body references to a fresh name (<name>__bp<id>), making the two distinct as Ruby's block-parameter shadowing requires. It fires only when the parameter name is also assigned outside the block body (the case that pollutes the shared type), and only for the forms the block inliner does not already shadow-handle: lambdas lowered to standalone proc functions and instance_eval/exec splice bodies. Ordinary iteration blocks keep their existing codegen save/restore path (test/block_param_shadow stays green).

Adds nt_set_str to mutate a node's string field in place.

Fixes i919 (with Proc#curry) and instance_eval_block_params.

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

Details

Commit
036ccc2f424f
Parent
5125b0cf6795

src/analyze.cmodifiedView file

@@ -7238,11 +7238,148 @@
72387238 return (g_ie_node_class && node >= 0) ? g_ie_node_class[node] : -1;
72397239 }
72407240
7241+/* ---- Block/lambda parameter alpha-renaming ----------------------------
7242+ * Block and lambda parameters are interned into the *enclosing* scope, so a
7243+ * parameter sharing a name with an enclosing local collapses onto a single
7244+ * LocalVar (hence one type), corrupting both. Ruby semantics say the two are
7245+ * distinct (the parameter shadows). When the name is also assigned outside the
7246+ * block body -- the case that pollutes the shared type -- rename the parameter
7247+ * and its in-body references to a fresh, collision-free name so they become
7248+ * separate variables. Runs before walk_scope so all downstream interning and
7249+ * codegen see the disambiguated names. */
7250+
7251+/* The ParametersNode for a block (BlockParametersNode -> ParametersNode) or a
7252+ lambda (ParametersNode directly). -1 if none / not a plain ParametersNode. */
7253+static int blkp_params_node(Compiler *c, int create) {
7254+ const NodeTable *nt = c->nt;
7255+ int pn = nt_ref(nt, create, "parameters");
7256+ if (pn < 0) return -1;
7257+ const char *pty = nt_type(nt, pn);
7258+ if (pty && !strcmp(pty, "BlockParametersNode")) pn = nt_ref(nt, pn, "parameters");
7259+ return pn;
7260+}
7261+
7262+static int blkp_binds_param(Compiler *c, int create, const char *name) {
7263+ int pn = blkp_params_node(c, create);
7264+ if (pn < 0) return 0;
7265+ const char *pty = nt_type(c->nt, pn);
7266+ if (!pty || strcmp(pty, "ParametersNode")) return 0;
7267+ int rn = 0; const int *reqs = nt_arr(c->nt, pn, "requireds", &rn);
7268+ for (int i = 0; i < rn; i++) {
7269+ const char *p = nt_str(c->nt, reqs[i], "name");
7270+ if (p && !strcmp(p, name)) return 1;
7271+ }
7272+ return 0;
7273+}
7274+
7275+static int lv_node_is_named_ref(const char *ty) {
7276+ return ty && (!strcmp(ty, "LocalVariableReadNode") || !strcmp(ty, "LocalVariableWriteNode") ||
7277+ !strcmp(ty, "LocalVariableTargetNode") || !strcmp(ty, "LocalVariableOperatorWriteNode") ||
7278+ !strcmp(ty, "LocalVariableOrWriteNode") || !strcmp(ty, "LocalVariableAndWriteNode"));
7279+}
7280+static int lv_node_is_write(const char *ty) {
7281+ return ty && (!strcmp(ty, "LocalVariableWriteNode") || !strcmp(ty, "LocalVariableTargetNode") ||
7282+ !strcmp(ty, "LocalVariableOperatorWriteNode") || !strcmp(ty, "LocalVariableOrWriteNode") ||
7283+ !strcmp(ty, "LocalVariableAndWriteNode"));
7284+}
7285+
7286+/* Rewrite references to `oldn` -> `newn`, stopping at nested defs/classes and
7287+ at nested blocks/lambdas that re-bind `oldn`. */
7288+static void blkp_rewrite_refs(Compiler *c, int node, const char *oldn, const char *newn) {
7289+ if (node < 0) return;
7290+ const NodeTable *nt = c->nt;
7291+ const char *ty = nt_type(nt, node);
7292+ if (!ty) return;
7293+ if (!strcmp(ty, "DefNode") || !strcmp(ty, "ClassNode") || !strcmp(ty, "ModuleNode")) return;
7294+ if ((!strcmp(ty, "BlockNode") || !strcmp(ty, "LambdaNode")) && blkp_binds_param(c, node, oldn)) return;
7295+ if (lv_node_is_named_ref(ty)) {
7296+ const char *nm = nt_str(nt, node, "name");
7297+ if (nm && !strcmp(nm, oldn)) nt_set_str((NodeTable *)nt, node, "name", newn);
7298+ }
7299+ int nr = nt_num_refs(nt, node);
7300+ for (int i = 0; i < nr; i++) blkp_rewrite_refs(c, nt_ref_at(nt, node, i), oldn, newn);
7301+ int na = nt_num_arrs(nt, node);
7302+ 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_rewrite_refs(c, ids[k], oldn, newn); }
7303+}
7304+
7305+static void blkp_mark_subtree(const NodeTable *nt, int node, char *marks) {
7306+ if (node < 0) return;
7307+ marks[node] = 1;
7308+ int nr = nt_num_refs(nt, node);
7309+ for (int i = 0; i < nr; i++) blkp_mark_subtree(nt, nt_ref_at(nt, node, i), marks);
7310+ int na = nt_num_arrs(nt, node);
7311+ 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); }
7312+}
7313+
7314+/* Iteration block parameters (each/map/select/...) are already shadow-safe via
7315+ a codegen save/restore around the inlined body. Renaming is only needed for
7316+ forms the inliner does not cover: lambdas lowered to standalone proc
7317+ functions, and instance_eval/exec (and trampoline) block bodies spliced at
7318+ the call site. Returns 1 if `L` is such a node. */
7319+static int blkp_needs_rename(Compiler *c, int L) {
7320+ const NodeTable *nt = c->nt;
7321+ const char *ty = nt_type(nt, L);
7322+ if (ty && !strcmp(ty, "LambdaNode")) return 1;
7323+ 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). */
7327+ for (int id = 0; id < nt->count; id++) {
7328+ if (nt_ref(nt, id, "block") != L) continue;
7329+ const char *cn = nt_str(nt, id, "name");
7330+ if (cn && (!strcmp(cn, "instance_eval") || !strcmp(cn, "instance_exec"))) return 1;
7331+ return 0;
7332+ }
7333+ return 0;
7334+}
7335+
7336+static void rename_shadowing_block_params(Compiler *c) {
7337+ const NodeTable *nt = c->nt;
7338+ int n = nt->count;
7339+ char *inbody = malloc((size_t)n);
7340+ if (!inbody) return;
7341+ for (int L = 0; L < n; L++) {
7342+ const char *ty = nt_type(nt, L);
7343+ if (!ty || (strcmp(ty, "BlockNode") && strcmp(ty, "LambdaNode"))) continue;
7344+ if (!blkp_needs_rename(c, L)) continue;
7345+ int pn = blkp_params_node(c, L);
7346+ if (pn < 0) continue;
7347+ const char *pty = nt_type(nt, pn);
7348+ if (!pty || strcmp(pty, "ParametersNode")) continue; /* numbered params handled elsewhere */
7349+ int rn = 0; const int *reqs = nt_arr(nt, pn, "requireds", &rn);
7350+ if (rn == 0) continue;
7351+ int body = nt_ref(nt, L, "body");
7352+ if (body < 0) continue;
7353+ memset(inbody, 0, (size_t)n);
7354+ blkp_mark_subtree(nt, body, inbody);
7355+ for (int i = 0; i < rn; i++) {
7356+ const char *p = nt_str(nt, reqs[i], "name");
7357+ if (!p) continue;
7358+ /* collision: a write to `p` anywhere outside this block's body */
7359+ int collide = 0;
7360+ for (int w = 0; w < n && !collide; w++) {
7361+ if (inbody[w]) continue;
7362+ if (!lv_node_is_write(nt_type(nt, w))) continue;
7363+ const char *wn = nt_str(nt, w, "name");
7364+ if (wn && !strcmp(wn, p)) collide = 1;
7365+ }
7366+ if (!collide) continue;
7367+ char oldn[160], newn[176];
7368+ snprintf(oldn, sizeof oldn, "%s", p); /* copy: nt_set_str frees p's storage */
7369+ snprintf(newn, sizeof newn, "%s__bp%d", oldn, L);
7370+ nt_set_str((NodeTable *)nt, reqs[i], "name", newn);
7371+ blkp_rewrite_refs(c, body, oldn, newn);
7372+ }
7373+ }
7374+ free(inbody);
7375+}
7376+
72417377 void analyze_program(Compiler *c) {
72427378 /* scope 0 = top level */
72437379 Scope *top = comp_scope_new(c, NULL, -1);
72447380 top->body = nt_ref(c->nt, c->nt->root_id, "statements");
72457381
7382+ rename_shadowing_block_params(c);
72467383 walk_scope(c, c->nt->root_id, 0, -1);
72477384 register_structs(c);
72487385 fix_struct_block_scopes(c);