Files
ww/test/wcc/989_catA_f2_reject.c
Hojun-Cho 30a4920ccf wcc: reject break/continue outside loop in wwstage, align to cstage (#7)
The selfhost checker's resolvewalk had no loop-nesting guard and no
N_BREAK/N_CONTINUE arm, so `break`/`continue` outside any loop fell through
the generic child recursion and was silently accepted -- while cstage
(cmd/wcc/check.c) correctly rejects them. A cs!=ww checker divergence
(rule 10); cstage is correct (break/continue outside a loop is an error in
Hare/C/Go), so align wwstage DOWN, not cstage up.

Mirror cstage's mechanism exactly (check.c:598/2494/2529/2611): a `loops`
counter incremented around for and for-range bodies -- the for-`else` and
the init/cond/post walked OUTSIDE the count, since a break there targets an
enclosing loop -- rejecting break/continue when loops==0 with a
byte-identical `file:line:col: error: <kw> outside loop` diagnostic.
match/switch are not loop targets, matching cstage.

The divergence survived because 300_check.c only exercised the in-process C
checker, never w6c_ww; the fix adds 4 rows to the both-stage
989_catA_f2_reject carrier (break/continue outside loop, the for-else
els-outside-count edge, and an in-loop control). make clean && make test:
all 402 passed, byte-id self-compile gates 990-996 green.
2026-06-23 00:23:15 +09:00

431 lines
13 KiB
C

/*
* 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 <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 expect_build; /* 1 = build+run to want_exit; 0 = must FAIL */
int want_exit;
const char *diag; /* cstage/shared reject diagnostic (NULL for build rows) */
const char *wdiag; /* wwstage override when stages diverge (NULL = use diag) */
};
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, "array length must be an integer literal" },
/* #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, "arithmetic on non-numeric type" },
/* #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, "bitwise on non-integer type" },
/* #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, "ordered comparison on non-numeric" },
/* #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, "~ on non-integer" },
/* #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, "unknown type 'v'" },
/* #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, "not assignable to declared", "tuple literal has 3 elements" },
/* #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, "not assignable to declared", "tuple literal has 2 elements" },
/* #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, "multi-assign rhs is not a tuple" },
/* #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, "multi-assign rhs is not a tuple" },
/* #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, "multi-success union unwired" },
/* #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 arm yields" },
/* 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 },
/* break/continue outside any enclosing for/for-range → REJECT. Pre-fix
* wwstage had no loop-nesting guard (check.ww resolvewalk) and silently
* accepted, emitting a JMP to an undefined loop label; cstage rejects at
* cmd/wcc/check.c:2611-2615 (c->loops == 0). Both diagnostics match. */
{ "break_outside_loop",
"package main;\n"
"export fn main() void = {\n"
" break;\n"
"};\n",
0, 0, "break outside loop" },
{ "continue_outside_loop",
"package main;\n"
"export fn main() void = {\n"
" continue;\n"
"};\n",
0, 0, "continue outside loop" },
/* break in a for's `else` block with NO enclosing loop → REJECT. The
* else runs at normal cond-false exit (skipped by break) and targets an
* ENCLOSING loop, so it is OUTSIDE this loop's break count — cstage
* cmd/wcc/check.c:2546-2549 keeps els out of the count; wwstage mirrors. */
{ "break_in_else_outside_loop",
"package main;\n"
"export fn main() void = {\n"
" let i: i32 = 0;\n"
" for (i < 1) { i = i + 1; } else { break; };\n"
"};\n",
0, 0, "break outside loop" },
/* control — break/continue INSIDE a loop are valid → run 5. */
{ "break_continue_inloop_ok",
"package main;\n"
"export fn main() i32 = {\n"
" let i: i32 = 0;\n"
" for (i < 10) {\n"
" i = i + 1;\n"
" if (i == 3) { continue; };\n"
" if (i == 5) { break; };\n"
" };\n"
" return i;\n"
"};\n",
1, 5 },
};
static int
run_build(const char *driver, const struct row *r, int i)
{
char src[128], tmpdir[64], outbin[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/f2r_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/f2r_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/f2r_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -2; }
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
driver, outbin, src);
int brc = runwait(cmd);
int got = -1;
if (brc == 0) got = runwait(outbin);
runwait(rmcmd);
return brc == 0 ? got : -1;
}
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 — the build must FAIL *and* emit `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[128], tmpdir[64], outbin[128], errf[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/f2n_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(s, sizeof s, "%s/f2n_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/f2n_%d_%d", tmpdir, getpid(), i);
snprintf(errf, sizeof errf, "%s/err", tmpdir);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(s, "wb");
if (!f) { runwait(rmcmd); return -1; }
fputs(src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s >/dev/null 2>%s",
driver, outbin, s, errf);
int rc = runwait(cmd);
int hasmsg = filehas(errf, diag);
runwait(rmcmd);
/* build must NOT succeed AND must emit its diagnostic. */
return (rc != 0 && hasmsg) ? 0 : -1;
}
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,
drivers[d].gated && rows[i].wdiag
? rows[i].wdiag : rows[i].diag,
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;
}