wwstage: reject error-type vs int comparison (#246)

cstage cbinop routes every comparison through unify_arith
(cmd/wcc/check.c:952), which loud-rejects an error-typed operand
paired with a differing type (e.g. strconv.invalid != i32). wwstage
binoptype returned bool for comparisons without any unify step, so it
silently accepted a program cstage rejects -- a rule-10 break (align
the leaner-but-leniner wwstage DOWN to cstage).

Scope the rejection to an error operand (varianterr) mismatched with
the other (typeeqast) so the broad differing-types diagnostic -- whose
typeeqast-vs-cstage-type_eq asymmetry risk could reject valid bootstrap
code -- stays out of wwstage. Covers the whole comparison family
(EQ/NEQ/LT/LE/GT/GE), all of which cstage routes through unify_arith.

Found by impl-strconv3 writing the strconv test. Gate-blind: the
bootstrap never compares an error type to an int, so byte-id stayed
green while the stages disagreed on what's a valid program.

test/wcc/949_errtype_compare.c: both drivers reject invalid !=/==/< i32
(K_BUILDERR); same-error-type and plain-int compares still accept on
both stages + cs==ww byte-id (K_RUN). 12/12.
This commit is contained in:
2026-06-01 23:10:46 +09:00
parent a11785273a
commit dbc169a025
5 changed files with 333 additions and 6 deletions

View File

@@ -293,6 +293,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_overcap_tuple_field_store_run \
$(BIN)/test_mixed_scalar_tuple_sret_run \
$(BIN)/test_tuple_in_union_run \
$(BIN)/test_errtype_compare \
$(BIN)/test_tuple_elem_slice_len_run \
$(BIN)/test_str_forrange_loopvar_run \
$(BIN)/test_composite_call_arg \
@@ -1204,6 +1205,12 @@ $(BIN)/test_tuple_in_union_run: test/wcc/940_tuple_in_union_run.c \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_errtype_compare: test/wcc/949_errtype_compare.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_composite_call_arg: test/wcc/723_composite_call_arg.c \
$(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
$(CC) $(CFLAGS) -o $@ $<

View File

@@ -12384,8 +12384,23 @@ fn binoptype(c: *checker, e: *node) *node = {
};
if (op == tkind.TK_EQ || op == tkind.TK_NEQ ||
op == tkind.TK_LT || op == tkind.TK_LE ||
op == tkind.TK_GT || op == tkind.TK_GE ||
op == tkind.TK_AND || op == tkind.TK_OR) {
op == tkind.TK_GT || op == tkind.TK_GE) {
// cstage routes every comparison through unify_arith (check.c:952/
// 955 cbinop), which loud-rejects an error-typed operand paired
// with a differing type, e.g. strconv.invalid != i32. wwstage's
// unifyarith stays silent on generic typed mismatches (5-lite-b),
// but the error-type case is a real cs!=ww edge and must reject to
// match cstage (#246, rule 10). Scoped to error operands so the
// broad differing-types flip (typeeqast vs cstage type_eq
// asymmetry risk) stays out.
if (varianterr(c, ltn) || varianterr(c, rtn)) {
if (!typeeqast(ltn, rtn)) {
deffolderr(c, e, "operands have differing types");
};
};
return mktname(c, "bool");
};
if (op == tkind.TK_AND || op == tkind.TK_OR) {
return mktname(c, "bool");
};
// Unreachable for valid input: op is one of TK_PLUS/MINUS/STAR/

View File

@@ -2037,8 +2037,23 @@ fn binoptype(c: *checker, e: *node) *node = {
};
if (op == tkind.TK_EQ || op == tkind.TK_NEQ ||
op == tkind.TK_LT || op == tkind.TK_LE ||
op == tkind.TK_GT || op == tkind.TK_GE ||
op == tkind.TK_AND || op == tkind.TK_OR) {
op == tkind.TK_GT || op == tkind.TK_GE) {
// cstage routes every comparison through unify_arith (check.c:952/
// 955 cbinop), which loud-rejects an error-typed operand paired
// with a differing type, e.g. strconv.invalid != i32. wwstage's
// unifyarith stays silent on generic typed mismatches (5-lite-b),
// but the error-type case is a real cs!=ww edge and must reject to
// match cstage (#246, rule 10). Scoped to error operands so the
// broad differing-types flip (typeeqast vs cstage type_eq
// asymmetry risk) stays out.
if (varianterr(c, ltn) || varianterr(c, rtn)) {
if (!typeeqast(ltn, rtn)) {
deffolderr(c, e, "operands have differing types");
};
};
return mktname(c, "bool");
};
if (op == tkind.TK_AND || op == tkind.TK_OR) {
return mktname(c, "bool");
};
// Unreachable for valid input: op is one of TK_PLUS/MINUS/STAR/

View File

@@ -12384,8 +12384,23 @@ fn binoptype(c: *checker, e: *node) *node = {
};
if (op == tkind.TK_EQ || op == tkind.TK_NEQ ||
op == tkind.TK_LT || op == tkind.TK_LE ||
op == tkind.TK_GT || op == tkind.TK_GE ||
op == tkind.TK_AND || op == tkind.TK_OR) {
op == tkind.TK_GT || op == tkind.TK_GE) {
// cstage routes every comparison through unify_arith (check.c:952/
// 955 cbinop), which loud-rejects an error-typed operand paired
// with a differing type, e.g. strconv.invalid != i32. wwstage's
// unifyarith stays silent on generic typed mismatches (5-lite-b),
// but the error-type case is a real cs!=ww edge and must reject to
// match cstage (#246, rule 10). Scoped to error operands so the
// broad differing-types flip (typeeqast vs cstage type_eq
// asymmetry risk) stays out.
if (varianterr(c, ltn) || varianterr(c, rtn)) {
if (!typeeqast(ltn, rtn)) {
deffolderr(c, e, "operands have differing types");
};
};
return mktname(c, "bool");
};
if (op == tkind.TK_AND || op == tkind.TK_OR) {
return mktname(c, "bool");
};
// Unreachable for valid input: op is one of TK_PLUS/MINUS/STAR/

View File

@@ -0,0 +1,275 @@
/*
* 949_errtype_compare — #246: the wwstage checker must REJECT a
* comparison whose operand is an error type (`!T`) paired with a
* differing type, e.g. `strconv.invalid != i32`. cstage's cbinop
* (cmd/wcc/check.c:952) routes every comparison through unify_arith,
* which loud-rejects the differing-types pair; the wwstage checker's
* binoptype returned `bool` for comparisons WITHOUT any unify step, so
* it silently accepted a program cstage rejects — a rule-10 break.
*
* The fix (selfhost/cmd/wcc/check.ww binoptype) mirrors cstage DOWN
* (rule 10), scoped to the error-type operand so the broad
* differing-types diagnostic (typeeqast-vs-cstage-type_eq asymmetry
* risk) stays out of wwstage.
*
* K_BUILDERR rows: build FAILS with the differing-types diagnostic on
* BOTH drivers. K_RUN rows: build+run exit 0 on BOTH drivers AND the
* cs==ww .s is byte-identical — the positive controls pin that an
* error-type compared with itself, and a plain int compare, still pass
* (cstage allows both via type_eq).
*
* 940/945 precedent: ww_ww builds /tmp fixtures, so the selfhost-tree
* sibling-write race does not apply.
*/
#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;
}
static int
slurp_eq(const char *a, const char *b)
{
FILE *fa = fopen(a, "rb");
FILE *fb = fopen(b, "rb");
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
int rc = 0;
for (;;) {
int ca = fgetc(fa), cb = fgetc(fb);
if (ca != cb) { rc = -1; break; }
if (ca == EOF) break;
}
fclose(fa); fclose(fb);
return rc;
}
static int
file_contains(const char *path, const char *needle)
{
FILE *f = fopen(path, "rb");
if (!f) return 0;
char buf[8192];
size_t n = fread(buf, 1, sizeof buf - 1, f);
fclose(f);
buf[n] = '\0';
return strstr(buf, needle) != NULL;
}
#define K_RUN 0 /* build+run both drivers, exit==want, + cs==ww byte-id */
#define K_BUILDERR 1 /* build must FAIL with experr on BOTH drivers (rule 7) */
struct row { const char *label; const char *src; int kind; int want;
const char *experr; };
static const struct row rows[] = {
/* the headline #246 case: error type `invalid` (!i32) compared with
* a plain i32 via `!=`. cstage rejects; wwstage must now too. */
{ "neq_invalid_i32",
"package main;\n"
"type invalid = !i32;\n"
"export fn main() i32 = {\n"
" let e: invalid = 5: invalid;\n"
" let n: i32 = 3;\n"
" if (e != n) { return 1; };\n"
" return 0;\n"
"};\n",
K_BUILDERR, 0, "operands have differing types" },
/* sibling op: `==` over the same mismatched error/int pair. */
{ "eq_invalid_i32",
"package main;\n"
"type invalid = !i32;\n"
"export fn main() i32 = {\n"
" let e: invalid = 5: invalid;\n"
" let n: i32 = 3;\n"
" if (e == n) { return 1; };\n"
" return 0;\n"
"};\n",
K_BUILDERR, 0, "operands have differing types" },
/* ordered comparison shares cstage's unify_arith route; the error
* operand must reject there too (closes the comparison family, not
* just `!=`). */
{ "lt_invalid_i32",
"package main;\n"
"type invalid = !i32;\n"
"export fn main() i32 = {\n"
" let e: invalid = 5: invalid;\n"
" let n: i32 = 3;\n"
" if (e < n) { return 1; };\n"
" return 0;\n"
"};\n",
K_BUILDERR, 0, "operands have differing types" },
/* positive control: error type compared with ITSELF. cstage's
* type_eq holds, so unify_arith accepts — wwstage must not
* over-reject (the fix is scoped to a DIFFERING pair). */
{ "neq_same_error",
"package main;\n"
"type invalid = !i32;\n"
"export fn main() i32 = {\n"
" let e: invalid = 5: invalid;\n"
" let f: invalid = 6: invalid;\n"
" if (e != f) { return 0; };\n"
" return 0;\n"
"};\n",
K_RUN, 0, NULL },
/* positive control: plain int compare, untouched by the fix. */
{ "neq_int_int",
"package main;\n"
"export fn main() i32 = {\n"
" let a: i32 = 1;\n"
" let b: i32 = 2;\n"
" if (a != b) { return 0; };\n"
" return 1;\n"
"};\n",
K_RUN, 0, NULL },
};
/* build+run via a driver (ww / ww_ww); returns 0 pass, nonzero fail. */
static int
run_driver(const char *driver, const struct row *r, int i)
{
char src[96], tmpdir[96], errf[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/etc_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/etc_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/etc_%d_e_%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 >/dev/null 2>%s",
tmpdir, driver, src, errf);
int brc = runwait(cmd);
if (r->kind == K_BUILDERR) {
int ok = (brc != 0)
&& (r->experr == NULL || file_contains(errf, r->experr));
if (!ok)
fprintf(stderr, "row[%s]: %s expected loud #246 builderr "
"(brc=%d)\n", r->label, driver, brc);
unlink(src); unlink(errf); rmdir(tmpdir);
return ok ? 0 : 1;
}
if (brc != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); unlink(errf); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[256];
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); unlink(errf); rmdir(tmpdir);
if (got != r->want) {
fprintf(stderr, "row[%s]: %s exit %d, want %d\n",
r->label, driver, got, r->want);
return 1;
}
return 0;
}
/* cs==ww .s byte-id (rule 10) for the K_RUN rows. */
static int
byteid(const char *w6c, const char *w6c_ww, const struct row *r, int i)
{
char src[96], cs_s[96], ws_s[96], cmd[1024];
snprintf(src, sizeof src, "/tmp/etc_bi_%d_%d.ww", getpid(), i);
snprintf(cs_s, sizeof cs_s, "/tmp/etc_bi_%d_%d_cs.s", getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/etc_bi_%d_%d_ww.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
int rc = 0;
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src);
if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c failed\n", r->label); rc = 1; }
else {
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c_ww, ws_s, src);
if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: w6c_ww failed\n", r->label); rc = 1; }
else if (slurp_eq(cs_s, ws_s) != 0) {
fprintf(stderr, "row[%s]: cstage/wwstage .s DIFFER (#246)\n",
r->label);
rc = 1;
}
}
unlink(src); unlink(cs_s); unlink(ws_s);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[512];
if (bin[0] != '/') {
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[640], wdrv[640], w6c[640], w6c_ww[640];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
struct { const char *name; const char *path; 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].path, X_OK) != 0) {
fprintf(stderr, "errtype_compare: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
total++;
if (run_driver(drivers[d].path, &rows[i], i) != 0) fail++;
}
}
if (access(w6c_ww, X_OK) == 0) {
for (int i = 0; i < n; i++) {
if (rows[i].kind != K_RUN) continue;
total++;
if (byteid(w6c, w6c_ww, &rows[i], i) != 0) fail++;
}
}
if (fail) {
fprintf(stderr, "errtype_compare: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("errtype_compare: %d/%d ok\n", total, total);
return 0;
}