Commit 884b6f66

884b6f664d8eMerged

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

Description

analyze: qualify class/module names colliding across modules

Classes and modules live in a flat sp_ C namespace keyed by the leaf name, so same-leaf-name classes in different namespaces (Web::Response vs Chat::Response) collapsed onto one ClassInfo: their structs and methods merged and only the last-defined initialize survived, leaving the other class's ivars unset. With tep's three Response classes (Tep::Response, Tep::Llm::Response, Tep::Http::Response) all mangling to one sp_Response, Tep::Response.new ran the wrong initialize, @set_cookies stayed NULL, and the first push SIGSEGV'd at boot. The legacy backend keyed class identity by the module-qualified path, so this was a rewrite regression.

Add qualify_colliding_classes, the class-definition analogue of the existing nested-constant pass (qualify_colliding_consts): collect every class/module definition with its enclosing module path, and when the same leaf name is defined under 2+ distinct paths, rename each nested definition to a qualified <Mod>__..__<Leaf> and rewrite every reference to whichever qualified class it denotes (relative reads resolve against the lexical module chain, ::-anchored from the root), reusing qc_rewrite_reads / qc_qualified_name. It runs before walk_scope, so registration and every reference lookup key on the now-distinct names and the emitted C identifier is distinct too -- no change to the many leaf-name read sites. Collision-gated: programs with unique class names (the common case, optcarrot, the self-host build) are untouched.

seed_class_index also tries the ::->__ form so an RBS seed for a renamed colliding class still resolves.

Fixes #1425 Refs #1420

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

Details

Commit
884b6f664d8e
Parent
57e6383a0c3c

src/analyze.cmodifiedView file

@@ -918,6 +918,81 @@
918918 free(ws);
919919 }
920920
921+/* ---- Colliding class/module-name qualification ------------------------
922+ * Classes/modules live in a flat sp_<Name> C namespace keyed by the leaf
923+ * name, so `Web::Response` and `Chat::Response` collapse onto one ClassInfo
924+ * (struct + methods merge, the last `initialize` wins -> uninitialized ivars
925+ * -> SIGSEGV). This is the class-definition analogue of the nested-constant
926+ * pass above: when the same class/module leaf name is defined under 2+
927+ * distinct module paths, rename each nested definition to a qualified
928+ * `<Mod>__..__<NAME>` and rewrite every reference to whichever qualified
929+ * class it denotes (relative reads prefer the lexically enclosing module
930+ * chain; `::`-anchored reads resolve from the root). Collision-gated: a
931+ * program whose class names are all unique is untouched (so the common case,
932+ * optcarrot, and the self-host build see zero change). The rewrite happens
933+ * before walk_scope, so registration (comp_class_new) and every reference
934+ * lookup (comp_class_index) naturally key on the now-distinct names, and the
935+ * emitted C identifier (sp_<name>) is distinct too -- no change to the many
936+ * leaf-name read sites. */
937+void qc_collect_class_writes(Compiler *c, int node, char (*path)[64], int depth,
938+ QCWrite **ws, int *n, int *cap) {
939+ const NodeTable *nt = c->nt;
940+ if (node < 0) return;
941+ const char *ty = nt_type(nt, node);
942+ if (!ty) return;
943+ if ((!strcmp(ty, "ModuleNode") || !strcmp(ty, "ClassNode")) && depth < QC_MAXDEPTH) {
944+ int cp = nt_ref(nt, node, "constant_path");
945+ const char *mn = cp >= 0 ? nt_str(nt, cp, "name") : NULL;
946+ if (mn) {
947+ /* record this class/module definition as a "write" at the current
948+ (pre-push) depth -- ws[i].node is the constant_path node whose name
949+ the write-rewrite pass will qualify. */
950+ if (*n >= *cap) { *cap = *cap ? *cap * 2 : 16; *ws = realloc(*ws, sizeof(QCWrite) * (size_t)*cap); }
951+ QCWrite *w = &(*ws)[(*n)++];
952+ w->node = cp; w->depth = depth;
953+ for (int i = 0; i < depth; i++) snprintf(w->path[i], 64, "%s", path[i]);
954+ snprintf(w->name, sizeof w->name, "%s", mn);
955+ snprintf(path[depth], 64, "%s", mn);
956+ depth++;
957+ }
958+ }
959+ int nr = nt_num_refs(nt, node);
960+ for (int i = 0; i < nr; i++) qc_collect_class_writes(c, nt_ref_at(nt, node, i), path, depth, ws, n, cap);
961+ int na = nt_num_arrs(nt, node);
962+ 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_class_writes(c, ids[k], path, depth, ws, n, cap); }
963+}
964+
965+void qualify_colliding_classes(Compiler *c) {
966+ const NodeTable *nt = c->nt;
967+ QCWrite *ws = NULL; int wn = 0, wcap = 0;
968+ char path[QC_MAXDEPTH][64];
969+ qc_collect_class_writes(c, nt->root_id, path, 0, &ws, &wn, &wcap);
970+ /* keep only leaf names defined under 2+ distinct module paths */
971+ int any = 0;
972+ for (int i = 0; i < wn; i++) {
973+ int collide = 0;
974+ for (int j = 0; j < wn && !collide; j++) {
975+ if (i == j || strcmp(ws[i].name, ws[j].name)) continue;
976+ if (ws[i].depth != ws[j].depth) { collide = 1; break; }
977+ for (int k = 0; k < ws[i].depth; k++) if (strcmp(ws[i].path[k], ws[j].path[k])) { collide = 1; break; }
978+ }
979+ if (!collide) { ws[i] = ws[--wn]; i--; continue; }
980+ any = 1;
981+ }
982+ if (any) {
983+ /* rewrite references first (they match against the original leaf names),
984+ then qualify the nested definitions themselves */
985+ char mods[QC_MAXDEPTH][64];
986+ qc_rewrite_reads(c, nt->root_id, mods, 0, ws, wn);
987+ for (int i = 0; i < wn; i++) {
988+ if (ws[i].depth == 0) continue;
989+ char qn[512]; qc_qualified_name(qn, sizeof qn, &ws[i]);
990+ nt_set_str((NodeTable *)nt, ws[i].node, "name", qn);
991+ }
992+ }
993+ free(ws);
994+}
995+
921996 void rename_shadowing_block_params(Compiler *c) {
922997 const NodeTable *nt = c->nt;
923998 int n = nt->count;
@@ -1046,6 +1121,16 @@
10461121 buf[j] = '\0';
10471122 int direct = comp_class_index(c, buf);
10481123 if (direct >= 0) return direct;
1124+ /* A class whose leaf name collided across modules was renamed to the
1125+ `<Mod>__..__<Leaf>` form by qualify_colliding_classes; match that too. */
1126+ char buf2[256];
1127+ size_t j2 = 0;
1128+ for (const char *p = name; *p && j2 < sizeof buf2 - 1; ) {
1129+ if (p[0] == ':' && p[1] == ':') { if (j2 + 2 < sizeof buf2) { buf2[j2++] = '_'; buf2[j2++] = '_'; } p += 2; }
1130+ else buf2[j2++] = *p++;
1131+ }
1132+ buf2[j2] = '\0';
1133+ if (strcmp(buf, buf2)) { int q2 = comp_class_index(c, buf2); if (q2 >= 0) return q2; }
10491134 /* A module-nested class (`module M; class C`) is stored under its leaf name
10501135 `C` with enclosing_class = M, but the extractor emits the qualified
10511136 `M_C`. Match against each class's reconstructed qualified name so the
@@ -1231,6 +1316,7 @@
12311316
12321317 rename_shadowing_block_params(c);
12331318 qualify_colliding_consts(c);
1319+ qualify_colliding_classes(c);
12341320 walk_scope(c, c->nt->root_id, 0, -1);
12351321 register_structs(c);
12361322 fix_struct_block_scopes(c);