Files
ww/test/wcc/684_arr_infer_len.c
Hojun-Cho a95a7a316b test/wcc: carrier ownership repair and driver-contract adaptation
Every surviving carrier now owns its artifacts: checked mkdir/mkdtemp/
fopen acquisition, one all-exit cleanup funnel per carrier, ENOENT-
tolerant checked unlinks, exact-path deletion (rm -rf only for an
owned pid-keyed dir or a .sepwork beneath one), and cleanup failure
fails a passing carrier without overwriting its diagnostic. In the
same pass the carriers adapt to the driver contract this branch lands:
--sep and WW_PKGCACHE are gone, -S and the /tmp/ww_run_<pid> scratch
contract are asserted, and rows whose runtime or reject coverage moved
to test/wcc/data fixtures or test/lang @test owners are trimmed to the
byte/artifact/diagnostic observations only they can make.

Repair and adaptation ride together because most files interleave both
in the same hunks; splitting would manufacture intermediate carrier
states that never existed and cannot run against either driver.
2026-08-07 23:21:04 +09:00

409 lines
12 KiB
C

/*
* 684_arr_infer_len — cstage and wwstage agree, byte-for-byte and at
* runtime, that a `[_]T = [...]` array infers its length from the
* initialiser's element count (task #7, a canonical Hare form).
*
* The bug: `[_]T` silently miscompiled to a zero-length array. The
* parser already left the array type's length child nil as the infer
* sentinel (distinct from an explicit `[N]`), but neither checker
* stamped the real count — so `len(x)` / `x.len` returned 0 with no
* diagnostic (rule-7 silent miscompile). Module-level was worse on
* wwstage: `x.len` on ANY global array (even explicit `[N]`) fell to
* the SB fallback and mis-emitted `MOVQ len(SB), AX` (linker:
* undefined reference to len).
*
* The fix (BOTH stages, converged byte-identical):
* - checker (cmd/wcc/check.c clet + module-level N_LET;
* selfhost/cmd/wcc/check.ww inferarraylen): count the array-literal
* elements and stamp the length onto the array TYPE. cgen already
* keys stride/length/data off the stamped length, so it "just
* works" (rob's #7 ruling). A `[_]T` with no array-literal init
* can't infer → LOUD error, never a silent zero-length array.
* - cgen (selfhost/cmd/wcc/cgenexpr.ww cgdot): a top-level `[N]T`
* global's `.len` / `.ptr` pseudo-fields, the wwstage arm that was
* missing (cstage cgen.c:8011 already handled it).
*
* Coverage: `[_]int` / `[_]str` / `[_]u8`, both local and module-level,
* `len` read-back and element read-back, dual-stage runtime + asm
* byte-id. Mutation-sanity: the old collapse-to-0 fails every `*_len`
* row (len would be 0, not the count). Plus three negative rows where
* `[_]T` can't infer (no init / non-array init) — both stages must
* FAIL the build.
*
* row | shape | want
* ----------------+--------------------------------------+------
* local_int_len | local [_]int=[10,20,30,40], x.len | 4
* local_int_elem | local, x[2] | 30
* mod_int_len | global [_]int=[..], x.len | 4
* mod_int_elem | global, x[2] | 30
* local_str_len | local [_]str=["a","b","c"], x.len | 3
* local_str_elem | local [_]str=["ab","cde"], x[0].len | 2
* mod_str_len | global [_]str=["a","b","c"], x.len | 3
* local_u8_len | local [_]u8=[1..5], x.len | 5
* local_u8_elem | local, x[4] | 5
* local_one_len | local [_]int=[7], x.len (1-elem edge)| 1
* mod_one_len | global [_]int=[7], x.len | 1
* mod_one_elem | global, x[0] | 7
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.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[] = {
{ "local_int_len",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x: [_]int = [10, 20, 30, 40];\n"
"\treturn x.len: i32;\n"
"};\n",
4 },
{ "local_int_elem",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x: [_]int = [10, 20, 30, 40];\n"
"\treturn x[2]: i32;\n"
"};\n",
30 },
{ "mod_int_len",
"package main;\n"
"let x: [_]int = [10, 20, 30, 40];\n"
"export fn main() i32 = {\n"
"\treturn x.len: i32;\n"
"};\n",
4 },
{ "mod_int_elem",
"package main;\n"
"let x: [_]int = [10, 20, 30, 40];\n"
"export fn main() i32 = {\n"
"\treturn x[2]: i32;\n"
"};\n",
30 },
{ "local_str_len",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x: [_]str = [\"a\", \"b\", \"c\"];\n"
"\treturn x.len: i32;\n"
"};\n",
3 },
{ "local_str_elem",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x: [_]str = [\"ab\", \"cde\"];\n"
"\treturn x[0].len: i32;\n"
"};\n",
2 },
{ "mod_str_len",
"package main;\n"
"let x: [_]str = [\"a\", \"b\", \"c\"];\n"
"export fn main() i32 = {\n"
"\treturn x.len: i32;\n"
"};\n",
3 },
{ "local_u8_len",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x: [_]u8 = [1u8, 2u8, 3u8, 4u8, 5u8];\n"
"\treturn x.len: i32;\n"
"};\n",
5 },
{ "local_u8_elem",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x: [_]u8 = [1u8, 2u8, 3u8, 4u8, 5u8];\n"
"\treturn x[4]: i32;\n"
"};\n",
5 },
/* 1-element edge — the minimal non-empty count; under the old
* collapse-to-0 bug `.len` reads 0, not 1, so this row also fails
* the mutation. */
{ "local_one_len",
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x: [_]int = [7];\n"
"\treturn x.len: i32;\n"
"};\n",
1 },
{ "mod_one_len",
"package main;\n"
"let x: [_]int = [7];\n"
"export fn main() i32 = {\n"
"\treturn x.len: i32;\n"
"};\n",
1 },
{ "mod_one_elem",
"package main;\n"
"let x: [_]int = [7];\n"
"export fn main() i32 = {\n"
"\treturn x[0]: i32;\n"
"};\n",
7 },
};
/* `[_]T` that can't infer its length — both stages must FAIL the build
* (loud diagnostic, not a silent zero-length array). */
static const char *neg[] = {
/* no init, local */
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x: [_]str;\n"
"\treturn 0;\n"
"};\n",
/* no init, module-level */
"package main;\n"
"let x: [_]int;\n"
"export fn main() i32 = { return 0; };\n",
/* non-array initialiser */
"package main;\n"
"export fn main() i32 = {\n"
"\tlet x: [_]int = 5;\n"
"\treturn 0;\n"
"};\n",
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], scratch[160], rmcmd[192], cmd[1024];
int result = -1, setupfail = 0, cleanfail = 0;
snprintf(tmpdir, sizeof tmpdir, "/tmp/ail_%d_d_%d", getpid(), i);
if (mkdir(tmpdir, 0755) != 0) return -1;
snprintf(src, sizeof src, "%s/ail_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/ail_%d_%d", tmpdir, getpid(), i);
snprintf(scratch, sizeof scratch, "%s.sepwork", outbin);
FILE *f = fopen(src, "wb");
if (!f) { setupfail = 1; goto cleanup; }
int writefail = fputs(r->src, f) == EOF;
if (fclose(f) != 0) writefail = 1;
if (writefail) { setupfail = 1; goto cleanup; }
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);
goto cleanup;
}
result = runwait(outbin);
cleanup:
if (setupfail)
fprintf(stderr, "row[%s]: temporary setup failed\n", r->label);
snprintf(rmcmd, sizeof rmcmd, "rm -rf -- %s", scratch);
if (runwait(rmcmd) != 0) cleanfail = 1;
if (unlink(src) != 0 && errno != ENOENT) cleanfail = 1;
if (unlink(outbin) != 0 && errno != ENOENT) cleanfail = 1;
if (rmdir(tmpdir) != 0) cleanfail = 1;
if (cleanfail) {
fprintf(stderr, "row[%s]: temporary cleanup failed\n", r->label);
if (result == r->want) return -1;
}
return result;
}
/* build_should_fail — a `[_]T` that can't infer must error on `driver`;
* returns 0 when the build correctly FAILS, positive when it wrongly
* succeeds, and negative on fixture infrastructure failure. */
static int
build_should_fail(const char *driver, const char *src, int i)
{
char tmpdir[64], s[128], outbin[128], scratch[160], rmcmd[192], cmd[1024];
int result = -1, setupfail = 0, cleanfail = 0;
snprintf(tmpdir, sizeof tmpdir, "/tmp/ailn_%d_d_%d", getpid(), i);
if (mkdir(tmpdir, 0755) != 0) return -1;
snprintf(s, sizeof s, "%s/ailn_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/ailn_%d_%d", tmpdir, getpid(), i);
snprintf(scratch, sizeof scratch, "%s.sepwork", outbin);
FILE *f = fopen(s, "wb");
if (!f) { setupfail = 1; goto cleanup; }
int writefail = fputs(src, f) == EOF;
if (fclose(f) != 0) writefail = 1;
if (writefail) { setupfail = 1; goto cleanup; }
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
driver, outbin, s);
int rc = runwait(cmd);
result = rc == 0 ? 1 : 0; /* build must NOT succeed */
cleanup:
if (setupfail)
fprintf(stderr, "arr_infer_len[neg%d]: temporary setup failed\n", i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf -- %s", scratch);
if (runwait(rmcmd) != 0) cleanfail = 1;
if (unlink(s) != 0 && errno != ENOENT) cleanfail = 1;
if (unlink(outbin) != 0 && errno != ENOENT) cleanfail = 1;
if (rmdir(tmpdir) != 0) cleanfail = 1;
if (cleanfail) {
fprintf(stderr, "arr_infer_len[neg%d]: temporary cleanup failed\n", i);
if (result == 0) return -1;
}
return result;
}
/* 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 tmpdir[64], src[96], cs[96], ws[96], cmd[1024];
int rc = -1, cleanfail = 0;
snprintf(tmpdir, sizeof tmpdir, "/tmp/ail_asm_%d_%d", getpid(), i);
if (mkdir(tmpdir, 0755) != 0) return -1;
snprintf(src, sizeof src, "%s/t.ww", tmpdir);
snprintf(cs, sizeof cs, "%s/c.s", tmpdir);
snprintf(ws, sizeof ws, "%s/w.s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) goto cleanup;
int writefail = fputs(r->src, f) == EOF;
if (fclose(f) != 0) writefail = 1;
if (writefail) goto cleanup;
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);
goto cleanup;
}
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);
goto cleanup;
}
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
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);
cleanup:
if (unlink(src) != 0 && errno != ENOENT) cleanfail = 1;
if (unlink(cs) != 0 && errno != ENOENT) cleanfail = 1;
if (unlink(ws) != 0 && errno != ENOENT) cleanfail = 1;
if (rmdir(tmpdir) != 0) cleanfail = 1;
if (cleanfail) {
fprintf(stderr, "row[%s]: temporary cleanup failed\n", r->label);
return -1;
}
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_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,
"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++;
int nr = build_should_fail(drivers[d].path, neg[i], 100 + i);
if (nr != 0) {
if (nr > 0)
fprintf(stderr,
"arr_infer_len[%s][neg%d]: built ok, "
"expected a loud error\n",
drivers[d].name, i);
else
fprintf(stderr,
"arr_infer_len[%s][neg%d]: fixture infrastructure failed\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_infer_len: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("arr_infer_len: %d/%d ok\n", total, total);
return 0;
}