Commit ecfd6cd4

ecfd6cd49affMerged

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

Description

Qualify colliding nested constants; absolute-path Fiber methods

Constants live in a flat cst_ namespace, so RootNS::Mid::LEAF and Lex::RootNS::Mid::LEAF collided on cst_LEAF. A pre-walk_scope pass now detects constant names written under 2+ distinct module paths and alpha-renames each nested write to a qualified <Mod>__..__<NAME>, rewriting every read to the constant it denotes: path reads reconstruct their full chain (::-anchored resolve from the root, relative ones try the lexically enclosing module prefixes innermost-first), and bare reads inside module bodies resolve lexically the same way. Collision-gated: programs with unique constant names are untouched.

Also: ::Fiber.yield / ::Fiber.new (root-anchored ConstantPathNode) route to the Fiber machinery like the bare constant, and blocks lifted to standalone proc/fiber functions (proc/lambda/Proc.new/Fiber.new/Thread.new) participate in shadowing block-param renaming.

Fixes constant_path.

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

Details

Commit
ecfd6cd49aff
Parent
56c546695745

src/analyze.cmodifiedView file

@@ -6176,7 +6176,11 @@
61766176 const char *cname = nt_str(nt, id, "name");
61776177 if (!cname || strcmp(cname, "new")) continue;
61786178 int recv = nt_ref(nt, id, "receiver");
6179- if (recv < 0 || !nt_type(nt, recv) || strcmp(nt_type(nt, recv), "ConstantReadNode")) continue;
6179+ if (recv < 0 || !nt_type(nt, recv)) continue;
6180+ const char *rrty = nt_type(nt, recv);
6181+ int is_const = !strcmp(rrty, "ConstantReadNode") ||
6182+ (!strcmp(rrty, "ConstantPathNode") && nt_ref(nt, recv, "parent") < 0);
6183+ if (!is_const) continue;
61806184 const char *rn = nt_str(nt, recv, "name");
61816185 if (!rn || strcmp(rn, "Fiber")) continue;
61826186 int blk = nt_ref(nt, id, "block");
@@ -7544,13 +7548,223 @@
75447548 for (int id = 0; id < nt->count; id++) {
75457549 if (nt_ref(nt, id, "block") != L) continue;
75467550 const char *cn = nt_str(nt, id, "name");
7547- if (cn && (!strcmp(cn, "instance_eval") || !strcmp(cn, "instance_exec") ||
7548- !strcmp(cn, "inject") || !strcmp(cn, "reduce"))) return 1;
7551+ if (!cn) return 0;
7552+ if (!strcmp(cn, "instance_eval") || !strcmp(cn, "instance_exec") ||
7553+ !strcmp(cn, "inject") || !strcmp(cn, "reduce")) return 1;
7554+ /* blocks lifted to standalone proc/fiber functions: proc {} / lambda {} /
7555+ Proc.new {} / Fiber.new {} / Thread.new {}. Their params are typed by a
7556+ dedicated pass, so renaming a shadowing param is safe. */
7557+ int rcv = nt_ref(nt, id, "receiver");
7558+ if (rcv < 0 && (!strcmp(cn, "proc") || !strcmp(cn, "lambda"))) return 1;
7559+ if (rcv >= 0 && !strcmp(cn, "new")) {
7560+ const char *rty = nt_type(nt, rcv);
7561+ int is_const = rty && (!strcmp(rty, "ConstantReadNode") ||
7562+ (!strcmp(rty, "ConstantPathNode") && nt_ref(nt, rcv, "parent") < 0));
7563+ const char *rn = is_const ? nt_str(nt, rcv, "name") : NULL;
7564+ if (rn && (!strcmp(rn, "Proc") || !strcmp(rn, "Fiber") || !strcmp(rn, "Thread"))) return 1;
7565+ }
75497566 return 0;
75507567 }
75517568 return 0;
75527569 }
75537570
7571+/* ---- Colliding nested-constant qualification --------------------------
7572+ * Constants live in a flat cst_<NAME> namespace, so `RootNS::Mid::LEAF` and
7573+ * `Lex::RootNS::Mid::LEAF` collide. When the same constant name is written
7574+ * under 2+ distinct module paths, rename each nested write to a qualified
7575+ * `<Mod>__..__<NAME>` and rewrite every path read to whichever qualified
7576+ * constant it denotes (relative reads prefer the lexically enclosing module
7577+ * chain; `::`-anchored reads resolve from the root). Collision-gated: programs
7578+ * with unique constant names are untouched. */
7579+
7580+#define QC_MAXDEPTH 8
7581+typedef struct { int node; char path[QC_MAXDEPTH][64]; int depth; char name[128]; } QCWrite;
7582+
7583+static void qc_collect_writes(Compiler *c, int node, char (*path)[64], int depth,
7584+ QCWrite **ws, int *n, int *cap) {
7585+ const NodeTable *nt = c->nt;
7586+ if (node < 0) return;
7587+ const char *ty = nt_type(nt, node);
7588+ if (!ty) return;
7589+ if ((!strcmp(ty, "ModuleNode") || !strcmp(ty, "ClassNode")) && depth < QC_MAXDEPTH) {
7590+ int cp = nt_ref(nt, node, "constant_path");
7591+ const char *mn = cp >= 0 ? nt_str(nt, cp, "name") : NULL;
7592+ if (mn) {
7593+ snprintf(path[depth], 64, "%s", mn);
7594+ depth++;
7595+ }
7596+ }
7597+ else if (!strcmp(ty, "ConstantWriteNode")) {
7598+ const char *nm = nt_str(nt, node, "name");
7599+ if (nm) {
7600+ if (*n >= *cap) { *cap = *cap ? *cap * 2 : 16; *ws = realloc(*ws, sizeof(QCWrite) * (size_t)*cap); }
7601+ QCWrite *w = &(*ws)[(*n)++];
7602+ w->node = node; w->depth = depth;
7603+ for (int i = 0; i < depth; i++) snprintf(w->path[i], 64, "%s", path[i]);
7604+ snprintf(w->name, sizeof w->name, "%s", nm);
7605+ }
7606+ }
7607+ int nr = nt_num_refs(nt, node);
7608+ for (int i = 0; i < nr; i++) qc_collect_writes(c, nt_ref_at(nt, node, i), path, depth, ws, n, cap);
7609+ int na = nt_num_arrs(nt, node);
7610+ for (int i = 0; i < na; i++) { int m = 0; const int *ids = nt_arr_at(nt, node, i, &m); for (int k = 0; k < m; k++) qc_collect_writes(c, ids[k], path, depth, ws, n, cap); }
7611+}
7612+
7613+/* Reconstruct a path read's chain (["RootNS","Mid","LEAF"]) and whether it is
7614+ root-anchored. Returns the chain length, or 0 if unsupported. */
7615+static int qc_read_chain(const NodeTable *nt, int node, char (*chain)[64], int *abs_anchor) {
7616+ char rev[QC_MAXDEPTH + 1][64];
7617+ int n = 0;
7618+ int cur = node;
7619+ *abs_anchor = 0;
7620+ while (cur >= 0 && n <= QC_MAXDEPTH) {
7621+ const char *ty = nt_type(nt, cur);
7622+ const char *nm = nt_str(nt, cur, "name");
7623+ if (!ty || !nm) return 0;
7624+ snprintf(rev[n++], 64, "%s", nm);
7625+ if (!strcmp(ty, "ConstantReadNode")) break;
7626+ if (strcmp(ty, "ConstantPathNode")) return 0;
7627+ int par = nt_ref(nt, cur, "parent");
7628+ if (par < 0) { *abs_anchor = 1; break; }
7629+ cur = par;
7630+ }
7631+ for (int i = 0; i < n; i++) snprintf(chain[i], 64, "%s", rev[n - 1 - i]);
7632+ return n;
7633+}
7634+
7635+static void qc_qualified_name(char *out, size_t cap, const QCWrite *w) {
7636+ out[0] = 0;
7637+ for (int i = 0; i < w->depth; i++) { strncat(out, w->path[i], cap - strlen(out) - 1); strncat(out, "__", cap - strlen(out) - 1); }
7638+ strncat(out, w->name, cap - strlen(out) - 1);
7639+}
7640+
7641+static void qc_rewrite_reads(Compiler *c, int node, char (*mods)[64], int mdepth,
7642+ QCWrite *ws, int wn) {
7643+ const NodeTable *nt = c->nt;
7644+ if (node < 0) return;
7645+ const char *ty = nt_type(nt, node);
7646+ if (!ty) return;
7647+ int depth = mdepth;
7648+ char (*path)[64] = mods;
7649+ if ((!strcmp(ty, "ModuleNode") || !strcmp(ty, "ClassNode")) && depth < QC_MAXDEPTH) {
7650+ int cp = nt_ref(nt, node, "constant_path");
7651+ const char *mn = cp >= 0 ? nt_str(nt, cp, "name") : NULL;
7652+ if (mn) { snprintf(path[depth], 64, "%s", mn); depth++; }
7653+ }
7654+ else if (!strcmp(ty, "ConstantReadNode")) {
7655+ /* bare constant read inside a module body: resolve lexically innermost-
7656+ first against the colliding writes. Skip reads that are a path's parent
7657+ (handled via the chain) or a class/module definition name. */
7658+ const char *nm = nt_str(nt, node, "name");
7659+ int part_of_other = 0;
7660+ for (int q = 0; q < nt->count && !part_of_other; q++) {
7661+ const char *qt = nt_type(nt, q);
7662+ if (!qt) continue;
7663+ if (!strcmp(qt, "ConstantPathNode") && nt_ref(nt, q, "parent") == node) part_of_other = 1;
7664+ if ((!strcmp(qt, "ClassNode") || !strcmp(qt, "ModuleNode")) &&
7665+ nt_ref(nt, q, "constant_path") == node) part_of_other = 1;
7666+ }
7667+ if (nm && !part_of_other) {
7668+ int involved = 0;
7669+ for (int i = 0; i < wn; i++) if (!strcmp(ws[i].name, nm)) { involved = 1; break; }
7670+ if (involved) {
7671+ for (int pref = depth; pref >= 0; pref--) {
7672+ int matched = -1;
7673+ for (int i = 0; i < wn && matched < 0; i++) {
7674+ if (strcmp(ws[i].name, nm) || ws[i].depth != pref) continue;
7675+ int ok = 1;
7676+ for (int j = 0; j < pref && ok; j++) if (strcmp(ws[i].path[j], path[j])) ok = 0;
7677+ if (ok) matched = i;
7678+ }
7679+ if (matched >= 0) {
7680+ if (ws[matched].depth > 0) {
7681+ char qn[512]; qc_qualified_name(qn, sizeof qn, &ws[matched]);
7682+ nt_set_str((NodeTable *)nt, node, "name", qn);
7683+ }
7684+ break;
7685+ }
7686+ }
7687+ }
7688+ }
7689+ }
7690+ else if (!strcmp(ty, "ConstantPathNode")) {
7691+ /* only process path heads: skip if this node is some other path's parent */
7692+ int is_parent = 0;
7693+ for (int q = 0; q < nt->count && !is_parent; q++) {
7694+ const char *qt = nt_type(nt, q);
7695+ if (qt && !strcmp(qt, "ConstantPathNode") && nt_ref(nt, q, "parent") == node) is_parent = 1;
7696+ }
7697+ if (!is_parent) {
7698+ char chain[QC_MAXDEPTH + 1][64];
7699+ int abs_anchor = 0;
7700+ int cl = qc_read_chain(nt, node, chain, &abs_anchor);
7701+ if (cl >= 2) {
7702+ const char *cname = chain[cl - 1];
7703+ /* does this name participate in a collision? */
7704+ int involved = 0;
7705+ for (int i = 0; i < wn; i++) if (!strcmp(ws[i].name, cname)) { involved = 1; break; }
7706+ if (involved) {
7707+ /* try lexical prefixes innermost-first (relative), or only the root (::) */
7708+ int max_pref = abs_anchor ? 0 : depth;
7709+ for (int pref = max_pref; pref >= 0; pref--) {
7710+ int matched = -1;
7711+ for (int i = 0; i < wn && matched < 0; i++) {
7712+ if (strcmp(ws[i].name, cname)) continue;
7713+ if (ws[i].depth != pref + (cl - 1)) continue;
7714+ int ok = 1;
7715+ for (int j = 0; j < pref && ok; j++) if (strcmp(ws[i].path[j], path[j])) ok = 0;
7716+ for (int j = 0; j < cl - 1 && ok; j++) if (strcmp(ws[i].path[pref + j], chain[j])) ok = 0;
7717+ if (ok) matched = i;
7718+ }
7719+ if (matched >= 0) {
7720+ if (ws[matched].depth > 0) {
7721+ char qn[512]; qc_qualified_name(qn, sizeof qn, &ws[matched]);
7722+ nt_set_str((NodeTable *)nt, node, "name", qn);
7723+ }
7724+ break;
7725+ }
7726+ }
7727+ }
7728+ }
7729+ }
7730+ }
7731+ int nr = nt_num_refs(nt, node);
7732+ for (int i = 0; i < nr; i++) qc_rewrite_reads(c, nt_ref_at(nt, node, i), path, depth, ws, wn);
7733+ int na = nt_num_arrs(nt, node);
7734+ for (int i = 0; i < na; i++) { int m = 0; const int *ids = nt_arr_at(nt, node, i, &m); for (int k = 0; k < m; k++) qc_rewrite_reads(c, ids[k], path, depth, ws, wn); }
7735+}
7736+
7737+static void qualify_colliding_consts(Compiler *c) {
7738+ const NodeTable *nt = c->nt;
7739+ QCWrite *ws = NULL; int wn = 0, wcap = 0;
7740+ char path[QC_MAXDEPTH][64];
7741+ qc_collect_writes(c, nt->root_id, path, 0, &ws, &wn, &wcap);
7742+ /* keep only names written under 2+ distinct module paths */
7743+ int any = 0;
7744+ for (int i = 0; i < wn; i++) {
7745+ int collide = 0;
7746+ for (int j = 0; j < wn && !collide; j++) {
7747+ if (i == j || strcmp(ws[i].name, ws[j].name)) continue;
7748+ if (ws[i].depth != ws[j].depth) { collide = 1; break; }
7749+ for (int k = 0; k < ws[i].depth; k++) if (strcmp(ws[i].path[k], ws[j].path[k])) { collide = 1; break; }
7750+ }
7751+ if (!collide) { ws[i] = ws[--wn]; i--; continue; }
7752+ any = 1;
7753+ }
7754+ if (any) {
7755+ /* rewrite reads first (they match against the original write names) */
7756+ char mods[QC_MAXDEPTH][64];
7757+ qc_rewrite_reads(c, nt->root_id, mods, 0, ws, wn);
7758+ /* then qualify the nested writes themselves */
7759+ for (int i = 0; i < wn; i++) {
7760+ if (ws[i].depth == 0) continue;
7761+ char qn[512]; qc_qualified_name(qn, sizeof qn, &ws[i]);
7762+ nt_set_str((NodeTable *)nt, ws[i].node, "name", qn);
7763+ }
7764+ }
7765+ free(ws);
7766+}
7767+
75547768 static void rename_shadowing_block_params(Compiler *c) {
75557769 const NodeTable *nt = c->nt;
75567770 int n = nt->count;
@@ -7609,6 +7823,7 @@
76097823 top->body = nt_ref(c->nt, c->nt->root_id, "statements");
76107824
76117825 rename_shadowing_block_params(c);
7826+ qualify_colliding_consts(c);
76127827 walk_scope(c, c->nt->root_id, 0, -1);
76137828 register_structs(c);
76147829 fix_struct_block_scopes(c);