test/wcc: carrier ownership repair and driver-contract adaptation

Every surviving carrier now owns its artifacts: checked mkdir/mkdtemp/
fopen acquisition, one all-exit cleanup funnel per carrier, ENOENT-
tolerant checked unlinks, exact-path deletion (rm -rf only for an
owned pid-keyed dir or a .sepwork beneath one), and cleanup failure
fails a passing carrier without overwriting its diagnostic. In the
same pass the carriers adapt to the driver contract this branch lands:
--sep and WW_PKGCACHE are gone, -S and the /tmp/ww_run_<pid> scratch
contract are asserted, and rows whose runtime or reject coverage moved
to test/wcc/data fixtures or test/lang @test owners are trimmed to the
byte/artifact/diagnostic observations only they can make.

Repair and adaptation ride together because most files interleave both
in the same hunks; splitting would manufacture intermediate carrier
states that never existed and cannot run against either driver.
This commit is contained in:
2026-08-07 23:02:47 +09:00
parent b2899dd8d3
commit a95a7a316b
132 changed files with 7573 additions and 6172 deletions

View File

@@ -36,7 +36,7 @@
* type table, so cstage's single `size`/offset and wwstage's tinfo +
* astsize/astoffset folds emit identical asm.
*
* IDENTITY (identity_reject, CSTAGE-asserted): assigning a packed struct
* IDENTITY (identity_reject, stage-asymmetric): assigning a packed struct
* value to a structurally-identical UNPACKED twin is a type error — proves
* packed is part of type identity (harec types.c:621). cstage rejects it
* (structural type_eq honours packed). wwstage's struct-vs-struct
@@ -44,9 +44,9 @@
* (#224/#10: a bare cross-module same-leaf type name can mis-resolve, so
* wwstage cannot confidently reject ANY same-kind struct mismatch — it
* already accepts `struct{a:u8}` into `struct{x:u64,y:u64}`); the packed
* twin rides that same deferred arc. So this row asserts ONLY that cstage
* rejects; wwstage's accept is the documented #224/#10 divergence, not a
* #51 regression.
* twin rides that same deferred arc. The row explicitly requires cstage to
* reject and wwstage to accept; the latter is the documented #224/#10
* divergence, not a #51 regression.
*
* WWI ROUND-TRIP (wwi_roundtrip): a library package exports
* `type Ev = struct @packed {...}`; an importing package computes size(Ev)
@@ -58,6 +58,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
@@ -72,7 +73,7 @@ runwait(const char *cmd)
}
/* want sentinels (outside any real exit code 0..255). */
#define REJECT_CSTAGE (-2147483647 - 1) /* cstage must FAIL to build */
#define REJECT_CSTAGE (-2147483647 - 1) /* cstage fails; wwstage runs 0 */
/* The packed/unpacked twins shared by the layout rows. Each row's `expr`
* is spliced into a fn body that has `p` (packed) and `u` (unpacked) of
@@ -199,9 +200,8 @@ static const struct row rows[] = {
"\treturn (p.b: i32) + after - (p.a: i32);\n"
"};\n", 20 },
/* identity: packed twin not assignable to unpacked twin. cstage
* rejects (structural type_eq honours packed); wwstage rides the
* #224/#10 struct-leniency (see header). CSTAGE-asserted only. */
/* identity: cstage rejects the packed/unpacked assignment; wwstage
* accepts it through the documented #224/#10 leniency (see header). */
{ "identity_reject",
"package main;\n"
"type A = struct @packed { x: u8, y: u64 };\n"
@@ -211,76 +211,93 @@ static const struct row rows[] = {
"\tlet b: B = a;\n"
"\treturn 0;\n"
"};\n", REJECT_CSTAGE },
/* unknown struct attribute is a loud parse error (parse.c /
* parse.ww `@%s != packed` branch). cstage-asserted (the reject
* harness only pins cstage); wwstage errmsg rejects symmetrically. */
{ "unknown_attr_reject",
"package main;\n"
"type P = struct @foo { a: u8 };\n"
"export fn main() i32 = { return 0; };\n", REJECT_CSTAGE },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024];
char tmpdir[64], src[128], outbin[128], scratch[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/pk51_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/pk51_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/pk51_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
snprintf(scratch, sizeof scratch, "%s.sepwork", outbin);
if (mkdir(tmpdir, 0755) != 0) {
perror(tmpdir);
return -2;
}
int result = -2;
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -2; }
if (!f) { perror(src); goto cleanup; }
fputs(r->src, f);
fclose(f);
if (fclose(f) != 0) { perror(src); goto cleanup; }
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
driver, outbin, src);
int built = runwait(cmd);
if (built != 0) {
/* build failed (a reject, or a real error). Caller decides. */
runwait(rmcmd);
return -1;
result = -1;
goto cleanup;
}
int got = runwait(outbin);
result = runwait(outbin);
runwait(rmcmd);
return got;
cleanup:
/* `ww build -o` retains this exact caller-owned scratch tree. */
snprintf(cmd, sizeof cmd, "rm -rf %s", scratch);
int cleanfail = runwait(cmd) != 0;
if (unlink(outbin) != 0 && errno != ENOENT) {
perror(outbin); cleanfail = 1;
}
if (unlink(src) != 0 && errno != ENOENT) {
perror(src); cleanfail = 1;
}
if (rmdir(tmpdir) != 0) {
perror(tmpdir); cleanfail = 1;
}
if (cleanfail) {
fprintf(stderr, "row[%s]: cleanup failed after exit=%d\n",
r->label, result);
return -2;
}
return result;
}
/* asm_byte_identical — w6c (cstage) vs w6c_ww (wwstage) .s diff. */
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[64], cs[64], ws[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/pk51a_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/pk51a_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/pk51a_%d_%d_w.s", getpid(), i);
char tmpdir[64], src[128], cs[128], ws[128], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/pk51a_%d_%d", getpid(), i);
snprintf(src, sizeof src, "%s/input.ww", tmpdir);
snprintf(cs, sizeof cs, "%s/c.s", tmpdir);
snprintf(ws, sizeof ws, "%s/w.s", tmpdir);
if (mkdir(tmpdir, 0755) != 0) {
perror(tmpdir);
return -1;
}
int rc = -1;
FILE *f = fopen(src, "wb");
if (!f) return -1;
if (!f) { perror(src); goto cleanup; }
fputs(r->src, f);
fclose(f);
if (fclose(f) != 0) { perror(src); goto cleanup; }
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
unlink(src);
return -1;
goto cleanup;
}
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null", bin, ws, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
unlink(src); unlink(cs);
return -1;
goto cleanup;
}
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
int rc = 0;
rc = 0;
if (!fc || !fw) {
rc = -1;
} else {
@@ -296,7 +313,20 @@ asm_byte_identical(const char *bin, const struct row *r, int i)
if (rc != 0)
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
r->label);
unlink(src); unlink(cs); unlink(ws);
cleanup:
if (unlink(src) != 0 && errno != ENOENT) {
perror(src); rc = -1;
}
if (unlink(cs) != 0 && errno != ENOENT) {
perror(cs); rc = -1;
}
if (unlink(ws) != 0 && errno != ENOENT) {
perror(ws); rc = -1;
}
if (rmdir(tmpdir) != 0) {
perror(tmpdir); rc = -1;
}
return rc;
}
@@ -308,20 +338,32 @@ asm_byte_identical(const char *bin, const struct row *r, int i)
static int
wwi_roundtrip(const char *driver, int idx)
{
char root[80], libdir[128], appww[160], cmd[1024], outbin[200], rm[200];
char root[80], libroot[112], libdir[128], appdir[112];
char pk2[160], appww[160], outbin[200], scratch[220], cmd[1024];
snprintf(root, sizeof root, "/tmp/pk51w_%d_%d", getpid(), idx);
snprintf(libroot, sizeof libroot, "%s/lib", root);
snprintf(libdir, sizeof libdir, "%s/lib/pk2", root);
snprintf(appdir, sizeof appdir, "%s/app", root);
snprintf(pk2, sizeof pk2, "%s/pk2.ww", libdir);
snprintf(appww, sizeof appww, "%s/app/main.ww", root);
snprintf(rm, sizeof rm, "rm -rf %s", root);
snprintf(outbin, sizeof outbin, "%s/app/m", root);
snprintf(scratch, sizeof scratch, "%s.sepwork", outbin);
if (mkdir(root, 0755) != 0) {
perror(root);
return -1;
}
int result = -1;
int cleanfail;
int libroot_owned = 0, libdir_owned = 0, app_owned = 0;
if (mkdir(libroot, 0755) != 0) { perror(libroot); goto cleanup; }
libroot_owned = 1;
if (mkdir(libdir, 0755) != 0) { perror(libdir); goto cleanup; }
libdir_owned = 1;
if (mkdir(appdir, 0755) != 0) { perror(appdir); goto cleanup; }
app_owned = 1;
char mk[256];
snprintf(mk, sizeof mk, "mkdir -p %s/lib/pk2 %s/app", root, root);
if (runwait(mk) != 0) { runwait(rm); return -1; }
char pk2[256];
snprintf(pk2, sizeof pk2, "%s/lib/pk2/pk2.ww", root);
FILE *f = fopen(pk2, "wb");
if (!f) { runwait(rm); return -1; }
if (!f) { perror(pk2); goto cleanup; }
/* pk2 exports ONLY the packed type — no helper fns. The size/offset
* are computed on the IMPORTER side (below), so the value depends on
* the .wwi re-parse laying Ev out packed. (If evsize()/evoff() lived
@@ -329,10 +371,10 @@ wwi_roundtrip(const char *driver, int idx)
* @packed re-parse would never be exercised.) */
fputs("package pk2;\n"
"export type Ev = struct @packed { tag: u8, data: u64 };\n", f);
fclose(f);
if (fclose(f) != 0) { perror(pk2); goto cleanup; }
f = fopen(appww, "wb");
if (!f) { runwait(rm); return -1; }
if (!f) { perror(appww); goto cleanup; }
/* importer computes layout from the re-parsed .wwi: packed → 9*10+1
* = 91; a dropped @packed (unpacked Ev) would give 16*10+8 = 168. */
fputs("package main;\n"
@@ -341,20 +383,45 @@ wwi_roundtrip(const char *driver, int idx)
"\tlet e: pk2.Ev = pk2.Ev { tag = 1u8, data = 2u64 };\n"
"\treturn size(pk2.Ev): i32 * 10 + offset(e.data): i32;\n"
"};\n", f);
fclose(f);
if (fclose(f) != 0) { perror(appww); goto cleanup; }
snprintf(cmd, sizeof cmd,
"cd %s/app && %s build -I %s/lib -o m main.ww 2>/dev/null",
root, driver, root);
if (runwait(cmd) != 0) {
runwait(rm);
return -1;
goto cleanup;
}
snprintf(outbin, sizeof outbin, "%s/app/m", root);
int got = runwait(outbin);
result = runwait(outbin);
runwait(rm);
return got;
cleanup:
cleanfail = 0;
if (app_owned) {
snprintf(cmd, sizeof cmd, "rm -rf %s", scratch);
if (runwait(cmd) != 0) cleanfail = 1;
if (unlink(outbin) != 0 && errno != ENOENT) {
perror(outbin); cleanfail = 1;
}
if (unlink(appww) != 0 && errno != ENOENT) {
perror(appww); cleanfail = 1;
}
}
if (libdir_owned && unlink(pk2) != 0 && errno != ENOENT) {
perror(pk2); cleanfail = 1;
}
if (app_owned && rmdir(appdir) != 0) {
perror(appdir); cleanfail = 1;
}
if (libdir_owned && rmdir(libdir) != 0) {
perror(libdir); cleanfail = 1;
}
if (libroot_owned && rmdir(libroot) != 0) {
perror(libroot); cleanfail = 1;
}
if (rmdir(root) != 0) {
perror(root); cleanfail = 1;
}
if (cleanfail && result == 91) result = -1;
return result;
}
int
@@ -394,23 +461,14 @@ main(void)
for (int i = 0; i < n; i++) {
int got = run_driver(drivers[d].path, &rows[i], i);
total++;
int bad;
if (rows[i].want == REJECT_CSTAGE) {
/* cstage must reject (build fail → got==-1). wwstage
* rides the #224/#10 struct-leniency (header) — its
* disposition is not asserted here. */
if (is_cstage)
bad = (got != -1);
else
bad = 0;
} else {
bad = (got != rows[i].want);
}
int want = rows[i].want;
if (want == REJECT_CSTAGE) want = is_cstage ? -1 : 0;
int bad = (got != want);
if (bad) {
fprintf(stderr,
"struct_packed[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
got, want);
fail++;
}
}