Files
ww/test/wcc/950_selfcheck.c
Hojun-Cho cdc8bda721 test/wcc: extend carrier ownership to the bootstrap and platform gates
Same contract as the 27 repaired byte/artifact survivors: checked
acquisition, one all-exit cleanup funnel per carrier, ENOENT-tolerant
checked unlinks, exact-path deletion, and cleanup failure fails a
passing carrier without overwriting its diagnostic. Also closes the
vacuous-green channels: empty-artifact pairs no longer byte-compare
equal, capture and staging failures fail their row loudly, 950 requires
a clean checker exit on no-error rows and hard-fails on a missing
wwdump_ww, 992 keys its scratch per invocation so a failed cleanup
cannot leak stale objects into the next tool's compare, and 995 no
longer pre-sweeps a workdir it never created. No assertion is weakened;
990_selfhost is retired in the next commit rather than repaired.
2026-08-07 23:28:49 +09:00

267 lines
8.1 KiB
C

/*
* 950_selfcheck — selfhost typechecker emits the same errors the
* C-side checker does for tagged-union misuse.
*
* Each row is a .ww fixture + an expected stderr substring. We run
* `wwdump_ww -r` (which invokes the selfhost checker's resolve +
* typed passes) and grep for the substring.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include <sys/wait.h>
#include "wwtestpkg.h"
static int
runcap(const char *cmd, char **outerr, size_t *outlen, int *outrc)
{
char tmp[256];
snprintf(tmp, sizeof tmp, "/tmp/wwd_sc_%d", getpid());
char full[2048];
snprintf(full, sizeof full, "%s 2>%s 1>/dev/null", cmd, tmp);
int ret = -1;
FILE *f = NULL;
char *b = NULL;
long n = 0;
int rc = system(full);
if (rc == -1 || !WIFEXITED(rc)) goto cleanup;
f = fopen(tmp, "rb");
if (!f) { perror(tmp); goto cleanup; }
if (fseek(f, 0, SEEK_END) != 0) { perror(tmp); goto cleanup; }
n = ftell(f);
if (n < 0) { perror(tmp); goto cleanup; }
if (fseek(f, 0, SEEK_SET) != 0) { perror(tmp); goto cleanup; }
b = malloc((size_t)n + 1);
if (!b) goto cleanup;
if (fread(b, 1, (size_t)n, f) != (size_t)n) goto cleanup;
b[n] = '\0';
ret = 0;
cleanup:
/* the shell may never have created tmp (fork failure). */
if (f) fclose(f);
if (unlink(tmp) != 0 && errno != ENOENT) {
perror(tmp);
ret = -1;
}
if (ret != 0) {
free(b);
return -1;
}
*outerr = b; *outlen = (size_t)n; *outrc = WEXITSTATUS(rc);
return 0;
}
static int
writefile(const char *path, const char *src)
{
FILE *f = fopen(path, "wb");
if (!f) return -1;
wwtest_fputs(src, f);
/* wwtest_fputs is void; ferror + fclose's flush surface a short
* write (ENOSPC) that would otherwise compile a truncated fixture. */
int werr = ferror(f) != 0;
if (fclose(f) != 0 || werr) {
perror(path);
if (unlink(path) != 0 && errno != ENOENT)
perror(path);
return -1;
}
return 0;
}
struct row { const char *src; const char *expect; };
static const struct row rows[] = {
/* match: missing variant */
{ "fn pick() (i32 | str | bool) = { return 1; };\n"
"fn caller() void = {\n"
" let v: (i32 | str | bool) = pick();\n"
" match (v) {\n"
" case let x: i32 => { };\n"
" case let x: str => { };\n"
" };\n"
"};\n",
"match: variant not handled" },
/* ?: enclosing not tagged */
{ "fn inner() (i32 | str) = { return 1; };\n"
"fn caller() i32 = {\n"
" let v: i32 = inner()?;\n"
" return v;\n"
"};\n",
"?: enclosing fn return is not tagged" },
/* ?: error variant not in enclosing return */
{ "fn inner() (i32 | str | bool) = { return 1; };\n"
"fn caller() (i32 | str) = {\n"
" let v: i32 = inner()?;\n"
" return v;\n"
"};\n",
"?: error variant not in enclosing return" },
/* !-flag aware: legal case (one success, two errors propagated) */
{ "type invalid = !i32;\n"
"type overflow = !void;\n"
"fn parse() (i64 | invalid | overflow) = { return 0i64; };\n"
"fn caller() (i64 | invalid | overflow) = {\n"
" let v: i64 = parse()?;\n"
" return v + 1;\n"
"};\n",
NULL }, /* expect no error */
/* !-flag aware: missing one error variant in enclosing return */
{ "type invalid = !i32;\n"
"type overflow = !void;\n"
"fn parse() (i64 | invalid | overflow) = { return 0i64; };\n"
"fn caller() (i64 | invalid) = {\n"
" let v: i64 = parse()?;\n"
" return v + 1;\n"
"};\n",
"?: error variant not in enclosing return" },
/* case T => names a non-variant */
{ "fn pick() (i32 | str) = { return 1; };\n"
"fn caller() void = {\n"
" let v: (i32 | str) = pick();\n"
" match (v) {\n"
" case let n: i32 => { };\n"
" case let s: str => { };\n"
" case let f: f64 => { };\n"
" };\n"
"};\n",
"case: not a variant" },
/* is T where T isn't a variant of the operand */
{ "fn pick() (i32 | str) = { return 1; };\n"
"fn caller() void = {\n"
" let v: (i32 | str) = pick();\n"
" if (v is f64) { };\n"
"};\n",
"is/as: not a variant" },
/* let init-type mismatch on primitives */
{ "fn caller() void = {\n"
" let x: bool = 42;\n"
"};\n",
"let: not assignable" },
/* return type mismatch */
{ "fn caller() i32 = {\n"
" return \"hi\";\n"
"};\n",
"return: not assignable" },
/* #31: alloc(value) returns (*T | nomem); bare LHS without `!`
* must be rejected — pre-fix, exprtype returned the seeded
* builtin's nil lhs and checkletassign silently passed. */
{ "fn caller() void = {\n"
" let p: *u8 = alloc(0u8: u8);\n"
"};\n",
"let: not assignable" },
/* #31: with `!` the unwrap projects the *u8 success variant,
* so the let must be accepted. */
{ "fn caller() void = {\n"
" let p: *u8 = alloc(0u8: u8)!;\n"
"};\n",
NULL },
/* #31: slice form `alloc([], n)` returns ([]u8 | nomem); the
* tagged → non-tagged rule must also fire for the bare LHS. */
{ "fn caller() void = {\n"
" let s: []u8 = alloc([], 16);\n"
"};\n",
"let: not assignable" },
/* #31: with `!` the slice success variant projects to []u8. */
{ "fn caller() void = {\n"
" let s: []u8 = alloc([], 16)!;\n"
"};\n",
NULL },
/* #45: alloc([], n) defers element type to LHS context — `[]str`
* with `!` must be accepted (cstage clet retypes, wwstage
* checkletassign mirror). Pre-#45 this errored with
* "init []u8 not assignable to declared []str" on cstage. */
{ "fn caller() void = {\n"
" let s: []str = alloc([], 4)!;\n"
"};\n",
NULL },
/* #45: same idiom in `?` form inside a (T | nomem) fn. */
{ "fn caller() (i32 | nomem) = {\n"
" let s: []str = alloc([], 4)?;\n"
" return s.cap: i32;\n"
"};\n",
NULL },
};
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char wwdump[2048];
snprintf(wwdump, sizeof wwdump, "%s/wwdump_ww", bin);
/* bootstrap gate: a missing selfhost checker must FAIL, not skip. */
if (access(wwdump, X_OK) != 0) {
fprintf(stderr, "selfcheck: %s missing or not executable\n",
wwdump);
return 1;
}
int n = sizeof rows / sizeof rows[0];
int fail = 0, cleanfail = 0;
for (int i = 0; i < n; i++) {
char path[64];
snprintf(path, sizeof path, "/tmp/wwd_sc_%d_%d.ww", getpid(), i);
if (writefile(path, rows[i].src) != 0) { fail++; continue; }
char cmd[2048];
snprintf(cmd, sizeof cmd, "%s -r %s", wwdump, path);
char *err = NULL; size_t elen = 0;
int xrc = -1;
if (runcap(cmd, &err, &elen, &xrc) != 0) {
fprintf(stderr, "row %d: capture failed\n", i);
fail++;
goto rowcleanup;
}
const char *want = rows[i].expect;
int got_match = (err && want && strstr(err, want) != NULL);
int expected_no_err = (want == NULL);
int err_present = err && strstr(err,
"match: variant not handled") != NULL;
err_present = err_present || (err && strstr(err,
"?: error variant not in enclosing return") != NULL);
err_present = err_present || (err && strstr(err,
"?: enclosing fn return is not tagged") != NULL);
err_present = err_present || (err && strstr(err,
"?: enclosing fn has no tagged-union return") != NULL);
err_present = err_present || (err && strstr(err,
"case: not a variant") != NULL);
err_present = err_present || (err && strstr(err,
"is/as: not a variant") != NULL);
err_present = err_present || (err && strstr(err,
"is/as: operand is not a tagged union") != NULL);
err_present = err_present || (err && strstr(err,
"let: not assignable") != NULL);
err_present = err_present || (err && strstr(err,
"return: not assignable") != NULL);
int ok;
/* a checker that dies with empty stderr must not pass a
* no-error row: require a clean exit too. */
if (expected_no_err) ok = !err_present && xrc == 0;
else ok = got_match;
if (!ok) {
fprintf(stderr,
"row %d failed: expected %s, got stderr:\n%s\n",
i, want ? want : "(no error)", err ? err : "(empty)");
fail++;
}
rowcleanup:
free(err);
if (unlink(path) != 0 && errno != ENOENT) {
perror(path);
cleanfail = 1;
}
}
if (fail) {
fprintf(stderr, "%d/%d selfcheck rows failed\n", fail, n);
return 1;
}
if (cleanfail) {
fprintf(stderr, "selfcheck: cleanup failed\n");
return 1;
}
printf("selfcheck: %d/%d ok\n", n, n);
return 0;
}