diff --git a/Makefile b/Makefile index d5509d38..2ef60386 100644 --- a/Makefile +++ b/Makefile @@ -372,7 +372,7 @@ SEP_WW_TARGETS = $(SEP_WW_TESTS:%=wwtest/%) # label-mangle runs, cross-module typecheck rejects, enumerator # capacity, directive adjacency). Compiler/driver gates like test/sep: # they run under test-compiler. -XMOD_WW_TESTS = test/xmod/collide_test.ww +XMOD_WW_TESTS = test/xmod/collide_test.ww test/xmod/m1_test.ww XMOD_WW_TARGETS = $(XMOD_WW_TESTS:%=wwtest/%) BOOTSTRAP_WRAPPER_SOURCES = test/wcc/950_selfcheck.c \ test/wcc/991_w6a_ww.c \ diff --git a/test/wcc/989_m1mangle_run.c b/test/wcc/989_m1mangle_run.c deleted file mode 100644 index 9f019bd4..00000000 --- a/test/wcc/989_m1mangle_run.c +++ /dev/null @@ -1,271 +0,0 @@ -/* - * 989_m1mangle_run — M1 (#22 + #32): path-qualified symbol mangling and - * root-unit entry detection. Table-driven, both stages (rule-10: cstage - * `ww` and wwstage `ww_ww` must AGREE and hit want_exit). - * - * Each row lays out a tiny package tree next to a root `main.ww` and - * builds+runs the root. Because a directory import path-mangles its - * symbols (`aa.bb.val` etc.), a broken hint or skip rule shows up as a - * link failure (-1) or wrong runtime value — observable end-to-end. - * - * row | proves - * -----------------+-------------------------------------------------- - * nested_call | DIRECTORY import path-mangles + the qualified-ref - * | hint resolves to the PATH (aa.bb.val), not the - * | leaf alias — cross-boundary call returns 42 - * imported_main | #32/#31: an imported pkg's `fn main` mangles - * | (aa.bb.main) instead of colliding with the bare - * | root entry; both link, root main runs → 7 - * priv_global | a module-PRIVATE value global mangles on the path - * | (aa.bb.pv) and the owning module resolves it → 9 - * single_level | control: a single-level package's symbols are - * | UNCHANGED (cc.v stays cc.v) — still resolves → 5 - * deep_nest | 2-level nest aa.bb.cc → aa.bb.cc.val path-mangle → 3 - */ -#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; -} - -struct file { const char *path; const char *content; }; - -struct row { - const char *label; - struct file files[4]; /* {NULL,NULL}-terminated package files */ - const char *root; /* root main.ww content */ - int want_exit; -}; - -static const struct row rows[] = { - { "nested_call", - { { "aa/bb/bb.ww", - "package bb;\n" - "export fn val() int = { return 42; };\n" }, - { NULL, NULL } }, - "package main;\n" - "import aa.bb;\n" - "export fn main() int = { return bb.val(); };\n", - 42 }, - - { "imported_main", - { { "aa/bb/bb.ww", - "package bb;\n" - "fn main() int = { return 7; };\n" - "export fn run() int = { return main(); };\n" }, - { NULL, NULL } }, - "package main;\n" - "import aa.bb;\n" - "export fn main() int = { return bb.run(); };\n", - 7 }, - - { "priv_global", - { { "aa/bb/bb.ww", - "package bb;\n" - "let pv: int = 9;\n" - "export fn getpv() int = { return pv; };\n" }, - { NULL, NULL } }, - "package main;\n" - "import aa.bb;\n" - "export fn main() int = { return bb.getpv(); };\n", - 9 }, - - { "single_level", - { { "cc/cc.ww", - "package cc;\n" - "export fn v() int = { return 5; };\n" }, - { NULL, NULL } }, - "package main;\n" - "import cc;\n" - "export fn main() int = { return cc.v(); };\n", - 5 }, - - { "deep_nest", - { { "aa/bb/cc/cc.ww", - "package cc;\n" - "export fn val() int = { return 3; };\n" }, - { NULL, NULL } }, - "package main;\n" - "import aa.bb.cc;\n" - "export fn main() int = { return cc.val(); };\n", - 3 }, -}; - -/* write_file — create `dir/rel` in an already-owned package tree. */ -static int -write_file(const char *dir, const char *rel, const char *content, int *acquired) -{ - char path[512]; - *acquired = 0; - snprintf(path, sizeof path, "%s/%s", dir, rel); - FILE *f = fopen(path, "wb"); - if (!f) return -1; - *acquired = 1; - int rc = fputs(content, f) == EOF ? -1 : 0; - if (fclose(f) != 0) rc = -1; - return rc; -} - -/* run_build — lay out the row's package tree + root under a temp dir, - * build+run the root via `driver`; return the binary's exit code, or -1 - * on a build failure. */ -static int -run_build(const char *driver, const struct row *r, int i, int *cleanup_failed) -{ - char dir[64], path[512], cmd[2048]; - int brc = -1, got = -2, cleanfail = 0; - int aaowned = 0, bbowned = 0, topccowned = 0, deepccowned = 0; - int fileowned[4] = { 0, 0, 0, 0 }, mainowned = 0, build_started = 0; - *cleanup_failed = 0; - snprintf(dir, sizeof dir, "/tmp/m1mangle_%d_%d", getpid(), i); - if (mkdir(dir, 0755) != 0) return -2; - if (i == 3) { - snprintf(path, sizeof path, "%s/cc", dir); - if (mkdir(path, 0755) != 0) goto cleanup; - topccowned = 1; - } else { - snprintf(path, sizeof path, "%s/aa", dir); - if (mkdir(path, 0755) != 0) goto cleanup; - aaowned = 1; - snprintf(path, sizeof path, "%s/aa/bb", dir); - if (mkdir(path, 0755) != 0) goto cleanup; - bbowned = 1; - if (i == 4) { - snprintf(path, sizeof path, "%s/aa/bb/cc", dir); - if (mkdir(path, 0755) != 0) goto cleanup; - deepccowned = 1; - } - } - - for (int k = 0; r->files[k].path; k++) - if (write_file(dir, r->files[k].path, r->files[k].content, - &fileowned[k]) != 0) - goto cleanup; - if (write_file(dir, "main.ww", r->root, &mainowned) != 0) goto cleanup; - - /* #94 sep layout: cd into the build dir so the source-dir-first search - * path finds the sibling package tree (mirrors Hare's CWD == - * module-dir); emits per-package units to main.sepwork/ and links - * the runnable binary at main. #99 (imported-pkg `fn main` mangle under - * sep) is what makes the imported_main row link here. */ - snprintf(cmd, sizeof cmd, - "cd '%s' && %s build -o main main.ww " - "2>/dev/null", dir, driver); - build_started = 1; - brc = runwait(cmd); - - got = -1; - if (brc == 0) { - char outbin[128]; - snprintf(outbin, sizeof outbin, "%s/main", dir); - got = runwait(outbin); - } - -cleanup: - if (build_started) { - snprintf(path, sizeof path, "%s/main.sepwork", dir); - snprintf(cmd, sizeof cmd, "rm -rf '%s'", path); - if (runwait(cmd) != 0) cleanfail = 1; - snprintf(path, sizeof path, "%s/main", dir); - if (unlink(path) != 0 && access(path, F_OK) == 0) cleanfail = 1; - } - if (mainowned) { - snprintf(path, sizeof path, "%s/main.ww", dir); - if (unlink(path) != 0 && access(path, F_OK) == 0) cleanfail = 1; - } - for (int k = 0; r->files[k].path; k++) { - if (!fileowned[k]) continue; - snprintf(path, sizeof path, "%s/%s", dir, r->files[k].path); - if (unlink(path) != 0 && access(path, F_OK) == 0) cleanfail = 1; - } - if (deepccowned) { - snprintf(path, sizeof path, "%s/aa/bb/cc", dir); - if (rmdir(path) != 0 && access(path, F_OK) == 0) cleanfail = 1; - } - if (topccowned) { - snprintf(path, sizeof path, "%s/cc", dir); - if (rmdir(path) != 0 && access(path, F_OK) == 0) cleanfail = 1; - } - if (bbowned) { - snprintf(path, sizeof path, "%s/aa/bb", dir); - if (rmdir(path) != 0 && access(path, F_OK) == 0) cleanfail = 1; - } - if (aaowned) { - snprintf(path, sizeof path, "%s/aa", dir); - if (rmdir(path) != 0 && access(path, F_OK) == 0) cleanfail = 1; - } - if (rmdir(dir) != 0) cleanfail = 1; - *cleanup_failed = cleanfail; - return brc == 0 ? got : -1; -} - -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], wdrv[1024]; - snprintf(cdrv, sizeof cdrv, "%s/ww", bin); - snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); - - struct { const char *name; const char *drv; int gated; } - drivers[] = { - { "cstage", cdrv, 0 }, - { "wwstage", wdrv, 1 }, - { NULL, NULL, 0 }, - }; - - int n = (int)(sizeof rows / sizeof rows[0]); - int total = 0, fail = 0; - - for (int d = 0; drivers[d].name; d++) { - if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) { - fprintf(stderr, "m1mangle_run: skip %s (no %s)\n", - drivers[d].name, drivers[d].drv); - continue; - } - for (int i = 0; i < n; i++) { - total++; - int cleanfail = 0; - int got = run_build(drivers[d].drv, &rows[i], i, - &cleanfail); - if (got != rows[i].want_exit) { - fprintf(stderr, "m1mangle_run[%s][%s]: exit=%d " - "want=%d\n", drivers[d].name, rows[i].label, - got, rows[i].want_exit); - fail++; - } - if (cleanfail) { - fprintf(stderr, "m1mangle_run[%s][%s]: temporary cleanup " - "failed\n", drivers[d].name, rows[i].label); - fail++; - } - } - } - - if (fail) { - fprintf(stderr, "m1mangle_run: %d/%d fixtures failed\n", - fail, total); - return 1; - } - printf("m1mangle_run: %d/%d ok\n", total, total); - return 0; -} diff --git a/test/wcc/989_m1usehint_run.c b/test/wcc/989_m1usehint_run.c deleted file mode 100644 index 0bd0b9b2..00000000 --- a/test/wcc/989_m1usehint_run.c +++ /dev/null @@ -1,387 +0,0 @@ -/* - * 989_m1usehint_run — M1 (#40): the cgen mangle-hint must be MODULE-scoped, - * not unit-global. When two directory-packages in the same compilation unit - * export the same leaf fn (a.math and b.math both `export fn pick`), and two - * referencing modules each `import .math` aliasing `math`, every - * `math.pick()` call must mangle to the package its OWN module imported — - * not to whichever `use` the file-global hint collected first. - * - * Pre-#40 the hint (use_hint / usehint) was file-global first-match: both - * modules' `math.pick()` resolved to the same package, so one call landed - * the wrong body and returned the wrong value — IDENTICALLY on both stages - * (a byte-id-green #263-class miscompile). Gate-visible: each module's call - * has a distinct expected value, so a mis-route changes the runtime exit. - * - * row | proves - * -----------------+-------------------------------------------------- - * leaf_collision | two same-leaf exported fns; each module's qualified - * | call mangles to its OWN import (111 / 222) -> 0 - * collision_diff | same, with mismatched signatures — the checker's - * | curmod-preferenced resolve agrees with the hint - * | (pick(int) vs pick()) -> 0 - * - * Plus a byte-id gate: cstage.s == wwstage.s on the leaf_collision unit - * (rule-10). - */ -#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; -} - -struct file { const char *path; const char *content; }; - -struct row { - const char *label; - struct file files[6]; /* {NULL,NULL}-terminated package files */ - const char *root; /* root main.ww content */ - int want_exit; -}; - -static const struct row rows[] = { - { "leaf_collision", - { { "a/math/math.ww", - "package math;\n" - "export fn pick() int = { return 111; };\n" }, - { "b/math/math.ww", - "package math;\n" - "export fn pick() int = { return 222; };\n" }, - { "one/one.ww", - "package one;\n" - "import a.math;\n" - "export fn getone() int = { return math.pick(); };\n" }, - { "two/two.ww", - "package two;\n" - "import b.math;\n" - "export fn gettwo() int = { return math.pick(); };\n" }, - { NULL, NULL } }, - "package main;\n" - "import one;\n" - "import two;\n" - "export fn main() int = {\n" - " if (one.getone() != 111) { return 1; };\n" - " if (two.gettwo() != 222) { return 2; };\n" - " return 0;\n" - "};\n", - 0 }, - - { "collision_diff", - { { "a/math/math.ww", - "package math;\n" - "export fn pick(x: int) int = { return x + 100; };\n" }, - { "b/math/math.ww", - "package math;\n" - "export fn pick() int = { return 222; };\n" }, - { "one/one.ww", - "package one;\n" - "import a.math;\n" - "export fn getone() int = { return math.pick(11); };\n" }, - { "two/two.ww", - "package two;\n" - "import b.math;\n" - "export fn gettwo() int = { return math.pick(); };\n" }, - { NULL, NULL } }, - "package main;\n" - "import one;\n" - "import two;\n" - "export fn main() int = {\n" - " if (one.getone() != 111) { return 1; };\n" - " if (two.gettwo() != 222) { return 2; };\n" - " return 0;\n" - "};\n", - 0 }, -}; - -/* write_file — create `dir/rel` in an already-owned package tree. */ -static int -write_file(const char *dir, const char *rel, const char *content, int *acquired) -{ - char path[512]; - *acquired = 0; - snprintf(path, sizeof path, "%s/%s", dir, rel); - FILE *f = fopen(path, "wb"); - if (!f) return -1; - *acquired = 1; - int rc = fputs(content, f) == EOF ? -1 : 0; - if (fclose(f) != 0) rc = -1; - return rc; -} - -struct layout_owned { - int root, a, amath, b, bmath, one, two; - int files[6], mainfile; -}; - -/* Remove exactly the files and semantic directories acquired for one row. */ -static int -cleanup_layout(const char *dir, const struct row *r, int byteid, - const struct layout_owned *owned, const int started[2]) -{ - char path[512], cmd[1024]; - int fail = 0; - if (byteid) { - const char *tags[] = { "cstage", "wwstage" }; - for (int i = 0; i < 2; i++) { - if (!started[i]) continue; - snprintf(path, sizeof path, "%s/%s.sepwork", dir, tags[i]); - snprintf(cmd, sizeof cmd, "rm -rf '%s'", path); - if (runwait(cmd) != 0) fail = 1; - snprintf(path, sizeof path, "%s/%s", dir, tags[i]); - if (unlink(path) != 0 && access(path, F_OK) == 0) fail = 1; - snprintf(path, sizeof path, "%s/%s.s", dir, tags[i]); - if (unlink(path) != 0 && access(path, F_OK) == 0) fail = 1; - } - } else { - if (started[0]) { - snprintf(path, sizeof path, "%s/main.sepwork", dir); - snprintf(cmd, sizeof cmd, "rm -rf '%s'", path); - if (runwait(cmd) != 0) fail = 1; - snprintf(path, sizeof path, "%s/main", dir); - if (unlink(path) != 0 && access(path, F_OK) == 0) fail = 1; - } - } - if (owned->mainfile) { - snprintf(path, sizeof path, "%s/main.ww", dir); - if (unlink(path) != 0 && access(path, F_OK) == 0) fail = 1; - } - for (int k = 0; r->files[k].path; k++) { - if (!owned->files[k]) continue; - snprintf(path, sizeof path, "%s/%s", dir, r->files[k].path); - if (unlink(path) != 0 && access(path, F_OK) == 0) fail = 1; - } - if (owned->amath) { - snprintf(path, sizeof path, "%s/a/math", dir); - if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1; - } - if (owned->a) { - snprintf(path, sizeof path, "%s/a", dir); - if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1; - } - if (owned->bmath) { - snprintf(path, sizeof path, "%s/b/math", dir); - if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1; - } - if (owned->b) { - snprintf(path, sizeof path, "%s/b", dir); - if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1; - } - if (owned->one) { - snprintf(path, sizeof path, "%s/one", dir); - if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1; - } - if (owned->two) { - snprintf(path, sizeof path, "%s/two", dir); - if (rmdir(path) != 0 && access(path, F_OK) == 0) fail = 1; - } - if (owned->root && rmdir(dir) != 0) fail = 1; - return fail ? -1 : 0; -} - -/* layout — acquire and lay out the row's package tree + root under `dir`. */ -static int -layout(const char *dir, const struct row *r, struct layout_owned *owned) -{ - char path[512]; - memset(owned, 0, sizeof *owned); - if (mkdir(dir, 0755) != 0) return -1; - owned->root = 1; - snprintf(path, sizeof path, "%s/a", dir); - if (mkdir(path, 0755) != 0) goto fail; - owned->a = 1; - snprintf(path, sizeof path, "%s/a/math", dir); - if (mkdir(path, 0755) != 0) goto fail; - owned->amath = 1; - snprintf(path, sizeof path, "%s/b", dir); - if (mkdir(path, 0755) != 0) goto fail; - owned->b = 1; - snprintf(path, sizeof path, "%s/b/math", dir); - if (mkdir(path, 0755) != 0) goto fail; - owned->bmath = 1; - snprintf(path, sizeof path, "%s/one", dir); - if (mkdir(path, 0755) != 0) goto fail; - owned->one = 1; - snprintf(path, sizeof path, "%s/two", dir); - if (mkdir(path, 0755) != 0) goto fail; - owned->two = 1; - for (int k = 0; r->files[k].path; k++) - if (write_file(dir, r->files[k].path, r->files[k].content, - &owned->files[k]) != 0) - goto fail; - if (write_file(dir, "main.ww", r->root, &owned->mainfile) != 0) goto fail; - return 0; - -fail: - { - int started[2] = { 0, 0 }; - if (cleanup_layout(dir, r, 0, owned, started) != 0) - fprintf(stderr, "m1usehint_run: temporary cleanup failed during setup\n"); - } - return -1; -} - -/* run_build — build+run the row's root via `driver`; return the binary's - * exit code, or -1 on a build failure. */ -static int -run_build(const char *driver, const struct row *r, int i, int *cleanup_failed) -{ - char dir[64], cmd[2048]; - struct layout_owned owned; - int started[2] = { 0, 0 }; - *cleanup_failed = 0; - snprintf(dir, sizeof dir, "/tmp/m1usehint_%d_%d", getpid(), i); - if (layout(dir, r, &owned) != 0) return -2; - - /* #93 sep layout: `-o main` emits per-unit asm under - * main.sepwork/ (each dir-pkg in its own .s); binary stays - * /main. The whole tree is removed below. */ - snprintf(cmd, sizeof cmd, - "cd '%s' && %s build -o main " - "main.ww 2>/dev/null", dir, driver); - started[0] = 1; - int brc = runwait(cmd); - - int got = -1; - if (brc == 0) { - char outbin[128]; - snprintf(outbin, sizeof outbin, "%s/main", dir); - got = runwait(outbin); - } - if (cleanup_layout(dir, r, 0, &owned, started) != 0) { - *cleanup_failed = 1; - } - return brc == 0 ? got : -1; -} - -/* byteid — build the leaf_collision unit with both drivers and cmp the - * concatenated per-package .s (rule 10). Returns 0 on byte-identical. */ -static int -byteid(const char *cdrv, const char *wdrv, const struct row *r, - int *cleanup_failed) -{ - char dir[64], cmd[2048]; - char cstem[128], wstem[128], cs[160], ws[160]; - struct layout_owned owned; - int started[2] = { 0, 0 }; - *cleanup_failed = 0; - snprintf(dir, sizeof dir, "/tmp/m1usehint_bid_%d", getpid()); - if (layout(dir, r, &owned) != 0) return -1; - snprintf(cstem, sizeof cstem, "%s/cstage", dir); - snprintf(wstem, sizeof wstem, "%s/wwstage", dir); - snprintf(cs, sizeof cs, "%s.s", cstem); - snprintf(ws, sizeof ws, "%s.s", wstem); - - /* #93 sep layout: each stage emits per-unit asm under .sepwork/ - * (a.math.s, b.math.s, one.s, two.s, __root.s). The rule-10 byte-id - * gate concats each tree's per-unit *.s (sorted glob order is - * deterministic) into a flat .s and cmps the two. */ - int rc = -1; - snprintf(cmd, sizeof cmd, - "cd '%s' && %s build -o '%s' " - "main.ww 2>/dev/null && cat '%s.sepwork/'*.s > '%s'", - dir, cdrv, cstem, cstem, cs); - started[0] = 1; - int cb = runwait(cmd); - snprintf(cmd, sizeof cmd, - "cd '%s' && %s build -o '%s' " - "main.ww 2>/dev/null && cat '%s.sepwork/'*.s > '%s'", - dir, wdrv, wstem, wstem, ws); - started[1] = 1; - int wb = runwait(cmd); - if (cb == 0 && wb == 0) { - snprintf(cmd, sizeof cmd, "cmp -s '%s' '%s'", cs, ws); - rc = runwait(cmd); - } else { - fprintf(stderr, "m1usehint_run: byte-id build failed " - "(cstage=%d wwstage=%d)\n", cb, wb); - } - if (cleanup_layout(dir, r, 1, &owned, started) != 0) { - *cleanup_failed = 1; - } - return rc; -} - -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], wdrv[1024]; - snprintf(cdrv, sizeof cdrv, "%s/ww", bin); - snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); - - struct { const char *name; const char *drv; int gated; } - drivers[] = { - { "cstage", cdrv, 0 }, - { "wwstage", wdrv, 1 }, - { NULL, NULL, 0 }, - }; - - int n = (int)(sizeof rows / sizeof rows[0]); - int total = 0, fail = 0; - - for (int d = 0; drivers[d].name; d++) { - if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) { - fprintf(stderr, "m1usehint_run: skip %s (no %s)\n", - drivers[d].name, drivers[d].drv); - continue; - } - for (int i = 0; i < n; i++) { - total++; - int cleanfail = 0; - int got = run_build(drivers[d].drv, &rows[i], i, - &cleanfail); - if (got != rows[i].want_exit) { - fprintf(stderr, "m1usehint_run[%s][%s]: exit=%d " - "want=%d\n", drivers[d].name, rows[i].label, - got, rows[i].want_exit); - fail++; - } - if (cleanfail) { - fprintf(stderr, "m1usehint_run[%s][%s]: temporary cleanup " - "failed\n", drivers[d].name, rows[i].label); - fail++; - } - } - } - - /* rule-10 byte-id gate on the leaf_collision unit. */ - if (access(wdrv, X_OK) == 0) { - total++; - int cleanfail = 0; - if (byteid(cdrv, wdrv, &rows[0], &cleanfail) != 0) { - fprintf(stderr, "m1usehint_run: cstage.s != wwstage.s " - "(byte-id break)\n"); - fail++; - } - if (cleanfail) { - fprintf(stderr, "m1usehint_run: byte-id temporary cleanup " - "failed\n"); - fail++; - } - } - - if (fail) { - fprintf(stderr, "m1usehint_run: %d/%d failed\n", fail, total); - return 1; - } - printf("m1usehint_run: %d/%d ok\n", total, total); - return 0; -} diff --git a/test/xmod/m1_test.ww b/test/xmod/m1_test.ww new file mode 100644 index 00000000..7cd0528c --- /dev/null +++ b/test/xmod/m1_test.ww @@ -0,0 +1,254 @@ +package m1_test; + +// M1 path-mangle RUN-level observers over runtime-written dir-package +// trees, both driver stages. Ports of the retired native carriers +// test/wcc/989_m1mangle_run.c and 989_m1usehint_run.c; every assertion +// preserved. The symbol-level cousin claims live in +// test/byteid/mangle_test.ww; these rows own the build/link/run legs. +// +// m1mangle (#22 + #32) rows, each a tree beside a root main.ww built +// with `build -o main main.ww` (cwd = tree, Hare CWD==module-dir +// mirror) and run: +// nested_call | dir import path-mangles; qualified-ref hint +// | resolves the PATH aa.bb.val -> exit 42 +// imported_main | imported pkg's private `fn main` mangles aa.bb.main +// | instead of colliding with the root entry -> 7 +// priv_global | module-PRIVATE value global mangles aa.bb.pv -> 9 +// single_level | control: single-level cc.v unchanged -> 5 +// deep_nest | 2-level nest aa.bb.cc.val path-mangle -> 3 +// +// m1usehint (#40) — the cgen mangle-hint must be MODULE-scoped, not +// unit-global: two dir-packages both `package math` export the same +// leaf `pick`, and each referencing module's `math.pick()` must bind +// its OWN import (a.math=111 / b.math=222; mis-route exits 1/2): +// leaf_collision | same-leaf exported fns, distinct bodies -> exit 0 +// collision_diff | same, mismatched signatures (checker curmod- +// | preference agrees with the hint) -> exit 0 +// plus the rule-10 byte-id leg: cstage vs wwstage sepwork *.s concat +// on the leaf_collision unit, byte-identical (concat order +// strengthened from the carrier's shell glob to explicit +// byte-lexicographic listdir). +// +// Dropped C machinery, not assertions: the ww_ww-absent skip gate +// (the Make target declares both drivers) and the per-path owned-flag +// cleanup ledger (testenv.clean asserts the removal). + +import os; +import os.exec; +import strings; +import testenv; +import time; + +fn fail(label: str, why: str) void = { + let m: str = strings.concat("m1 FAIL: ", label, " -- ", why, "\n"); + os.write(2, m.ptr, m.len: u64); + assert(false); +}; + +fn tmo() time.duration = { + return (240i64 * (time.second: i64)): time.duration; +}; + +// -1 encodes an abnormal (non-EXIT) termination, never a valid code. +fn runcode(dir: str, name: str, argv: []str) i32 = { + let co: testenv.commandout; + testenv.runcommand(dir, dir, name, argv, tmo(), &co); + if (co.termination != exec.termination.EXIT) { return -1; }; + return co.code; +}; + +// Every per-package .s under `sepdir`, concatenated in byte-sorted +// order; "" when the directory is missing or holds no .s. +fn catasm(sepdir: str) str = { + if (!testenv.isdir(sepdir)) { return ""; }; + let names: []str = testenv.listdir(sepdir); + let out: str = ""; + let i: i32 = 0; + for (i < names.len) { + if (strings.hassuffix(names[i], ".s")) { + out = strings.concat(out, + testenv.readfile(strings.concat(sepdir, "/", names[i]))); + }; + i += 1; + }; + return out; +}; + +fn mkdirs(td: str, dirs: []str) void = { + let d: i32 = 0; + for (d < dirs.len) { + assert(os.mkdir(strings.concat(td, "/", dirs[d]), 493) == 0); + d += 1; + }; +}; + +// ---- m1mangle (#22 + #32) ---------------------------------------------- + +// dirs are '/'-joined mkdir steps, parents before children. +fn m1row(label: str, dirs: []str, rel: str, content: str, root: str, + want: i32) void = { + let drvs: []str = ["ww", "ww_ww"]; + let s: i32 = 0; + for (s < 2) { + let td: str = testenv.fresh(); + mkdirs(td, dirs); + testenv.writefile(strings.concat(td, "/", rel), content); + testenv.writefile(strings.concat(td, "/main.ww"), root); + let av: []str = [testenv.driver(drvs[s]), "build", "-o", "main", + "main.ww"]; + if (runcode(td, strings.concat("build_", drvs[s]), av) != 0) { + fail(label, strings.concat(drvs[s], " build failed (broken ", + "mangle hint or skip rule -> link failure)")); + }; + let rav: []str = [strings.concat(td, "/main")]; + if (runcode(td, strings.concat("run_", drvs[s]), rav) != want) { + fail(label, strings.concat(drvs[s], + " run-exit differs from the row's want")); + }; + testenv.clean(td); + s += 1; + }; +}; + +@test fn nested_call() void = { + let dirs: []str = ["aa", "aa/bb"]; + m1row("nested_call", dirs, "aa/bb/bb.ww", strings.concat( + "package bb;\n", + "export fn val() int = { return 42; };\n"), strings.concat( + "package main;\n", + "import aa.bb;\n", + "export fn main() int = { return bb.val(); };\n"), 42); +}; + +@test fn imported_main() void = { + let dirs: []str = ["aa", "aa/bb"]; + m1row("imported_main", dirs, "aa/bb/bb.ww", strings.concat( + "package bb;\n", + "fn main() int = { return 7; };\n", + "export fn run() int = { return main(); };\n"), strings.concat( + "package main;\n", + "import aa.bb;\n", + "export fn main() int = { return bb.run(); };\n"), 7); +}; + +@test fn priv_global() void = { + let dirs: []str = ["aa", "aa/bb"]; + m1row("priv_global", dirs, "aa/bb/bb.ww", strings.concat( + "package bb;\n", + "let pv: int = 9;\n", + "export fn getpv() int = { return pv; };\n"), strings.concat( + "package main;\n", + "import aa.bb;\n", + "export fn main() int = { return bb.getpv(); };\n"), 9); +}; + +@test fn single_level() void = { + let dirs: []str = ["cc"]; + m1row("single_level", dirs, "cc/cc.ww", strings.concat( + "package cc;\n", + "export fn v() int = { return 5; };\n"), strings.concat( + "package main;\n", + "import cc;\n", + "export fn main() int = { return cc.v(); };\n"), 5); +}; + +@test fn deep_nest() void = { + let dirs: []str = ["aa", "aa/bb", "aa/bb/cc"]; + m1row("deep_nest", dirs, "aa/bb/cc/cc.ww", + strings.concat( + "package cc;\n", + "export fn val() int = { return 3; };\n"), strings.concat( + "package main;\n", + "import aa.bb.cc;\n", + "export fn main() int = { return cc.val(); };\n"), 3); +}; + +// ---- m1usehint (#40) --------------------------------------------------- + +fn uselayout(td: str, apick: str, onecall: str) void = { + let dirs: []str = ["a", "a/math", "b", "b/math", "one", "two"]; + mkdirs(td, dirs); + testenv.writefile(strings.concat(td, "/a/math/math.ww"), + strings.concat("package math;\n", apick)); + testenv.writefile(strings.concat(td, "/b/math/math.ww"), + strings.concat( + "package math;\n", + "export fn pick() int = { return 222; };\n")); + testenv.writefile(strings.concat(td, "/one/one.ww"), strings.concat( + "package one;\n", + "import a.math;\n", + "export fn getone() int = { return ", onecall, "; };\n")); + testenv.writefile(strings.concat(td, "/two/two.ww"), strings.concat( + "package two;\n", + "import b.math;\n", + "export fn gettwo() int = { return math.pick(); };\n")); + testenv.writefile(strings.concat(td, "/main.ww"), strings.concat( + "package main;\n", + "import one;\n", + "import two;\n", + "export fn main() int = {\n", + " if (one.getone() != 111) { return 1; };\n", + " if (two.gettwo() != 222) { return 2; };\n", + " return 0;\n", + "};\n")); +}; + +fn userow(label: str, apick: str, onecall: str) void = { + let drvs: []str = ["ww", "ww_ww"]; + let s: i32 = 0; + for (s < 2) { + let td: str = testenv.fresh(); + uselayout(td, apick, onecall); + let av: []str = [testenv.driver(drvs[s]), "build", "-o", "main", + "main.ww"]; + if (runcode(td, strings.concat("build_", drvs[s]), av) != 0) { + fail(label, strings.concat(drvs[s], " build failed")); + }; + let rav: []str = [strings.concat(td, "/main")]; + if (runcode(td, strings.concat("run_", drvs[s]), rav) != 0) { + fail(label, strings.concat(drvs[s], " run-exit != 0 (a ", + "file-global first-match hint routes one math.pick() to ", + "the wrong package -- #40)")); + }; + testenv.clean(td); + s += 1; + }; +}; + +@test fn leaf_collision() void = { + userow("leaf_collision", + "export fn pick() int = { return 111; };\n", "math.pick()"); +}; + +@test fn collision_diff() void = { + userow("collision_diff", + "export fn pick(x: int) int = { return x + 100; };\n", + "math.pick(11)"); +}; + +@test fn usehint_byteid() void = { + let td: str = testenv.fresh(); + uselayout(td, "export fn pick() int = { return 111; };\n", + "math.pick()"); + let drvs: []str = ["ww", "ww_ww"]; + let tags: []str = ["cstage", "wwstage"]; + let asms: []str = ["", ""]; + let s: i32 = 0; + for (s < 2) { + let av: []str = [testenv.driver(drvs[s]), "build", "-o", tags[s], + "main.ww"]; + if (runcode(td, strings.concat("bid_", tags[s]), av) != 0) { + fail("usehint_byteid", strings.concat(tags[s], + " build failed")); + }; + asms[s] = catasm(strings.concat(td, "/", tags[s], ".sepwork")); + s += 1; + }; + if (asms[0].len == 0 || asms[1].len == 0) { + fail("usehint_byteid", "empty .s concat"); + }; + if (!testenv.same(asms[0], asms[1])) { + fail("usehint_byteid", "cstage.s != wwstage.s (byte-id break)"); + }; + testenv.clean(td); +};