From 87367ae332819ff8accce537f4d9f3d20f48c635 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Fri, 12 Jun 2026 11:31:53 +0900 Subject: [PATCH] wcc/ww: match-as-expression unifies arm yields (coarse-family gate) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit exprtype N_MATCH took the first arm's yield type without walking the rest — int-vs-str arms silently produced garbage downstream. Walk all arms: typeeq-equal accepts; a definite coarse-family mismatch (num/str/bool via the new yieldclass classifier, unknown classes stay lenient) rejects. Same-family non-assignable pairs remain lenient — the documented precision residual is task #52 (needs tinfo-level type_assignable). The wave's table-driven reject test lands here: 989_catA_f2_reject, 19 rows x 2 stages, each member pre-fix-red-proven. --- Makefile | 12 + selfhost/cmd/w6c/main.combined.ww | 55 +++- selfhost/cmd/wcc/check.ww | 55 +++- selfhost/cmd/wwdump/main.combined.ww | 55 +++- test/wcc/989_catA_f2_reject.c | 374 +++++++++++++++++++++++++++ 5 files changed, 548 insertions(+), 3 deletions(-) create mode 100644 test/wcc/989_catA_f2_reject.c diff --git a/Makefile b/Makefile index 5901f87c..564e7752 100644 --- a/Makefile +++ b/Makefile @@ -248,6 +248,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_arr_cap_reject \ $(BIN)/test_tagged_subset_reject \ $(BIN)/test_callarg_typecheck \ + $(BIN)/test_catA_f2_reject \ $(BIN)/test_idxarg_run \ $(BIN)/test_chainidx_run \ $(BIN)/test_tupfieldsize_run \ @@ -648,6 +649,17 @@ $(BIN)/test_callarg_typecheck: test/wcc/989_callarg_typecheck.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_catA_f2_reject drives the #38/F2 checker-reject wave (review items +# #2/#6/#7/#10/#39): each REJECT row must FAIL to build on BOTH driver +# twins, each control must build+run. Needs the full cstage + wwstage tool +# sets plus libwwrt for the link. +$(BIN)/test_catA_f2_reject: test/wcc/989_catA_f2_reject.c \ + $(BIN)/ww $(BIN)/ww_ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + # 989_idxarg_run (F7-c2, #45/#46): an indexed slice/str element passed as # a call arg must push its full multi-word header. Builds+runs each fixture # on BOTH the cstage `ww` and wwstage `ww_ww` drivers (rule-10), so it needs diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index b5938fbf..e43c01e4 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -11840,6 +11840,25 @@ fn matchyieldtype(c: *checker, body: *node, bname: str, btype: *node, return nil; }; +// yieldclass — #38/F2 (review item 6): the coarse assignability family of a +// match-arm yield type, used to approximate cstage's type_assignable in the +// cross-arm unification (ww has tinfo typeeq but no tinfo type_assignable). +// 1=numeric (int/float/enum/rune + untyped_int/float/rune, NAMED-chased), +// 2=str (str + untyped_str), 3=bool, 0=unknown/other (ptr/struct/tuple/slice/ +// tagged/...). Class 0 stays LENIENT so the unification rejects only a DEFINITE +// family mismatch (the catA repro: an int arm vs a str arm) and never an +// untyped->concrete promotion (untyped_int vs i32/f64 both land in class 1) +// that cstage accepts. The families mirror typeisnum/typeisstr (lib/ww/typ.ww). +fn yieldclass(t: *tinfo) i32 = { + let u: *tinfo = tichase(t); + if (u == nil) { return 0i32; }; + if (typeisnum(u)) { return 1i32; }; + if (typeisstr(u)) { return 2i32; }; + if (u.kind == tykind.TY_BOOL) { return 3i32; }; + if (u.kind == tykind.TY_UNTYPED_BOOL) { return 3i32; }; + return 0i32; +}; + // astoffset — byte offset of `dot.str` inside the struct type of // `dot.lhs`. Mirrors cstage cmd/wcc/check.c:932-961: peel one N_TPTR // (for `p.field` where p is *Struct), require N_TSTRUCT, walk fields @@ -14288,7 +14307,41 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = { // scope-popped `yield ` fallback. let armn: *node = nil; let t: *tinfo = matchyieldtype(c, cs.body, cs.str, cs.lhs, &armn); - if (t != nil) { yt = t; retn = armn; break; }; + if (t != nil) { + if (yt == nil) { + // First yielding arm: it is the match's type; + // retn carries its *node for the consumers (#264). + yt = t; retn = armn; + } else { + // #38/F2 (review item 6): cross-arm yield + // unification. cstage check.c:2080 rejects an arm + // whose yield is neither type_eq nor type_assignable + // to the first arm's. ww has tinfo typeeq but no + // tinfo type_assignable, so reject only a DEFINITE + // coarse-family mismatch (yieldclass) — closing the + // catA silent accept (int arm vs str arm reads the + // str header through an int-stamped slot at runtime) + // while staying lenient on same-family / untyped + // promotions cstage admits. Walks ALL arms (no early + // break): the post-walk matchyieldtype reads each + // arm's cached operand stamp, a pure read (#264). + // RETAINED DIVERGENCE (match-yield precision-gap task, + // lead #52 - distinct from the enum-reinterpret #52 + // note elsewhere): the coarse yieldclass ALSO under- + // rejects same-family-but-not-assignable arms cstage + // DOES reject (untyped_int vs i64 / untyped_int vs + // untyped_float) - a precision gap, not a silent + // miscompile of the catA repro; closeable only with a + // real tinfo type_assignable. + if (!typeeq(yt, t)) { + let ca: i32 = yieldclass(yt); + let cb: i32 = yieldclass(t); + if (ca != 0i32 && cb != 0i32 && ca != cb) { + deffolderr(c, cs, "match arm yields an incompatible type"); + }; + }; + }; + }; cs = cs.next; }; if (yt == nil) { diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index 40444a57..94f6276d 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -1420,6 +1420,25 @@ fn matchyieldtype(c: *checker, body: *node, bname: str, btype: *node, return nil; }; +// yieldclass — #38/F2 (review item 6): the coarse assignability family of a +// match-arm yield type, used to approximate cstage's type_assignable in the +// cross-arm unification (ww has tinfo typeeq but no tinfo type_assignable). +// 1=numeric (int/float/enum/rune + untyped_int/float/rune, NAMED-chased), +// 2=str (str + untyped_str), 3=bool, 0=unknown/other (ptr/struct/tuple/slice/ +// tagged/...). Class 0 stays LENIENT so the unification rejects only a DEFINITE +// family mismatch (the catA repro: an int arm vs a str arm) and never an +// untyped->concrete promotion (untyped_int vs i32/f64 both land in class 1) +// that cstage accepts. The families mirror typeisnum/typeisstr (lib/ww/typ.ww). +fn yieldclass(t: *tinfo) i32 = { + let u: *tinfo = tichase(t); + if (u == nil) { return 0i32; }; + if (typeisnum(u)) { return 1i32; }; + if (typeisstr(u)) { return 2i32; }; + if (u.kind == tykind.TY_BOOL) { return 3i32; }; + if (u.kind == tykind.TY_UNTYPED_BOOL) { return 3i32; }; + return 0i32; +}; + // astoffset — byte offset of `dot.str` inside the struct type of // `dot.lhs`. Mirrors cstage cmd/wcc/check.c:932-961: peel one N_TPTR // (for `p.field` where p is *Struct), require N_TSTRUCT, walk fields @@ -3868,7 +3887,41 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = { // scope-popped `yield ` fallback. let armn: *node = nil; let t: *tinfo = matchyieldtype(c, cs.body, cs.str, cs.lhs, &armn); - if (t != nil) { yt = t; retn = armn; break; }; + if (t != nil) { + if (yt == nil) { + // First yielding arm: it is the match's type; + // retn carries its *node for the consumers (#264). + yt = t; retn = armn; + } else { + // #38/F2 (review item 6): cross-arm yield + // unification. cstage check.c:2080 rejects an arm + // whose yield is neither type_eq nor type_assignable + // to the first arm's. ww has tinfo typeeq but no + // tinfo type_assignable, so reject only a DEFINITE + // coarse-family mismatch (yieldclass) — closing the + // catA silent accept (int arm vs str arm reads the + // str header through an int-stamped slot at runtime) + // while staying lenient on same-family / untyped + // promotions cstage admits. Walks ALL arms (no early + // break): the post-walk matchyieldtype reads each + // arm's cached operand stamp, a pure read (#264). + // RETAINED DIVERGENCE (match-yield precision-gap task, + // lead #52 - distinct from the enum-reinterpret #52 + // note elsewhere): the coarse yieldclass ALSO under- + // rejects same-family-but-not-assignable arms cstage + // DOES reject (untyped_int vs i64 / untyped_int vs + // untyped_float) - a precision gap, not a silent + // miscompile of the catA repro; closeable only with a + // real tinfo type_assignable. + if (!typeeq(yt, t)) { + let ca: i32 = yieldclass(yt); + let cb: i32 = yieldclass(t); + if (ca != 0i32 && cb != 0i32 && ca != cb) { + deffolderr(c, cs, "match arm yields an incompatible type"); + }; + }; + }; + }; cs = cs.next; }; if (yt == nil) { diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index e44b6287..718bb3ea 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -11840,6 +11840,25 @@ fn matchyieldtype(c: *checker, body: *node, bname: str, btype: *node, return nil; }; +// yieldclass — #38/F2 (review item 6): the coarse assignability family of a +// match-arm yield type, used to approximate cstage's type_assignable in the +// cross-arm unification (ww has tinfo typeeq but no tinfo type_assignable). +// 1=numeric (int/float/enum/rune + untyped_int/float/rune, NAMED-chased), +// 2=str (str + untyped_str), 3=bool, 0=unknown/other (ptr/struct/tuple/slice/ +// tagged/...). Class 0 stays LENIENT so the unification rejects only a DEFINITE +// family mismatch (the catA repro: an int arm vs a str arm) and never an +// untyped->concrete promotion (untyped_int vs i32/f64 both land in class 1) +// that cstage accepts. The families mirror typeisnum/typeisstr (lib/ww/typ.ww). +fn yieldclass(t: *tinfo) i32 = { + let u: *tinfo = tichase(t); + if (u == nil) { return 0i32; }; + if (typeisnum(u)) { return 1i32; }; + if (typeisstr(u)) { return 2i32; }; + if (u.kind == tykind.TY_BOOL) { return 3i32; }; + if (u.kind == tykind.TY_UNTYPED_BOOL) { return 3i32; }; + return 0i32; +}; + // astoffset — byte offset of `dot.str` inside the struct type of // `dot.lhs`. Mirrors cstage cmd/wcc/check.c:932-961: peel one N_TPTR // (for `p.field` where p is *Struct), require N_TSTRUCT, walk fields @@ -14288,7 +14307,41 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = { // scope-popped `yield ` fallback. let armn: *node = nil; let t: *tinfo = matchyieldtype(c, cs.body, cs.str, cs.lhs, &armn); - if (t != nil) { yt = t; retn = armn; break; }; + if (t != nil) { + if (yt == nil) { + // First yielding arm: it is the match's type; + // retn carries its *node for the consumers (#264). + yt = t; retn = armn; + } else { + // #38/F2 (review item 6): cross-arm yield + // unification. cstage check.c:2080 rejects an arm + // whose yield is neither type_eq nor type_assignable + // to the first arm's. ww has tinfo typeeq but no + // tinfo type_assignable, so reject only a DEFINITE + // coarse-family mismatch (yieldclass) — closing the + // catA silent accept (int arm vs str arm reads the + // str header through an int-stamped slot at runtime) + // while staying lenient on same-family / untyped + // promotions cstage admits. Walks ALL arms (no early + // break): the post-walk matchyieldtype reads each + // arm's cached operand stamp, a pure read (#264). + // RETAINED DIVERGENCE (match-yield precision-gap task, + // lead #52 - distinct from the enum-reinterpret #52 + // note elsewhere): the coarse yieldclass ALSO under- + // rejects same-family-but-not-assignable arms cstage + // DOES reject (untyped_int vs i64 / untyped_int vs + // untyped_float) - a precision gap, not a silent + // miscompile of the catA repro; closeable only with a + // real tinfo type_assignable. + if (!typeeq(yt, t)) { + let ca: i32 = yieldclass(yt); + let cb: i32 = yieldclass(t); + if (ca != 0i32 && cb != 0i32 && ca != cb) { + deffolderr(c, cs, "match arm yields an incompatible type"); + }; + }; + }; + }; cs = cs.next; }; if (yt == nil) { diff --git a/test/wcc/989_catA_f2_reject.c b/test/wcc/989_catA_f2_reject.c new file mode 100644 index 00000000..fc0bb6d0 --- /dev/null +++ b/test/wcc/989_catA_f2_reject.c @@ -0,0 +1,374 @@ +/* + * 989_catA_f2_reject — #38/F2 checker-reject wave: align the wwstage + * checker UP to cstage's harec-faithful rejects (cat-A silent-accept + * family from the 2026-06-11 stability review). Each row is a program + * cstage loud-rejects but pre-fix wwstage silently accepted (then built + * a wrong binary). The build-based reject-matrix idiom (sibling + * 989_callarg_typecheck.c): a REJECT row must FAIL to build on every + * driver; an ACCEPT control must build + run to its expected exit. + * + * Members (review item → check.ww seam → cstage twin): + * #2 arrayelen non-const dim tinfofornode N_TARRAY check.c:715 + * #6 binoptype/unoptype kinds binoptype/unoptype check.c:1186-1238 + * #7 size/align unknown type size intercept check.c:93/1538 + * #10 tuple-literal arity checktuplearrfits type.c:416/check.c:2386 + * #39 non-tuple massign rhs N_MASSIGN resolvewalk check.c:2562 + * #8 checktryprop spread-blind trycountvariants check.c:2160 (F3) + * #6' match-yield unification exprtype N_MATCH check.c:2080 + * + * Rule-10: every row runs on cstage `ww` and, when present, wwstage + * `ww_ww`; both must agree. wwstage rows are gated on out/bin/ww_ww. + */ +#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 expect_build; /* 1 = build+run to want_exit; 0 = must FAIL */ + int want_exit; +}; + +static const struct row rows[] = { + /* #2 — a runtime-valued array dimension is non-const → REJECT. Pre-fix + * wwstage folded it to a 0-sized slot aliasing the dim variable. */ + { "arrlen_runtime", + "package main;\n" + "export fn main() i32 = {\n" + " let n: i32 = 3;\n" + " let xs: [n]u8;\n" + " xs[0] = 77u8;\n" + " return n;\n" + "};\n", + 0, 0 }, + + /* #2 control — a literal dimension stays valid → run 0. */ + { "arrlen_const_ok", + "package main;\n" + "export fn main() i32 = {\n" + " let xs: [3]u8;\n" + " xs[0] = 1u8;\n" + " return 0;\n" + "};\n", + 1, 0 }, + + /* #6 — str + str is arithmetic on a non-numeric → REJECT. Pre-fix + * wwstage emitted an integer ADD of the two header pointers. */ + { "str_plus_str", + "package main;\n" + "export fn main() i32 = {\n" + " let a: str = \"x\";\n" + " let b: str = \"y\";\n" + " let c: str = a + b;\n" + " return c.len: i32;\n" + "};\n", + 0, 0 }, + + /* #6 — bitwise & on str is non-integer → REJECT. */ + { "str_bitand", + "package main;\n" + "export fn main() i32 = {\n" + " let a: str = \"x\";\n" + " let b: str = \"y\";\n" + " let n: i32 = (a & b): i32;\n" + " return n;\n" + "};\n", + 0, 0 }, + + /* #6 — ordered comparison on str is non-numeric → REJECT (EQ/NEQ stay + * valid; only the ordered <,<=,>,>= are gated, matching cstage). */ + { "str_lt", + "package main;\n" + "export fn main() i32 = {\n" + " let a: str = \"x\";\n" + " let b: str = \"y\";\n" + " if (a < b) { return 1; };\n" + " return 0;\n" + "};\n", + 0, 0 }, + + /* #6 — unary ~ on a non-integer (str) → REJECT. */ + { "tilde_str", + "package main;\n" + "export fn main() i32 = {\n" + " let a: str = \"x\";\n" + " let n: i32 = (~a): i32;\n" + " return n;\n" + "};\n", + 0, 0 }, + + /* #6 control — integer arithmetic, comparison, bitwise all valid. */ + { "int_ops_ok", + "package main;\n" + "export fn main() i32 = {\n" + " let a: i32 = 3;\n" + " let b: i32 = 4;\n" + " let c: i32 = (a + b) & 7;\n" + " if (a < b) { c = c + 1; };\n" + " return c;\n" + "};\n", + 1, 8 }, + + /* #7 — size() of a value (not a type) is an unknown type → REJECT. + * Pre-fix wwstage folded the unresolved name to 0 silently. */ + { "size_localvar", + "package main;\n" + "export fn main() i32 = {\n" + " let v: i32 = 5;\n" + " return size(v): i32;\n" + "};\n", + 0, 0 }, + + /* #7 control — size() of a real type folds → run 4. */ + { "size_type_ok", + "package main;\n" + "export fn main() i32 = { return size(i32): i32; };\n", + 1, 4 }, + + /* #10 — an over-long tuple literal → REJECT. */ + { "tuple_overlong", + "package main;\n" + "export fn main() i32 = {\n" + " let t: (i32, i32) = (1, 2, 3);\n" + " return 0;\n" + "};\n", + 0, 0 }, + + /* #10 — a short tuple literal → REJECT (the live miscompile: cgen + * filled the missing slot with a stale register). */ + { "tuple_short", + "package main;\n" + "export fn main() i32 = {\n" + " let t: (i32, i32, i32) = (1, 2);\n" + " return 0;\n" + "};\n", + 0, 0 }, + + /* #10 control — a matching-arity tuple literal → run 3. */ + { "tuple_match_ok", + "package main;\n" + "export fn main() i32 = {\n" + " let t: (i32, i32) = (1, 2);\n" + " return t.0 + t.1;\n" + "};\n", + 1, 3 }, + + /* #39 — multi-assign rhs is a tuple ALIAS, not a bare tuple → REJECT + * (cstage does not chase the NAMED wrapper here; nor does ww). */ + { "massign_alias", + "package main;\n" + "type pair = (i64, str);\n" + "fn f() pair = { return (7i64, \"hey\"); };\n" + "export fn main() i32 = {\n" + " let a: i64 = 0i64;\n" + " let s: str = \"\";\n" + " a, s = f();\n" + " return 0;\n" + "};\n", + 0, 0 }, + + /* #39 — multi-assign rhs is a plain scalar, not a tuple → REJECT. */ + { "massign_scalar", + "package main;\n" + "fn g() i64 = { return 7i64; };\n" + "export fn main() i32 = {\n" + " let a: i64 = 0i64;\n" + " let s: i64 = 0i64;\n" + " a, s = g();\n" + " return 0;\n" + "};\n", + 0, 0 }, + + /* #39 control — a bare-tuple-returning fn drives a valid massign. */ + { "massign_tuple_ok", + "package main;\n" + "fn f() (i64, i64) = { return (3i64, 4i64); };\n" + "export fn main() i32 = {\n" + " let a: i64 = 0i64;\n" + " let b: i64 = 0i64;\n" + " a, b = f();\n" + " return (a + b): i32;\n" + "};\n", + 1, 7 }, + + /* #8 — a `...inner` spread makes the union multi-success; the F8 gate + * must fire over the FLATTENED members → REJECT. Pre-fix wwstage counted + * the spread as one success (nsucc=1) and silently accepted. */ + { "try_spread_multisuccess", + "package main;\n" + "type inner = (i32 | bool);\n" + "type e = !void;\n" + "fn g() (...inner | e) = { return true; };\n" + "export fn main() i32 = { let x = g()!; return 0; };\n", + 0, 0 }, + + /* #8 control — a single-success tagged union try unwraps fine → run 5. */ + { "try_single_ok", + "package main;\n" + "type e = !void;\n" + "fn g() (i32 | e) = { return 5; };\n" + "export fn main() i32 = { let x = g()!; return x; };\n", + 1, 5 }, + + /* match-yield — arms yielding incompatible types (int vs str) → REJECT. + * Pre-fix wwstage took the first arm's type and ran the str arm's pointer + * through an int-stamped slot. */ + { "match_mixed_yield", + "package main;\n" + "fn pick(b: bool) (i32 | str) = { if (b) { return 7; }; return \"x\"; };\n" + "export fn main() i32 = {\n" + " let v: (i32 | str) = pick(true);\n" + " let r = match (v) { case i32 => yield 7; case str => yield \"oops\"; };\n" + " return 0;\n" + "};\n", + 0, 0 }, + + /* match-yield control — same-family arm yields (untyped_int + i32) must + * NOT over-reject (the untyped->concrete promotion cstage admits) → run 9. */ + { "match_same_yield_ok", + "package main;\n" + "fn pick(b: bool) (i32 | str) = { if (b) { return 4; }; return \"x\"; };\n" + "export fn main() i32 = {\n" + " let v: (i32 | str) = pick(true);\n" + " let r: i32 = match (v) { case i32 => yield 9; case let s: str => yield 5; };\n" + " return r;\n" + "};\n", + 1, 9 }, +}; + +static int +run_build(const char *driver, const struct row *r, int i) +{ + char src[64], tmpdir[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/f2r_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/f2r_%d_d_%d", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -2; + fputs(r->src, f); + fclose(f); + + mkdir(tmpdir, 0755); + snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", + tmpdir, driver, src); + int brc = runwait(cmd); + + 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 = -1; + if (brc == 0) got = runwait(outbin); + + unlink(src); unlink(outbin); rmdir(tmpdir); + return brc == 0 ? got : -1; +} + +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/f2n_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/f2n_%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], wdrv[1024]; + snprintf(cdrv, sizeof cdrv, "%s/ww", bin); + snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); + + struct { const char *name; const char *drv; int gated; } + drivers[] = { + { "cstage", cdrv, 0 }, + { "wwstage", wdrv, 1 }, + { NULL, NULL, 0 }, + }; + + int n = (int)(sizeof rows / sizeof rows[0]); + int total = 0, fail = 0; + + for (int d = 0; drivers[d].name; d++) { + if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) { + fprintf(stderr, "catA_f2_reject: skip %s (no %s)\n", + drivers[d].name, drivers[d].drv); + continue; + } + for (int i = 0; i < n; i++) { + total++; + if (rows[i].expect_build) { + int got = run_build(drivers[d].drv, &rows[i], i); + if (got != rows[i].want_exit) { + fprintf(stderr, "catA_f2_reject[%s][%s]: " + "exit=%d want=%d\n", drivers[d].name, + rows[i].label, got, rows[i].want_exit); + fail++; + } + } else { + if (build_should_fail(drivers[d].drv, rows[i].src, + 100 + i) != 0) { + fprintf(stderr, "catA_f2_reject[%s][%s]: " + "built ok, expected a loud reject\n", + drivers[d].name, rows[i].label); + fail++; + } + } + } + } + + if (fail) { + fprintf(stderr, "catA_f2_reject: %d/%d fixtures failed\n", + fail, total); + return 1; + } + printf("catA_f2_reject: %d/%d ok\n", total, total); + return 0; +}