Files
ww/test/wcc/826_alias_tuple_coerce.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

350 lines
11 KiB
C

/*
* 826_alias_tuple_coerce — an alias of a tuple type, initialised with an
* UNTYPED tuple literal, must coerce element-wise just like a direct tuple
* (task #99). `type pair = (int, int); let x: pair = (3, 4)` was rejected by
* cstage — `init (untyped_int, untyped_int) not assignable to declared pair`
* — while the direct `let x: (int,int) = (3,4)` and the typed-alias
* `let x: pair = (3:int, 4:int)` both compiled. wwstage already accepted+ran
* the untyped-alias form correctly.
*
* ROOT (cstage only): cmd/wcc/type.c type_assignable's tuple-to-tuple arm
* gated on the UN-chased `dst->kind == TY_TUPLE`. For `pair` (a TY_NAMED
* alias) dst->kind is TY_NAMED, so the arm was skipped and the per-element
* untyped->int coercion never ran. The fix chases TY_NAMED on both sides
* before the tuple check (mirroring the #258 slice-borrow arm just below),
* so an alias-of-tuple gets the same element-wise coercion as a direct tuple.
* type_assignable is the shared assignability predicate, so INIT, FN-ARG and
* RETURN positions all flip reject->accept in one spot. cstage-only —
* wwstage check.ww/cgen are unchanged.
*
* The coercion still type-checks per element, so a genuinely mismatched
* element type or wrong arity STILL louds — those are the negative controls.
*
* row | shape | want
* ------------+------------------------------------------------+------
* init | type pair=(int,int); let x:pair=(3,4); x.0+x.1 | 7
* fnarg | fn g(p:pair); g((3,4)); p.0+p.1 | 7
* ret | fn f() pair = { return (3,4); }; r.0+r.1 | 7
* mixed | type box=(int,u16); let x:box=(3,4); coerce ea | 7
* ctrl_dir | direct (int,int)=(3,4) (no-regress) | 7
* ctrl_typed | alias (3:int,4:int) (no-regress) | 7
*
* init / fnarg / ret / mixed are the mutation-sane rows: pre-fix cstage
* build-FAILS them (the untyped->alias coercion was rejected); pre-fix
* wwstage built but a two-field alias-tuple fn-arg read garbage (the param
* spill half). Post-fix BOTH stages build+run the want.
*
* Negative controls (cstage must STILL loud — don't over-accept):
* neg_type | let x:pair=(3,"s") (element type mismatch)
* neg_arity | let x:pair=(3,4,5) (wrong arity)
*
* byte-id: the init, fnarg and ret rows are asserted byte-identical
* (cstage==wwstage .s). #99 shipped in two halves — the cstage type.c chase
* (init/fnarg/ret reject->accept) AND the wwstage cgendecl.ww tuple-PARAM
* spill chase (the param twin: an alias param gated on the SYNTACTIC
* N_TTUPLE node fell through to the scalar path, spilling one arg reg and
* dropping the second tuple word, so a two-field read of an alias-tuple
* fn-arg returned garbage in wwstage). With the wwstage half landed the
* fnarg/ret prologues converge to cstage, so both read both fields and
* match byte-for-byte. mixed (an init row) stays runtime-only.
*/
#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; int byteid; };
static const struct row rows[] = {
/* init — THE construct: untyped tuple literal -> alias, read both
* fields. byte-id clean across stages. */
{ "init",
"package main;\n"
"type pair = (int, int);\n"
"export fn main() i32 = {\n"
"\tlet x: pair = (3, 4);\n"
"\treturn (x.0 + x.1): i32;\n"
"};\n",
7, 1 },
/* fnarg — untyped tuple literal coerced into an alias-typed param at
* the call site, reading BOTH fields. The wwstage tuple-PARAM spill
* chase (cgendecl.ww, #99 second half) sizes the alias param slot 16B
* and spills both arg regs, so p.0+p.1 is correct and byte-identical
* to cstage. */
{ "fnarg",
"package main;\n"
"type pair = (int, int);\n"
"fn g(p: pair) i32 = {\n"
"\treturn (p.0 + p.1): i32;\n"
"};\n"
"export fn main() i32 = {\n"
"\treturn g((3, 4));\n"
"};\n",
7, 1 },
/* ret — untyped tuple literal coerced into the declared alias return
* type; the receiving alias-tuple let reads both fields, byte-id. */
{ "ret",
"package main;\n"
"type pair = (int, int);\n"
"fn f() pair = {\n"
"\treturn (3, 4);\n"
"};\n"
"export fn main() i32 = {\n"
"\tlet r: pair = f();\n"
"\treturn (r.0 + r.1): i32;\n"
"};\n",
7, 1 },
/* mixed — heterogeneous element types under the alias; each untyped
* element coerces to its declared field type. runtime-only. */
{ "mixed",
"package main;\n"
"type box = (int, u16);\n"
"export fn main() i32 = {\n"
"\tlet x: box = (3, 4);\n"
"\treturn (x.0 + (x.1: int)): i32;\n"
"};\n",
7, 0 },
/* ctrl_dir — direct (un-aliased) tuple init; already worked, guards
* against a regression in the original tuple path. */
{ "ctrl_dir",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x: (int, int) = (3, 4);\n"
"\treturn (x.0 + x.1): i32;\n"
"};\n",
7, 0 },
/* ctrl_typed — alias init with already-typed elements (no untyped
* coercion); already worked via the named-arm type_eq. */
{ "ctrl_typed",
"package main;\n"
"type pair = (int, int);\n"
"export fn main() i32 = {\n"
"\tlet x: pair = (3: int, 4: int);\n"
"\treturn (x.0 + x.1): i32;\n"
"};\n",
7, 0 },
};
/* neg — cstage must STILL reject these (the coercion type-checks per
* element; a type mismatch or wrong arity is not assignable). cstage-only:
* wwstage pre-existingly over-accepts a mismatched tuple init (a separate
* wwstage bug, out of #99 scope), so the negative is asserted against the
* cstage driver only — the side #99 touches. */
static const char *neg[] = {
/* neg_type — second element str vs declared int. */
"package main;\n"
"type pair = (int, int);\n"
"export fn main() i32 = {\n"
"\tlet x: pair = (3, \"s\");\n"
"\treturn x.0: i32;\n"
"};\n",
/* neg_arity — three elements vs the two-element alias. */
"package main;\n"
"type pair = (int, int);\n"
"export fn main() i32 = {\n"
"\tlet x: pair = (3, 4, 5);\n"
"\treturn x.0: i32;\n"
"};\n",
};
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/atc_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/atc_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/atc_%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;
}
/* build_should_fail — the negative control must error on `driver`; returns
* 0 when the build correctly FAILS, non-zero when it wrongly succeeded. */
static int
build_should_fail(const char *driver, const char *src, int i)
{
char tmpdir[64], s[128], outbin[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/atc_neg_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(s, sizeof s, "%s/atc_neg_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/atc_neg_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(s, "wb");
if (!f) { runwait(rmcmd); return -1; }
fputs(src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
driver, outbin, s);
int rc = runwait(cmd);
runwait(rmcmd);
return rc == 0 ? -1 : 0; /* build must NOT succeed */
}
/* 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/atc_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/atc_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/atc_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 nneg = (int)(sizeof neg / sizeof neg[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, "alias_tuple_coerce: 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,
"alias_tuple_coerce[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
/* Negative controls — cstage must STILL reject (don't over-accept).
* cstage-only: the #99 fix is cstage-side, and wwstage pre-existingly
* over-accepts a mismatched tuple init (out-of-scope wwstage bug). */
for (int i = 0; i < nneg; i++) {
total++;
if (build_should_fail(cdrv, neg[i], i) != 0) {
fprintf(stderr,
"alias_tuple_coerce[cstage][neg_%d]: built (should loud)\n",
i);
fail++;
}
}
/* byte-id — only rows flagged byteid (the INIT form). */
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
if (!rows[i].byteid) continue;
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0)
fail++;
}
}
if (fail) {
fprintf(stderr,
"alias_tuple_coerce: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("alias_tuple_coerce: %d/%d ok\n", total, total);
return 0;
}