Files
ww/test/wcc/817_arr_cap_reject.c
Hojun-Cho e8ef1e061c test: require diagnostic on reject rows, close crash-vacuity (#20)
Reject helpers checked only rc!=0, so a SEGFAULT (exit 139, or the
driver's "w6c failed" exit 1) counted as a clean reject -- a wwstage
crash could pass vacuously (it did, latently, on bodied bare-... pre
#11). Every reject row now captures stderr and requires the actual
diagnostic substring (rc!=0 AND strstr) across all 16 reject tests; a
crash emits no diagnostic, so it now fails. This is the
differential-reject backstop (#15): both stages must cleanly reject
with the expected message.

Two genuine cstage/wwstage diagnostic-body divergences are documented
inline via per-stage substrings, not papered over (845 tuple parse,
catA_f2 tuple arity); catalogued in #21.
2026-06-21 13:04:59 +09:00

343 lines
10 KiB
C

/*
* 817_arr_cap_reject — a fixed-size array has NO capacity word; `.cap` on an
* array is INVALID ww and BOTH stages must LOUDLY REJECT at check time
* (GAP-A.cap, task #12; drew ruling .ai/drew-gapa-ptr-ruling.md). `.len`
* (GAP-A.len, 7b0e09e) AND `.ptr` (≡ &A[0], the sanctioned ww backing-
* pointer spelling, task #13 divergence-record) stay VALID on an array —
* only `.cap` is rejected.
*
* The bug (each stage misbehaved DIFFERENTLY on the same invalid construct,
* hence both-stage):
* - cstage check.c typed `.cap` on a TY_ARRAY as i32 → cgen then hard-
* rejected with a generic "unsupported field-read shape" (loud but at
* the WRONG layer, vague message).
* - wwstage check.ww stamped `.cap` → cgen link-error (def-global
* `A.cap` → w6l undefined 'cap') or SILENT garbage (local `a.cap`→30).
*
* The fix (BOTH checkers, one `.cap`-on-array gate each, byte-identical
* diagnostic body): reject early with "no field 'cap' on a fixed-size array
* (arrays have no capacity; use .len)". Checker-only: the reject makes the
* buggy cgen `.cap` array paths unreachable (close by construction).
*
* Mutation-sanity: the genuinely-silent pre-fix case is wwstage's LOCAL
* .cap, which BUILT and returned garbage (30) — neg_local_cap is the row
* with real mutation power (built-ok pre-fix → caught here). The other
* neg rows were already loud pre-fix but at the WRONG layer (cstage cgen
* vague-reject on use; wwstage global link-error); this commit converges
* all of them onto one early checker diagnostic.
*
* neg row | shape | gate
* -----------------+----------------------------------------+----------
* neg_local_cap | local [3]int, a.cap | build FAIL
* neg_def_cap | def A:[3]int, A.cap | build FAIL
* neg_let_glob_cap | let G:[3]int (module), G.cap | build FAIL
* neg_infer_cap | def A:[_]int, A.cap (#11 infer path) | build FAIL
*
* pos row | shape | want
* -----------------+----------------------------------------+------
* pos_local_len | local [3]int, a.len | 3
* pos_def_len | def A:[_]int, A.len | 3
* pos_local_ptr | local [3]int, *a.ptr (sanctioned idiom) | 10
*
* The pos_local_ptr row pins that `.ptr`-on-array stays VALID (cgen
* unchanged in this commit; the def-global `.ptr` cgen fix + its build+run
* pin are task #11 / test 818, NOT here — so this control uses a LOCAL
* array `.ptr`, which is correct in both stages today).
*/
#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;
}
struct row { const char *label; const char *src; int want; };
static const struct row rows[] = {
{ "pos_local_len",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet a: [3]int = [10, 20, 30];\n"
"\treturn a.len: i32;\n"
"};\n",
3 },
{ "pos_def_len",
"package main;\n"
"def A: [_]int = [10, 20, 30];\n"
"export fn main() i32 = {\n"
"\treturn A.len: i32;\n"
"};\n",
3 },
/* `.ptr`-on-array is the sanctioned ww spelling for &a[0] (task #13).
* LOCAL array `.ptr` is correct in BOTH stages today (cgen unchanged
* here); the def-global `.ptr` cgen fix is task #11 / test 818. */
{ "pos_local_ptr",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet a: [3]int = [10, 20, 30];\n"
"\tlet p: *int = a.ptr;\n"
"\treturn (*p): i32;\n"
"};\n",
10 },
};
/* `.cap` on a fixed array — both stages must FAIL the build (loud checker
* diagnostic, not silent garbage / link-error). */
static const char *neg[] = {
/* neg_local_cap */
"package main;\n"
"export fn main() i32 = {\n"
"\tlet a: [3]int = [1, 2, 3];\n"
"\treturn a.cap: i32;\n"
"};\n",
/* neg_def_cap */
"package main;\n"
"def A: [3]int = [1, 2, 3];\n"
"export fn main() i32 = {\n"
"\treturn A.cap: i32;\n"
"};\n",
/* neg_let_glob_cap */
"package main;\n"
"let G: [3]int = [1, 2, 3];\n"
"export fn main() i32 = {\n"
"\treturn G.cap: i32;\n"
"};\n",
/* neg_infer_cap — the #11 [_] infer path */
"package main;\n"
"def A: [_]int = [1, 2, 3];\n"
"export fn main() i32 = {\n"
"\treturn A.cap: i32;\n"
"};\n",
};
/* Per-neg expected diagnostic body (parallel to neg[]); all four rows hit
* the same "no field 'cap' on a fixed-size array" path on both stages. */
static const char *neg_diag[] = {
"no field 'cap' on a fixed-size array",
"no field 'cap' on a fixed-size array",
"no field 'cap' on a fixed-size array",
"no field 'cap' on a fixed-size array",
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/acr_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/acr_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return got;
}
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 — `.cap` on an array must error on `driver`; returns 0
* when the build FAILS *and* emits `diag`. A crash (segfault) wraps as a
* nonzero "w6c failed" with no diagnostic, so the substring distinguishes
* it from a clean reject (#20). */
static int
build_should_fail(const char *driver, const char *src, const char *diag,
int i)
{
char s[64], tmpdir[64], cmd[1024], errf[64];
snprintf(s, sizeof s, "/tmp/acrn_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/acrn_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/acrn_%d_e_%d.txt", getpid(), i);
FILE *f = fopen(s, "wb");
if (!f) return -1;
fputs(src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s >/dev/null 2>%s",
tmpdir, driver, s, errf);
int rc = runwait(cmd);
int hasmsg = filehas(errf, diag);
unlink(s);
unlink(errf);
/* clean any emitted binary */
const char *base = strrchr(s, '/');
base = base ? base + 1 : s;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
unlink(outbin);
rmdir(tmpdir);
/* build must NOT succeed AND must emit its diagnostic. */
return (rc != 0 && hasmsg) ? 0 : -1;
}
/* 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/acr_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/acr_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/acr_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 nn = (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, "arr_cap_reject: 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,
"arr_cap_reject[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
for (int i = 0; i < nn; i++) {
total++;
if (build_should_fail(drivers[d].path, neg[i],
neg_diag[i], 100 + i) != 0) {
fprintf(stderr,
"arr_cap_reject[%s][neg%d]: built ok, "
"expected a loud error\n",
drivers[d].name, i);
fail++;
}
}
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0)
fail++;
}
}
if (fail) {
fprintf(stderr,
"arr_cap_reject: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("arr_cap_reject: %d/%d ok\n", total, total);
return 0;
}