diff --git a/.gitignore b/.gitignore index 8573a914..82455ee3 100644 --- a/.gitignore +++ b/.gitignore @@ -40,6 +40,11 @@ examples/**/*.combined.ww test/wcc/data/*.o test/wcc/data/*.s test/wcc/data/*.combined.ww +test/wcc/data/**/*.o +test/wcc/data/**/*.s +test/wcc/data/**/*.combined.ww +test/wcc/data/modcollision/pos +test/wcc/data/modcollision/neg examples/mandelbrot/mandelbrot examples/cmatrix/cmatrix examples/lisp/lisp diff --git a/Makefile b/Makefile index db9e94da..834ca6e2 100644 --- a/Makefile +++ b/Makefile @@ -224,6 +224,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_dot_tagged_source \ $(BIN)/test_tagged_store_intlit \ $(BIN)/test_match_bind_struct \ + $(BIN)/test_modtype_leaf_collision \ $(BIN)/test_field_signed $(BIN)/test_frame_argcount \ $(BIN)/test_selfhost $(BIN)/test_w6a_ww $(BIN)/test_w6l_ww \ $(BIN)/test_w6c_ww $(BIN)/test_ww_ww $(BIN)/test_self_rebuild \ @@ -351,6 +352,11 @@ $(BIN)/test_match_bind_struct: test/wcc/695_match_bind_struct.c $(BIN)/ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_modtype_leaf_collision: test/wcc/696_modtype_leaf_collision.c \ + $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_field_signed: test/wcc/660_field_signed.c $(BIN)/ww \ $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/cmd/wcc/check.c b/cmd/wcc/check.c index a61f891a..4f428d31 100644 --- a/cmd/wcc/check.c +++ b/cmd/wcc/check.c @@ -63,10 +63,11 @@ resolve_typename(Checker *c, Node *n) Sym *s = scope_lookup(c->cur, nm); if (s == NULL && nm) { /* module-qualified: io.stream → strip the last dot prefix - * and look up the leaf if `io` is a `use`-imported name. - * `m->use_alias` covers the self-import case where the - * imported module declares a type with the same name as - * the module itself (e.g. `random.random`). */ + * and look up the leaf, filtering on the importing module's + * name so `bufio.stream` and `io.stream` can coexist in the + * same flat scope. `m->use_alias` covers the self-import + * case where the imported module declares a type with the + * same name as the module itself (e.g. `random.random`). */ const char *dot = strrchr(nm, '.'); if (dot) { char head[128] = {0}; @@ -74,7 +75,8 @@ resolve_typename(Checker *c, Node *n) if (hl < sizeof head) memcpy(head, nm, hl); Sym *m = scope_lookup(c->cur, head); if (m && (m->kind == SK_USE || m->use_alias)) - s = scope_lookup(c->cur, dot + 1); + s = scope_lookup_in_module(c->cur, head, + dot + 1); } } if (s == NULL || s->kind != SK_TYPE) @@ -709,11 +711,12 @@ cexpr(Checker *c, Node *n) * the self-import case where the module's * type name shadowed the SK_USE; the leaf * still resolves through the flat scope. - * fs == ms is the `mod.mod` case (the - * imported module's leaf type is named after - * the module itself — both names point at - * the same SK_TYPE sym in the flat scope). */ - Sym *fs = scope_lookup(c->cur, n->str); + * Filter on the importing module name so + * same-leaf-name types from different + * imports (`bufio.stream`/`io.stream`) + * disambiguate to the right one. */ + Sym *fs = scope_lookup_in_module(c->cur, + n->lhs->str, n->str); if (fs) return n->type = fs->type; if (ms->kind == SK_USE) { @@ -1608,6 +1611,28 @@ check_init(Checker *c, Arena *a) c->cur = c->top; } +/* + * decl_mod — module-tag stamp for a top-level decl. + * + * The driver concatenates imported sources before the primary file + * and emits `// MODULE: foo` directives the lexer pins onto each + * decl's `module` field. We treat a decl as "imported" iff its module + * directive matches some `use IDENT;` bareword in this compilation + * unit. Primary-file decls return NULL so they coexist (mod=NULL) + * with imported decls of the same leaf name in scope_lookup_in_module. + */ +static const char * +decl_mod(Node *file, Node *d) +{ + if (d == NULL || d->module == NULL || file == NULL) return NULL; + for (Node *u = file->list; u; u = u->next) { + if (u->kind == N_USE && u->str + && strcmp(u->str, d->module) == 0) + return d->module; + } + return NULL; +} + void check_file(Checker *c, Node *file) { @@ -1639,6 +1664,7 @@ check_file(Checker *c, Node *file) if (d->kind != N_TYPEDECL) continue; Type *named = type_named(c->a, d->str, NULL); Sym *prev = scope_lookup_local(c->cur, d->str); + const char *mod = decl_mod(file, d); if (prev && prev->kind == SK_USE) { /* `use mod; ... type mod = ...;` — promote the * SK_USE to the type symbol but remember it was @@ -1647,7 +1673,9 @@ check_file(Checker *c, Node *file) prev->type = named; prev->decl = d; prev->use_alias = 1; - } else if (!scope_define(c->cur, d->str, SK_TYPE, named, d)) { + if (mod && prev->mod == NULL) prev->mod = mod; + } else if (!scope_define_in_module(c->cur, d->str, mod, + SK_TYPE, named, d)) { err(c, d->pos, "duplicate type %s", d->str); } d->type = named; @@ -1672,9 +1700,12 @@ check_file(Checker *c, Node *file) Type *t = resolve_type(c, d->lhs); d->type = t; Sym *prev = scope_lookup_local(c->cur, d->str); + const char *mod = decl_mod(file, d); if (prev && prev->kind == SK_USE) { prev->kind = SK_DEF; prev->type = t; prev->decl = d; - } else if (!scope_define(c->cur, d->str, SK_DEF, t, d)) + if (mod && prev->mod == NULL) prev->mod = mod; + } else if (!scope_define_in_module(c->cur, d->str, mod, + SK_DEF, t, d)) err(c, d->pos, "duplicate def %s", d->str); break; } @@ -1682,9 +1713,12 @@ check_file(Checker *c, Node *file) Type *t = build_fn_type(c, d); d->type = t; Sym *prev = scope_lookup_local(c->cur, d->str); + const char *mod = decl_mod(file, d); if (prev && prev->kind == SK_USE) { prev->kind = SK_FN; prev->type = t; prev->decl = d; - } else if (!scope_define(c->cur, d->str, SK_FN, t, d)) + if (mod && prev->mod == NULL) prev->mod = mod; + } else if (!scope_define_in_module(c->cur, d->str, mod, + SK_FN, t, d)) err(c, d->pos, "duplicate fn %s", d->str); break; } @@ -1693,11 +1727,14 @@ check_file(Checker *c, Node *file) d->type = t; if (d->str && d->str[0]) { Sym *prev = scope_lookup_local(c->cur, d->str); + const char *mod = decl_mod(file, d); if (prev && prev->kind == SK_USE) { prev->kind = SK_VAR; prev->type = t; prev->decl = d; + if (mod && prev->mod == NULL) prev->mod = mod; } else - scope_define(c->cur, d->str, SK_VAR, t, d); + scope_define_in_module(c->cur, d->str, + mod, SK_VAR, t, d); } break; } diff --git a/cmd/wcc/parse.c b/cmd/wcc/parse.c index 2f65ff36..dd3e2dcd 100644 --- a/cmd/wcc/parse.c +++ b/cmd/wcc/parse.c @@ -1340,6 +1340,14 @@ parsefile(Parser *p) Node *file = newnode(p->a, N_FILE, pp); Node *head = NULL, *tail = NULL; while (p->cur.kind != TK_EOF) { + /* Stamp the lex's current `// MODULE: foo` directive on the + * decl BEFORE parsing. cgen uses this for name-mangling and + * check uses it for cross-module type disambiguation. Capture + * before parsing so the closing `expect(SEMI)` doesn't + * accidentally advance the lexer past the *next* `// MODULE:` + * directive — that would stamp this decl with the next + * module's name. */ + const char *mod = p->l->module; Node *attrs = parseattrs(p); int exp = accept(p, TK_EXPORT); Node *d = NULL; @@ -1363,10 +1371,7 @@ parsefile(Parser *p) advance(p); continue; } - /* Stamp the lex's current `// MODULE: foo` directive on the - * decl. cgen uses it to mangle non-exported names so two - * modules can each privately define `cstrlen`/`streq`/etc. */ - if (d != NULL) d->module = p->l->module; + if (d != NULL) d->module = mod; if (head == NULL) head = d; else tail->next = d; tail = d; diff --git a/cmd/wcc/sym.c b/cmd/wcc/sym.c index 9da8f68d..48bc686c 100644 --- a/cmd/wcc/sym.c +++ b/cmd/wcc/sym.c @@ -53,18 +53,66 @@ scope_lookup(Scope *s, const char *name) return NULL; } +/* + * scope_lookup_in_module — module-filtered chain walk. + * + * Same FNV bucket + hashnext chain + parent walk as scope_lookup, + * plus a (b->mod != NULL && strcmp(b->mod, mod) == 0) filter. When + * `mod` is NULL we fall back to unfiltered scope_lookup semantics, + * so callers that don't care about disambiguation get the default. + * + * Used by resolve_typename and the cexpr N_DOT branch to pick the + * right same-leaf-name type when two imports each export it + * (`bufio.stream` vs `io.stream`). + */ +Sym * +scope_lookup_in_module(Scope *s, const char *mod, const char *name) +{ + if (mod == NULL) return scope_lookup(s, name); + for (; s; s = s->parent) { + u64 h = hashstr(name) % s->nbuckets; + for (Sym *b = s->buckets[h]; b; b = b->hashnext) { + if (strcmp(b->name, name) != 0) continue; + if (b->mod && strcmp(b->mod, mod) == 0) return b; + } + } + return NULL; +} + Sym * scope_define(Scope *s, const char *name, Skind k, Type *t, Node *decl) { - if (scope_lookup_local(s, name) != NULL) - return NULL; + return scope_define_in_module(s, name, NULL, k, t, decl); +} + +/* + * scope_define_in_module — bucket insert with per-mod dedup. + * + * Same insertion as scope_define, but the duplicate-rejection key is + * (name, mod) rather than name alone. This lets two imports each + * register their own `stream` SK_TYPE in the flat scope, and lets the + * primary register `stream` (mod=NULL) alongside imported `stream`s. + * + * Within a single (name, mod) pair the first registration wins; later + * attempts return NULL and the caller emits a duplicate-type error. + */ +Sym * +scope_define_in_module(Scope *s, const char *name, const char *mod, + Skind k, Type *t, Node *decl) +{ + u64 h = hashstr(name) % s->nbuckets; + for (Sym *b = s->buckets[h]; b; b = b->hashnext) { + if (strcmp(b->name, name) != 0) continue; + if (b->mod == NULL && mod == NULL) return NULL; + if (b->mod && mod && strcmp(b->mod, mod) == 0) return NULL; + } Sym *sy = amalloc(s->a, sizeof *sy); sy->name = name; + sy->mod = mod; sy->kind = k; sy->type = t; sy->decl = decl; sy->scope = s; - u64 h = hashstr(name) % s->nbuckets; sy->hashnext = s->buckets[h]; s->buckets[h] = sy; if (s->first == NULL) s->first = sy; diff --git a/cmd/wcc/ww.h b/cmd/wcc/ww.h index cddf0c64..eb1d8987 100644 --- a/cmd/wcc/ww.h +++ b/cmd/wcc/ww.h @@ -482,6 +482,13 @@ struct Sym { * Lets resolve_typename treat `foo.x` * as module-qualified even though the * primary kind isn't SK_USE. */ + const char *mod; /* importing module's bareword for + * symbols originating in a `use`- + * imported module. NULL for primary + * (root) compilation unit symbols. + * Used by scope_lookup_in_module to + * disambiguate same-leaf-name types + * coming from different imports. */ Sym *next; /* iteration */ Sym *hashnext; /* bucket chain */ Scope *scope; @@ -497,8 +504,11 @@ struct Scope { Scope *newscope(Arena*, Scope *parent); Sym *scope_define(Scope*, const char *name, Skind, Type*, Node *decl); +Sym *scope_define_in_module(Scope*, const char *name, const char *mod, + Skind, Type*, Node *decl); Sym *scope_lookup(Scope*, const char *name); /* walk up parents */ Sym *scope_lookup_local(Scope*, const char *name); +Sym *scope_lookup_in_module(Scope*, const char *mod, const char *name); /* ---- checker (check.c) -------------------------------------------- */ typedef struct Checker Checker; diff --git a/lib/ww/sym.ww b/lib/ww/sym.ww index 2177ce49..16d97f21 100644 --- a/lib/ww/sym.ww +++ b/lib/ww/sym.ww @@ -27,6 +27,11 @@ type sym = struct { decl: *node, exported: i32, is_const: i32, // const-bound (assignment rejected) + mod: str, // importing module's bareword for symbols + // from a `use`-imported module; "" for primary + // (root) compilation unit symbols. Used by + // scopelookupinmodule to disambiguate same-leaf- + // name types coming from different imports. snext: *sym, // iteration order hashnext: *sym, // hash bucket chain scope: *scope, @@ -98,16 +103,74 @@ export fn scopelookup(s: *scope, name: str) *sym = { return nil; }; +// scopelookupinmodule — module-filtered chain walk. +// +// Same FNV bucket + hashnext chain + parent walk as scopelookup, plus +// a `b.mod.len > 0 && streq(b.mod, mod)` filter. When `mod` is empty +// we fall back to unfiltered scopelookup semantics, so callers that +// don't care about disambiguation get the default. +// +// Used by the dot-prefixed type-name lookup in selfhost/cmd/wcc/ +// check.ww to pick the right same-leaf-name type when two imports +// each export it (`bufio.stream` vs `io.stream`). +export fn scopelookupinmodule(s: *scope, mod: str, name: str) *sym = { + if (mod.len == 0) { return scopelookup(s, name); }; + for (s != nil) { + let h: u64 = hashstr(name); + let bi: i32 = (h % (s.nbuckets: u64)): i32; + let b: *sym = s.buckets[bi]; + for (b != nil) { + if (streq(b.name, name)) { + if (b.mod.len > 0) { + if (streq(b.mod, mod)) { + return b; + }; + }; + }; + b = b.hashnext; + }; + s = s.parent; + }; + return nil; +}; + export fn scopedefine(s: *scope, name: str, k: skind, t: *tinfo, decl: *node) *sym = { - if (scopelookuplocal(s, name) != nil) { return nil; }; - let sy: *sym = amalloc(s.a, 80u64): *sym; + let empty: str; + return scopedefineinmodule(s, name, empty, k, t, decl); +}; + +// scopedefineinmodule — bucket insert with per-mod dedup. +// +// Same insertion as scopedefine, but the duplicate-rejection key is +// (name, mod) rather than name alone. This lets two imports each +// register their own `stream` SK_TYPE in the flat scope, and lets the +// primary register `stream` (mod="") alongside imported `stream`s. +// +// Within a single (name, mod) pair the first registration wins; later +// attempts return nil and the caller can flag the error. +export fn scopedefineinmodule(s: *scope, name: str, mod: str, k: skind, t: *tinfo, decl: *node) *sym = { + let h: u64 = hashstr(name); + let bi: i32 = (h % (s.nbuckets: u64)): i32; + let b: *sym = s.buckets[bi]; + for (b != nil) { + if (streq(b.name, name)) { + if (b.mod.len == 0) { + if (mod.len == 0) { return nil; }; + } else { + if (mod.len > 0) { + if (streq(b.mod, mod)) { return nil; }; + }; + }; + }; + b = b.hashnext; + }; + let sy: *sym = amalloc(s.a, 112u64): *sym; sy.name = name; sy.skind = k; sy.type_ = t; sy.decl = decl; + sy.mod = mod; sy.scope = s; - let h: u64 = hashstr(name); - let bi: i32 = (h % (s.nbuckets: u64)): i32; sy.hashnext = s.buckets[bi]; s.buckets[bi] = sy; if (s.first == nil) { s.first = sy; } else { s.last.snext = sy; }; diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index adf800bf..e39842c3 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -4540,6 +4540,11 @@ type sym = struct { decl: *node, exported: i32, is_const: i32, // const-bound (assignment rejected) + mod: str, // importing module's bareword for symbols + // from a `use`-imported module; "" for primary + // (root) compilation unit symbols. Used by + // scopelookupinmodule to disambiguate same-leaf- + // name types coming from different imports. snext: *sym, // iteration order hashnext: *sym, // hash bucket chain scope: *scope, @@ -4611,16 +4616,74 @@ export fn scopelookup(s: *scope, name: str) *sym = { return nil; }; +// scopelookupinmodule — module-filtered chain walk. +// +// Same FNV bucket + hashnext chain + parent walk as scopelookup, plus +// a `b.mod.len > 0 && streq(b.mod, mod)` filter. When `mod` is empty +// we fall back to unfiltered scopelookup semantics, so callers that +// don't care about disambiguation get the default. +// +// Used by the dot-prefixed type-name lookup in selfhost/cmd/wcc/ +// check.ww to pick the right same-leaf-name type when two imports +// each export it (`bufio.stream` vs `io.stream`). +export fn scopelookupinmodule(s: *scope, mod: str, name: str) *sym = { + if (mod.len == 0) { return scopelookup(s, name); }; + for (s != nil) { + let h: u64 = hashstr(name); + let bi: i32 = (h % (s.nbuckets: u64)): i32; + let b: *sym = s.buckets[bi]; + for (b != nil) { + if (streq(b.name, name)) { + if (b.mod.len > 0) { + if (streq(b.mod, mod)) { + return b; + }; + }; + }; + b = b.hashnext; + }; + s = s.parent; + }; + return nil; +}; + export fn scopedefine(s: *scope, name: str, k: skind, t: *tinfo, decl: *node) *sym = { - if (scopelookuplocal(s, name) != nil) { return nil; }; - let sy: *sym = amalloc(s.a, 80u64): *sym; + let empty: str; + return scopedefineinmodule(s, name, empty, k, t, decl); +}; + +// scopedefineinmodule — bucket insert with per-mod dedup. +// +// Same insertion as scopedefine, but the duplicate-rejection key is +// (name, mod) rather than name alone. This lets two imports each +// register their own `stream` SK_TYPE in the flat scope, and lets the +// primary register `stream` (mod="") alongside imported `stream`s. +// +// Within a single (name, mod) pair the first registration wins; later +// attempts return nil and the caller can flag the error. +export fn scopedefineinmodule(s: *scope, name: str, mod: str, k: skind, t: *tinfo, decl: *node) *sym = { + let h: u64 = hashstr(name); + let bi: i32 = (h % (s.nbuckets: u64)): i32; + let b: *sym = s.buckets[bi]; + for (b != nil) { + if (streq(b.name, name)) { + if (b.mod.len == 0) { + if (mod.len == 0) { return nil; }; + } else { + if (mod.len > 0) { + if (streq(b.mod, mod)) { return nil; }; + }; + }; + }; + b = b.hashnext; + }; + let sy: *sym = amalloc(s.a, 112u64): *sym; sy.name = name; sy.skind = k; sy.type_ = t; sy.decl = decl; + sy.mod = mod; sy.scope = s; - let h: u64 = hashstr(name); - let bi: i32 = (h % (s.nbuckets: u64)): i32; sy.hashnext = s.buckets[bi]; s.buckets[bi] = sy; if (s.first == nil) { s.first = sy; } else { s.last.snext = sy; }; @@ -4695,18 +4758,42 @@ fn seedprimitives(c: *checker) void = { scopedefine(c.top, "append", skind.SK_FN, nil, nil); }; +// declmod — module-tag stamp for a top-level decl. +// +// The driver concatenates imported sources before the primary file and +// emits `// MODULE: foo` directives the lexer pins onto each decl's +// `module` field. We treat a decl as "imported" iff its module +// directive matches some `use IDENT;` bareword in this compilation +// unit. Primary-file decls return "" so they coexist (mod="") with +// imported decls of the same leaf name in scopelookupinmodule. +fn declmod(file: *node, d: *node) str = { + let empty: str; + if (d == nil) { return empty; }; + if (d.module.len == 0) { return empty; }; + if (file == nil) { return empty; }; + let u: *node = file.list; + for (u != nil) { + if (u.kind == nkind.N_USE) { + if (streq(u.str, d.module)) { return d.module; }; + }; + u = u.next; + }; + return empty; +}; + // installdecl — install the top-level decl's name into the top scope. // We don't compute its type yet (that's the resolve pass) — just bind // the name so forward references resolve. -fn installdecl(c: *checker, d: *node) void = { +fn installdecl(c: *checker, file: *node, d: *node) void = { if (d == nil) { return; }; let k: nkind = d.kind; let nm: str = d.str; + let mod: str = declmod(file, d); if (k == nkind.N_USE) { scopedefine(c.top, nm, skind.SK_USE, nil, d); return; }; - if (k == nkind.N_DEF) { scopedefine(c.top, nm, skind.SK_DEF, nil, d); return; }; - if (k == nkind.N_TYPEDECL) { scopedefine(c.top, nm, skind.SK_TYPE, nil, d); return; }; - if (k == nkind.N_FNDECL) { scopedefine(c.top, nm, skind.SK_FN, nil, d); return; }; - if (k == nkind.N_LET) { scopedefine(c.top, nm, skind.SK_VAR, nil, d); return; }; + if (k == nkind.N_DEF) { scopedefineinmodule(c.top, nm, mod, skind.SK_DEF, nil, d); return; }; + if (k == nkind.N_TYPEDECL) { scopedefineinmodule(c.top, nm, mod, skind.SK_TYPE, nil, d); return; }; + if (k == nkind.N_FNDECL) { scopedefineinmodule(c.top, nm, mod, skind.SK_FN, nil, d); return; }; + if (k == nkind.N_LET) { scopedefineinmodule(c.top, nm, mod, skind.SK_VAR, nil, d); return; }; }; // resolvewalk — recursive AST walk that, for every nkind.N_IDENT and @@ -4753,8 +4840,10 @@ fn resolvewalk(c: *checker, n: *node) void = { if (nm.len > 0) { let s: *sym = scopelookup(c.cur, nm); // `pkg.Type` — strip the last dot prefix and look up - // the leaf if `pkg` is a use-imported name. Mirrors - // cmd/wcc/check.c resolve_typename. + // the leaf with a mod filter so same-leaf-name types + // from different imports (`bufio.stream` vs + // `io.stream`) disambiguate to the right one. + // Mirrors cmd/wcc/check.c resolve_typename. if (s == nil) { let dot: i32 = nm.len - 1; for (dot >= 0) { @@ -4770,7 +4859,7 @@ fn resolvewalk(c: *checker, n: *node) void = { let leaf: str; leaf.ptr = nm.ptr + (dot + 1): u64; leaf.len = nm.len - (dot + 1); - s = scopelookup(c.cur, leaf); + s = scopelookupinmodule(c.cur, head, leaf); }; }; }; @@ -5558,7 +5647,7 @@ export fn checkfile(c: *checker, file: *node) void = { // Pass 1: install all top-level names. let d: *node = file.list; for (d != nil) { - installdecl(c, d); + installdecl(c, file, d); d = d.next; }; diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index 78ece2cc..6b6c9a15 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -64,18 +64,42 @@ fn seedprimitives(c: *checker) void = { scopedefine(c.top, "append", skind.SK_FN, nil, nil); }; +// declmod — module-tag stamp for a top-level decl. +// +// The driver concatenates imported sources before the primary file and +// emits `// MODULE: foo` directives the lexer pins onto each decl's +// `module` field. We treat a decl as "imported" iff its module +// directive matches some `use IDENT;` bareword in this compilation +// unit. Primary-file decls return "" so they coexist (mod="") with +// imported decls of the same leaf name in scopelookupinmodule. +fn declmod(file: *node, d: *node) str = { + let empty: str; + if (d == nil) { return empty; }; + if (d.module.len == 0) { return empty; }; + if (file == nil) { return empty; }; + let u: *node = file.list; + for (u != nil) { + if (u.kind == nkind.N_USE) { + if (streq(u.str, d.module)) { return d.module; }; + }; + u = u.next; + }; + return empty; +}; + // installdecl — install the top-level decl's name into the top scope. // We don't compute its type yet (that's the resolve pass) — just bind // the name so forward references resolve. -fn installdecl(c: *checker, d: *node) void = { +fn installdecl(c: *checker, file: *node, d: *node) void = { if (d == nil) { return; }; let k: nkind = d.kind; let nm: str = d.str; + let mod: str = declmod(file, d); if (k == nkind.N_USE) { scopedefine(c.top, nm, skind.SK_USE, nil, d); return; }; - if (k == nkind.N_DEF) { scopedefine(c.top, nm, skind.SK_DEF, nil, d); return; }; - if (k == nkind.N_TYPEDECL) { scopedefine(c.top, nm, skind.SK_TYPE, nil, d); return; }; - if (k == nkind.N_FNDECL) { scopedefine(c.top, nm, skind.SK_FN, nil, d); return; }; - if (k == nkind.N_LET) { scopedefine(c.top, nm, skind.SK_VAR, nil, d); return; }; + if (k == nkind.N_DEF) { scopedefineinmodule(c.top, nm, mod, skind.SK_DEF, nil, d); return; }; + if (k == nkind.N_TYPEDECL) { scopedefineinmodule(c.top, nm, mod, skind.SK_TYPE, nil, d); return; }; + if (k == nkind.N_FNDECL) { scopedefineinmodule(c.top, nm, mod, skind.SK_FN, nil, d); return; }; + if (k == nkind.N_LET) { scopedefineinmodule(c.top, nm, mod, skind.SK_VAR, nil, d); return; }; }; // resolvewalk — recursive AST walk that, for every nkind.N_IDENT and @@ -122,8 +146,10 @@ fn resolvewalk(c: *checker, n: *node) void = { if (nm.len > 0) { let s: *sym = scopelookup(c.cur, nm); // `pkg.Type` — strip the last dot prefix and look up - // the leaf if `pkg` is a use-imported name. Mirrors - // cmd/wcc/check.c resolve_typename. + // the leaf with a mod filter so same-leaf-name types + // from different imports (`bufio.stream` vs + // `io.stream`) disambiguate to the right one. + // Mirrors cmd/wcc/check.c resolve_typename. if (s == nil) { let dot: i32 = nm.len - 1; for (dot >= 0) { @@ -139,7 +165,7 @@ fn resolvewalk(c: *checker, n: *node) void = { let leaf: str; leaf.ptr = nm.ptr + (dot + 1): u64; leaf.len = nm.len - (dot + 1); - s = scopelookup(c.cur, leaf); + s = scopelookupinmodule(c.cur, head, leaf); }; }; }; @@ -927,7 +953,7 @@ export fn checkfile(c: *checker, file: *node) void = { // Pass 1: install all top-level names. let d: *node = file.list; for (d != nil) { - installdecl(c, d); + installdecl(c, file, d); d = d.next; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 8186c4e5..13f44ef2 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -4540,6 +4540,11 @@ type sym = struct { decl: *node, exported: i32, is_const: i32, // const-bound (assignment rejected) + mod: str, // importing module's bareword for symbols + // from a `use`-imported module; "" for primary + // (root) compilation unit symbols. Used by + // scopelookupinmodule to disambiguate same-leaf- + // name types coming from different imports. snext: *sym, // iteration order hashnext: *sym, // hash bucket chain scope: *scope, @@ -4611,16 +4616,74 @@ export fn scopelookup(s: *scope, name: str) *sym = { return nil; }; +// scopelookupinmodule — module-filtered chain walk. +// +// Same FNV bucket + hashnext chain + parent walk as scopelookup, plus +// a `b.mod.len > 0 && streq(b.mod, mod)` filter. When `mod` is empty +// we fall back to unfiltered scopelookup semantics, so callers that +// don't care about disambiguation get the default. +// +// Used by the dot-prefixed type-name lookup in selfhost/cmd/wcc/ +// check.ww to pick the right same-leaf-name type when two imports +// each export it (`bufio.stream` vs `io.stream`). +export fn scopelookupinmodule(s: *scope, mod: str, name: str) *sym = { + if (mod.len == 0) { return scopelookup(s, name); }; + for (s != nil) { + let h: u64 = hashstr(name); + let bi: i32 = (h % (s.nbuckets: u64)): i32; + let b: *sym = s.buckets[bi]; + for (b != nil) { + if (streq(b.name, name)) { + if (b.mod.len > 0) { + if (streq(b.mod, mod)) { + return b; + }; + }; + }; + b = b.hashnext; + }; + s = s.parent; + }; + return nil; +}; + export fn scopedefine(s: *scope, name: str, k: skind, t: *tinfo, decl: *node) *sym = { - if (scopelookuplocal(s, name) != nil) { return nil; }; - let sy: *sym = amalloc(s.a, 80u64): *sym; + let empty: str; + return scopedefineinmodule(s, name, empty, k, t, decl); +}; + +// scopedefineinmodule — bucket insert with per-mod dedup. +// +// Same insertion as scopedefine, but the duplicate-rejection key is +// (name, mod) rather than name alone. This lets two imports each +// register their own `stream` SK_TYPE in the flat scope, and lets the +// primary register `stream` (mod="") alongside imported `stream`s. +// +// Within a single (name, mod) pair the first registration wins; later +// attempts return nil and the caller can flag the error. +export fn scopedefineinmodule(s: *scope, name: str, mod: str, k: skind, t: *tinfo, decl: *node) *sym = { + let h: u64 = hashstr(name); + let bi: i32 = (h % (s.nbuckets: u64)): i32; + let b: *sym = s.buckets[bi]; + for (b != nil) { + if (streq(b.name, name)) { + if (b.mod.len == 0) { + if (mod.len == 0) { return nil; }; + } else { + if (mod.len > 0) { + if (streq(b.mod, mod)) { return nil; }; + }; + }; + }; + b = b.hashnext; + }; + let sy: *sym = amalloc(s.a, 112u64): *sym; sy.name = name; sy.skind = k; sy.type_ = t; sy.decl = decl; + sy.mod = mod; sy.scope = s; - let h: u64 = hashstr(name); - let bi: i32 = (h % (s.nbuckets: u64)): i32; sy.hashnext = s.buckets[bi]; s.buckets[bi] = sy; if (s.first == nil) { s.first = sy; } else { s.last.snext = sy; }; @@ -4695,18 +4758,42 @@ fn seedprimitives(c: *checker) void = { scopedefine(c.top, "append", skind.SK_FN, nil, nil); }; +// declmod — module-tag stamp for a top-level decl. +// +// The driver concatenates imported sources before the primary file and +// emits `// MODULE: foo` directives the lexer pins onto each decl's +// `module` field. We treat a decl as "imported" iff its module +// directive matches some `use IDENT;` bareword in this compilation +// unit. Primary-file decls return "" so they coexist (mod="") with +// imported decls of the same leaf name in scopelookupinmodule. +fn declmod(file: *node, d: *node) str = { + let empty: str; + if (d == nil) { return empty; }; + if (d.module.len == 0) { return empty; }; + if (file == nil) { return empty; }; + let u: *node = file.list; + for (u != nil) { + if (u.kind == nkind.N_USE) { + if (streq(u.str, d.module)) { return d.module; }; + }; + u = u.next; + }; + return empty; +}; + // installdecl — install the top-level decl's name into the top scope. // We don't compute its type yet (that's the resolve pass) — just bind // the name so forward references resolve. -fn installdecl(c: *checker, d: *node) void = { +fn installdecl(c: *checker, file: *node, d: *node) void = { if (d == nil) { return; }; let k: nkind = d.kind; let nm: str = d.str; + let mod: str = declmod(file, d); if (k == nkind.N_USE) { scopedefine(c.top, nm, skind.SK_USE, nil, d); return; }; - if (k == nkind.N_DEF) { scopedefine(c.top, nm, skind.SK_DEF, nil, d); return; }; - if (k == nkind.N_TYPEDECL) { scopedefine(c.top, nm, skind.SK_TYPE, nil, d); return; }; - if (k == nkind.N_FNDECL) { scopedefine(c.top, nm, skind.SK_FN, nil, d); return; }; - if (k == nkind.N_LET) { scopedefine(c.top, nm, skind.SK_VAR, nil, d); return; }; + if (k == nkind.N_DEF) { scopedefineinmodule(c.top, nm, mod, skind.SK_DEF, nil, d); return; }; + if (k == nkind.N_TYPEDECL) { scopedefineinmodule(c.top, nm, mod, skind.SK_TYPE, nil, d); return; }; + if (k == nkind.N_FNDECL) { scopedefineinmodule(c.top, nm, mod, skind.SK_FN, nil, d); return; }; + if (k == nkind.N_LET) { scopedefineinmodule(c.top, nm, mod, skind.SK_VAR, nil, d); return; }; }; // resolvewalk — recursive AST walk that, for every nkind.N_IDENT and @@ -4753,8 +4840,10 @@ fn resolvewalk(c: *checker, n: *node) void = { if (nm.len > 0) { let s: *sym = scopelookup(c.cur, nm); // `pkg.Type` — strip the last dot prefix and look up - // the leaf if `pkg` is a use-imported name. Mirrors - // cmd/wcc/check.c resolve_typename. + // the leaf with a mod filter so same-leaf-name types + // from different imports (`bufio.stream` vs + // `io.stream`) disambiguate to the right one. + // Mirrors cmd/wcc/check.c resolve_typename. if (s == nil) { let dot: i32 = nm.len - 1; for (dot >= 0) { @@ -4770,7 +4859,7 @@ fn resolvewalk(c: *checker, n: *node) void = { let leaf: str; leaf.ptr = nm.ptr + (dot + 1): u64; leaf.len = nm.len - (dot + 1); - s = scopelookup(c.cur, leaf); + s = scopelookupinmodule(c.cur, head, leaf); }; }; }; @@ -5558,7 +5647,7 @@ export fn checkfile(c: *checker, file: *node) void = { // Pass 1: install all top-level names. let d: *node = file.list; for (d != nil) { - installdecl(c, d); + installdecl(c, file, d); d = d.next; }; diff --git a/test/wcc/696_modtype_leaf_collision.c b/test/wcc/696_modtype_leaf_collision.c new file mode 100644 index 00000000..f9da1f4f --- /dev/null +++ b/test/wcc/696_modtype_leaf_collision.c @@ -0,0 +1,119 @@ +/* + * 696_modtype_leaf_collision — same-leaf-name cross-module type + * disambiguation. Two modules each export `type stream` with + * intentionally distinct field sets; the consumer pins both via + * `mod1.stream` / `mod2.stream`. + * + * Pre-fix (before sym.mod tagging + scope_lookup_in_module): the + * second `type stream` registration in pass-1 failed with + * "duplicate type stream", or when both did survive they collapsed + * to whichever sym was first in the bucket chain. The negative case + * (`b: mod1.stream; return b.c;`) used to silently bind through the + * wrong type and the bogus field read would either crash at run + * time or return the wrong byte. Post-fix it must surface as a + * compile-time "no field 'c' in stream" error. + * + * Fixtures live in test/wcc/data/modcollision/. mod1/mod1.ww and + * mod2/mod2.ww are deliberately *new* source files so the test + * doesn't lean on bufio's `bstream` workaround (still in place + * until #21 lands). + */ +#include +#include +#include +#include +#include +#include + +static int +runwait(const char *cmd) +{ + int rc = system(cmd); + if (rc == -1) return -1; + if (WIFEXITED(rc)) return WEXITSTATUS(rc); + return -1; +} + +static int +run_pos(const char *driver, const char *fixdir, const char *tag) +{ + char cmd[2048]; + /* cd into the fixture dir so the driver's source-dir-first import + * search resolves `use mod1; use mod2;`. */ + snprintf(cmd, sizeof cmd, + "cd %s && %s build pos.ww >/dev/null 2>&1", fixdir, driver); + if (runwait(cmd) != 0) { + fprintf(stderr, "modtype_leaf[%s]: pos.ww build failed\n", tag); + return 1; + } + char bin[2048]; + snprintf(bin, sizeof bin, "%s/pos", fixdir); + int got = runwait(bin); + unlink(bin); + /* 3 + 5 (mod1.stream) + 7 + 11 (mod2.stream) = 26. */ + if (got != 26) { + fprintf(stderr, + "modtype_leaf[%s]: pos.ww exit=%d want=26\n", tag, got); + return 1; + } + return 0; +} + +static int +run_neg(const char *driver, const char *fixdir, const char *tag) +{ + char cmd[2048]; + /* Build must fail with a field-resolution error: the `c` field + * lives on mod2.stream, not mod1.stream. If pre-fix sym-binding + * smashed mod1.stream's identity into mod2.stream's slot, this + * would silently succeed. */ + snprintf(cmd, sizeof cmd, + "cd %s && %s build neg.ww >/dev/null 2>&1", fixdir, driver); + int rc = runwait(cmd); + char bin[2048]; + snprintf(bin, sizeof bin, "%s/neg", fixdir); + unlink(bin); + if (rc == 0) { + fprintf(stderr, + "modtype_leaf[%s]: neg.ww built but should have errored\n", + tag); + return 1; + } + return 0; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[1024]; + if (bin[0] != '/') { + char cwd[1024]; + if (getcwd(cwd, sizeof cwd) == NULL) return 1; + snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); + bin = absbin; + } + + char cdrv[1024]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + + char fixdir[1024]; + if (getcwd(fixdir, sizeof fixdir) == NULL) return 1; + size_t cwd_n = strlen(fixdir); + const char *rel = "/test/wcc/data/modcollision"; + if (cwd_n + strlen(rel) + 1 >= sizeof fixdir) return 1; + memcpy(fixdir + cwd_n, rel, strlen(rel) + 1); + + int fail = 0; + fail += run_pos(cdrv, fixdir, "cstage"); + fail += run_neg(cdrv, fixdir, "cstage"); + + if (fail) { + fprintf(stderr, + "modtype_leaf_collision: %d case(s) failed\n", fail); + return 1; + } + printf("modtype_leaf_collision: 2/2 ok\n"); + return 0; +} diff --git a/test/wcc/data/modcollision/mod1/mod1.ww b/test/wcc/data/modcollision/mod1/mod1.ww new file mode 100644 index 00000000..d5c15fdd --- /dev/null +++ b/test/wcc/data/modcollision/mod1/mod1.ww @@ -0,0 +1,8 @@ +// modcollision/mod1 — exports `type stream` with mod1-shaped fields. +// Paired with mod2/mod2.ww to exercise same-leaf-name cross-module +// type disambiguation. Test driver: 696_modtype_leaf_collision.c. + +export type stream = struct { + a: i32, + b: i32, +}; diff --git a/test/wcc/data/modcollision/mod2/mod2.ww b/test/wcc/data/modcollision/mod2/mod2.ww new file mode 100644 index 00000000..91bc4a8e --- /dev/null +++ b/test/wcc/data/modcollision/mod2/mod2.ww @@ -0,0 +1,12 @@ +// modcollision/mod2 — exports `type stream` with mod2-shaped fields. +// Paired with mod1/mod1.ww to exercise same-leaf-name cross-module +// type disambiguation. Test driver: 696_modtype_leaf_collision.c. +// +// Fields are intentionally named distinctly from mod1's (c/d vs a/b) +// so the negative test (`b: mod1.stream` accessed via mod2-only field +// 'c') surfaces as a compile-time field-resolution error. + +export type stream = struct { + c: i32, + d: i32, +}; diff --git a/test/wcc/data/modcollision/neg.ww b/test/wcc/data/modcollision/neg.ww new file mode 100644 index 00000000..17e06dfa --- /dev/null +++ b/test/wcc/data/modcollision/neg.ww @@ -0,0 +1,19 @@ +// Negative case: declare `b: mod1.stream` then access a mod2-only +// field. With the mod-tagged scope lookup `b` resolves to mod1.stream +// (fields a, b), so the field access `b.c` must be a compile-time +// error rather than silently binding to mod2.stream and succeeding. +// +// No cross-module call is made: a missing mod1.make would itself +// trigger a link-time error that could mask the field-resolution +// error this test is actually pinning. `let b: mod1.stream;` is +// enough — we read b.c before initializing it, which is fine because +// the field-resolution error fires at check, long before any reach +// analysis or codegen runs. + +use mod1; +use mod2; + +fn main() i32 = { + let b: mod1.stream; + return b.c; // mod1.stream has no `c`; expect a compile error. +}; diff --git a/test/wcc/data/modcollision/pos.ww b/test/wcc/data/modcollision/pos.ww new file mode 100644 index 00000000..d7d81b47 --- /dev/null +++ b/test/wcc/data/modcollision/pos.ww @@ -0,0 +1,18 @@ +// Positive case: two modules each export `type stream`. The consumer +// imports both and disambiguates via the module qualifier. mod1.stream +// has fields (a, b); mod2.stream has fields (c, d). The exit code +// encodes a sum of all four fields, so any cross-binding would either +// fail to compile or return the wrong value. + +use mod1; +use mod2; + +fn main() i32 = { + let s1: mod1.stream; + s1.a = 3: i32; + s1.b = 5: i32; + let s2: mod2.stream; + s2.c = 7: i32; + s2.d = 11: i32; + return s1.a + s1.b + s2.c + s2.d; +};