wcc/cgen: #52 error-first tagged-union success tag — successtag helper not hardcoded 0 (wwstage)

An error-first tagged union -- error variant at tag 0, success at tag 1+,
e.g. (myerr | u16) -- was silently miscompiled by wwstage: the try/propagate
codegen hardcoded success = tag 0, so the actual success value (tag 1)
failed the CMPQ $0 and fell to the error path -> exit(1) instead of the
value (44). cstage was correct (computes the success tag via
cg_tagged_success_tag = first non-error variant).

wwstage-only: a successtag/successvariant helper (mirroring cstage) replaces
the hardcoded tag-0 / first-param assumption at all four try sites --
cgtryprop (?), cgtryunw (!), and the two latent shift sites cgtrytupleshift
+ cgtrytaggedshift (which bite an error-first union with an aggregate
success payload). Success-first unions (the Hare idiom + what the selfhost
uses) keep successtag=0 -> CMPQ $0 unchanged -> byte-id-neutral on 990-997.
cstage untouched (w6c md5 unchanged).

byte-id 990-997 8/8. test/wcc/825 table-driven (errfirst must/prop +
tuple-success + success-first control). A separate nested-tagged-union
construction divergence is filed (#10/#125).
This commit is contained in:
2026-06-08 22:15:19 +09:00
parent ef6fcbfc04
commit fca979470f
5 changed files with 502 additions and 45 deletions

View File

@@ -0,0 +1,279 @@
/*
* 825_errfirst_union_try — error-first tagged-union try (`?`/`!`) success-tag
* (#52, the #216 deferred divergence). wwstage HARDCODED the success tag as 0,
* so for an ERROR-FIRST union — error variant at tag 0, success at tag 1+ (e.g.
* `(e | u16)` with `type e = !i32`) — the success (tag 1) failed the `CMPQ $0`
* success check and fell to the error path: `!` → exit(1), `?` → propagate.
* cstage was already correct via cg_tagged_success_tag (cmd/w6c/cgen.c:857):
* if any variant is an error, the success tag is the first NON-error variant's
* index, else 0. The `.s` diff was literally `CMPQ $1` (cstage) vs `CMPQ $0`
* (wwstage).
*
* The fix (selfhost cgenexpr.ww only — cstage UNCHANGED): a `successtag`
* helper mirroring cstage's, applied at the FOUR sites that shared the tag-0 /
* first-param assumption — cgtryprop CMPQ, cgtryunw CMPQ, and the
* cgtrytupleshift / cgtrytaggedshift payload-variant lookups (successvariant).
*
* row | shape | want
* ----------------+----------------------------------------+------
* errfirst_must | (e|u16) `!` unwrap (THE repro) | 44
* errfirst_prop | (e|u16) `?` propagate through outer fn | 44
* succfirst_ctrl | (u16|e) `!` — successtag=0, CMPQ $0 | 7
* errfirst_3var | (e1|e2|u16) success at index 2 | 99
* errfirst_tuple | (e|(u16,u16)) `!` — successvariant shift| 51
*
* errfirst_tuple is the LATENT-site row: the cgtrytupleshift payload-shift only
* bites an error-first union whose SUCCESS variant is itself a tuple. Pre-fix it
* fell two ways — the CMPQ $0 rejected tag-1 (→ exit 1), AND even with the CMPQ
* fixed the shift read ou.params.type_ (the error variant, not a tuple) so the
* rvalue tuple never filled the cursor (verified mutation: 88, not 51). The
* successvariant fix routes the shift to the tuple. The sibling cgtrytaggedshift
* (nested-tagged success) shares the identical successvariant call; it is not
* exercised here because nested-tagged union *construction* (the return-coercion
* box, main.ok) has a separate pre-existing cs!=ww divergence — #52-independent
* (present success-first too) — filed for its own fix.
*
* Exit codes are the process low-8-bits: 300 & 0xFF = 44. errfirst_* are the
* mutation-sane rows: pre-fix wwstage = exit 1 (wrong-tag → error path),
* cstage = correct → cs!=ww. succfirst_ctrl is the byte-id no-regress guard:
* successtag returns 0 there, so its `CMPQ $0` is UNCHANGED. A byte-id pass
* pins cstage==wwstage `.s` for every row.
*/
#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 want; };
static const struct row rows[] = {
/* errfirst_must — THE repro: error-first `(e|u16)`, `!` unwrap of a
* tag-1 success. Pre-fix wwstage CMPQ $0 vs tag 1 → error → exit 1. */
{ "errfirst_must",
"package main;\n"
"type e = !i32;\n"
"type u = (e | u16);\n"
"fn ok() u = { return 300u16; };\n"
"export fn main() i32 = {\n"
"\tlet x = ok()!;\n"
"\treturn x: i32;\n"
"};\n",
44 },
/* errfirst_prop — the `?` twin: outer propagates inner's success
* through the same error-first union, then `!` unwraps. */
{ "errfirst_prop",
"package main;\n"
"type e = !i32;\n"
"type u = (e | u16);\n"
"fn inner() u = { return 300u16; };\n"
"fn outer() u = {\n"
"\tlet v = inner()?;\n"
"\treturn v;\n"
"};\n"
"export fn main() i32 = {\n"
"\treturn outer()!: i32;\n"
"};\n",
44 },
/* succfirst_ctrl — success-FIRST `(u16|e)`, successtag=0: the byte-id
* no-regress guard, CMPQ $0 unchanged. */
{ "succfirst_ctrl",
"package main;\n"
"type e = !i32;\n"
"type u2 = (u16 | e);\n"
"fn g() u2 = { return 7u16; };\n"
"export fn main() i32 = {\n"
"\treturn g()!: i32;\n"
"};\n",
7 },
/* errfirst_3var — two error variants then success: successtag walks
* past index 0 AND 1 to return 2, pinning the walk (not 0, not 1). */
{ "errfirst_3var",
"package main;\n"
"type e1 = !i32;\n"
"type e2 = !i64;\n"
"type u3 = (e1 | e2 | u16);\n"
"fn h() u3 = { return 99u16; };\n"
"export fn main() i32 = {\n"
"\treturn h()!: i32;\n"
"};\n",
99 },
/* errfirst_tuple — error-first union whose SUCCESS variant is a tuple:
* exercises cgtrytupleshift via successvariant. Pre-fix exit 1 (CMPQ $0
* rejects tag 1); CMPQ-fixed-but-shift-reverted gives 88; full fix → 51
* (300+7 = 307, 307 & 0xFF = 51). */
{ "errfirst_tuple",
"package main;\n"
"type e = !i32;\n"
"type ut = (e | (u16, u16));\n"
"fn ok() ut = { return (300u16, 7u16); };\n"
"export fn main() i32 = {\n"
"\tlet t = ok()!;\n"
"\tlet (a, b) = t;\n"
"\treturn (a: i32) + (b: i32);\n"
"};\n",
51 },
};
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/efu_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/efu_%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;
}
/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[64], cs[64], ws[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/efu_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/efu_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/efu_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
unlink(src);
return -1;
}
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
bin, ws, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
unlink(src); unlink(cs);
return -1;
}
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
int rc = 0;
if (!fc || !fw) {
rc = -1;
} else {
for (;;) {
int a = fgetc(fc);
int b = fgetc(fw);
if (a != b) { rc = -1; break; }
if (a == EOF) break;
}
}
if (fc) fclose(fc);
if (fw) fclose(fw);
if (rc != 0)
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
r->label);
unlink(src); unlink(cs); unlink(ws);
return rc;
}
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 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, "errfirst_union_try: 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,
"errfirst_union_try[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0)
fail++;
}
}
if (fail) {
fprintf(stderr,
"errfirst_union_try: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("errfirst_union_try: %d/%d ok\n", total, total);
return 0;
}