From 7f60ebbe44fa5324255132b13bfde228af60f2a6 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Fri, 15 May 2026 15:06:37 +0900 Subject: [PATCH] =?UTF-8?q?cstage+test:=20SK=5FUSE=E2=86=92SK=5FX=20promot?= =?UTF-8?q?ion=20sets=20use=5Falias?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit In cmd/wcc/check.c the pass-1.5 SK_USE→SK_DEF/SK_FN/SK_VAR promotion sites forgot to set prev->use_alias = 1 when the imported module's top-level decl shadowed the SK_USE leaf in flat scope. Downstream dot-prefixed lookups (resolve_typename L77, N_DOT L709) gate the module-head walk on (SK_USE || use_alias), so `mod.flag` resolution fell through to "unknown type". The SK_TYPE precedent at L1660 had the line; the three sister sites at L1709/L1722/L1736 now do too, in the same one-line shape and field-set order. The wwstage selfhost/cmd/wcc/check.ww uses coexistence rather than in-place promotion: SK_USE and same-leaf SK_TYPE/FN/DEF/VAR live as separate entries differentiated by sym.mod, and scopelookupinmodule's mod-filter already disambiguates dotted lookups — no use_alias flag needed, so the cstage bug is structurally non-reachable there. An architectural note at installdecl documents this divergence-by-design and warns against porting the flag (adding a field to `sym` changes its size and risks the wwstage cgen amalloc-undersize trap). Audit covered every SK_USE→SK_X promotion path in check.c (4 sites: SK_TYPE already-correct as precedent, SK_DEF/SK_FN/SK_VAR fixed). The surfacing case was lib/fnmatch: `fn fnmatch(...)` shadows the SK_USE leaf, so `fnmatch.flag` failed in worker-fnmatch's WIP — that test (972_fnmatch_run) now flips PASS as live integration proof. test/wcc/699_use_promote_alias.c pins all four rows with a single table-driven driver (type/fn/def/var → use mod; let m: mod.flag = mod.flag.A; return m: i32, expecting exit 42 per row). 995_self_rebuild byte-identity holds. --- cmd/wcc/check.c | 18 ++++ selfhost/cmd/wcc/check.ww | 14 +++ test/wcc/699_use_promote_alias.c | 117 ++++++++++++++++++++++ test/wcc/data/usepromote/defmod/defmod.ww | 13 +++ test/wcc/data/usepromote/fnmod/fnmod.ww | 17 ++++ test/wcc/data/usepromote/pos_def.ww | 12 +++ test/wcc/data/usepromote/pos_fn.ww | 15 +++ test/wcc/data/usepromote/pos_type.ww | 14 +++ test/wcc/data/usepromote/pos_var.ww | 12 +++ test/wcc/data/usepromote/typmod/typmod.ww | 17 ++++ test/wcc/data/usepromote/varmod/varmod.ww | 12 +++ 11 files changed, 261 insertions(+) create mode 100644 test/wcc/699_use_promote_alias.c create mode 100644 test/wcc/data/usepromote/defmod/defmod.ww create mode 100644 test/wcc/data/usepromote/fnmod/fnmod.ww create mode 100644 test/wcc/data/usepromote/pos_def.ww create mode 100644 test/wcc/data/usepromote/pos_fn.ww create mode 100644 test/wcc/data/usepromote/pos_type.ww create mode 100644 test/wcc/data/usepromote/pos_var.ww create mode 100644 test/wcc/data/usepromote/typmod/typmod.ww create mode 100644 test/wcc/data/usepromote/varmod/varmod.ww diff --git a/cmd/wcc/check.c b/cmd/wcc/check.c index 55c5d9f6..75cddec0 100644 --- a/cmd/wcc/check.c +++ b/cmd/wcc/check.c @@ -1707,7 +1707,13 @@ check_file(Checker *c, Node *file) Sym *prev = scope_lookup_local(c->cur, d->str); const char *mod = decl_mod(file, d); if (prev && prev->kind == SK_USE) { + /* `use mod; ... def mod = ...;` — promote the + * SK_USE to the def symbol but remember it was + * also a module name so dotted qualifiers + * (`mod.x`) keep resolving via the N_DOT path's + * use_alias branch. Mirrors L1677. */ prev->kind = SK_DEF; prev->type = t; prev->decl = d; + prev->use_alias = 1; if (mod && prev->mod == NULL) prev->mod = mod; } else if (!scope_define_in_module(c->cur, d->str, mod, SK_DEF, t, d)) @@ -1720,7 +1726,15 @@ check_file(Checker *c, Node *file) Sym *prev = scope_lookup_local(c->cur, d->str); const char *mod = decl_mod(file, d); if (prev && prev->kind == SK_USE) { + /* `use mod; ... fn mod(...) ...;` — promote + * but remember the module-alias so dotted + * qualifiers (`mod.x`) keep resolving. The + * lib/fnmatch case: `fn fnmatch(...)` shadows + * the SK_USE leaf, and without use_alias the + * dot-prefix path in resolve_typename loses + * the `fnmatch.flag` lookup. */ prev->kind = SK_FN; prev->type = t; prev->decl = d; + prev->use_alias = 1; if (mod && prev->mod == NULL) prev->mod = mod; } else if (!scope_define_in_module(c->cur, d->str, mod, SK_FN, t, d)) @@ -1734,8 +1748,12 @@ check_file(Checker *c, Node *file) Sym *prev = scope_lookup_local(c->cur, d->str); const char *mod = decl_mod(file, d); if (prev && prev->kind == SK_USE) { + /* `use mod; ... let mod: T = ...;` — + * same promote-and-alias shape as the + * SK_DEF / SK_FN cases above. */ prev->kind = SK_VAR; prev->type = t; prev->decl = d; + prev->use_alias = 1; if (mod && prev->mod == NULL) prev->mod = mod; } else scope_define_in_module(c->cur, d->str, diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index b77673e4..255c6976 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -94,6 +94,20 @@ fn declmod(file: *node, d: *node) str = { // 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. +// +// Architectural note: wwstage uses COEXISTENCE rather than the cstage +// promote-SK_USE-in-place approach in cmd/wcc/check.c. SK_USE and any +// same-leaf SK_TYPE/SK_FN/SK_DEF/SK_VAR live as separate entries in +// the same scope-bucket, distinguished by `sym.mod`. The dot-prefix +// lookup in resolvewalk + scopelookupinmodule's mod-filter already +// disambiguate `fnmatch.flag` against an `fn fnmatch(...)` of the same +// leaf — no `use_alias` flag needed. So the cstage L1722-class bug +// (promotion missing use_alias) is structurally non-reachable here. +// Don't port the use_alias flag from cstage without first re-reading +// the architecture: adding a field to `sym` changes its size and risks +// the wwstage cgen amalloc-undersize trap (rob-pike). #11 (wwstage +// checkfile pass) will reconsider this when wwstage grows a real check +// pass on the cgen path. fn installdecl(c: *checker, file: *node, d: *node) void = { if (d == nil) { return; }; let k: nkind = d.kind; diff --git a/test/wcc/699_use_promote_alias.c b/test/wcc/699_use_promote_alias.c new file mode 100644 index 00000000..8d523430 --- /dev/null +++ b/test/wcc/699_use_promote_alias.c @@ -0,0 +1,117 @@ +/* + * 699_use_promote_alias — SK_USE→SK_X promotion preserves use_alias. + * + * Sweep of every reachable promotion site in cmd/wcc/check.c where an + * SK_USE leaf gets in-place-promoted to a same-named decl. With driver + * concatenation, the imported module's top-level decls appear in + * file.list BEFORE the primary's `use foo;`, so pass-1 (N_USE + + * N_TYPEDECL only) installs an SK_USE("foo") that pass-1.5 later + * promotes when it walks the imported N_DEF / N_FNDECL / N_LET of the + * same name. Each promotion must set `prev->use_alias = 1` so the + * dot-prefixed lookups in resolve_typename (L77) and N_DOT (L709) + * continue to walk the leaf as a module head. + * + * The fnmatch.flag-style "unknown type" failure that surfaced this is + * the SK_FN row; the SK_TYPE row pins the random.random precedent + * against regression; SK_DEF / SK_VAR are the same structural shape + * with `def` / top-level `let` standing in for `fn`. + * + * row | imported decl shape | path | check.c line + * -----+-----------------------------+----------+------------- + * type | `type typmod = struct {...}`| L1660/77 | regression pin + * fn | `fn fnmod() i32 = ...` | L1722 | the bug + * def | `def defmod: i32 = 0` | L1709 | same shape + * var | `let varmod: i32 = 0` | L1736 | same shape + * + * Fixtures: test/wcc/data/usepromote/{type,fn,def,var}mod/.ww + * primary: test/wcc/data/usepromote/pos_{type,fn,def,var}.ww + * + * Each primary returns `flag.A` (= 42) so a successful build + run + + * exit=42 proves the lookup chain end-to-end. Cstage-only: wwstage + * uses a coexistence-not-promotion model in selfhost/cmd/wcc/check.ww + * (separate sym entries differentiated by `mod`, no use_alias flag at + * all), so the bug is structurally non-reachable on that side — see + * the architectural note at installdecl in check.ww. Task #11 will + * revisit when wwstage grows a real check pass on the cgen path. + */ +#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_row(const char *driver, const char *fixdir, const char *tag) +{ + char src[64]; + snprintf(src, sizeof src, "pos_%s.ww", tag); + char cmd[2048]; + /* cd into the fixture dir so the driver's source-dir-first + * import search resolves `use mod;`. */ + snprintf(cmd, sizeof cmd, + "cd %s && %s build %s >/dev/null 2>&1", fixdir, driver, src); + if (runwait(cmd) != 0) { + fprintf(stderr, + "use_promote_alias[%s]: build failed — SK_USE→SK_X " + "promotion likely dropped use_alias\n", tag); + return 1; + } + char bin[2048]; + snprintf(bin, sizeof bin, "%s/pos_%s", fixdir, tag); + int got = runwait(bin); + unlink(bin); + if (got != 42) { + fprintf(stderr, + "use_promote_alias[%s]: exit=%d want=42\n", tag, got); + 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/usepromote"; + if (cwd_n + strlen(rel) + 1 >= sizeof fixdir) return 1; + memcpy(fixdir + cwd_n, rel, strlen(rel) + 1); + + int fail = 0; + fail += run_row(cdrv, fixdir, "type"); + fail += run_row(cdrv, fixdir, "fn"); + fail += run_row(cdrv, fixdir, "def"); + fail += run_row(cdrv, fixdir, "var"); + + if (fail) { + fprintf(stderr, + "use_promote_alias: %d row(s) failed\n", fail); + return 1; + } + printf("use_promote_alias: 4/4 ok\n"); + return 0; +} diff --git a/test/wcc/data/usepromote/defmod/defmod.ww b/test/wcc/data/usepromote/defmod/defmod.ww new file mode 100644 index 00000000..99278de1 --- /dev/null +++ b/test/wcc/data/usepromote/defmod/defmod.ww @@ -0,0 +1,13 @@ +// usepromote/defmod — exports a `def defmod` with the same leaf as +// the module name. Paired with pos_def.ww to exercise the SK_USE→ +// SK_DEF promotion (cmd/wcc/check.c L1709). Mirrors the SK_FN bug +// shape: without `use_alias = 1`, the consumer's `defmod.flag` lookup +// fails because the promoted-in-place SK_DEF leaf no longer advertises +// itself as a module head. + +export def defmod: i32 = 0i32; + +export type flag = enum i32 { + NONE = 0, + A = 42, +}; diff --git a/test/wcc/data/usepromote/fnmod/fnmod.ww b/test/wcc/data/usepromote/fnmod/fnmod.ww new file mode 100644 index 00000000..349151c2 --- /dev/null +++ b/test/wcc/data/usepromote/fnmod/fnmod.ww @@ -0,0 +1,17 @@ +// usepromote/fnmod — exports a `fn fnmod()` with the same leaf as the +// module name. Paired with pos_fn.ww to exercise the bug fix at +// cmd/wcc/check.c L1722 (SK_USE→SK_FN promotion was missing the +// `use_alias = 1` line, so the consumer's `fnmod.flag` lookup walked +// resolve_typename's dot branch, found head `fnmod` as SK_FN, and +// skipped the module-filtered leaf path → "unknown type fnmod.flag". +// +// lib/fnmatch is the real-world instance that surfaced this. + +export type flag = enum i32 { + NONE = 0, + A = 42, +}; + +export fn fnmod() i32 = { + return 0i32; +}; diff --git a/test/wcc/data/usepromote/pos_def.ww b/test/wcc/data/usepromote/pos_def.ww new file mode 100644 index 00000000..d1db25d4 --- /dev/null +++ b/test/wcc/data/usepromote/pos_def.ww @@ -0,0 +1,12 @@ +// SK_USE→SK_DEF promotion. Same shape as pos_fn.ww but the imported +// module exports `def defmod: i32 = 0` instead of `fn defmod()`. +// Pass-1.5 N_DEF case (check.c L1709) promotes the SK_USE leaf to +// SK_DEF. Pre-fix it forgot use_alias=1, so `defmod.flag` resolution +// failed. Post-fix the build succeeds and exit code = flag.A = 42. + +use defmod; + +fn main() i32 = { + let m: defmod.flag = defmod.flag.A; + return m: i32; +}; diff --git a/test/wcc/data/usepromote/pos_fn.ww b/test/wcc/data/usepromote/pos_fn.ww new file mode 100644 index 00000000..3ac98ded --- /dev/null +++ b/test/wcc/data/usepromote/pos_fn.ww @@ -0,0 +1,15 @@ +// SK_USE→SK_FN promotion (the bug). fnmod exports `fn fnmod()` and +// `type flag = enum`. Pass-1 installs SK_TYPE("flag", mod="fnmod") and +// SK_USE("fnmod") (the `fn fnmod` isn't installed in pass-1; pass-1 +// only handles N_USE + N_TYPEDECL). Pass-1.5 then sees the N_FNDECL +// "fnmod", finds the SK_USE local, and promotes in place to SK_FN. +// Pre-fix that promotion forgot `use_alias = 1`, so `fnmod.flag` +// resolution failed with "unknown type fnmod.flag". Post-fix the +// build succeeds and exit code = flag.A = 42. + +use fnmod; + +fn main() i32 = { + let m: fnmod.flag = fnmod.flag.A; + return m: i32; +}; diff --git a/test/wcc/data/usepromote/pos_type.ww b/test/wcc/data/usepromote/pos_type.ww new file mode 100644 index 00000000..39bbe4e8 --- /dev/null +++ b/test/wcc/data/usepromote/pos_type.ww @@ -0,0 +1,14 @@ +// SK_USE→SK_TYPE regression pin. typmod exports `type typmod = ...` +// AND `type flag = enum`. Driver concatenation puts imported decls +// first, so pass-1 installs SK_TYPE("typmod") first; the primary's +// `use typmod` then hits the self-import branch (check.c L1660) and +// sets use_alias=1 on the SK_TYPE entry. We rely on use_alias=1 here +// so the dot-prefixed `typmod.flag` lookup resolves to mod="typmod"'s +// flag entry. Exit code = flag.A = 42 verifies end-to-end. + +use typmod; + +fn main() i32 = { + let m: typmod.flag = typmod.flag.A; + return m: i32; +}; diff --git a/test/wcc/data/usepromote/pos_var.ww b/test/wcc/data/usepromote/pos_var.ww new file mode 100644 index 00000000..08d8a52b --- /dev/null +++ b/test/wcc/data/usepromote/pos_var.ww @@ -0,0 +1,12 @@ +// SK_USE→SK_VAR promotion. Imported module exports a top-level +// `let varmod: i32 = 0`. Pass-1.5 N_LET case (check.c L1736) promotes +// the SK_USE leaf to SK_VAR. Pre-fix it forgot use_alias=1, so +// `varmod.flag` resolution failed. Post-fix the build succeeds and +// exit code = flag.A = 42. + +use varmod; + +fn main() i32 = { + let m: varmod.flag = varmod.flag.A; + return m: i32; +}; diff --git a/test/wcc/data/usepromote/typmod/typmod.ww b/test/wcc/data/usepromote/typmod/typmod.ww new file mode 100644 index 00000000..995148d4 --- /dev/null +++ b/test/wcc/data/usepromote/typmod/typmod.ww @@ -0,0 +1,17 @@ +// usepromote/typmod — exports a `type typmod` with the same leaf as +// the module name. Paired with pos_type.ww to pin the SK_USE→SK_TYPE +// promotion's use_alias=1 behaviour (cmd/wcc/check.c L1660 / L1677): +// the consumer's `typmod.flag` lookup must walk through the alias +// even though the leaf sym is SK_TYPE, not SK_USE. +// +// random.random in lib/ is the canonical real-world instance of this +// shape; this fixture replays it as a regression pin. + +export type typmod = struct { + x: i32, +}; + +export type flag = enum i32 { + NONE = 0, + A = 42, +}; diff --git a/test/wcc/data/usepromote/varmod/varmod.ww b/test/wcc/data/usepromote/varmod/varmod.ww new file mode 100644 index 00000000..bc1c0b5d --- /dev/null +++ b/test/wcc/data/usepromote/varmod/varmod.ww @@ -0,0 +1,12 @@ +// usepromote/varmod — exports a top-level `let varmod` with the same +// leaf as the module name. Paired with pos_var.ww to exercise the +// SK_USE→SK_VAR promotion at cmd/wcc/check.c L1736. Same shape as +// SK_DEF / SK_FN — without `use_alias = 1`, the consumer's +// `varmod.flag` lookup fails. + +export let varmod: i32 = 0i32; + +export type flag = enum i32 { + NONE = 0, + A = 42, +};