Files
ww/test/wcc/814_def_arr_infer_len.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

379 lines
12 KiB
C

/*
* 814_def_arr_infer_len — cstage and wwstage agree, byte-for-byte and at
* runtime, that a `def NAME: [_]T = [...]` array infers its length from
* the initialiser's element count — the `def` twin of the `let` path
* pinned by 684 (task #11 / #5; #7 wired only the let decl path).
*
* The bug (BOTH stages, byte-id identical): the `[_]` infer sentinel
* (alen=0 / nil length-child) was stamped on the `let` decl path but NOT
* on the `def` decl path. A `def [_]int = [10,20,30]` stayed sized 0,
* emitted no/short DATA, and indexed reads (`TAB[1]`) / `.len` returned
* garbage with no diagnostic (rule-7 silent miscompile).
*
* The fix (CHECKER ONLY — cgen already keys stride/length/DATA off the
* stamped length, so `def [3]int` works today):
* - cstage cmd/wcc/check.c N_DEF pass-2: mirror the module N_LET path —
* if d->type is `[_]T` (TY_ARRAY, alen==0), chase the rhs type,
* rebuild type_array(.., iu->alen), and re-point BOTH d->type AND the
* installed SK_DEF Sym (an indexed read resolves the def through its
* Sym, so the Sym repoint is required).
* - wwstage selfhost/cmd/wcc/check.ww N_DEF arm: call inferarraylen(c,d)
* BEFORE resolvewalk stamps d.lhs's tinfo (the N_TARRAY AST is the
* SSoT; no Sym repoint needed). A `def [_]T` whose init is not an
* array literal can't infer → LOUD error, never a silent length-0.
*
* `def` is MODULE-SCOPE ONLY in both stages, so there is exactly one def
* site per stage and every element type rides the single inference arm
* (it is gated only on TY_ARRAY+alen==0, agnostic to the element type).
*
* SCOPE (corrected at impl-probe — the inference is complete, but two
* adjacent shapes hit SEPARATE PRE-EXISTING cgen gaps that fail the SAME
* way on an explicit-length `def [N]T`, so they are NOT #11 and are
* excluded here; each is filed as its own task):
* - `def`-array `.len`/`.ptr` on wwstage falls to the SB fallback and
* emits `MOVQ len(SB)` (w6l: undefined reference). cstage handles it
* (so cstage vs wwstage asm also DIVERGES) — the wwstage cgdot
* def-array arm is missing, the def twin of #7's let-global cgdot
* fix. Every `.len` row is dropped for this reason.
* - `def [_]str` / `def [N]str` emit no DATA symbol (w6l: undefined
* reference, BOTH stages) — the #270 / str-slice-array-element DATA
* lineage. Every str row is dropped for this reason.
* The pin therefore exercises INDEX reads across int / u8 / multi-dim,
* which is exactly the shape #11 corrupts and is mutation-sensitive
* (a collapse-to-0 def lays no DATA row, so the indexed read strikes
* garbage / segv).
*
* The NEG row (`def [_]int = 5`, non-array init) proves the loud-error
* arm fires (rule-7: never a silent zero-length array).
*
* row | shape | want
* --------------+--------------------------------------------+-----
* def_int_i1 | def [_]int=[10,20,30], TAB[1] (ken oracle)| 20
* def_int_last | def [_]int=[10,20,30], TAB[2] (full len) | 30
* def_one_elem | def [_]int=[7], A[0] (1-elem edge) | 7
* def_u8_first | def [_]u8=[1..5], B[0] (narrow stride) | 1
* def_u8_last | def [_]u8=[1..5], B[4] | 5
* def_2d_first | def [_][2]int=[[1,2],[3,4],[5,6]], D[0][0] | 1
* def_2d_last | def [_][2]int=[..], D[2][1] | 6
* NEG def_noarr | def [_]int = 5; (non-array init) | BUILD FAIL
*/
#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[] = {
/* INDEX reads only — the shape #11 corrupts. Reading the LAST
* element of an N-element def proves the inference stamped the full
* length: a collapse-to-0 lays no DATA row at that offset, so the
* read strikes garbage / segv. This is mutation-sensitive without
* `.len` (whose def-array codegen is a SEPARATE pre-existing gap —
* see the header). */
{ "def_int_i1",
"package main;\n"
"def TAB: [_]int = [10, 20, 30];\n"
"export fn main() i32 = {\n"
"\treturn TAB[1]: i32;\n"
"};\n",
20 },
{ "def_int_last",
"package main;\n"
"def TAB: [_]int = [10, 20, 30];\n"
"export fn main() i32 = {\n"
"\treturn TAB[2]: i32;\n"
"};\n",
30 },
/* 1-element edge — the minimal non-empty count. */
{ "def_one_elem",
"package main;\n"
"def A: [_]int = [7];\n"
"export fn main() i32 = {\n"
"\treturn A[0]: i32;\n"
"};\n",
7 },
{ "def_u8_first",
"package main;\n"
"def B: [_]u8 = [1u8, 2u8, 3u8, 4u8, 5u8];\n"
"export fn main() i32 = {\n"
"\treturn B[0]: i32;\n"
"};\n",
1 },
/* narrow (1B) stride — the last element of 5 proves both the stride
* and the inferred length. */
{ "def_u8_last",
"package main;\n"
"def B: [_]u8 = [1u8, 2u8, 3u8, 4u8, 5u8];\n"
"export fn main() i32 = {\n"
"\treturn B[4]: i32;\n"
"};\n",
5 },
/* multi-dim: outer `[_]` infers 3 from the row count, element type
* is the explicit `[2]int`. The def static-init DATA path lays the
* full 3x2 aggregate (the `let` runtime-store path for this shape is
* separately #270-1c-blocked, so this is def-only today). */
{ "def_2d_first",
"package main;\n"
"def D: [_][2]int = [[1, 2], [3, 4], [5, 6]];\n"
"export fn main() i32 = {\n"
"\treturn D[0][0]: i32;\n"
"};\n",
1 },
{ "def_2d_last",
"package main;\n"
"def D: [_][2]int = [[1, 2], [3, 4], [5, 6]];\n"
"export fn main() i32 = {\n"
"\treturn D[2][1]: i32;\n"
"};\n",
6 },
};
/* `def [_]T` whose initialiser is not an array literal can't infer its
* length — both stages must FAIL the build (loud diagnostic, not a silent
* zero-length array). `def` requires an `= value` init (parser), so the
* no-init case can't reach the checker; the non-array init is the only
* negative shape. */
static const char *neg[] = {
/* non-array initialiser */
"package main;\n"
"def TAB: [_]int = 5;\n"
"export fn main() i32 = { return 0; };\n",
};
/* Per-neg expected diagnostic body (parallel to neg[]); shared by both
* stages (cstage prepends file:line:col, wwstage does not — #20). */
static const char *neg_diag[] = {
"[_]T needs an array-literal initialiser",
};
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/dail_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/dail_%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 — a `def [_]T` that can't infer 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 check 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/dailn_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/dailn_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/dailn_%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.
* The bug is byte-id-BLIND (both stages emitted identical wrong asm), so
* this is a rule-10 convergence invariant, not a mutation detector. */
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/dail_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/dail_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/dail_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, "def_arr_infer_len: 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,
"def_arr_infer_len[%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,
"def_arr_infer_len[%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,
"def_arr_infer_len: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("def_arr_infer_len: %d/%d ok\n", total, total);
return 0;
}