Files
ww/test/wcc/949_intbinop_mismatch.c
Hojun-Cho 9fcb3be541 wcc/ww: reject mismatched integer binop operands (#26)
cstage rejects a binop whose two integer operands have different
types (e.g. int vs i32 from len()); wwstage accepted it, miscompiling
under no-implicit-promotion. Align wwstage UP: unifyarith now chases
aliases and loud-rejects an integer-type mismatch, routing the
ordered-comparison ops through the same path with the error message
threaded on `e`. Per the user's no-implicit-promotion decision.

Scope carve-outs: EQ/NEQ stay out of the reject (#34, the comparison
operators keep their own widening rule) and a rune literal is exempt
(#35, N_RUNELIT is still untyped at this point). Adds the 29-case
test/wcc/949_intbinop_mismatch.c and its Makefile wiring.
2026-06-15 01:48:17 +09:00

341 lines
10 KiB
C

/*
* 949_intbinop_mismatch — #26: ww requires an explicit integer
* conversion in binary ops (Go-faithful, user-blessed). cstage's cbinop
* routes arithmetic/bitwise/comparison through unify_arith
* (cmd/wcc/check.c:1034), which loud-rejects a typed/typed operand
* mismatch (check.c:1068). The wwstage checker's unifyarith returned the
* lhs type for any mismatch (silent accept) AND the comparison arm never
* called it at all, so `int < len(s)` (len() = i32) and `int & i32`
* silently compiled where cstage rejects — a rule-10 break (and a latent
* signed/unsigned miscompile for `int < uint`).
*
* The fix (selfhost/cmd/wcc/check.ww) aligns wwstage DOWN: unifyarith
* loud-rejects a mismatched typed pair, keeping only cstage's one-sided
* alias-vs-base promotion (check.c:1060), and the ORDERED comparison arm
* now routes through unifyarith. A rune LITERAL stays assignable to an
* integer (cstage's untyped_rune, type.c:379), so `c - '0'` is unaffected.
*
* 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 matched
* pairs, an explicit cast, the alias-vs-base promotion (part (a), the
* LOAD-BEARING row), and a rune-literal arithmetic mix all still pass.
*
* 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[] = {
/* headline #26: `int < len(s)` (len() returns i32) — the latent
* signed-mix comparison that wwstage silently accepted. */
{ "int_lt_len_i32",
"package main;\n"
"export fn main() i32 = {\n"
" let a: int = 1;\n"
" let s: []u8 = [];\n"
" if (a < len(s)) { return 1; };\n"
" return 0;\n"
"};\n",
K_BUILDERR, 0, "operands have differing types" },
/* signed/unsigned mismatch — the miscompile-prone shape. */
{ "int_lt_uint",
"package main;\n"
"export fn main() i32 = {\n"
" let a: int = 1;\n"
" let b: uint = 2;\n"
" if (a < b) { return 1; };\n"
" return 0;\n"
"};\n",
K_BUILDERR, 0, "operands have differing types" },
/* differing-width signed pair. */
{ "int_lt_i64",
"package main;\n"
"export fn main() i32 = {\n"
" let a: int = 1;\n"
" let b: i64 = 2i64;\n"
" if (a < b) { return 1; };\n"
" return 0;\n"
"};\n",
K_BUILDERR, 0, "operands have differing types" },
/* bitwise feeds the same unifyarith path (check.ww :2598). */
{ "int_and_i32",
"package main;\n"
"export fn main() i32 = {\n"
" let a: int = 1;\n"
" let b: i32 = 2;\n"
" let r: int = a & b;\n"
" if (r < 0) { return 1; };\n"
" return 0;\n"
"};\n",
K_BUILDERR, 0, "operands have differing types" },
/* positive control: matched int/int comparison. */
{ "int_lt_int",
"package main;\n"
"export fn main() i32 = {\n"
" let a: int = 1;\n"
" let b: int = 2;\n"
" if (a < b) { return 0; };\n"
" return 0;\n"
"};\n",
K_RUN, 0, NULL },
/* positive control: matched i32/i32. */
{ "i32_lt_i32",
"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 0;\n"
"};\n",
K_RUN, 0, NULL },
/* positive control: i32 < len(s) — both i32, the canonical loop. */
{ "i32_lt_len",
"package main;\n"
"export fn main() i32 = {\n"
" let s: []u8 = [];\n"
" let a: i32 = 0;\n"
" if (a < len(s)) { return 0; };\n"
" return 0;\n"
"};\n",
K_RUN, 0, NULL },
/* positive control: explicit cast resolves the #26 mix. */
{ "int_cast_len",
"package main;\n"
"export fn main() i32 = {\n"
" let s: []u8 = [];\n"
" let a: int = 0;\n"
" if (a < len(s): int) { return 0; };\n"
" return 0;\n"
"};\n",
K_RUN, 0, NULL },
/* LOAD-BEARING positive control for part (a): a one-sided alias vs
* its base promotes (cstage check.c:1060). Drops if the alias-chase
* arm is missing or mis-detects a primitive as a named type. */
{ "alias_vs_base",
"package main;\n"
"type myint = i32;\n"
"export fn main() i32 = {\n"
" let a: myint = 1: myint;\n"
" let b: i32 = 2;\n"
" if (a < b) { return 0; };\n"
" return 0;\n"
"};\n",
K_RUN, 0, NULL },
/* positive control: a rune LITERAL stays assignable to an integer
* (cstage's untyped_rune, type.c:379); `c - '0'` must NOT trip the
* #26 reject. */
{ "rune_lit_arith",
"package main;\n"
"export fn main() i32 = {\n"
" let c: u8 = 53u8;\n"
" let r: u8 = c - '0';\n"
" if (r < 10u8) { return 0; };\n"
" return 0;\n"
"};\n",
K_RUN, 0, NULL },
/* LOAD-BEARING positive control: a chained same-enum bitwise OR
* (`flag.A | flag.B | flag.C`, the w6l os.flag pattern). Both operands
* are the SAME enum type, which cstage's unify_arith accepts via
* type_eq's identity check (type.c:250) — wwstage must accept it too.
* Drops if typeeqast lacks the identity fast-path: the #26 reject then
* over-rejects a same-enum binop, breaking the w6l self-compile (994). */
{ "enum_flag_or",
"package main;\n"
"type flag = enum uint { A = 1, B = 2, C = 4 };\n"
"export fn main() i32 = {\n"
" let f: flag = flag.A | flag.B | flag.C;\n"
" return (f: i32) - 7;\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/ibm_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/ibm_%d_d_%d", getpid(), i);
snprintf(errf, sizeof errf, "/tmp/ibm_%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 #26 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/ibm_bi_%d_%d.ww", getpid(), i);
snprintf(cs_s, sizeof cs_s, "/tmp/ibm_bi_%d_%d_cs.s", getpid(), i);
snprintf(ws_s, sizeof ws_s, "/tmp/ibm_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 (#26)\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, "intbinop_mismatch: 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, "intbinop_mismatch: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("intbinop_mismatch: %d/%d ok\n", total, total);
return 0;
}