Files
ww/test/wcc/825_errfirst_union_try.c
Hojun-Cho ce3a25a0b4 test: contain sepwork scratch per-driver tmpdir, fix /tmp+in-repo leak (#8)
The wcc test drivers ran `ww build <bare-/tmp src>` with no -o, so the
compiler's <stem>.sepwork scratch landed beside the source and was never
cleaned: unbounded /tmp growth (2195 stale dirs observed) that fills tmpfs
and fabricates phantom test failures + silent harness aborts, and for
in-repo fixture builds leaked .sepwork into the tracked tree.

Each leaking build now writes its source + output inside a per-invocation
tmpdir, passes -o <tmpdir>/<stem> so the .sepwork lands inside it, and
rm -rf's the tmpdir on every exit path -- including fopen-fail and the
expected-fail reject builds (scratch is mkdir'd before the build can fail).
`ww run` and explicit-`-o`/byte-id helpers are left as-is; the 990/993
byte-id comparison logic is byte-for-byte unchanged.

Two items filed separately (this commit holds the no-Makefile / no-main.c
rail):
- #13: a stale <src>.s byte-id readback (749) silently no-ops since
  separate-compile emits .s to <ostem>.sepwork/__root.s; documented inline.
- #14: build-system Makefile recipes build selfhost/cmd/*/main.ww with no
  -o and leak main.sepwork in-tree (bounded, gitignored; own commit).

One concern -- sepwork leak hygiene -- across 228 drivers; uniform
transform applied per-file and two-round reviewed. make test: all 402
passed, zero net-new /tmp scratch, zero test-driven in-repo .sepwork.
2026-06-22 23:29:39 +09:00

275 lines
8.4 KiB
C

/*
* 825_errfirst_union_try — error-first tagged-union try (`?`/`!`) success-tag
* (#52, the #216 deferred divergence). wwstage HARDCODED the success tag as 0,
* so for an ERROR-FIRST union — error variant at tag 0, success at tag 1+ (e.g.
* `(e | u16)` with `type e = !i32`) — the success (tag 1) failed the `CMPQ $0`
* success check and fell to the error path: `!` → exit(1), `?` → propagate.
* cstage was already correct via cg_tagged_success_tag (cmd/w6c/cgen.c:857):
* if any variant is an error, the success tag is the first NON-error variant's
* index, else 0. The `.s` diff was literally `CMPQ $1` (cstage) vs `CMPQ $0`
* (wwstage).
*
* The fix (selfhost cgenexpr.ww only — cstage UNCHANGED): a `successtag`
* helper mirroring cstage's, applied at the FOUR sites that shared the tag-0 /
* first-param assumption — cgtryprop CMPQ, cgtryunw CMPQ, and the
* cgtrytupleshift / cgtrytaggedshift payload-variant lookups (successvariant).
*
* row | shape | want
* ----------------+----------------------------------------+------
* errfirst_must | (e|u16) `!` unwrap (THE repro) | 44
* errfirst_prop | (e|u16) `?` propagate through outer fn | 44
* succfirst_ctrl | (u16|e) `!` — successtag=0, CMPQ $0 | 7
* errfirst_3var | (e1|e2|u16) success at index 2 | 99
* errfirst_tuple | (e|(u16,u16)) `!` — successvariant shift| 51
*
* errfirst_tuple is the LATENT-site row: the cgtrytupleshift payload-shift only
* bites an error-first union whose SUCCESS variant is itself a tuple. Pre-fix it
* fell two ways — the CMPQ $0 rejected tag-1 (→ exit 1), AND even with the CMPQ
* fixed the shift read ou.params.type_ (the error variant, not a tuple) so the
* rvalue tuple never filled the cursor (verified mutation: 88, not 51). The
* successvariant fix routes the shift to the tuple. The sibling cgtrytaggedshift
* (nested-tagged success) shares the identical successvariant call; it is not
* exercised here because nested-tagged union *construction* (the return-coercion
* box, main.ok) has a separate pre-existing cs!=ww divergence — #52-independent
* (present success-first too) — filed for its own fix.
*
* Exit codes are the process low-8-bits: 300 & 0xFF = 44. errfirst_* are the
* mutation-sane rows: pre-fix wwstage = exit 1 (wrong-tag → error path),
* cstage = correct → cs!=ww. succfirst_ctrl is the byte-id no-regress guard:
* successtag returns 0 there, so its `CMPQ $0` is UNCHANGED. A byte-id pass
* pins cstage==wwstage `.s` for every row.
*/
#include <stdio.h>
#include <stdlib.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;
}
struct row { const char *label; const char *src; int want; };
static const struct row rows[] = {
/* errfirst_must — THE repro: error-first `(e|u16)`, `!` unwrap of a
* tag-1 success. Pre-fix wwstage CMPQ $0 vs tag 1 → error → exit 1. */
{ "errfirst_must",
"package main;\n"
"type e = !i32;\n"
"type u = (e | u16);\n"
"fn ok() u = { return 300u16; };\n"
"export fn main() i32 = {\n"
"\tlet x = ok()!;\n"
"\treturn x: i32;\n"
"};\n",
44 },
/* errfirst_prop — the `?` twin: outer propagates inner's success
* through the same error-first union, then `!` unwraps. */
{ "errfirst_prop",
"package main;\n"
"type e = !i32;\n"
"type u = (e | u16);\n"
"fn inner() u = { return 300u16; };\n"
"fn outer() u = {\n"
"\tlet v = inner()?;\n"
"\treturn v;\n"
"};\n"
"export fn main() i32 = {\n"
"\treturn outer()!: i32;\n"
"};\n",
44 },
/* succfirst_ctrl — success-FIRST `(u16|e)`, successtag=0: the byte-id
* no-regress guard, CMPQ $0 unchanged. */
{ "succfirst_ctrl",
"package main;\n"
"type e = !i32;\n"
"type u2 = (u16 | e);\n"
"fn g() u2 = { return 7u16; };\n"
"export fn main() i32 = {\n"
"\treturn g()!: i32;\n"
"};\n",
7 },
/* errfirst_3var — two error variants then success: successtag walks
* past index 0 AND 1 to return 2, pinning the walk (not 0, not 1). */
{ "errfirst_3var",
"package main;\n"
"type e1 = !i32;\n"
"type e2 = !i64;\n"
"type u3 = (e1 | e2 | u16);\n"
"fn h() u3 = { return 99u16; };\n"
"export fn main() i32 = {\n"
"\treturn h()!: i32;\n"
"};\n",
99 },
/* errfirst_tuple — error-first union whose SUCCESS variant is a tuple:
* exercises cgtrytupleshift via successvariant. Pre-fix exit 1 (CMPQ $0
* rejects tag 1); CMPQ-fixed-but-shift-reverted gives 88; full fix → 51
* (300+7 = 307, 307 & 0xFF = 51). */
{ "errfirst_tuple",
"package main;\n"
"type e = !i32;\n"
"type ut = (e | (u16, u16));\n"
"fn ok() ut = { return (300u16, 7u16); };\n"
"export fn main() i32 = {\n"
"\tlet t = ok()!;\n"
"\tlet (a, b) = t;\n"
"\treturn (a: i32) + (b: i32);\n"
"};\n",
51 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/efu_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/efu_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/efu_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -1; }
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
driver, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
runwait(rmcmd);
return -1;
}
int got = runwait(outbin);
runwait(rmcmd);
return got;
}
/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */
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/efu_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/efu_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/efu_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
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;
}
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;
}
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
int rc = 0;
if (!fc || !fw) {
rc = -1;
} else {
for (;;) {
int a = fgetc(fc);
int b = fgetc(fw);
if (a != b) { rc = -1; break; }
if (a == EOF) break;
}
}
if (fc) fclose(fc);
if (fw) fclose(fw);
if (rc != 0)
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
r->label);
unlink(src); unlink(cs); unlink(ws);
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];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char wdrv[1024];
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *path; int gated_on_existence; }
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_on_existence
&& access(drivers[d].path, X_OK) != 0) {
fprintf(stderr, "errfirst_union_try: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
int got = run_driver(drivers[d].path, &rows[i], i);
total++;
if (got != rows[i].want) {
fprintf(stderr,
"errfirst_union_try[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0)
fail++;
}
}
if (fail) {
fprintf(stderr,
"errfirst_union_try: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("errfirst_union_try: %d/%d ok\n", total, total);
return 0;
}