Files
ww/test/wcc/687_slice_literal_global.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

417 lines
14 KiB
C

/*
* 687_slice_literal_global — module-level slice-literal static init
* `let g: []T = [v0, v1, …];` materializes a writable backing + a 24B
* { ptr, len, cap } header + a DATAR patching ptr -> backing, and cstage
* / wwstage agree byte-for-byte and at runtime (task #10 part a).
*
* The bug: emit_lets / emitletdataw had str-lit + array-lit arms but no
* slice-lit arm, so a `let g: []u8 = [1u8,2u8,3u8]` emitted NO `DATAW
* main.g` — BOTH stages failed to link ("undefined reference to main.g").
* byte-id-blind; only the link step exposed it. (wwstage further needed
* #18: its checker desugared the module-level initializer to an N_SLICE
* runtime borrow, so the rhs reached cgen as N_SLICE not N_ARRLIT — fixed
* first so this arm sees the raw array literal, mirroring cstage.)
*
* The fix (BOTH stages, byte-identical): emit_slice_data / emitslicedata
* route the k element bytes through the emit_array_lit_bytes choke-point
* (synthesized [k]T), then emit the header (ptr placeholder + LE len + LE
* cap, all = k) and the ptr-patch DATAR. Backing symbol "<mangled g>.d".
*
* Coverage: []u8 (element read-back + len + cap + sum), []i64 (wider
* element), []i32, and a 1-element edge. Plus dual-stage asm byte-id.
* Mutation-sanity: the old no-emit fails every row at LINK. Plus negative
* rows where the slice-literal static init must FAIL the build loudly
* (rule 7): read-only `def`, `...` repeat, and slice-of-str (per-element
* relocs / #17 deferred).
*
* row | shape | want
* ----------------+------------------------------------------+------
* u8_e0 | let g:[]u8=[1,2,3]; g[0] | 1
* u8_e2 | g[2] | 3
* u8_len | g.len | 3
* u8_cap | g.cap | 3
* u8_sum | g[0]+g[1]+g[2]+g.len+g.cap | 12
* i64_e1 | let g:[]i64=[10,20,30]; g[1] | 20
* i64_lencap | g.len+g.cap | 6
* i32_sum | let g:[]i32=[7,8,9]; g[0]+g[1]+g[2] | 24
* one_edge | let g:[]u8=[42]; g[0]+g.len | 43
* struct_pt | []pt=[pt{1,2},pt{3,4}]; sum+len+cap | 14
* struct_3f | []q (u8,i64,i32) two elems; sum+len | 23
* struct_one | []pt=[pt{5,6}]; sum+len+cap (count==1) | 13
* struct_arrvar_l | local let arr:[2]pt; let g:[]pt=arr | 14
*
* #19: wwstage's checker over-rejected the inline NAMED-STRUCT-element
* array->slice (`let g:[]pt=[pt{..},pt{..}]`) — its N_ARRLIT element-type
* inference returned the N_STRUCTLIT body (N_TSTRUCT, per #66) and the
* array->slice isassignable typeeqast saw a TNAME-vs-TSTRUCT kind mismatch
* against the declared N_TNAME element. cstage already inferred the NAMED
* type. Fix: infer the named type for a named struct-literal first element
* (check.ww N_ARRLIT arm); typeeqast untouched.
*/
#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[] = {
{ "u8_e0",
"package main;\n"
"let g: []u8 = [1u8, 2u8, 3u8];\n"
"export fn main() i32 = { return g[0]: i32; };\n",
1 },
{ "u8_e2",
"package main;\n"
"let g: []u8 = [1u8, 2u8, 3u8];\n"
"export fn main() i32 = { return g[2]: i32; };\n",
3 },
{ "u8_len",
"package main;\n"
"let g: []u8 = [1u8, 2u8, 3u8];\n"
"export fn main() i32 = { return g.len: i32; };\n",
3 },
{ "u8_cap",
"package main;\n"
"let g: []u8 = [1u8, 2u8, 3u8];\n"
"export fn main() i32 = { return g.cap: i32; };\n",
3 },
{ "u8_sum",
"package main;\n"
"let g: []u8 = [1u8, 2u8, 3u8];\n"
"export fn main() i32 = {\n"
"\treturn (g[0]:i32)+(g[1]:i32)+(g[2]:i32)+(g.len:i32)+(g.cap:i32);\n"
"};\n",
12 },
{ "i64_e1",
"package main;\n"
"let g: []i64 = [10i64, 20i64, 30i64];\n"
"export fn main() i32 = { return g[1]: i32; };\n",
20 },
{ "i64_lencap",
"package main;\n"
"let g: []i64 = [10i64, 20i64, 30i64];\n"
"export fn main() i32 = { return (g.len:i32)+(g.cap:i32); };\n",
6 },
{ "i32_sum",
"package main;\n"
"let g: []i32 = [7i32, 8i32, 9i32];\n"
"export fn main() i32 = { return (g[0]:i32)+(g[1]:i32)+(g[2]:i32); };\n",
24 },
{ "one_edge",
"package main;\n"
"let g: []u8 = [42u8];\n"
"export fn main() i32 = { return (g[0]:i32)+(g.len:i32); };\n",
43 },
/* aliased slice type: `type S = []u8` IS a slice — both stages must
* route through emit_slice_data. cstage resolves it via type_unwrap;
* wwstage letvarisslice resolves the alias chain (#10). */
{ "alias_slice",
"package main;\n"
"type S = []u8;\n"
"let g: S = [1u8, 2u8, 3u8];\n"
"export fn main() i32 = { return (g[0]:i32)+(g.len:i32)+(g.cap:i32); };\n",
7 },
/* #19: inline NAMED-STRUCT-element array->slice. wwstage's checker
* over-rejected this — the N_ARRLIT element-type inference picked up
* the N_STRUCTLIT's body (N_TSTRUCT, per #66) and the array->slice
* isassignable typeeqast saw a TNAME-vs-TSTRUCT kind mismatch (the
* declared element is N_TNAME). Fix infers the NAMED element. cstage
* already accepted; emit_array_lit_bytes' struct-field recursion
* makes the backing byte-identical. */
{ "struct_pt",
"package main;\n"
"type pt = struct { a: i64, b: i64 };\n"
"let g: []pt = [pt{a=1i64, b=2i64}, pt{a=3i64, b=4i64}];\n"
"export fn main() i32 = {\n"
"\treturn (g[0].a:i32)+(g[0].b:i32)+(g[1].a:i32)+(g[1].b:i32)+(g.len:i32)+(g.cap:i32);\n"
"};\n",
14 },
/* 3-field mixed-width struct stresses field offsets in the backing
* (u8 @0, i64 @8, i32 @16 — non-uniform strides). */
{ "struct_3f",
"package main;\n"
"type q = struct { a: u8, b: i64, c: i32 };\n"
"let g: []q = [q{a=1u8, b=2i64, c=3i32}, q{a=4u8, b=5i64, c=6i32}];\n"
"export fn main() i32 = {\n"
"\treturn (g[0].a:i32)+(g[0].b:i32)+(g[0].c:i32)+(g[1].a:i32)+(g[1].b:i32)+(g[1].c:i32)+(g.len:i32);\n"
"};\n",
23 },
/* single-element []struct (count==1 backing edge). */
{ "struct_one",
"package main;\n"
"type pt = struct { a: i64, b: i64 };\n"
"let g: []pt = [pt{a=5i64, b=6i64}];\n"
"export fn main() i32 = {\n"
"\treturn (g[0].a:i32)+(g[0].b:i32)+(g.len:i32)+(g.cap:i32);\n"
"};\n",
13 },
/* regression: the array-VARIABLE form already accepted (arr carries a
* clean N_TNAME). Local scope, since the MODULE-scope variable form is
* the still-deferred #22 (no backing symbol -> link fail on BOTH
* stages). */
{ "struct_arrvar_local",
"package main;\n"
"type pt = struct { a: i64, b: i64 };\n"
"export fn main() i32 = {\n"
"\tlet arr: [2]pt = [pt{a=1i64, b=2i64}, pt{a=3i64, b=4i64}];\n"
"\tlet g: []pt = arr;\n"
"\treturn (g[0].a:i32)+(g[0].b:i32)+(g[1].a:i32)+(g[1].b:i32)+(g.len:i32)+(g.cap:i32);\n"
"};\n",
14 },
};
/* slice-literal static init that must FAIL the build loudly (rule 7). */
static const char *neg[] = {
/* read-only `def` can't carry the ptr reloc (DATAR holder must be
* DATAW, w6a asm.c:362). */
"package main;\n"
"def g: []u8 = [1u8, 2u8, 3u8];\n"
"export fn main() i32 = { return g.len: i32; };\n",
/* `...` repeat has no target length in a slice literal. Uses the
* reachable repeat spelling `[v...]` (no comma) so the cgen loud-stop
* is exercised — the comma form `[v, ...]` parse-errors first and
* would test the parser, not the emit_slice_data guard. */
"package main;\n"
"let g: []u8 = [1u8, 2u8...];\n"
"export fn main() i32 = { return g.len: i32; };\n",
/* slice-of-str needs per-element relocs (#17) — deferred loud-stop. */
"package main;\n"
"let g: []str = [\"a\", \"b\"];\n"
"export fn main() i32 = { return g.len: i32; };\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/slg_%d_d_%d", getpid(), i);
if (mkdir(tmpdir, 0755) != 0) return -1;
snprintf(src, sizeof src, "%s/slg_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/slg_%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; }
/* Ordinary build retains caller-owned .sepwork scratch beside -o. */
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 — 0 on the expected rejection, positive on an
* unexpected successful build, 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/slgn_%d_d_%d", getpid(), i);
if (mkdir(tmpdir, 0755) != 0) return -1;
snprintf(s, sizeof s, "%s/slgn_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/slgn_%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; }
/* A rejected ordinary build can also leave caller-owned .sepwork. */
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, "slice_literal_global[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, "slice_literal_global[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/slg_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, "slice_literal_global: 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,
"slice_literal_global[%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,
"slice_literal_global[%s][neg%d]: built ok, "
"expected a loud error\n",
drivers[d].name, i);
else
fprintf(stderr,
"slice_literal_global[%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,
"slice_literal_global: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("slice_literal_global: %d/%d ok\n", total, total);
return 0;
}