Files
ww/test/wcc/845_tuple_trailing_comma_reject.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

225 lines
7.1 KiB
C

/*
* 845_tuple_trailing_comma_reject (catB-92) — cstage and wwstage
* LOUD-REJECT a single-element trailing-comma tuple `(a,)`.
*
* A trailing comma is legal ONLY after >=2 tuple elements; `(a,)` is a
* parse error, `(a, b)` and `(a, b,)` are 2-tuples. cstage parse.c:552-
* 558 parses each element BEFORE the RPAREN-break, so `(a,)` errors at
* the next parseexpr (it sees `)`). wwstage's lib/ww/parse/expr.ww tuple
* loop checked the RPAREN-break at the TOP, so `(a,)` parsed as a
* 1-element N_TUPLE and reached cgen — a silent wrong-accept. The fix
* mirrors cstage's loop order (break sits AFTER accept(COMMA)).
*
* The reject row is `let t = (5,);` — an INFERRED-type 1-tuple, the
* context the old wwstage built rc=0 and codegen'd (a 2-tuple-DECLARED
* `(5,)` already errored on arity downstream, masking the parse bug;
* the inferred form is the clean discriminator). Pre-fix wwstage built
* it rc=0; cstage rejected it. Post-fix both reject at parse.
*
* row | shape | expect
* -----------+-----------------+--------
* one_comma | let t = (5,); | REJECT (parse)
* two_elem | (3, 4) | BUILD, run 7
* two_comma | (3, 4,) | BUILD, run 7 (trailing OK after >=2)
*/
#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;
}
/* outbin — derive the driver's default binary name from a *.ww source
* (strip dir + the .ww suffix), placed in tmpdir. */
static void
outbin(char *dst, size_t n, const char *tmpdir, const char *src)
{
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
snprintf(dst, n, "%s/%s", tmpdir, base);
char *dot = strrchr(dst, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
}
static int
filehas(const char *path, const char *needle)
{
char buf[8192];
FILE *f = fopen(path, "rb");
if (!f) return 0;
size_t n = fread(buf, 1, sizeof buf - 1, f);
fclose(f);
buf[n] = '\0';
return strstr(buf, needle) != NULL;
}
/* build_should_fail — returns 0 when the build FAILS *and* the stage's
* own parse diagnostic appears; a crash wraps as a nonzero "w6c failed"
* with no diagnostic, so the substring distinguishes it (#20). The two
* stages' parse messages diverge with no common core (cstage "unexpected
* token in expression"/"expected ), got ;" vs wwstage "parse: expected
* expression"/"parse: expected ')' in tuple"), so `diag` is passed
* per-stage by the caller (a real fidelity gap, filed under #20). */
static int
build_should_fail(const char *driver, const char *label, const char *src,
const char *diag, int i)
{
char s[128], tmpdir[64], cmd[1024], bin[128], errf[128], rmcmd[160];
snprintf(tmpdir, sizeof tmpdir, "/tmp/tuptc_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(s, sizeof s, "%s/tuptc_%d_%d.ww", tmpdir, getpid(), i);
snprintf(errf, sizeof errf, "%s/tuptc_%d_e_%d.txt", 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);
/* explicit -o INSIDE tmpdir so the binary AND its <stem>.sepwork land
* under tmpdir and die with rm -rf; a reject leaks the mkdir'd scratch
* too, so clean on every exit path. */
outbin(bin, sizeof bin, tmpdir, s);
snprintf(cmd, sizeof cmd, "%s build -o %s %s >/dev/null 2>%s",
driver, bin, s, errf);
int rc = runwait(cmd);
int hasmsg = filehas(errf, diag);
runwait(rmcmd);
if (rc == 0) {
fprintf(stderr, "tuptc[%s][%s]: built ok, expected a reject\n",
driver, label);
return -1;
}
if (!hasmsg) {
fprintf(stderr, "tuptc[%s][%s]: nonzero exit but missing "
"diagnostic \"%s\" (a crash, not a clean reject)\n",
driver, label, diag);
return -1;
}
return 0;
}
/* run_build — build a program that MUST compile, run it, return exit. */
static int
run_build(const char *driver, const char *label, const char *src, int i)
{
char s[128], tmpdir[64], cmd[1024], bin[128], rmcmd[160];
snprintf(tmpdir, sizeof tmpdir, "/tmp/tuptc_ok_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(s, sizeof s, "%s/tuptc_ok_%d_%d.ww", tmpdir, getpid(), i);
snprintf(bin, sizeof bin, "%s/tuptc_ok_%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, bin, s);
if (runwait(cmd) != 0) {
fprintf(stderr, "tuptc[%s][%s]: build failed (a valid tuple "
"must compile)\n", driver, label);
runwait(rmcmd);
return -1;
}
int got = runwait(bin);
runwait(rmcmd);
return got;
}
struct rejrow {
const char *label;
const char *src;
const char *cdiag; /* cstage diagnostic body substring */
const char *wdiag; /* wwstage diagnostic body substring (#20 gap) */
};
static const struct rejrow reject_rows[] = {
{ "one_comma",
"package main;\n"
"fn main() i32 = { let t = (5,); return 0; };\n",
"unexpected token in expression", "expected expression" },
};
struct okrow { const char *label; const char *src; int want; };
static const struct okrow ok_rows[] = {
{ "two_elem",
"package main;\n"
"fn pair() (i32, i32) = { return (3, 4); };\n"
"fn main() i32 = { let a, b = pair(); return a + b; };\n", 7 },
{ "two_comma",
"package main;\n"
"fn pair() (i32, i32) = { return (3, 4,); };\n"
"fn main() i32 = { let a, b = pair(); return a + b; };\n", 7 },
};
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], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *path; int gated; } drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int nrej = (int)(sizeof reject_rows / sizeof reject_rows[0]);
int nok = (int)(sizeof ok_rows / sizeof ok_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, "tuptc: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < nrej; i++) {
total++;
if (build_should_fail(drivers[d].path,
reject_rows[i].label, reject_rows[i].src,
drivers[d].gated ? reject_rows[i].wdiag
: reject_rows[i].cdiag, d * 100 + i) != 0)
fail++;
}
for (int i = 0; i < nok; i++) {
total++;
int got = run_build(drivers[d].path, ok_rows[i].label,
ok_rows[i].src, d * 100 + i);
if (got != ok_rows[i].want) {
fprintf(stderr, "tuptc[%s][%s]: exit=%d want=%d\n",
drivers[d].name, ok_rows[i].label,
got, ok_rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "tuptc: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("tuptc: %d/%d ok\n", total, total);
return 0;
}