/* * 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 #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) { /* Keep the output and its caller-owned `.sepwork/` in one * invocation-owned directory outside the repository. */ char td[64]; snprintf(td, sizeof td, "/tmp/usepr_%d_%s", getpid(), tag); if (mkdir(td, 0755) != 0) { fprintf(stderr, "use_promote_alias[%s]: mkdir %s: %s\n", tag, td, strerror(errno)); return 1; } char src[64]; snprintf(src, sizeof src, "pos_%s.ww", tag); char bin[2048], sepdir[2048], rmcmd[2112]; snprintf(bin, sizeof bin, "%s/out", td); snprintf(sepdir, sizeof sepdir, "%s.sepwork", bin); snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", sepdir); char cmd[2048]; int failed = 0, got; /* cd into the fixture dir so the driver's source-dir-first * import search resolves `use mod;`; -o points OUT of the repo. */ snprintf(cmd, sizeof cmd, "cd %s && %s build -o %s %s >/dev/null 2>&1", fixdir, driver, bin, src); if (runwait(cmd) != 0) { fprintf(stderr, "use_promote_alias[%s]: build failed — SK_USE→SK_X " "promotion likely dropped use_alias\n", tag); failed = 1; goto cleanup; } got = runwait(bin); if (got != 42) { fprintf(stderr, "use_promote_alias[%s]: exit=%d want=42\n", tag, got); failed = 1; } cleanup: { int cleanup_failed = 0; if (runwait(rmcmd) != 0) { fprintf(stderr, "use_promote_alias[%s]: remove %s failed\n", tag, sepdir); cleanup_failed = 1; } if (unlink(bin) != 0 && errno != ENOENT) { fprintf(stderr, "use_promote_alias[%s]: unlink %s: %s\n", tag, bin, strerror(errno)); cleanup_failed = 1; } if (rmdir(td) != 0) { fprintf(stderr, "use_promote_alias[%s]: rmdir %s: %s\n", tag, td, strerror(errno)); cleanup_failed = 1; } if (!failed && cleanup_failed) failed = 1; } return failed; } 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; }