test: port the sep-layout observers to ww; retire 989_{sepbuild,seproot_export,sepstructdef,wwispread}
Birth test/sep/ (single-file ww observers on test/testenv, run by the new SEP_WW_TARGETS pattern rule under test-compiler) with sepbuild_test.ww: the build_one_sep real-chain gate incl. the bodies==.wwi keystone recompile, the #69 root-without--I contract, the #70 aggregate-def .wwi prototypes, and the #95 spread-marker round-trip. Every carrier assertion has a successor row; the C cleanup-accounting ledgers collapse into testenv.clean.
This commit is contained in:
@@ -1,400 +0,0 @@
|
||||
/*
|
||||
* 989_sepbuild_run — M3-tail commit-3 `ww build` driver gate (#46).
|
||||
*
|
||||
* Certifies build_one_sep (cmd/ww/main.c + selfhost/cmd/ww/main.ww twin)
|
||||
* END-TO-END through the REAL `ww` / `ww_ww` drivers on a REAL lib chain
|
||||
* (NOT the 989_m3sep synth fixture, which exercises the M3-CORE codegen
|
||||
* via a hand harness). The driver adds ONLY orchestration around that
|
||||
* proven core: discover_deps, reverse-topo, the transitive `w6c -c -I`
|
||||
* producer loop, and `w6l` over the root `.o` plus reverse-topo dependency
|
||||
* `.a` archives.
|
||||
*
|
||||
* Chosen chain (smallest real multi-package graph with a genuine
|
||||
* multi-level dep edge): root → os → { rt, time }. rt and time are
|
||||
* true leaves; os imports both AND its public interface exposes a
|
||||
* transitive type (os returns/takes time.instant in sibling protos), so
|
||||
* the root's sep-unit needs the TRANSITIVE `.wwi` closure for name
|
||||
* resolution — direct-deps-only does not type-check (the lead-ratified
|
||||
* superseding of rob-c3-spec §1.3). The graph thus exercises:
|
||||
* - discovery of the transitive package set,
|
||||
* - reverse-topo dep-first ordering (time/rt before os before root),
|
||||
* - the transitive-closure `.wwi` prepend tagged by dotted path,
|
||||
* - one-pass `w6c -c -I` (consumer + producer) per package,
|
||||
* - `w6l` of the root `.o`, dependency `.a` set, and libwwrt.a.
|
||||
*
|
||||
* Asserts (fresh output stems under an invocation-owned root; retained
|
||||
* `<stem>.sepwork` is inspected, then removed by final carrier cleanup):
|
||||
* 1. Build + run, BOTH stages → exit EXPECT_EXIT (cross-boundary
|
||||
* os.getpid resolves across the root `.o` and dependency `.a` boundary;
|
||||
* the program runs).
|
||||
* 2. cs==ww (rule 10): per-package `.s`/`.wwi`/`.unit.ww` AND the final
|
||||
* binary are byte-identical between `ww` and `ww_ww`.
|
||||
* 3. KEYSTONE (load-bearing, §3.1) THROUGH the driver: for each non-leaf
|
||||
* package P, transform the driver's own `<P>.unit.ww` by substituting
|
||||
* each dep's `.wwi` section with that dep's full directory BODIES,
|
||||
* `w6c -c` it, and cmp vs the driver's `<P>.s`. Byte-identical proves
|
||||
* the `.wwi` conveys exactly the dep facts P's codegen needs. (Leaves
|
||||
* rt/time have no deps → keystone is vacuous; checked on os + root.)
|
||||
*
|
||||
* All intermediates are `-o`-redirected to /tmp for isolation. Models
|
||||
* 989_m3sep_run.c conventions; 989 prefix per the m3sep/m2wwi precedent.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/stat.h>
|
||||
#include <dirent.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define EXPECT_EXIT 7
|
||||
|
||||
static int
|
||||
runwait(const char *cmd)
|
||||
{
|
||||
int rc = system(cmd);
|
||||
if (rc == -1) return -1;
|
||||
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const char *
|
||||
absbin(void)
|
||||
{
|
||||
const char *b = getenv("BIN");
|
||||
if (!b) b = "out/bin";
|
||||
if (b[0] == '/') return b;
|
||||
static char buf[2048];
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return NULL;
|
||||
snprintf(buf, sizeof buf, "%s/%s", cwd, b);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int
|
||||
slurp(const char *path, char **outbuf, size_t *outlen)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return -1;
|
||||
fseek(f, 0, SEEK_END);
|
||||
long n = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (n < 0) { fclose(f); return -1; }
|
||||
char *b = malloc((size_t)n + 1);
|
||||
if (!b) { fclose(f); return -1; }
|
||||
if (fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); fclose(f); return -1; }
|
||||
b[n] = '\0';
|
||||
fclose(f);
|
||||
*outbuf = b;
|
||||
*outlen = (size_t)n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
files_eq(const char *a, const char *b)
|
||||
{
|
||||
char *ba = NULL, *bb = NULL;
|
||||
size_t na = 0, nb = 0;
|
||||
if (slurp(a, &ba, &na) < 0 || slurp(b, &bb, &nb) < 0) {
|
||||
free(ba); free(bb);
|
||||
return -1;
|
||||
}
|
||||
int eq = (na == nb && memcmp(ba, bb, na) == 0);
|
||||
free(ba); free(bb);
|
||||
return eq ? 0 : 1;
|
||||
}
|
||||
|
||||
static int
|
||||
write_file(const char *path, const char *body)
|
||||
{
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(body, f);
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
strs_cmp(const void *a, const void *b)
|
||||
{
|
||||
return strcmp(*(const char *const *)a, *(const char *const *)b);
|
||||
}
|
||||
|
||||
/* Concatenate a package directory's *.ww bodies (less *test.ww,
|
||||
* byte-sorted) into `out`. Mirrors the driver's body enumeration so
|
||||
* the substituted bodies-unit matches the sep-unit structurally. */
|
||||
static int
|
||||
append_dir_bodies(FILE *out, const char *dir)
|
||||
{
|
||||
DIR *d = opendir(dir);
|
||||
if (!d) return -1;
|
||||
char *names[256];
|
||||
int n = 0;
|
||||
struct dirent *ent;
|
||||
while ((ent = readdir(d)) != NULL && n < 256) {
|
||||
const char *nm = ent->d_name;
|
||||
size_t nl = strlen(nm);
|
||||
if (nl <= 3 || strcmp(nm + nl - 3, ".ww") != 0) continue;
|
||||
if (nl >= 7 && strcmp(nm + nl - 7, "test.ww") == 0) continue;
|
||||
names[n++] = strdup(nm);
|
||||
}
|
||||
closedir(d);
|
||||
if (n > 1) qsort(names, (size_t)n, sizeof names[0], strs_cmp);
|
||||
int rc = 0;
|
||||
for (int i = 0; i < n; i++) {
|
||||
char fp[1024];
|
||||
snprintf(fp, sizeof fp, "%s/%s", dir, names[i]);
|
||||
char *b = NULL;
|
||||
size_t bn = 0;
|
||||
if (slurp(fp, &b, &bn) == 0) {
|
||||
fwrite(b, 1, bn, out);
|
||||
fputc('\n', out);
|
||||
free(b);
|
||||
} else rc = -1;
|
||||
free(names[i]);
|
||||
}
|
||||
return rc;
|
||||
}
|
||||
|
||||
/* Transform the driver's sep-unit at `unitf` into a bodies-unit at
|
||||
* `bodiesf`: every `//ww:module <path>` dep section (its `.wwi` content)
|
||||
* is replaced by <path>'s full directory bodies; the trailing
|
||||
* `//ww:module-reset` primary body is copied verbatim. The result is the
|
||||
* SAME unit P would compile with deps-as-bodies — the keystone's other
|
||||
* arm. <libdir> roots the dotted-path → dir mapping (dots → slashes). */
|
||||
static int
|
||||
compose_bodies(const char *unitf, const char *libdir, const char *bodiesf)
|
||||
{
|
||||
char *buf = NULL;
|
||||
size_t n = 0;
|
||||
if (slurp(unitf, &buf, &n) < 0) return -1;
|
||||
FILE *out = fopen(bodiesf, "wb");
|
||||
if (!out) { free(buf); return -1; }
|
||||
int rc = 0;
|
||||
size_t i = 0;
|
||||
while (i < n) {
|
||||
size_t j = i;
|
||||
while (j < n && buf[j] != '\n') j++;
|
||||
size_t linelen = j - i;
|
||||
if (strncmp(buf + i, "//ww:module-reset", 17) == 0) {
|
||||
/* primary body: copy this line + everything after. */
|
||||
fwrite(buf + i, 1, n - i, out);
|
||||
break;
|
||||
}
|
||||
if (strncmp(buf + i, "//ww:module ", 12) == 0) {
|
||||
/* dep section: emit the directive, then dir bodies;
|
||||
* skip the original `.wwi` content to the next
|
||||
* directive. */
|
||||
fwrite(buf + i, 1, linelen, out);
|
||||
fputc('\n', out);
|
||||
char path[256];
|
||||
size_t pl = linelen - 12;
|
||||
if (pl >= sizeof path) pl = sizeof path - 1;
|
||||
memcpy(path, buf + i + 12, pl);
|
||||
path[pl] = '\0';
|
||||
char dir[1024];
|
||||
char form[256];
|
||||
size_t k;
|
||||
for (k = 0; path[k]; k++)
|
||||
form[k] = (path[k] == '.') ? '/' : path[k];
|
||||
form[k] = '\0';
|
||||
snprintf(dir, sizeof dir, "%s/%s", libdir, form);
|
||||
if (append_dir_bodies(out, dir) < 0) rc = -1;
|
||||
/* advance past the wwi content to the next directive. */
|
||||
i = j + 1;
|
||||
while (i < n) {
|
||||
if (strncmp(buf + i, "//ww:module", 11) == 0) break;
|
||||
size_t e = i;
|
||||
while (e < n && buf[e] != '\n') e++;
|
||||
i = (e < n) ? e + 1 : n;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
i = j + 1;
|
||||
}
|
||||
fclose(out);
|
||||
free(buf);
|
||||
return rc;
|
||||
}
|
||||
|
||||
static const char *root_src =
|
||||
"package main;\n"
|
||||
"import os;\n"
|
||||
"fn main() i32 = { return os.getpid() - os.getpid() + 7; };\n";
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = absbin();
|
||||
if (!bin) return 1;
|
||||
char td[64], cmd[8192], libdir[2048];
|
||||
int fail = 0;
|
||||
|
||||
snprintf(libdir, sizeof libdir, "%s/../../lib", bin);
|
||||
snprintf(td, sizeof td, "/tmp/wwsep_%d", getpid());
|
||||
if (mkdir(td, 0755) != 0) {
|
||||
fprintf(stderr, "sepbuild FAIL: cannot acquire %s\n", td);
|
||||
return 1;
|
||||
}
|
||||
|
||||
char rootww[1024];
|
||||
snprintf(rootww, sizeof rootww, "%s/root.ww", td);
|
||||
if (write_file(rootww, root_src)) { fail++; goto out; }
|
||||
|
||||
/* The two driver stages and their scratch dirs. All per-package paths
|
||||
* are rebuilt from `td` (a small fixed buffer) + the stage tag rather
|
||||
* than chained through a large path buffer, so the snprintfs are
|
||||
* provably non-truncating (warning-clean, like 989_m3sep_run). */
|
||||
struct { const char *drv, *tag; char prog[1024]; }
|
||||
stg[] = { { "ww", "cs", {0} }, { "ww_ww", "ww", {0} } };
|
||||
|
||||
for (int s = 0; s < 2; s++) {
|
||||
snprintf(stg[s].prog, sizeof stg[s].prog, "%s/prog.%s", td, stg[s].tag);
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"timeout 240 %s/%s build -o %s %s >/dev/null 2>&1",
|
||||
bin, stg[s].drv, stg[s].prog, rootww);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "sepbuild FAIL: %s build\n", stg[s].drv);
|
||||
fail++;
|
||||
continue;
|
||||
}
|
||||
int rc = runwait(stg[s].prog);
|
||||
if (rc != EXPECT_EXIT) {
|
||||
fprintf(stderr, "sepbuild FAIL: %s prog exit=%d expected %d\n",
|
||||
stg[s].drv, rc, EXPECT_EXIT);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
/* reverse-topo correctness is implicit: every package compiled (a
|
||||
* dep's `.wwi` existed before its importer) → build succeeded above.
|
||||
* Assert the discovered package set materialized in the scratch. */
|
||||
const char *pkgs[] = { "time", "rt", "os", "__root" };
|
||||
for (int i = 0; i < 4; i++) {
|
||||
char p[1024];
|
||||
/* #69: the root is compiled WITHOUT `-I`, so it produces no `.wwi`
|
||||
* (its interface is never consumed); assert its `.s` materialized
|
||||
* instead. Dep packages still emit a `.wwi`. */
|
||||
int is_root = (strcmp(pkgs[i], "__root") == 0);
|
||||
snprintf(p, sizeof p, "%s/prog.cs.sepwork/%s%s", td, pkgs[i],
|
||||
is_root ? ".s" : ".wwi");
|
||||
if (access(p, 0) != 0) {
|
||||
fprintf(stderr, "sepbuild FAIL: missing %s%s (discovery/topo)\n",
|
||||
pkgs[i], is_root ? ".s" : ".wwi");
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
/* cs==ww (rule 10): per-package .s/.wwi/.unit.ww + final binary. The
|
||||
* root has no `.wwi` post-#69, so it is excluded from the .wwi compare. */
|
||||
for (int i = 0; i < 4; i++) {
|
||||
const char *suf[] = { ".s", ".wwi", ".unit.ww" };
|
||||
for (int k = 0; k < 3; k++) {
|
||||
if (strcmp(pkgs[i], "__root") == 0 && strcmp(suf[k], ".wwi") == 0)
|
||||
continue;
|
||||
char a[1024], b[1024];
|
||||
snprintf(a, sizeof a, "%s/prog.%s.sepwork/%s%s",
|
||||
td, stg[0].tag, pkgs[i], suf[k]);
|
||||
snprintf(b, sizeof b, "%s/prog.%s.sepwork/%s%s",
|
||||
td, stg[1].tag, pkgs[i], suf[k]);
|
||||
if (files_eq(a, b) != 0) {
|
||||
fprintf(stderr, "sepbuild FAIL: cs!=ww for %s%s (rule 10)\n",
|
||||
pkgs[i], suf[k]);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (files_eq(stg[0].prog, stg[1].prog) != 0) {
|
||||
fprintf(stderr, "sepbuild FAIL: cs exe != ww exe (rule 10)\n");
|
||||
fail++;
|
||||
}
|
||||
|
||||
/* KEYSTONE through the driver: bodies-unit == sep-unit codegen, for
|
||||
* each non-leaf package (os, root). Transform the driver's own
|
||||
* <P>.unit.ww (deps as .wwi) into a bodies-unit (deps as full dir
|
||||
* bodies), `w6c -c` it, cmp vs the driver's <P>.s. */
|
||||
const char *keypkgs[] = { "os", "__root" };
|
||||
for (int i = 0; i < 2; i++) {
|
||||
char unitf[1024], bodiesf[1024], bodies_s[1024], sep_s[1024];
|
||||
snprintf(unitf, sizeof unitf, "%s/prog.cs.sepwork/%s.unit.ww", td, keypkgs[i]);
|
||||
snprintf(bodiesf, sizeof bodiesf, "%s/%s.bodies.ww", td, keypkgs[i]);
|
||||
snprintf(bodies_s, sizeof bodies_s, "%s/%s.bodies.s", td, keypkgs[i]);
|
||||
snprintf(sep_s, sizeof sep_s, "%s/prog.cs.sepwork/%s.s", td, keypkgs[i]);
|
||||
if (compose_bodies(unitf, libdir, bodiesf) < 0) {
|
||||
fprintf(stderr, "sepbuild FAIL: compose bodies for %s\n", keypkgs[i]);
|
||||
fail++;
|
||||
continue;
|
||||
}
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"timeout 240 %s/w6c -c -o %s %s >/dev/null 2>&1",
|
||||
bin, bodies_s, bodiesf);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "sepbuild FAIL: %s bodies -c\n", keypkgs[i]);
|
||||
fail++;
|
||||
continue;
|
||||
}
|
||||
if (files_eq(bodies_s, sep_s) != 0) {
|
||||
fprintf(stderr, "sepbuild FAIL: %s — bodies.s != driver sep.s "
|
||||
"(the .wwi does not convey the dep facts P needs)\n", keypkgs[i]);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
/* `run` must compile and execute this cross-package graph through the
|
||||
* sole separate-compilation path. Both stages must agree and return
|
||||
* EXPECT_EXIT. */
|
||||
{
|
||||
int rc_cs, rc_ww;
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"%s/ww run %s >/dev/null 2>&1", bin, rootww);
|
||||
rc_cs = runwait(cmd);
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"%s/ww_ww run %s >/dev/null 2>&1", bin, rootww);
|
||||
rc_ww = runwait(cmd);
|
||||
if (rc_cs != rc_ww || rc_cs != EXPECT_EXIT) {
|
||||
fprintf(stderr, "sepbuild FAIL: run cs=%d ww=%d "
|
||||
"(both must run via sep, exit %d)\n", rc_cs, rc_ww,
|
||||
EXPECT_EXIT);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"rm -rf -- '%s/prog.cs.sepwork' '%s/prog.ww.sepwork'", td, td);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "sepbuild FAIL: cannot clean .sepwork trees\n");
|
||||
fail++;
|
||||
}
|
||||
{
|
||||
char path[1200];
|
||||
snprintf(path, sizeof path, "%s/root.ww", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "sepbuild FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/prog.cs", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "sepbuild FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/prog.ww", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "sepbuild FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/os.bodies.ww", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "sepbuild FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/os.bodies.s", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "sepbuild FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/__root.bodies.ww", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "sepbuild FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/__root.bodies.s", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "sepbuild FAIL: cannot remove %s\n", path); fail++; }
|
||||
}
|
||||
if (rmdir(td) != 0 && errno != ENOENT) {
|
||||
fprintf(stderr, "sepbuild FAIL: cannot remove %s\n", td);
|
||||
fail++;
|
||||
}
|
||||
if (fail) {
|
||||
fprintf(stderr, "sepbuild: %d check(s) failed\n", fail);
|
||||
return 1;
|
||||
}
|
||||
printf("sepbuild: real chain root->os->{rt,time} via build_one_sep — "
|
||||
"build+run (exit %d) + cs==ww per-pkg .s/.wwi/.unit + final binary "
|
||||
"+ transitive-topo discovery + keystone bodies==.wwi (os,root)\n",
|
||||
EXPECT_EXIT);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,323 +0,0 @@
|
||||
/*
|
||||
* 989_seproot_export_run — sep-build of a ROOT unit whose `export fn`
|
||||
* references an unexported LOCAL type (#69, BUG-1).
|
||||
*
|
||||
* Root cause it guards: the separate-compilation producer compiled EVERY package,
|
||||
* INCLUDING the root build-target, with the `.wwi`-producer `-I` flag.
|
||||
* `-I` triggers wwi_emit -> check_exported_type, which rejects an exported
|
||||
* declaration that references an unexported type. A terminal binary's root
|
||||
* legitimately has such a decl (`export fn use(a: *t)` over an unexported
|
||||
* local `type t`) — fine, because the root is never imported, so its `.wwi`
|
||||
* is never consumed. Historically, the separately compiled root was also
|
||||
* passed `-I`, so it rejected and blocked every real tool build.
|
||||
* Fix: for pi==root the producer invokes `w6c -c -o` WITHOUT `-I`.
|
||||
*
|
||||
* Graph (smallest reproducing shape): root -> { c (one real dep package) }.
|
||||
* The root exports `use(a: *t)` over the unexported local `type t`.
|
||||
*
|
||||
* Asserts (direct builds compile every package):
|
||||
* 1. Build + run, BOTH stages -> exit EXPECT_EXIT. Pre-fix the root w6c
|
||||
* pass exited 1 ("exported declaration references unexported type"),
|
||||
* so the build never produced a binary.
|
||||
* 2. cs==ww (rule 10): per-package `.s`/`.wwi`/`.unit.ww` for {c,__root}
|
||||
* AND the final binary are byte-identical between the two drivers.
|
||||
* 3. NON-VACUITY (forced -I reject): re-run the EXACT pre-fix root
|
||||
* invocation on the produced `__root.unit.ww` — `w6c -c -I <wwi> -o`
|
||||
* MUST exit non-zero (the export-check fires on this pattern), while
|
||||
* `w6c -c -o` (the post-fix root invocation) MUST exit 0. Demonstrated
|
||||
* for BOTH compilers (w6c, w6c_ww). This is the bug, isolated.
|
||||
* 4. The root `.s` is produced and defines the exported fn symbol.
|
||||
*
|
||||
* All intermediates are `-o`-redirected to /tmp for isolation. Models
|
||||
* 989_sepdotpath_run.c conventions; 989 prefix per the sep-gate precedent.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/stat.h>
|
||||
#include <errno.h>
|
||||
|
||||
#define EXPECT_EXIT 37
|
||||
|
||||
static int
|
||||
runwait(const char *cmd)
|
||||
{
|
||||
int rc = system(cmd);
|
||||
if (rc == -1) return -1;
|
||||
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const char *
|
||||
absbin(void)
|
||||
{
|
||||
const char *b = getenv("BIN");
|
||||
if (!b) b = "out/bin";
|
||||
if (b[0] == '/') return b;
|
||||
static char buf[2048];
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return NULL;
|
||||
snprintf(buf, sizeof buf, "%s/%s", cwd, b);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int
|
||||
slurp(const char *path, char **outbuf, size_t *outlen)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return -1;
|
||||
fseek(f, 0, SEEK_END);
|
||||
long n = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (n < 0) { fclose(f); return -1; }
|
||||
char *b = malloc((size_t)n + 1);
|
||||
if (!b) { fclose(f); return -1; }
|
||||
if (fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); fclose(f); return -1; }
|
||||
b[n] = '\0';
|
||||
fclose(f);
|
||||
*outbuf = b;
|
||||
*outlen = (size_t)n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
files_eq(const char *a, const char *b)
|
||||
{
|
||||
char *ba = NULL, *bb = NULL;
|
||||
size_t na = 0, nb = 0;
|
||||
if (slurp(a, &ba, &na) < 0 || slurp(b, &bb, &nb) < 0) {
|
||||
free(ba); free(bb);
|
||||
return -1;
|
||||
}
|
||||
int eq = (na == nb && memcmp(ba, bb, na) == 0);
|
||||
free(ba); free(bb);
|
||||
return eq ? 0 : 1;
|
||||
}
|
||||
|
||||
/* 0 if `needle` occurs in the file at `path`, 1 if absent, -1 on read err. */
|
||||
static int
|
||||
file_contains(const char *path, const char *needle)
|
||||
{
|
||||
char *b = NULL;
|
||||
size_t n = 0;
|
||||
if (slurp(path, &b, &n) < 0) return -1;
|
||||
int found = (strstr(b, needle) != NULL);
|
||||
free(b);
|
||||
return found ? 0 : 1;
|
||||
}
|
||||
|
||||
static int
|
||||
write_file(const char *path, const char *body)
|
||||
{
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(body, f);
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = absbin();
|
||||
if (!bin) return 1;
|
||||
char td[64], cmd[8192], p[1024];
|
||||
int fail = 0, have_lib = 0, have_c = 0;
|
||||
|
||||
snprintf(td, sizeof td, "/tmp/wwseproot_%d", getpid());
|
||||
if (mkdir(td, 0755) != 0) {
|
||||
fprintf(stderr, "seproot FAIL: cannot acquire %s\n", td);
|
||||
return 1;
|
||||
}
|
||||
|
||||
/* lib/c — one real dep package the root imports. */
|
||||
snprintf(p, sizeof p, "%s/lib", td);
|
||||
if (mkdir(p, 0755) != 0) {
|
||||
fprintf(stderr, "seproot FAIL: cannot create %s\n", p);
|
||||
fail++; goto out;
|
||||
}
|
||||
have_lib = 1;
|
||||
snprintf(p, sizeof p, "%s/lib/c", td);
|
||||
if (mkdir(p, 0755) != 0) {
|
||||
fprintf(stderr, "seproot FAIL: cannot create %s\n", p);
|
||||
fail++; goto out;
|
||||
}
|
||||
have_c = 1;
|
||||
snprintf(p, sizeof p, "%s/lib/c/mod.ww", td);
|
||||
if (write_file(p, "package c;\nexport fn cval() i32 = { return 5; };\n"))
|
||||
{ fail++; goto out; }
|
||||
|
||||
/* Root: `export fn use(a: *t)` over the UNEXPORTED local `type t` —
|
||||
* exactly the pattern `-I`'s check_exported_type rejected. */
|
||||
char rootww[1024];
|
||||
snprintf(rootww, sizeof rootww, "%s/root.ww", td);
|
||||
if (write_file(rootww,
|
||||
"package main;\n"
|
||||
"import c;\n"
|
||||
"type t = struct { v: i32 };\n"
|
||||
"export fn use(a: *t) i32 = { return a.v; };\n"
|
||||
"fn main() i32 = {\n"
|
||||
"\tlet x: t = t { v = 42 };\n"
|
||||
"\treturn use(&x) - c.cval();\n"
|
||||
"};\n"))
|
||||
{ fail++; goto out; }
|
||||
|
||||
struct { const char *drv, *tag; char prog[1024]; }
|
||||
stg[] = { { "ww", "cs", {0} }, { "ww_ww", "ww", {0} } };
|
||||
|
||||
for (int s = 0; s < 2; s++) {
|
||||
snprintf(stg[s].prog, sizeof stg[s].prog, "%s/prog.%s", td, stg[s].tag);
|
||||
/* Each direct build compiles every package, so the
|
||||
* `.s`/`.unit.ww` this gate inspects are always produced. */
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"timeout 240 %s/%s build "
|
||||
"-I %s/lib -o %s %s >/dev/null 2>&1",
|
||||
bin, stg[s].drv, td, stg[s].prog, rootww);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "seproot FAIL: %s build (pre-fix: -I on root "
|
||||
"rejects export-fn-over-unexported-type)\n", stg[s].drv);
|
||||
fail++;
|
||||
continue;
|
||||
}
|
||||
int rc = runwait(stg[s].prog);
|
||||
if (rc != EXPECT_EXIT) {
|
||||
fprintf(stderr, "seproot FAIL: %s prog exit=%d expected %d\n",
|
||||
stg[s].drv, rc, EXPECT_EXIT);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
/* cs==ww (rule 10): per-package .s/.unit.ww + final binary. The root's
|
||||
* `.wwi` is intentionally NOT produced (the fix), so it is excluded
|
||||
* here and asserted ABSENT below; the dep `c` still emits a `.wwi`. */
|
||||
const char *pkgs[] = { "c", "__root" };
|
||||
for (int i = 0; i < 2; i++) {
|
||||
const char *suf[] = { ".s", ".wwi", ".unit.ww" };
|
||||
for (int k = 0; k < 3; k++) {
|
||||
if (strcmp(pkgs[i], "__root") == 0 && strcmp(suf[k], ".wwi") == 0)
|
||||
continue;
|
||||
char a[1024], b[1024];
|
||||
snprintf(a, sizeof a, "%s/prog.%s.sepwork/%s%s",
|
||||
td, stg[0].tag, pkgs[i], suf[k]);
|
||||
snprintf(b, sizeof b, "%s/prog.%s.sepwork/%s%s",
|
||||
td, stg[1].tag, pkgs[i], suf[k]);
|
||||
if (files_eq(a, b) != 0) {
|
||||
fprintf(stderr, "seproot FAIL: cs!=ww for %s%s (rule 10)\n",
|
||||
pkgs[i], suf[k]);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (files_eq(stg[0].prog, stg[1].prog) != 0) {
|
||||
fprintf(stderr, "seproot FAIL: cs exe != ww exe (rule 10)\n");
|
||||
fail++;
|
||||
}
|
||||
|
||||
/* NON-VACUITY: the fix omits `-I` for the root, so the root's `.wwi`
|
||||
* (the producer flag's only effect) must NOT exist — BOTH stages. */
|
||||
for (int s = 0; s < 2; s++) {
|
||||
char rw[1024];
|
||||
snprintf(rw, sizeof rw, "%s/prog.%s.sepwork/__root.wwi", td, stg[s].tag);
|
||||
if (access(rw, 0) == 0) {
|
||||
fprintf(stderr, "seproot FAIL: %s produced __root.wwi (-I still "
|
||||
"passed for the root)\n", stg[s].drv);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
/* NON-VACUITY: replay the EXACT pre-fix root invocation on the produced
|
||||
* `__root.unit.ww`. With `-I` (the .wwi producer) the export-check fires
|
||||
* and w6c exits non-zero; without `-I` (the post-fix invocation) it
|
||||
* exits 0. Proven for BOTH compilers. */
|
||||
{
|
||||
char unit[1024], wwi[1024], asmf[1024];
|
||||
snprintf(unit, sizeof unit, "%s/prog.cs.sepwork/__root.unit.ww", td);
|
||||
struct { const char *comp; } cc[] = { { "w6c" }, { "w6c_ww" } };
|
||||
for (int j = 0; j < 2; j++) {
|
||||
snprintf(wwi, sizeof wwi, "%s/nv.%s.wwi", td, cc[j].comp);
|
||||
snprintf(asmf, sizeof asmf, "%s/nv.%s.s", td, cc[j].comp);
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"%s/%s -c -I %s -o %s %s >/dev/null 2>&1",
|
||||
bin, cc[j].comp, wwi, asmf, unit);
|
||||
if (runwait(cmd) == 0) {
|
||||
fprintf(stderr, "seproot FAIL: %s -c -I accepted the root "
|
||||
"export-over-unexported-type (bug not reproduced -> "
|
||||
"vacuous gate)\n", cc[j].comp);
|
||||
fail++;
|
||||
}
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"%s/%s -c -o %s %s >/dev/null 2>&1",
|
||||
bin, cc[j].comp, asmf, unit);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "seproot FAIL: %s -c -o (no -I) rejected the "
|
||||
"root unit (post-fix invocation must succeed)\n",
|
||||
cc[j].comp);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* The root `.s` is produced and defines the exported fn. */
|
||||
{
|
||||
char rs[1024];
|
||||
snprintf(rs, sizeof rs, "%s/prog.cs.sepwork/__root.s", td);
|
||||
if (file_contains(rs, "use") != 0) {
|
||||
fprintf(stderr, "seproot FAIL: __root.s lacks the exported `use` "
|
||||
"symbol (root compile produced no code)\n");
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"rm -rf -- '%s/prog.cs.sepwork' '%s/prog.ww.sepwork'", td, td);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "seproot FAIL: cannot clean .sepwork trees\n");
|
||||
fail++;
|
||||
}
|
||||
{
|
||||
char path[1200];
|
||||
snprintf(path, sizeof path, "%s/root.ww", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "seproot FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/prog.cs", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "seproot FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/prog.ww", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "seproot FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/nv.w6c.wwi", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "seproot FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/nv.w6c.s", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "seproot FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/nv.w6c_ww.wwi", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "seproot FAIL: cannot remove %s\n", path); fail++; }
|
||||
snprintf(path, sizeof path, "%s/nv.w6c_ww.s", td);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "seproot FAIL: cannot remove %s\n", path); fail++; }
|
||||
}
|
||||
if (have_c) {
|
||||
snprintf(p, sizeof p, "%s/lib/c", td);
|
||||
{
|
||||
char path[1200];
|
||||
snprintf(path, sizeof path, "%s/mod.ww", p);
|
||||
if (unlink(path) != 0 && errno != ENOENT) { fprintf(stderr, "seproot FAIL: cannot remove %s\n", path); fail++; }
|
||||
}
|
||||
if (rmdir(p) != 0 && errno != ENOENT) { fprintf(stderr, "seproot FAIL: cannot remove %s\n", p); fail++; }
|
||||
}
|
||||
if (have_lib) {
|
||||
snprintf(p, sizeof p, "%s/lib", td);
|
||||
if (rmdir(p) != 0 && errno != ENOENT) { fprintf(stderr, "seproot FAIL: cannot remove %s\n", p); fail++; }
|
||||
}
|
||||
if (rmdir(td) != 0 && errno != ENOENT) {
|
||||
fprintf(stderr, "seproot FAIL: cannot remove %s\n", td);
|
||||
fail++;
|
||||
}
|
||||
if (fail) {
|
||||
fprintf(stderr, "seproot: %d check(s) failed\n", fail);
|
||||
return 1;
|
||||
}
|
||||
printf("seproot: root `export fn use(a:*t)` over unexported `type t` "
|
||||
"via build_one_sep — build+run (exit %d) + cs==ww per-pkg + final "
|
||||
"binary + forced -I rejects (bug) while -o accepts (both compilers)\n",
|
||||
EXPECT_EXIT);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,275 +0,0 @@
|
||||
/*
|
||||
* 989_sepstructdef_run — sep-build of a dep package that EXPORTS aggregate-
|
||||
* initializer defs: `export def info: s = s{a=7,b=11};` (N_STRUCTLIT) and
|
||||
* `export def arr: [3]i32 = [1,2,3];` (N_ARRLIT) — BUG-2 (#70).
|
||||
*
|
||||
* Root cause it guards: the `.wwi` producer's const-expr emitter handled
|
||||
* scalar/N_BIN/N_UN/N_DOT but NOT N_STRUCTLIT(15)/N_ARRLIT(16). An exported
|
||||
* aggregate-init def aborted the producer with "unhandled const-expr node
|
||||
* kind 15", so the dep never produced a `.wwi` and the sep-build failed.
|
||||
*
|
||||
* Fix (producer, both stages): an aggregate-init def is a DATA-global (#52),
|
||||
* so the producer emits a value-LESS prototype `export def X: T;` — the
|
||||
* defining package's own `.o` emits the struct/array DATA; the importer
|
||||
* registers the def by TYPE only (cgen's emit_defs/emit_lets already skip a
|
||||
* NULL-rhs def, no dup DATA) and reads resolve to external refs the linker
|
||||
* fills. Consuming the prototype also required the def parser (both stages)
|
||||
* to accept the bodyless form, mirroring the existing fn-prototype arm.
|
||||
*
|
||||
* Graph (smallest reproducing shape): root -> { d (one real dep package) }.
|
||||
* d exports an aggregate STRUCT def (info), an aggregate ARRAY def (arr),
|
||||
* and three reader fns; root sums them at RUNTIME.
|
||||
*
|
||||
* Asserts (direct builds compile every package):
|
||||
* 1. Build + LINK + run, BOTH stages -> exit EXPECT_EXIT. The link+run is
|
||||
* the external-DATA-ref proof: the importer's CALL d.geta / d.getelem /
|
||||
* d.getb and d's own LEAQ d.info/d.arr(SB) refs resolve against the dep
|
||||
* `.o`. Pre-fix the dep producer aborted ("node kind 15") -> no binary.
|
||||
* 2. cs==ww (rule 10): per-package `.s`/`.wwi`/`.unit.ww` for {d,__root}
|
||||
* AND the final binary are byte-identical between the two drivers.
|
||||
* 3. The dep `.wwi` carries the value-LESS prototypes `export def info: s;`
|
||||
* and `export def arr: [3]i32;` (the fix's surface), and NOT the old
|
||||
* value-serialized `export def info: s = ` form (NON-VACUITY: proves the
|
||||
* producer took the new aggregate arm, not the scalar arm).
|
||||
*
|
||||
* All intermediates are `-o`-redirected to /tmp for isolation. Models
|
||||
* 989_seproot_export_run.c conventions; 989 prefix per the sep-gate precedent.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#define EXPECT_EXIT 20 /* info.a(7) + arr[1](2) + info.b(11) */
|
||||
|
||||
static int
|
||||
runwait(const char *cmd)
|
||||
{
|
||||
int rc = system(cmd);
|
||||
if (rc == -1) return -1;
|
||||
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const char *
|
||||
absbin(void)
|
||||
{
|
||||
const char *b = getenv("BIN");
|
||||
if (!b) b = "out/bin";
|
||||
if (b[0] == '/') return b;
|
||||
static char buf[2048];
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return NULL;
|
||||
snprintf(buf, sizeof buf, "%s/%s", cwd, b);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int
|
||||
slurp(const char *path, char **outbuf, size_t *outlen)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return -1;
|
||||
fseek(f, 0, SEEK_END);
|
||||
long n = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (n < 0) { fclose(f); return -1; }
|
||||
char *b = malloc((size_t)n + 1);
|
||||
if (!b) { fclose(f); return -1; }
|
||||
if (fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); fclose(f); return -1; }
|
||||
b[n] = '\0';
|
||||
fclose(f);
|
||||
*outbuf = b;
|
||||
*outlen = (size_t)n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
files_eq(const char *a, const char *b)
|
||||
{
|
||||
char *ba = NULL, *bb = NULL;
|
||||
size_t na = 0, nb = 0;
|
||||
if (slurp(a, &ba, &na) < 0 || slurp(b, &bb, &nb) < 0) {
|
||||
free(ba); free(bb);
|
||||
return -1;
|
||||
}
|
||||
int eq = (na == nb && memcmp(ba, bb, na) == 0);
|
||||
free(ba); free(bb);
|
||||
return eq ? 0 : 1;
|
||||
}
|
||||
|
||||
/* 0 if `needle` occurs in the file at `path`, 1 if absent, -1 on read err. */
|
||||
static int
|
||||
file_contains(const char *path, const char *needle)
|
||||
{
|
||||
char *b = NULL;
|
||||
size_t n = 0;
|
||||
if (slurp(path, &b, &n) < 0) return -1;
|
||||
int found = (strstr(b, needle) != NULL);
|
||||
free(b);
|
||||
return found ? 0 : 1;
|
||||
}
|
||||
|
||||
static int
|
||||
write_file(const char *path, const char *body)
|
||||
{
|
||||
FILE *f = fopen(path, "wb");
|
||||
if (!f) return -1;
|
||||
if (fputs(body, f) == EOF) { fclose(f); return -1; }
|
||||
return fclose(f);
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = absbin();
|
||||
if (!bin) return 1;
|
||||
char td[] = "/tmp/wwsepstructdef_XXXXXX";
|
||||
char cmd[8192], p[1024];
|
||||
int fail = 0, cleanup_fail = 0, have_lib = 0, have_d = 0;
|
||||
|
||||
if (mkdtemp(td) == NULL) return 1;
|
||||
|
||||
/* lib/d — the dep package exporting aggregate-init defs + reader fns.
|
||||
* The struct type must be EXPORTED so the prototype names a visible
|
||||
* type (an exported def over an unexported type is a separate loud
|
||||
* reject, check_exported_type). */
|
||||
snprintf(p, sizeof p, "%s/lib", td);
|
||||
if (mkdir(p, 0755) != 0) { fail++; goto out; }
|
||||
have_lib = 1;
|
||||
snprintf(p, sizeof p, "%s/lib/d", td);
|
||||
if (mkdir(p, 0755) != 0) { fail++; goto out; }
|
||||
have_d = 1;
|
||||
snprintf(p, sizeof p, "%s/lib/d/mod.ww", td);
|
||||
if (write_file(p,
|
||||
"package d;\n"
|
||||
"export type s = struct { a: i32, b: i32 };\n"
|
||||
"export def info: s = s { a = 7, b = 11 };\n"
|
||||
"export def arr: [3]i32 = [1, 2, 3];\n"
|
||||
"export fn geta() i32 = { return info.a; };\n"
|
||||
"export fn getb() i32 = { return info.b; };\n"
|
||||
"export fn getelem(i: i32) i32 = { return arr[i]; };\n"))
|
||||
{ fail++; goto out; }
|
||||
|
||||
char rootww[1024];
|
||||
snprintf(rootww, sizeof rootww, "%s/root.ww", td);
|
||||
if (write_file(rootww,
|
||||
"package main;\n"
|
||||
"import d;\n"
|
||||
"fn main() i32 = {\n"
|
||||
"\treturn d.geta() + d.getelem(1) + d.getb();\n"
|
||||
"};\n"))
|
||||
{ fail++; goto out; }
|
||||
|
||||
struct { const char *drv, *tag; char prog[1024]; }
|
||||
stg[] = { { "ww", "cs", {0} }, { "ww_ww", "ww", {0} } };
|
||||
|
||||
for (int s = 0; s < 2; s++) {
|
||||
snprintf(stg[s].prog, sizeof stg[s].prog, "%s/prog.%s", td, stg[s].tag);
|
||||
/* Each direct build compiles every package, so the
|
||||
* `.s`/`.wwi`/`.unit.ww` this gate inspects are always produced. */
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"timeout 240 %s/%s build "
|
||||
"-I %s/lib -o %s %s >/dev/null 2>&1",
|
||||
bin, stg[s].drv, td, stg[s].prog, rootww);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "sepstructdef FAIL: %s build (pre-fix: the "
|
||||
"aggregate-init def aborts the producer with \"node kind 15\")\n",
|
||||
stg[s].drv);
|
||||
fail++;
|
||||
continue;
|
||||
}
|
||||
int rc = runwait(stg[s].prog);
|
||||
if (rc != EXPECT_EXIT) {
|
||||
fprintf(stderr, "sepstructdef FAIL: %s prog exit=%d expected %d "
|
||||
"(external aggregate-DATA ref did not resolve)\n",
|
||||
stg[s].drv, rc, EXPECT_EXIT);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
/* cs==ww (rule 10): per-package .s/.wwi/.unit.ww + final binary. */
|
||||
const char *pkgs[] = { "d", "__root" };
|
||||
for (int i = 0; i < 2; i++) {
|
||||
const char *suf[] = { ".s", ".wwi", ".unit.ww" };
|
||||
for (int k = 0; k < 3; k++) {
|
||||
/* the root's `.wwi` is intentionally not produced (BUG-1 fix). */
|
||||
if (strcmp(pkgs[i], "__root") == 0 && strcmp(suf[k], ".wwi") == 0)
|
||||
continue;
|
||||
char a[1024], b[1024];
|
||||
snprintf(a, sizeof a, "%s/prog.%s.sepwork/%s%s",
|
||||
td, stg[0].tag, pkgs[i], suf[k]);
|
||||
snprintf(b, sizeof b, "%s/prog.%s.sepwork/%s%s",
|
||||
td, stg[1].tag, pkgs[i], suf[k]);
|
||||
if (files_eq(a, b) != 0) {
|
||||
fprintf(stderr, "sepstructdef FAIL: cs!=ww for %s%s (rule 10)\n",
|
||||
pkgs[i], suf[k]);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (files_eq(stg[0].prog, stg[1].prog) != 0) {
|
||||
fprintf(stderr, "sepstructdef FAIL: cs exe != ww exe (rule 10)\n");
|
||||
fail++;
|
||||
}
|
||||
|
||||
/* The dep `.wwi` carries the value-LESS aggregate prototypes — and NOT
|
||||
* the old value-serialized form (NON-VACUITY: the producer took the new
|
||||
* aggregate arm, not the scalar `= <init>` arm). */
|
||||
{
|
||||
char wwi[1024];
|
||||
snprintf(wwi, sizeof wwi, "%s/prog.cs.sepwork/d.wwi", td);
|
||||
if (file_contains(wwi, "export def info: s;\n") != 0) {
|
||||
fprintf(stderr, "sepstructdef FAIL: d.wwi lacks value-less "
|
||||
"`export def info: s;` (struct-lit def prototype)\n");
|
||||
fail++;
|
||||
}
|
||||
if (file_contains(wwi, "export def arr: [3]i32;\n") != 0) {
|
||||
fprintf(stderr, "sepstructdef FAIL: d.wwi lacks value-less "
|
||||
"`export def arr: [3]i32;` (array-lit def prototype)\n");
|
||||
fail++;
|
||||
}
|
||||
if (file_contains(wwi, "export def info: s = ") == 0) {
|
||||
fprintf(stderr, "sepstructdef FAIL: d.wwi serialized the aggregate "
|
||||
"value (`export def info: s = ...`) -> vacuous gate\n");
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
for (int s = 0; s < 2; s++) {
|
||||
const char *tag = s == 0 ? "cs" : "ww";
|
||||
snprintf(p, sizeof p, "%s/prog.%s.sepwork", td, tag);
|
||||
snprintf(cmd, sizeof cmd, "rm -rf -- '%s'", p);
|
||||
if (runwait(cmd) != 0) cleanup_fail = 1;
|
||||
snprintf(p, sizeof p, "%s/prog.%s", td, tag);
|
||||
if (unlink(p) != 0 && errno != ENOENT) cleanup_fail = 1;
|
||||
}
|
||||
snprintf(p, sizeof p, "%s/root.ww", td);
|
||||
if (unlink(p) != 0 && errno != ENOENT) cleanup_fail = 1;
|
||||
if (have_d) {
|
||||
snprintf(p, sizeof p, "%s/lib/d/mod.ww", td);
|
||||
if (unlink(p) != 0 && errno != ENOENT) cleanup_fail = 1;
|
||||
snprintf(p, sizeof p, "%s/lib/d", td);
|
||||
if (rmdir(p) != 0 && errno != ENOENT) cleanup_fail = 1;
|
||||
}
|
||||
if (have_lib) {
|
||||
snprintf(p, sizeof p, "%s/lib", td);
|
||||
if (rmdir(p) != 0 && errno != ENOENT) cleanup_fail = 1;
|
||||
}
|
||||
if (rmdir(td) != 0) cleanup_fail = 1;
|
||||
if (cleanup_fail) {
|
||||
fprintf(stderr, "sepstructdef FAIL: cleanup incomplete under %s\n", td);
|
||||
fail++;
|
||||
}
|
||||
if (fail) {
|
||||
fprintf(stderr, "sepstructdef: %d check(s) failed\n", fail);
|
||||
return 1;
|
||||
}
|
||||
printf("sepstructdef: exported struct-lit + array-lit defs via "
|
||||
"build_one_sep — build+link+run (exit %d) + cs==ww per-pkg + binary "
|
||||
"+ value-less `.wwi` prototypes (both stages)\n", EXPECT_EXIT);
|
||||
return 0;
|
||||
}
|
||||
@@ -1,196 +0,0 @@
|
||||
/*
|
||||
* 989_wwispread_sep — #95 .wwi producer must preserve the `...` union-spread
|
||||
* marker when serializing a tagged-union type under `ww build`.
|
||||
*
|
||||
* A spread variant `(...inner | str)` carries Node.op == TK_ELLIPSIS on the
|
||||
* variant node (parse.c:315-324). The N_TTAGGED serializer loop must re-emit
|
||||
* the `...` prefix purely syntactically (cstage wwi.c, selfhost wwi.ww) —
|
||||
* mirroring Hare's unparse (ref/hare/hare/unparse/type.ha:290-300), which
|
||||
* leaves flattening to the consumer's type-store. Before the fix the marker
|
||||
* was dropped: liba.wwi read back as `(inner | str)`, a consumer never
|
||||
* flattened inner's arms, and a bare i32/i64 member was rejected — so fmt /
|
||||
* log / getopt failed under the sep flip. (The old combined.ww path never
|
||||
* round-trips through .wwi, so it accepted; the bug was latent until the flip.)
|
||||
*
|
||||
* TABLE-DRIVEN over two spread shapes (2-arm `inner` and 3-arm `three`). For
|
||||
* each shape and each driver stage (cs `ww`, ww `ww_ww`) the test:
|
||||
* (a) builds the consumer via `ww build` and runs it — clean compile
|
||||
* + exit 0 proves the imported alias's arms flattened into the spread
|
||||
* type (the consumer assigns BARE members the spread must expose);
|
||||
* (b) greps the produced <pkg>.wwi and asserts the spread marker survived;
|
||||
* (c) cs==ww (rule 10): the .wwi from `ww` vs `ww_ww` must be byte-identical.
|
||||
*
|
||||
* NON-VACUITY: reverting the wwi.c/.ww edit reddens BOTH legs — the consumer
|
||||
* build fails with `init i32 not assignable to declared outer` AND the marker
|
||||
* grep finds nothing (verified by hand at fix time).
|
||||
*
|
||||
* Every build's intermediates are `-o`-redirected to a private /tmp directory.
|
||||
* Models 989_declns_sep conventions; 989 prefix per the sep-gate precedent.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <errno.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/wait.h>
|
||||
#include <sys/stat.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 const char *
|
||||
absbin(void)
|
||||
{
|
||||
const char *b = getenv("BIN");
|
||||
if (!b) b = "out/bin";
|
||||
if (b[0] == '/') return b;
|
||||
static char buf[2048];
|
||||
char cwd[1024];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return NULL;
|
||||
snprintf(buf, sizeof buf, "%s/%s", cwd, b);
|
||||
return buf;
|
||||
}
|
||||
|
||||
static int
|
||||
slurp(const char *path, char **outbuf, size_t *outlen)
|
||||
{
|
||||
FILE *f = fopen(path, "rb");
|
||||
if (!f) return -1;
|
||||
fseek(f, 0, SEEK_END);
|
||||
long n = ftell(f);
|
||||
fseek(f, 0, SEEK_SET);
|
||||
if (n < 0) { fclose(f); return -1; }
|
||||
char *b = malloc((size_t)n + 1);
|
||||
if (!b) { fclose(f); return -1; }
|
||||
if (fread(b, 1, (size_t)n, f) != (size_t)n) { free(b); fclose(f); return -1; }
|
||||
b[n] = '\0';
|
||||
fclose(f);
|
||||
*outbuf = b;
|
||||
*outlen = (size_t)n;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
files_eq(const char *a, const char *b)
|
||||
{
|
||||
char *ba = NULL, *bb = NULL;
|
||||
size_t na = 0, nb = 0;
|
||||
if (slurp(a, &ba, &na) < 0 || slurp(b, &bb, &nb) < 0) {
|
||||
free(ba); free(bb);
|
||||
return -1;
|
||||
}
|
||||
int eq = (na == nb && memcmp(ba, bb, na) == 0);
|
||||
free(ba); free(bb);
|
||||
return eq ? 0 : 1;
|
||||
}
|
||||
|
||||
/* 0 if `needle` occurs in the file at `path`, else 1 (or -1 on read err). */
|
||||
static int
|
||||
file_contains(const char *path, const char *needle)
|
||||
{
|
||||
char *b = NULL;
|
||||
size_t n = 0;
|
||||
if (slurp(path, &b, &n) < 0) return -1;
|
||||
int found = (strstr(b, needle) != NULL);
|
||||
free(b);
|
||||
return found ? 0 : 1;
|
||||
}
|
||||
|
||||
struct shape {
|
||||
const char *name; /* label */
|
||||
const char *main; /* consumer main.ww (imports the spread package) */
|
||||
const char *wwi; /* produced interface basename for the imported pkg */
|
||||
const char *marker; /* the spread marker that must survive serialization */
|
||||
};
|
||||
static const struct shape shapes[] = {
|
||||
{ "twoarm", "test/wcc/data/wwispread/main.ww", "liba.wwi", "...inner" },
|
||||
{ "threearm", "test/wcc/data/wwispread3/main.ww", "libb.wwi", "...three" },
|
||||
};
|
||||
|
||||
static const struct { const char *drv; const char *tag; } drivers[] = {
|
||||
{ "ww", "cs" },
|
||||
{ "ww_ww", "ww" },
|
||||
};
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = absbin();
|
||||
if (!bin) { fprintf(stderr, "wwispread FAIL: getcwd\n"); return 1; }
|
||||
|
||||
char td[] = "/tmp/wwwwispread_XXXXXX";
|
||||
char cmd[4096];
|
||||
if (mkdtemp(td) == NULL) return 1;
|
||||
|
||||
int fail = 0, cleanup_fail = 0;
|
||||
for (size_t s = 0; s < sizeof shapes / sizeof shapes[0]; s++) {
|
||||
char wwi[2][256] = {{0}};
|
||||
for (size_t d = 0; d < sizeof drivers / sizeof drivers[0]; d++) {
|
||||
char prog[160];
|
||||
snprintf(prog, sizeof prog, "%s/%s.%s", td, shapes[s].name,
|
||||
drivers[d].tag);
|
||||
snprintf(cmd, sizeof cmd, "%s/%s build -o %s %s 2>/dev/null",
|
||||
bin, drivers[d].drv, prog, shapes[s].main);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "wwispread FAIL: %s build %s "
|
||||
"(spread member not assignable → marker dropped?)\n",
|
||||
drivers[d].drv, shapes[s].name);
|
||||
fail++;
|
||||
continue;
|
||||
}
|
||||
int got = runwait(prog);
|
||||
if (got != 0) {
|
||||
fprintf(stderr, "wwispread FAIL: %s %s run exit=%d want=0\n",
|
||||
drivers[d].drv, shapes[s].name, got);
|
||||
fail++;
|
||||
}
|
||||
snprintf(wwi[d], sizeof wwi[d], "%s/%s.%s.sepwork/%s", td,
|
||||
shapes[s].name, drivers[d].tag, shapes[s].wwi);
|
||||
if (file_contains(wwi[d], shapes[s].marker) != 0) {
|
||||
fprintf(stderr, "wwispread FAIL: %s %s — %s missing `%s` "
|
||||
"(spread marker dropped in serializer)\n", drivers[d].drv,
|
||||
shapes[s].name, shapes[s].wwi, shapes[s].marker);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
/* cs==ww (rule 10): both stages emit the same interface text. */
|
||||
if (files_eq(wwi[0], wwi[1]) != 0) {
|
||||
fprintf(stderr, "wwispread FAIL: %s cs!=ww for %s (rule 10)\n",
|
||||
shapes[s].name, shapes[s].wwi);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t s = 0; s < sizeof shapes / sizeof shapes[0]; s++) {
|
||||
for (size_t d = 0; d < sizeof drivers / sizeof drivers[0]; d++) {
|
||||
char path[1200];
|
||||
snprintf(path, sizeof path, "%s/%s.%s.sepwork", td,
|
||||
shapes[s].name, drivers[d].tag);
|
||||
snprintf(cmd, sizeof cmd, "rm -rf -- '%s'", path);
|
||||
if (runwait(cmd) != 0) cleanup_fail = 1;
|
||||
snprintf(path, sizeof path, "%s/%s.%s", td,
|
||||
shapes[s].name, drivers[d].tag);
|
||||
if (unlink(path) != 0 && errno != ENOENT) cleanup_fail = 1;
|
||||
}
|
||||
}
|
||||
if (rmdir(td) != 0) cleanup_fail = 1;
|
||||
if (cleanup_fail) {
|
||||
fprintf(stderr, "wwispread FAIL: cleanup incomplete under %s\n", td);
|
||||
fail++;
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr, "wwispread: %d check(s) failed\n", fail);
|
||||
return 1;
|
||||
}
|
||||
printf("wwispread: `...` union-spread marker survives .wwi serialization "
|
||||
"(2-arm + 3-arm, both stages cs==ww), consumer flattens bare members "
|
||||
"→ exit 0 (#95)\n");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user