diff --git a/Makefile b/Makefile index 724739a3..f5c5484f 100644 --- a/Makefile +++ b/Makefile @@ -258,6 +258,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_str_eq \ $(BIN)/test_overlong_arrlit \ $(BIN)/test_tuple_elem_overlong \ + $(BIN)/test_tagged_arr_variant \ $(BIN)/test_inferred_array_global \ $(BIN)/test_slice_str_global_arg \ $(BIN)/test_slice_str_global_zero \ @@ -723,6 +724,12 @@ $(BIN)/test_tuple_elem_overlong: test/wcc/832_tuple_elem_overlong.c $(BIN)/ww \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_tagged_arr_variant: test/wcc/834_tagged_arr_variant.c $(BIN)/ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_inferred_array_global: test/wcc/833_inferred_array_global.c $(BIN)/ww \ $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/cmd/wcc/check.c b/cmd/wcc/check.c index 7317126b..2402812a 100644 --- a/cmd/wcc/check.c +++ b/cmd/wcc/check.c @@ -125,6 +125,39 @@ variant_present(Tparam *head, Type *vt) return 0; } +/* tagged_array_variant — #5/#60: when `src` is boxed into the tagged + * union `dst`, return the variant `src` selects IFF that variant chases + * to TY_ARRAY, else NULL. The array-payload box is unwired in cgen: the + * widen emitter zero-fills the slot at box materialization (a silent + * MOVQ $0 payload drop), so the construct must be rejected at the + * checker until the faithful array-block-store lands (deferred task #6). + * The tagged TYPE-decl with an array variant stays legal — test 944 + * declares (void|size|[5]size) and only boxes the narrow `size` variant + * — only the array-variant CONSTRUCTION is refused. Mirrors the + * concrete->tagged variant select in type_assignable (type.c:324-343) + * so the variant chosen here is the one the box would materialize. */ +static Type * +tagged_array_variant(Type *dst, Type *src) +{ + if (dst == NULL || src == NULL) return NULL; + Type *du = type_chase_named(dst); + Type *su = type_chase_named(src); + if (du == NULL || du->kind != TY_TAGGED) return NULL; + if (su && su->kind == TY_TAGGED) return NULL; /* tagged->tagged */ + for (Tparam *p = du->params; p; p = p->next) { + Type *pu = type_chase_named(p->type); + if (pu && pu->kind == TY_TAGGED) { + if (type_eq(p->type, src)) return NULL; + continue; + } + if (type_assignable(p->type, src)) { + if (pu && pu->kind == TY_ARRAY) return p->type; + return NULL; + } + } + return NULL; +} + /* match_yield_type — walk a match arm's body looking for the type * of its first `yield expr;` statement. Returns NULL if no yield * was found. Doesn't descend into nested match bodies — each match @@ -2356,6 +2389,11 @@ clet(Checker *c, Node *n) !assignable_addrfn(c, declared, n->rhs)) err(c, n->pos, "init %s not assignable to declared %s", type_name(c->a, initt), type_name(c->a, declared)); + /* #5/#60: reject boxing an array payload into a tagged variant. */ + if (declared && initt && initt != ty_err && + tagged_array_variant(declared, initt)) + err(c, n->pos, "array-typed tagged-union variant " + "construction unwired — reject (task #5 / #60)"); /* #104 fold-2: `let x: f32 = 1.0` — narrow the init literal to f32. */ coerce_floatlit(n->rhs, declared); /* #258: `let s: []T = arr` borrows the array as a full slice. */ @@ -2399,6 +2437,11 @@ cstmt(Checker *c, Node *n) && !assignable_addrfn(c, c->ret, n->lhs)) err(c, n->pos, "return %s not assignable to %s", type_name(c->a, rt), type_name(c->a, c->ret)); + /* #5/#60: reject returning an array payload into a tagged variant. */ + else if (c->ret != ty_void && rt != ty_err && c->ret != ty_err && + tagged_array_variant(c->ret, rt)) + err(c, n->pos, "array-typed tagged-union variant " + "construction unwired — reject (task #5 / #60)"); /* #104 fold-2: `fn g() f32 = { return 1.0; }` — narrow to f32. */ coerce_floatlit(n->lhs, c->ret); /* #258: `return arr` borrows the array as a full slice. diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 8ac8b845..c300abb3 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -14526,6 +14526,45 @@ fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = { c.errs += 1; }; +// taggedarrayvariantctor — #5/#60: true when `src` is boxed into the +// tagged union `dst` via a variant that chases to TY_ARRAY. The array- +// payload box is unwired in cgen (cstage zero-filled the slot at box +// materialization — a silent MOVQ $0 payload drop; wwstage only loud at +// the dead match arm, errbadcase:4107). Reject the CONSTRUCT until the +// faithful array-block-store lands (deferred task #6). The tagged TYPE- +// decl with an array variant stays legal — test 944 declares +// (void|size|[5]size) and boxes only the narrow `size` variant — only +// the array-variant box is refused. Mirrors the concrete→tagged variant +// select in isassignable (:3859) and cstage tagged_array_variant so the +// variant chosen here is the one the box would materialize. +fn taggedarrayvariantctor(c: *checker, dst: *node, src: *node) bool = { + if (dst == nil) { return false; }; + if (src == nil) { return false; }; + let du: *node = resolvealias(c, unwrapbang(dst)); + let su: *node = resolvealias(c, unwrapbang(src)); + if (du == nil) { return false; }; + if (du.kind != nkind.N_TTAGGED) { return false; }; + if (su != nil) { if (su.kind == nkind.N_TTAGGED) { return false; }; }; + let v: *node = du.list; + for (v != nil) { + let vspread: bool = (v.op == tkind.TK_ELLIPSIS); + let vu: *node = resolvealias(c, unwrapbang(v)); + let vtagged: bool = false; + if (vu != nil) { if (vu.kind == nkind.N_TTAGGED) { vtagged = true; }; }; + if (vtagged && !vspread) { + if (typeeqast(v, src)) { return false; }; + } else { + let innerconf: bool = false; + if (isassignable(c, v, src, &innerconf)) { + if (vu != nil) { if (vu.kind == nkind.N_TARRAY) { return true; }; }; + return false; + }; + }; + v = v.next; + }; + return false; +}; + // checkarrlitfits — #130/#251 array-init accept-if-fits, shared by the // let / def / struct-field init sites. arrtn is the declared [N]T type // node, rhs the N_ARRLIT. Per element: foldable int/rune literal → @@ -15098,6 +15137,13 @@ fn checkletassign(c: *checker, n: *node) void = { if (!ok) { if (assignableaddrfn(c, n.lhs, n.rhs)) { ok = true; }; }; if (!conf) { return; }; if (!ok) { errnotassign(c, n.lhs, src, "let"); }; + // #5/#60: reject boxing an array payload into a tagged variant. cerr + // alone does NOT fail the build — bump c.errs (errnotassign:4245 idiom). + if (taggedarrayvariantctor(c, n.lhs, src)) { + cerr("array-typed tagged-union variant construction unwired — reject (task #5 / #60)\n"); + c.errs += 1; + return; + }; // #258: `let s: []T = arr` borrows the array as a full slice. The // desugar lowers to a runtime `arr[0:len]` N_SLICE, so it only // applies to LOCAL lets (a fn body executes the borrow). A MODULE- @@ -15156,6 +15202,13 @@ fn checkretassign(c: *checker, n: *node) void = { if (!ok) { if (assignableaddrfn(c, c.fnret, n.lhs)) { ok = true; }; }; if (!conf) { return; }; if (!ok) { errnotassign(c, c.fnret, src, "return"); }; + // #5/#60: reject returning an array payload into a tagged variant. cerr + // alone does NOT fail the build — bump c.errs (errnotassign:4245 idiom). + if (taggedarrayvariantctor(c, c.fnret, src)) { + cerr("array-typed tagged-union variant construction unwired — reject (task #5 / #60)\n"); + c.errs += 1; + return; + }; // #258: `return arr` borrows the array as a full slice. // #31/#33: bare array-literal has no backing — loud-reject // (supported only at a `let`). diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index 7bf9eb21..7f6c449d 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -4245,6 +4245,45 @@ fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = { c.errs += 1; }; +// taggedarrayvariantctor — #5/#60: true when `src` is boxed into the +// tagged union `dst` via a variant that chases to TY_ARRAY. The array- +// payload box is unwired in cgen (cstage zero-filled the slot at box +// materialization — a silent MOVQ $0 payload drop; wwstage only loud at +// the dead match arm, errbadcase:4107). Reject the CONSTRUCT until the +// faithful array-block-store lands (deferred task #6). The tagged TYPE- +// decl with an array variant stays legal — test 944 declares +// (void|size|[5]size) and boxes only the narrow `size` variant — only +// the array-variant box is refused. Mirrors the concrete→tagged variant +// select in isassignable (:3859) and cstage tagged_array_variant so the +// variant chosen here is the one the box would materialize. +fn taggedarrayvariantctor(c: *checker, dst: *node, src: *node) bool = { + if (dst == nil) { return false; }; + if (src == nil) { return false; }; + let du: *node = resolvealias(c, unwrapbang(dst)); + let su: *node = resolvealias(c, unwrapbang(src)); + if (du == nil) { return false; }; + if (du.kind != nkind.N_TTAGGED) { return false; }; + if (su != nil) { if (su.kind == nkind.N_TTAGGED) { return false; }; }; + let v: *node = du.list; + for (v != nil) { + let vspread: bool = (v.op == tkind.TK_ELLIPSIS); + let vu: *node = resolvealias(c, unwrapbang(v)); + let vtagged: bool = false; + if (vu != nil) { if (vu.kind == nkind.N_TTAGGED) { vtagged = true; }; }; + if (vtagged && !vspread) { + if (typeeqast(v, src)) { return false; }; + } else { + let innerconf: bool = false; + if (isassignable(c, v, src, &innerconf)) { + if (vu != nil) { if (vu.kind == nkind.N_TARRAY) { return true; }; }; + return false; + }; + }; + v = v.next; + }; + return false; +}; + // checkarrlitfits — #130/#251 array-init accept-if-fits, shared by the // let / def / struct-field init sites. arrtn is the declared [N]T type // node, rhs the N_ARRLIT. Per element: foldable int/rune literal → @@ -4817,6 +4856,13 @@ fn checkletassign(c: *checker, n: *node) void = { if (!ok) { if (assignableaddrfn(c, n.lhs, n.rhs)) { ok = true; }; }; if (!conf) { return; }; if (!ok) { errnotassign(c, n.lhs, src, "let"); }; + // #5/#60: reject boxing an array payload into a tagged variant. cerr + // alone does NOT fail the build — bump c.errs (errnotassign:4245 idiom). + if (taggedarrayvariantctor(c, n.lhs, src)) { + cerr("array-typed tagged-union variant construction unwired — reject (task #5 / #60)\n"); + c.errs += 1; + return; + }; // #258: `let s: []T = arr` borrows the array as a full slice. The // desugar lowers to a runtime `arr[0:len]` N_SLICE, so it only // applies to LOCAL lets (a fn body executes the borrow). A MODULE- @@ -4875,6 +4921,13 @@ fn checkretassign(c: *checker, n: *node) void = { if (!ok) { if (assignableaddrfn(c, c.fnret, n.lhs)) { ok = true; }; }; if (!conf) { return; }; if (!ok) { errnotassign(c, c.fnret, src, "return"); }; + // #5/#60: reject returning an array payload into a tagged variant. cerr + // alone does NOT fail the build — bump c.errs (errnotassign:4245 idiom). + if (taggedarrayvariantctor(c, c.fnret, src)) { + cerr("array-typed tagged-union variant construction unwired — reject (task #5 / #60)\n"); + c.errs += 1; + return; + }; // #258: `return arr` borrows the array as a full slice. // #31/#33: bare array-literal has no backing — loud-reject // (supported only at a `let`). diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index edddf8e2..b08a3fe1 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -14526,6 +14526,45 @@ fn errnotassign(c: *checker, dst: *node, src: *node, where: str) void = { c.errs += 1; }; +// taggedarrayvariantctor — #5/#60: true when `src` is boxed into the +// tagged union `dst` via a variant that chases to TY_ARRAY. The array- +// payload box is unwired in cgen (cstage zero-filled the slot at box +// materialization — a silent MOVQ $0 payload drop; wwstage only loud at +// the dead match arm, errbadcase:4107). Reject the CONSTRUCT until the +// faithful array-block-store lands (deferred task #6). The tagged TYPE- +// decl with an array variant stays legal — test 944 declares +// (void|size|[5]size) and boxes only the narrow `size` variant — only +// the array-variant box is refused. Mirrors the concrete→tagged variant +// select in isassignable (:3859) and cstage tagged_array_variant so the +// variant chosen here is the one the box would materialize. +fn taggedarrayvariantctor(c: *checker, dst: *node, src: *node) bool = { + if (dst == nil) { return false; }; + if (src == nil) { return false; }; + let du: *node = resolvealias(c, unwrapbang(dst)); + let su: *node = resolvealias(c, unwrapbang(src)); + if (du == nil) { return false; }; + if (du.kind != nkind.N_TTAGGED) { return false; }; + if (su != nil) { if (su.kind == nkind.N_TTAGGED) { return false; }; }; + let v: *node = du.list; + for (v != nil) { + let vspread: bool = (v.op == tkind.TK_ELLIPSIS); + let vu: *node = resolvealias(c, unwrapbang(v)); + let vtagged: bool = false; + if (vu != nil) { if (vu.kind == nkind.N_TTAGGED) { vtagged = true; }; }; + if (vtagged && !vspread) { + if (typeeqast(v, src)) { return false; }; + } else { + let innerconf: bool = false; + if (isassignable(c, v, src, &innerconf)) { + if (vu != nil) { if (vu.kind == nkind.N_TARRAY) { return true; }; }; + return false; + }; + }; + v = v.next; + }; + return false; +}; + // checkarrlitfits — #130/#251 array-init accept-if-fits, shared by the // let / def / struct-field init sites. arrtn is the declared [N]T type // node, rhs the N_ARRLIT. Per element: foldable int/rune literal → @@ -15098,6 +15137,13 @@ fn checkletassign(c: *checker, n: *node) void = { if (!ok) { if (assignableaddrfn(c, n.lhs, n.rhs)) { ok = true; }; }; if (!conf) { return; }; if (!ok) { errnotassign(c, n.lhs, src, "let"); }; + // #5/#60: reject boxing an array payload into a tagged variant. cerr + // alone does NOT fail the build — bump c.errs (errnotassign:4245 idiom). + if (taggedarrayvariantctor(c, n.lhs, src)) { + cerr("array-typed tagged-union variant construction unwired — reject (task #5 / #60)\n"); + c.errs += 1; + return; + }; // #258: `let s: []T = arr` borrows the array as a full slice. The // desugar lowers to a runtime `arr[0:len]` N_SLICE, so it only // applies to LOCAL lets (a fn body executes the borrow). A MODULE- @@ -15156,6 +15202,13 @@ fn checkretassign(c: *checker, n: *node) void = { if (!ok) { if (assignableaddrfn(c, c.fnret, n.lhs)) { ok = true; }; }; if (!conf) { return; }; if (!ok) { errnotassign(c, c.fnret, src, "return"); }; + // #5/#60: reject returning an array payload into a tagged variant. cerr + // alone does NOT fail the build — bump c.errs (errnotassign:4245 idiom). + if (taggedarrayvariantctor(c, c.fnret, src)) { + cerr("array-typed tagged-union variant construction unwired — reject (task #5 / #60)\n"); + c.errs += 1; + return; + }; // #258: `return arr` borrows the array as a full slice. // #31/#33: bare array-literal has no backing — loud-reject // (supported only at a `let`). diff --git a/test/wcc/834_tagged_arr_variant.c b/test/wcc/834_tagged_arr_variant.c new file mode 100644 index 00000000..e05fd93e --- /dev/null +++ b/test/wcc/834_tagged_arr_variant.c @@ -0,0 +1,293 @@ +/* + * 834_tagged_arr_variant — constructing an ARRAY-typed payload into a + * tagged-union variant is unwired in cgen and must be LOUDLY REJECTED at + * check time on BOTH stages (#5 / #60, rob spec .ai/rob-24sib-spec.md). + * + * cstage zero-filled the array payload at box materialization (a silent + * MOVQ $0 drop — `let v:V=[7,8,9]` then `match` read 0); wwstage only + * loud at the dead match arm (errbadcase), SILENTLY accepting the + * construct. The fix is a construct-site checker reject keyed on: target + * chases TY_TAGGED AND the variant the source selects chases TY_ARRAY. + * cstage tagged_array_variant (check.c) + wwstage taggedarrayvariantctor + * (check.ww), at the let-init and return assignability paths. + * + * The tagged TYPE-decl WITH an array variant stays legal — only the + * array-variant CONSTRUCTION is refused. The narrow_in_wide positive + * (944's `(void|size|[5]size)`, boxing the scalar `size`) proves the + * type-decl + scalar ctor remain accepted. struct / slice / scalar / str + * variants all WORK (sib2/sib3) and are positive controls. Both stages + * REJECT the neg rows and emit no asm; selfhost never boxes an array, so + * 990-997 byte-id is untouched. Diagnostic TEXT is byte-id-blind; do not + * chase message parity. Full array-block-store fix deferred to task #6. + * + * neg row | shape | gate + * ----------------+---------------------------------------------+------ + * arrlit_ctor | V=([3]int|int); let v:V=[7,8,9] | FAIL + * arrvar_ctor | let a:[3]int=..; let v:V=a (variable src) | FAIL + * arr_return | fn()V { return [7,8,9]; } | FAIL + * + * pos row | shape | want + * ----------------+---------------------------------------------+------ + * struct_variant | V=(P|int); v=P{x=1,y=2}; match -> p.y | 2 + * slice_variant | V=([]int|int); v=s; match -> s[2] | 9 + * scalar_variant | V=(int|bool); v=5; match -> n | 5 + * str_variant | V=(str|int); v="hi"; match -> s.len | 2 + * narrow_in_wide | big=(void|size|[5]size); m=7:size; match | 7 + */ +#include +#include +#include +#include +#include +#include + +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[] = { + /* struct variant box + match (sib2 — works + byte-id). */ + { "struct_variant", + "package main;\n" + "type P = struct { x: int, y: int };\n" + "type V = (P | int);\n" + "export fn main() i32 = {\n" + "\tlet v: V = P { x = 1, y = 2 };\n" + "\tmatch (v) {\n" + "\tcase let p: P => return p.y: i32;\n" + "\tcase int => return 0;\n" + "\t};\n" + "\treturn 9;\n" + "};\n", + 2 }, + + /* slice variant box + match (sib3 — works + byte-id). */ + { "slice_variant", + "package main;\n" + "type V = ([]int | int);\n" + "export fn main() i32 = {\n" + "\tlet hb: [3]int = [7, 8, 9];\n" + "\tlet a: []int = hb;\n" + "\tlet v: V = a;\n" + "\tmatch (v) {\n" + "\tcase let s: []int => return s[2]: i32;\n" + "\tcase int => return 0;\n" + "\t};\n" + "\treturn 9;\n" + "};\n", + 9 }, + + /* scalar variant box + match. */ + { "scalar_variant", + "package main;\n" + "type V = (int | bool);\n" + "export fn main() i32 = {\n" + "\tlet v: V = 5;\n" + "\tmatch (v) {\n" + "\tcase let n: int => return n: i32;\n" + "\tcase bool => return 0;\n" + "\t};\n" + "\treturn 9;\n" + "};\n", + 5 }, + + /* str variant box + match (24B header payload). */ + { "str_variant", + "package main;\n" + "type V = (str | int);\n" + "export fn main() i32 = {\n" + "\tlet v: V = \"hi\";\n" + "\tmatch (v) {\n" + "\tcase let s: str => return s.len: i32;\n" + "\tcase int => return 0;\n" + "\t};\n" + "\treturn 9;\n" + "};\n", + 2 }, + + /* narrow_in_wide — 944's green shape: a tagged union that DECLARES an + * array variant ([5]size) but boxes only the narrow scalar `size`. + * Proves the type-decl + scalar ctor stay legal under the reject. */ + { "narrow_in_wide", + "package main;\n" + "type big = (void | size | [5]size);\n" + "export fn main() i32 = {\n" + "\tlet m: big = 7: size;\n" + "\tmatch (m) {\n" + "\tcase let s: size => return s: i32;\n" + "\tcase => return 9;\n" + "\t};\n" + "\treturn 9;\n" + "};\n", + 7 }, +}; + +/* Constructing an array payload into a tagged variant — both stages must + * FAIL the build (loud construct-site checker reject). */ +static const char *neg[] = { + /* arrlit_ctor — array LITERAL boxed into ([3]int|int). */ + "package main;\n" + "type V = ([3]int | int);\n" + "export fn main() i32 = {\n" + "\tlet v: V = [7, 8, 9];\n" + "\treturn 0;\n" + "};\n", + /* arrvar_ctor — array VARIABLE boxed (not literal-specific). */ + "package main;\n" + "type V = ([3]int | int);\n" + "export fn main() i32 = {\n" + "\tlet a: [3]int = [7, 8, 9];\n" + "\tlet v: V = a;\n" + "\treturn 0;\n" + "};\n", + /* arr_return — array boxed into a tagged return type. */ + "package main;\n" + "type V = ([3]int | int);\n" + "fn f() V = {\n" + "\treturn [7, 8, 9];\n" + "};\n" + "export fn main() i32 = {\n" + "\treturn 0;\n" + "};\n", +}; + +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/tav_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/tav_%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; +} + +/* build_should_fail — the array-variant construct must error on `driver`; + * returns 0 when the build correctly FAILS, non-zero when it wrongly + * succeeded. */ +static int +build_should_fail(const char *driver, const char *src, int i) +{ + char s[64], tmpdir[64], cmd[1024]; + snprintf(s, sizeof s, "/tmp/tavn_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/tavn_%d_d_%d", 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 2>/dev/null", + tmpdir, driver, s); + int rc = runwait(cmd); + unlink(s); + 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); + return rc == 0 ? -1 : 0; /* build must NOT succeed */ +} + +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, "tagged_arr_variant: 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, + "tagged_arr_variant[%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], + 100 + i) != 0) { + fprintf(stderr, + "tagged_arr_variant[%s][neg%d]: built ok, " + "expected a loud reject\n", + drivers[d].name, i); + fail++; + } + } + } + + if (fail) { + fprintf(stderr, + "tagged_arr_variant: %d/%d fixtures failed\n", fail, total); + return 1; + } + printf("tagged_arr_variant: %d/%d ok\n", total, total); + return 0; +}