Files
ww/test/wcc/699_use_promote_alias.c
Hojun-Cho 7f60ebbe44 cstage+test: SK_USE→SK_X promotion sets use_alias
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.
2026-05-15 15:06:37 +09:00

118 lines
3.8 KiB
C

/*
* 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/<name>.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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
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 <tag>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;
}