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.
345 lines
12 KiB
C
345 lines
12 KiB
C
/*
|
|
* 940_tuple_in_union_run — project #242: a mixed-scalar tuple WRAPPED IN A
|
|
* TAGGED UNION must construct, match-bind, and destructure IDENTICALLY in
|
|
* both stages and round-trip every element. BARE tuples already worked
|
|
* (#240/#83); only the union-wrapped variant was broken.
|
|
*
|
|
* Three SILENT, gate-blind (no bootstrap tuple-in-union) cs!=ww divergences
|
|
* met here, all on the `(neg, n)`-shape tuple Hare's strconv parseint returns
|
|
* (`((bool, u64) | invalid | overflow)`, stou.ha):
|
|
*
|
|
* (a) cstage CONSTRUCTION (cgen.c N_RETURN tagged arm): a tuple variant fell
|
|
* through the scalar shuffle, which ZEROED the whole value (MOVQ $0 to
|
|
* tag + payload) — the operands were never packed. Fix: route the tuple
|
|
* variant through the scratch-slot widen path (cg_widen_tagged_store's
|
|
* new TY_TUPLE arm), packing each element into the union payload + tag.
|
|
*
|
|
* (b) wwstage CHECKER (check.ww N_MLET): `let (a,b)=t` over a plain tuple
|
|
* ident (the match-bound union payload) left the un-annotated binders
|
|
* UNTYPED — the bin node reading them was untyped → asserttyped abort.
|
|
* The element-type distribution only fired for an N_CALL rhs. Fix:
|
|
* consume the rhs tuple type for ANY rhs (mirror cstage check.c:2017).
|
|
*
|
|
* (c) BOTH stages DESTRUCTURE (N_MLET / cgmlet): the register-cursor receive
|
|
* assumes the rhs left every element in AX/DX/CX (a call's tuple-return
|
|
* ABI). For a tuple IDENT cgexpr loads only word0->AX, so the 2nd binder
|
|
* read a STALE DX. Fix: copy each element from the ident's slot at the
|
|
* register-ABI 8B stride.
|
|
*
|
|
* Construction is correct at ANY variant position (the variant tag, not a
|
|
* default-0) — `tuple_tag1` and `tuple_after_int` place the tuple at index 1.
|
|
*
|
|
* SCOPE (rule 7 loud-stop, PINNED by the K_BUILDERR row): a tuple built
|
|
* from a BARE LITERAL element (cstage mis-types `true`/`false`/untyped `7` →
|
|
* tag unresolved, #241 literal-init family; wwstage types them but mirrors
|
|
* cstage's CONDITION down per rule 10) loud-stops in cgen on BOTH stages
|
|
* rather than silently miscompile. The old eightbyte-share loud-stop
|
|
* ((i32,i32,u64), #243) DISSOLVED with the slot-SSoT tuple layout (tuple
|
|
* arc C-t0) — that row graduated to K_RUN below. The K_RUN rows build
|
|
* their tuple from TYPED expressions — the supported, byte-identical shape.
|
|
*
|
|
* K_RUN rows: build+run exit 0 on BOTH drivers AND cs==ww byte-identical.
|
|
* K_BUILDERR rows: build FAILS with the #242 diagnostic on BOTH drivers.
|
|
* NNN<950, self-contained (/tmp, no imports), so rule-14's selfhost-sibling
|
|
* race does not apply (903/940/945 precedent).
|
|
*/
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <sys/stat.h>
|
|
#include <sys/wait.h>
|
|
|
|
static int
|
|
runwait(const char *cmd)
|
|
{
|
|
int rc = system(cmd);
|
|
if (rc == -1) return -1;
|
|
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
|
return -1;
|
|
}
|
|
|
|
static int
|
|
slurp_eq(const char *a, const char *b)
|
|
{
|
|
FILE *fa = fopen(a, "rb");
|
|
FILE *fb = fopen(b, "rb");
|
|
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
|
|
int rc = 0;
|
|
for (;;) {
|
|
int ca = fgetc(fa), cb = fgetc(fb);
|
|
if (ca != cb) { rc = -1; break; }
|
|
if (ca == EOF) break;
|
|
}
|
|
fclose(fa); fclose(fb);
|
|
return rc;
|
|
}
|
|
|
|
static int
|
|
file_contains(const char *path, const char *needle)
|
|
{
|
|
FILE *f = fopen(path, "rb");
|
|
if (!f) return 0;
|
|
char buf[8192];
|
|
size_t n = fread(buf, 1, sizeof buf - 1, f);
|
|
fclose(f);
|
|
buf[n] = '\0';
|
|
return strstr(buf, needle) != NULL;
|
|
}
|
|
|
|
#define K_RUN 0 /* build+run both drivers, exit==want, + cs==ww byte-id */
|
|
#define K_BUILDERR 1 /* build must FAIL with experr on BOTH drivers (rule 7) */
|
|
|
|
struct row { const char *label; const char *src; int kind; int want;
|
|
const char *experr; };
|
|
|
|
static const struct row rows[] = {
|
|
/* canonical #242 case: (bool,u64) at variant 0, match-bind +
|
|
* destructure, both elements checked. */
|
|
{ "bool_u64",
|
|
"package main;\n"
|
|
"fn mku(x: u64, s: bool) ((bool, u64) | void) = { return (s, x); };\n"
|
|
"export fn main() i32 = {\n"
|
|
" match (mku(7u64, true)) {\n"
|
|
" case let t: (bool, u64) => {\n"
|
|
" let (sg, u) = t;\n"
|
|
" if (u != 7u64) { return 1; };\n"
|
|
" if (!sg) { return 2; };\n"
|
|
" return 0;\n"
|
|
" };\n"
|
|
" case void => { return 3; };\n"
|
|
" };\n"
|
|
" return 4;\n"
|
|
"};\n", K_RUN, 0, NULL },
|
|
/* the void variant: bare `return;` packs the void tag, the match
|
|
* takes the void arm. The tuple branch still compiles (typed vars). */
|
|
{ "void_arm",
|
|
"package main;\n"
|
|
"fn mku(ok: bool, x: u64, s: bool) ((bool, u64) | void) = {\n"
|
|
" if (ok) { return (s, x); };\n"
|
|
" return;\n"
|
|
"};\n"
|
|
"export fn main() i32 = {\n"
|
|
" match (mku(false, 1u64, true)) {\n"
|
|
" case let t: (bool, u64) => { return 1; };\n"
|
|
" case void => { return 0; };\n"
|
|
" };\n"
|
|
" return 2;\n"
|
|
"};\n", K_RUN, 0, NULL },
|
|
/* tuple at variant 1 (`(void | (bool,u64))`): the construct must emit
|
|
* tag 1, not a default 0 — and the match must dispatch to it. */
|
|
{ "tuple_tag1",
|
|
"package main;\n"
|
|
"fn mku(x: u64, s: bool) (void | (bool, u64)) = { return (s, x); };\n"
|
|
"export fn main() i32 = {\n"
|
|
" match (mku(9u64, true)) {\n"
|
|
" case void => { return 50; };\n"
|
|
" case let t: (bool, u64) => {\n"
|
|
" let (sg, u) = t;\n"
|
|
" if (u != 9u64) { return 1; };\n"
|
|
" if (!sg) { return 2; };\n"
|
|
" return 0;\n"
|
|
" };\n"
|
|
" };\n"
|
|
" return 4;\n"
|
|
"};\n", K_RUN, 0, NULL },
|
|
/* tuple variant 1 of `(int | (bool,u64))`, selected at runtime, built
|
|
* from typed locals (the parseint shape: an int-or-tuple result). */
|
|
{ "tuple_after_int",
|
|
"package main;\n"
|
|
"fn mk(which: bool) (int | (bool, u64)) = {\n"
|
|
" let s: bool = true;\n"
|
|
" let v: u64 = 88u64;\n"
|
|
" if (which) { return (s, v); };\n"
|
|
" return 5;\n"
|
|
"};\n"
|
|
"export fn main() i32 = {\n"
|
|
" match (mk(true)) {\n"
|
|
" case let n: int => { return 1; };\n"
|
|
" case let t: (bool, u64) => {\n"
|
|
" let (sg, u) = t;\n"
|
|
" if (u != 88u64) { return 2; };\n"
|
|
" if (!sg) { return 3; };\n"
|
|
" return 0;\n"
|
|
" };\n"
|
|
" };\n"
|
|
" return 4;\n"
|
|
"};\n", K_RUN, 0, NULL },
|
|
/* C-t0 graduation (was the #242/#243 loud-stop): under the slot-SSoT
|
|
* tuple layout narrow elements never share an eightbyte —
|
|
* (i32,i32,u64) is 24B (3 slots), the slotted union write fits by
|
|
* construction, and the whole tuple round-trips through the union.
|
|
* cg_widen_tagged_store's size guard stays as a safety net but can
|
|
* no longer fire for an in-cap tuple. */
|
|
{ "eightbyte_share",
|
|
"package main;\n"
|
|
"fn f(a: i32, b: i32, c: u64) ((i32, i32, u64) | void) = {\n"
|
|
" return (a, b, c);\n"
|
|
"};\n"
|
|
"export fn main() i32 = {\n"
|
|
" match (f(-3, 4, 9u64)) {\n"
|
|
" case let t: (i32, i32, u64) => {\n"
|
|
" let (x, y, z) = t;\n"
|
|
" if (x != -3) { return 1; };\n"
|
|
" if (y != 4) { return 2; };\n"
|
|
" if (z != 9u64) { return 3; };\n"
|
|
" return 0;\n"
|
|
" };\n"
|
|
" case void => { return 4; };\n"
|
|
" };\n"
|
|
" return 5;\n"
|
|
"};\n", K_RUN, 0, NULL },
|
|
/* rule-7 loud-stop (ii): a tuple built from a BARE LITERAL element
|
|
* (`true`). cstage's cg_tag_for_variant can't type the literal (#241)
|
|
* so it returns -1 and loud-stops; wwstage types `true` as bool and
|
|
* WOULD resolve the tag, so it mirrors cstage's CONDITION with an
|
|
* explicit bare-literal guard (rule 10 — align the richer side DOWN).
|
|
* Both stages MUST loud-stop with the SAME diagnostic. Lift both guards
|
|
* together when #241 lands cstage literal typing. */
|
|
{ "bool_literal",
|
|
"package main;\n"
|
|
"fn f(x: u64) ((bool, u64) | void) = { return (true, x); };\n"
|
|
"export fn main() i32 = { return 0; };\n",
|
|
K_BUILDERR, 0, "variant tag unresolved (untyped/literal tuple element" },
|
|
};
|
|
|
|
/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */
|
|
static int
|
|
run_driver(const char *driver, const struct row *r, int i)
|
|
{
|
|
char src[128], tmpdir[96], errf[128], outbin[128], rmcmd[160], cmd[1024];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/tiu_%d_d_%d", getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
snprintf(src, sizeof src, "%s/tiu_%d_%d.ww", tmpdir, getpid(), i);
|
|
snprintf(errf, sizeof errf, "%s/tiu_%d_e_%d", tmpdir, getpid(), i);
|
|
snprintf(outbin, sizeof outbin, "%s/tiu_%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 >/dev/null 2>%s",
|
|
driver, outbin, src, errf);
|
|
int brc = runwait(cmd);
|
|
|
|
if (r->kind == K_BUILDERR) {
|
|
int ok = (brc != 0)
|
|
&& (r->experr == NULL || file_contains(errf, r->experr));
|
|
if (!ok)
|
|
fprintf(stderr, "row[%s]: %s expected loud #242 builderr "
|
|
"(brc=%d)\n", r->label, driver, brc);
|
|
runwait(rmcmd);
|
|
return ok ? 0 : 1;
|
|
}
|
|
|
|
if (brc != 0) {
|
|
fprintf(stderr, "row[%s]: build via %s failed\n",
|
|
r->label, driver);
|
|
runwait(rmcmd);
|
|
return -1;
|
|
}
|
|
|
|
int got = runwait(outbin);
|
|
|
|
runwait(rmcmd);
|
|
if (got != r->want) {
|
|
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
|
|
r->label, driver, got, r->want);
|
|
return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* cs==ww .s byte-id (rule 10). */
|
|
static int
|
|
byteid(const char *w6c, const char *w6c_ww, const struct row *r, int i)
|
|
{
|
|
char src[96], cs_s[96], ws_s[96], cmd[1024];
|
|
snprintf(src, sizeof src, "/tmp/tiu_bi_%d_%d.ww", getpid(), i);
|
|
snprintf(cs_s, sizeof cs_s, "/tmp/tiu_bi_%d_%d_cs.s", getpid(), i);
|
|
snprintf(ws_s, sizeof ws_s, "/tmp/tiu_bi_%d_%d_ww.s", getpid(), i);
|
|
|
|
FILE *f = fopen(src, "wb");
|
|
if (!f) return -1;
|
|
fputs(r->src, f);
|
|
fclose(f);
|
|
|
|
int rc = 0;
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src);
|
|
if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c failed\n", r->label); rc = 1; }
|
|
else {
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c_ww, ws_s, src);
|
|
if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c_ww failed\n", r->label); rc = 1; }
|
|
else if (slurp_eq(cs_s, ws_s) != 0) {
|
|
fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER "
|
|
"(#242 tuple-in-union construct/destructure regression)\n",
|
|
r->label);
|
|
rc = 1;
|
|
}
|
|
}
|
|
unlink(src); unlink(cs_s); unlink(ws_s);
|
|
return rc;
|
|
}
|
|
|
|
int
|
|
main(void)
|
|
{
|
|
const char *bin = getenv("BIN");
|
|
if (!bin) bin = "out/bin";
|
|
char absbin[512];
|
|
if (bin[0] != '/') {
|
|
char cwd[256];
|
|
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
|
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
|
bin = absbin;
|
|
}
|
|
|
|
char cdrv[640], wdrv[640], w6c[640], w6c_ww[640];
|
|
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
|
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
|
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
|
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
|
|
|
struct { const char *name; const char *path; 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].path, X_OK) != 0) {
|
|
fprintf(stderr, "tuple_in_union: skip %s (no %s)\n",
|
|
drivers[d].name, drivers[d].path);
|
|
continue;
|
|
}
|
|
for (int i = 0; i < n; i++) {
|
|
total++;
|
|
if (run_driver(drivers[d].path, &rows[i], i) != 0) fail++;
|
|
}
|
|
}
|
|
|
|
/* cs==ww byte-id for the K_RUN rows only; a K_BUILDERR row emits no
|
|
* .s on either stage (both loud-stop), so byte-id does not apply. */
|
|
if (access(w6c_ww, X_OK) == 0) {
|
|
for (int i = 0; i < n; i++) {
|
|
if (rows[i].kind != K_RUN) continue;
|
|
total++;
|
|
if (byteid(w6c, w6c_ww, &rows[i], i) != 0) fail++;
|
|
}
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr, "tuple_in_union: %d/%d checks failed\n",
|
|
fail, total);
|
|
return 1;
|
|
}
|
|
printf("tuple_in_union: %d/%d ok\n", total, total);
|
|
return 0;
|
|
}
|