Files
ww/test/wcc/944_alias_emit_b7_run.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

433 lines
16 KiB
C

/*
* 944_alias_emit_b7_run — #5 alias arc B7 (finale): the four cgen.c
* static-DATA emitter ELEM-type chases. All four sites already chased
* the OUTER type; the residual single peel was on the ELEMENT type
* (u->sub), so a 2-level-elem-alias global missed the TY_STRUCT /
* TY_STR / TY_TAGGED kind tests and fell off the foldable-literal
* path. cs-only: every graduation is a cs-side flip onto ww's
* already-correct emit (align cs UP); _ww binaries bit-identical
* across the commit (comment-only ww annotation bytes are
* codegen-inert).
*
* Sites (numbering at 05f7af7): emit_array_lit_bytes:14356 (struct
* arm + raw-bytes path), emit_strarray_data:14574 (str DATAW+DATAR
* rows), emit_slice_data:14788 (3-way slice-of-{str,slice,tagged}
* fatal), let_pre_intern:15088 (the :14574 row's label-order leg —
* MUST chase in the same commit or _S_ labels intern in emit order,
* not decl order; strarr_2lvl's byte-id IS the coupling proof).
*
* Row sources: ken's B7 first-position oracle (/tmp/ken_b5/src/kb7_*,
* .ai/ken-b7-oracle.md, matrix at 05f7af7) + impl pre-state probes.
* NOTE: `type el = el0` where el0 is itself a type decl is ALREADY two
* NAMED layers (NAMED(el)→NAMED(el0)→struct), so the "_1lvl" struct
* spelling graduates with the 2-level rows; only a direct alias of a
* primitive (type ms = str) is a true single layer.
*
* row | shape | cs/ww
* -------------------+--------------------------------------+------
* sarr_2lvl | kb7_sarr [2]el 2-lvl alias struct |
* | elem: cs emitted NO DATA (eu peel |
* | missed TY_STRUCT → return 0 → caller |
* | skipped the def) → link-loud. LIVE |
* | graduation → 0/0 byte-id | 0/0
* sarr_1lvl | [2]el, el = el0 — two NAMED layers |
* | by construction; same graduation | 0/0
* sarr_plain_ctl | kb7_sarr0 [2]el0 control | 0/0
* sarr_unref_2lvl | NEVER-REFERENCED 2-lvl-elem global: |
* | pre-B7 cs silently lacked DATA (no |
* | reference → no link error → latent |
* | silence, ken's caveat). Closure pin: |
* | byte-id vs ww (which always emitted) | 0/0
* strarr_2lvl | kb7_strarr [2]ms 2-lvl alias str: |
* | eu != TY_STR → generic path can't |
* | fold strlits → link-loud. Graduation |
* | + the :15088 intern-order coupling | 0/0
* strarr_1lvl_ctl | type ms = str — TRUE 1-level; worked |
* | pre-B7, byte-NEUTRAL regression net | 0/0
* strarr_plain_ctl | kb7_strarr0 [2]str control | 0/0
* scalararr_2lvl | [3]my64b raw-bytes path: scalar fold |
* | never consulted eu — worked pre-B7, |
* | byte-NEUTRAL regression net | 0/0
* slc_2lvl | kb7_slc []my64b: cs ok (synthesized- |
* | array choke-point + mirrored esz), |
* | ww checker-rejects ALL slice-literal |
* | globals ("let: not assignable", |
* | pre-existing acceptance divergence, |
* | #66-R2/#29 kin) — ww cell MUST NOT |
* | move across B7 | 0/err
* slc_plain_ctl | kb7_slc0 []i64 — same split | 0/err
* slcstr_2lvl | kb7_slcstr []ms: pre-B7 the alias |
* | ESCAPED the 3-way fatal and hit the |
* | downstream "not a foldable constant" |
* | — loud but WRONG DIAGNOSTIC. Post: |
* | the intended 3-way text (== control) | err/err
* slcstr_plain_ctl | kb7_slcstr0 []str pins the 3-way |
* | text on the control | err/err
* slctag_2lvl | []u1 2-lvl alias tagged elem: pre-B7 |
* | the alias DODGED the 3-way fatal |
* | ENTIRELY — silent accept + run |
* | (wrong-DATA latency). DESIGNED |
* | NARROWING: post-B7 louds with the |
* | 3-way text ("widen what the gate |
* | SEES", B6-c2 fsarg2 precedent) | err/err
* slcslc_2lvl | []ms (ms = []i64): nested slice-lit |
* | rejected UPSTREAM by the cs checker |
* | ("let G init not assignable") — the |
* | TY_SLICE arm of the 3-way is not |
* | reachable via a literal; pin the |
* | checker bound (alias-independent) | err/err
*
* K_RUN rows build+run BOTH drivers (exit==want) and assert cs/ww asm
* byte-id. K_BUILDERR rows must FAIL with experr on BOTH drivers;
* experr_ww overrides the ww text when the stages loud at different
* sites. K_CSRUN_WWERR rows run on the cs driver and must FAIL with
* experr_ww on the ww driver (ken's observation cells; byte-id N/A).
* NNN<950, self-contained /tmp sources, no imports (944 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;
}
#define K_RUN 0 /* build+run BOTH, exits==wants, + byte-id */
#define K_BUILDERR 1 /* build FAILS with experr on BOTH drivers;
* experr_ww overrides the ww-side text */
#define K_CSRUN_WWERR 2 /* cs builds+runs (exit==cswant); ww must
* FAIL with experr_ww (pre-existing
* acceptance divergence cell); byte-id
* N/A — ww emits nothing */
struct row { const char *label; const char *src;
int cswant; int wwwant; int kind;
const char *experr; const char *experr_ww; };
/* errlog_has — a builderr cell must fail WITH its diagnostic; any other
* failure (parse error, crash, hang-kill) is a vacuous reject (940
* precedent). */
static int
errlog_has(const char *path, const char *needle)
{
FILE *f = fopen(path, "rb");
if (!f) return 0;
char buf[8192];
size_t got = fread(buf, 1, sizeof buf - 1, f);
fclose(f);
buf[got] = '\0';
return strstr(buf, needle) != NULL;
}
#define ERR_3WAY "slice-of-{str,slice,tagged} literal static-init unsupported"
#define ERR_WWLET "let: not assignable"
static const struct row rows[] = {
/* ---- emit_array_lit_bytes struct arm (:14356) */
{ "sarr_2lvl",
"package main;\n"
"type el0 = struct { a: i64, b: i64 };\n"
"type el1 = el0;\n"
"type el = el1;\n"
"let G: [2]el = [el0 { a = 1, b = 2 }, el0 { a = 3, b = 4 }];\n"
"export fn main() i32 = {\n"
" if (G[0].a + G[0].b + G[1].a + G[1].b != 10) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
{ "sarr_1lvl",
"package main;\n"
"type el0 = struct { a: i64, b: i64 };\n"
"type el = el0;\n"
"let G: [2]el = [el0 { a = 1, b = 2 }, el0 { a = 3, b = 4 }];\n"
"export fn main() i32 = {\n"
" if (G[0].a + G[0].b + G[1].a + G[1].b != 10) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
{ "sarr_plain_ctl",
"package main;\n"
"type el0 = struct { a: i64, b: i64 };\n"
"let G: [2]el0 = [el0 { a = 1, b = 2 }, el0 { a = 3, b = 4 }];\n"
"export fn main() i32 = {\n"
" if (G[0].a + G[0].b + G[1].a + G[1].b != 10) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
/* Zero-consumer latent silence (ken's caveat): unreferenced, so
* the pre-B7 missing DATA never linked-loud — the byte-id cell
* vs ww (which always emitted the row) is the closure pin. */
{ "sarr_unref_2lvl",
"package main;\n"
"type el0 = struct { a: i64, b: i64 };\n"
"type el1 = el0;\n"
"type el = el1;\n"
"let G: [2]el = [el0 { a = 1, b = 2 }, el0 { a = 3, b = 4 }];\n"
"export fn main() i32 = {\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
/* ---- emit_strarray_data (:14574) + let_pre_intern (:15088) */
{ "strarr_2lvl",
"package main;\n"
"type ms0 = str;\n"
"type ms = ms0;\n"
"let G: [2]ms = [\"aa\", \"bbb\"];\n"
"export fn main() i32 = {\n"
" if (len(G[0]) != 2) { return 1; };\n"
" if (len(G[1]) != 3) { return 2; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
{ "strarr_1lvl_ctl",
"package main;\n"
"type ms = str;\n"
"let G: [2]ms = [\"aa\", \"bbb\"];\n"
"export fn main() i32 = {\n"
" if (len(G[0]) != 2) { return 1; };\n"
" if (len(G[1]) != 3) { return 2; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
{ "strarr_plain_ctl",
"package main;\n"
"let G: [2]str = [\"aa\", \"bbb\"];\n"
"export fn main() i32 = {\n"
" if (len(G[0]) != 2) { return 1; };\n"
" if (len(G[1]) != 3) { return 2; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
/* Raw-bytes scalar fold never consulted eu — byte-NEUTRAL net. */
{ "scalararr_2lvl",
"package main;\n"
"type my64 = i64;\n"
"type my64b = my64;\n"
"let G: [3]my64b = [5, 6, 7];\n"
"export fn main() i32 = {\n"
" if (G[0] + G[1] + G[2] != 18) { return 1; };\n"
" return 0;\n"
"};\n", 0, 0, K_RUN, NULL, NULL },
/* ---- emit_slice_data (:14788) — ken's ww observation cells
* (checker-rejects every slice-literal global) MUST NOT move. */
{ "slc_2lvl",
"package main;\n"
"type my64 = i64;\n"
"type my64b = my64;\n"
"let G: []my64b = [5, 6, 7];\n"
"export fn main() i32 = {\n"
" if (G[0] + G[1] + G[2] != 18) { return 1; };\n"
" if (len(G) != 3) { return 2; };\n"
" return 0;\n"
"};\n", 0, 1, K_CSRUN_WWERR, NULL, ERR_WWLET },
{ "slc_plain_ctl",
"package main;\n"
"let G: []i64 = [5, 6, 7];\n"
"export fn main() i32 = {\n"
" if (G[0] + G[1] + G[2] != 18) { return 1; };\n"
" if (len(G) != 3) { return 2; };\n"
" return 0;\n"
"};\n", 0, 1, K_CSRUN_WWERR, NULL, ERR_WWLET },
/* Diagnostic-routing fix: pre-B7 the 2-lvl alias escaped the
* 3-way fatal onto the downstream "not a foldable constant"
* text — loud either way, wrong message. Post: == control. */
{ "slcstr_2lvl",
"package main;\n"
"type ms0 = str;\n"
"type ms = ms0;\n"
"let G: []ms = [\"aa\", \"bbb\"];\n"
"export fn main() i32 = {\n"
" if (len(G[0]) != 2) { return 1; };\n"
" return 0;\n"
"};\n", 1, 1, K_BUILDERR, ERR_3WAY, ERR_WWLET },
/* ww cell observed: plain []str PASSES the ww checker and louds
* at ww's own emitslicedata 3-way — the shared needle pins both
* stages' texts (cs "emit_slice_data:", ww "emitslicedata:");
* only ALIAS slice spellings reject at the ww checker. */
{ "slcstr_plain_ctl",
"package main;\n"
"let G: []str = [\"aa\", \"bbb\"];\n"
"export fn main() i32 = {\n"
" if (len(G[0]) != 2) { return 1; };\n"
" return 0;\n"
"};\n", 1, 1, K_BUILDERR, ERR_3WAY, NULL },
/* DESIGNED NARROWING: pre-B7 the tagged-elem alias dodged the
* 3-way fatal entirely — cs silently accepted and ran (wrong-
* DATA latency). The chase widens what the gate SEES (B6-c2
* fsarg2 precedent); the loud stays loud for what it guards. */
{ "slctag_2lvl",
"package main;\n"
"type u0 = (void | i64);\n"
"type u1 = u0;\n"
"let G: []u1 = [1i64, 2i64];\n"
"export fn main() i32 = {\n"
" if (len(G) != 2) { return 1; };\n"
" return 0;\n"
"};\n", 1, 1, K_BUILDERR, ERR_3WAY, ERR_WWLET },
/* Slice-of-slice literal never reaches the emitter: the cs
* checker bounds it upstream, alias-independent. Pin the bound
* so a checker widening re-opens this row deliberately. */
{ "slcslc_2lvl",
"package main;\n"
"type ms0 = []i64;\n"
"type ms = ms0;\n"
"let G: []ms = [[1, 2], [3]];\n"
"export fn main() i32 = {\n"
" if (len(G) != 2) { return 1; };\n"
" return 0;\n"
"};\n", 1, 1, K_BUILDERR, "let G init not assignable", ERR_WWLET },
};
static int
run_driver(const char *driver, const struct row *r, int i, int expect_err,
int want, const char *experr)
{
char tmpdir[96], src[160], outbin[160], errf[160], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/ab7_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/ab7_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/ab7_%d_%d", tmpdir, getpid(), i);
snprintf(errf, sizeof errf, "%s/err", tmpdir);
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,
"timeout 20 %s build -o %s %s >/dev/null 2>%s",
driver, outbin, src, errf);
int brc = runwait(cmd);
if (expect_err) {
int ok = (brc != 0)
&& (experr == NULL || errlog_has(errf, experr));
if (!ok)
fprintf(stderr, "row[%s]: %s expected loud builderr "
"\"%s\" (brc=%d)\n", r->label, driver,
experr ? experr : "", 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 != want) {
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
r->label, driver, got, want);
return 1;
}
return 0;
}
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[96], cs[96], ws[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/ab7_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/ab7_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/ab7_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;
}
int rc = slurp_eq(cs, ws);
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[2080];
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[2120], wdrv[2120];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
int cs_err = rows[i].kind == K_BUILDERR;
if (run_driver(cdrv, &rows[i], i, cs_err,
rows[i].cswant, rows[i].experr) != 0) fail++;
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
int ww_err = rows[i].kind == K_BUILDERR
|| rows[i].kind == K_CSRUN_WWERR;
const char *we = rows[i].experr_ww
? rows[i].experr_ww : rows[i].experr;
if (run_driver(wdrv, &rows[i], i, ww_err,
rows[i].wwwant, we) != 0) fail++;
}
for (int i = 0; i < n; i++) {
if (rows[i].kind != K_RUN)
continue;
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "alias_emit_b7: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("alias_emit_b7: %d/%d ok\n", total, total);
return 0;
}