wcc/ww: tagged-union normalization at tinfofornode (never-drop, dedup, collapse, nullable fold)

wwstage computed tagged sizes/tags off the raw variant list — size()
folded wrong constants (size((*u8|void)) 16 vs 8, (i32|never) 16 vs 4)
and duplicate variants got divergent tag numbering vs cstage, while
ww's own cgen layout folded nullable but its size() didn't. Make
tinfofornode's N_TTAGGED arm the normalization SSoT mirroring cstage
resolve_type (check.c:801-882): never-drop, duplicate dedup via
structural typeeq, single-variant collapse, nullable fold on the
normalized pair; astsize/astalign delegate, and voidvariantindex reads
the normalized ti.params (cgen.c:900-911) so construct/match/void tag
readers agree. Corpus-neutral (zero-move on all combineds);
989_tagnorm_run pins the folds dual-stage, red-proven. Review items
#1/#3; residual #45 filed (AST-keyed nullable gate at global emit).
This commit is contained in:
2026-06-12 07:03:45 +09:00
parent d6052e0829
commit e0df2adf47
6 changed files with 666 additions and 201 deletions

271
test/wcc/989_tagnorm_run.c Normal file
View File

@@ -0,0 +1,271 @@
/*
* 989_tagnorm_run — F1 (#1/#3): tagged-union type-set normalization must be
* symmetric across stages. cstage resolve_type N_TTAGGED (cmd/wcc/check.c:
* 801-882) drops `never`, dedups variants (variant_match: NAMED nominal,
* else structural), collapses a lone survivor to that variant, and folds a
* two-variant `(*T|void)` to an 8B nullable pointer. The wwstage formerly
* skipped ALL four at BOTH size sources — astsize (selfhost/cmd/wcc/check.ww)
* and tinfofornode N_TTAGGED — so size()/align() folded the RAW declaration
* list and tag numbering ran off it too.
*
* THE BUG (cat-A silent miscompile, gate-blind): size((*u8|void)) folded
* 16 in ww vs 8 in cs; size((i32|never)) 16 vs 4; align((i32|never)) 8 vs 4;
* size((i32|i32)) 16 vs 4 — all rc=0 both stages, divergent constant baked
* into the .s (MOVQ $16 vs $8/$4). The corpus never declares a never/dup/
* collapsible/nullable union whose SIZE it folds, so 990-997 stayed green;
* only a runtime size-fold row catches it.
*
* THE FIX: ONE normalization site — tinfofornode N_TTAGGED (the tinfo SSoT
* cgen already reads via ti.params/ti.size) gained never-drop + dedup +
* single-collapse; astsize/astalign N_TTAGGED delegate to it (mirroring the
* existing N_TTUPLE arm). cgen's deduped tag numbering rides ti.params for
* free.
*
* Rows (cstage `ww` + gated wwstage `ww_ww`; rule-10 + absolute value):
* row | shape | want | pre-fix ww
* ---------------+-------------------------------+------+-----------
* nullable_size | size((*u8|void)) | 8 | 16 [#1]
* never_size | size((i32|never)) | 4 | 16 [#1]
* dup_size | size((i32|i32)) | 4 | 16 [#1]
* never_align | align((i32|never)) | 4 | 8 [#1]
* dedup_match | "hi": (i32|i32|str), match str | 7 | 7 (green
* | both — dedup keeps construct/match consistent; the
* | tag-NUMBER divergence #3 is byte-id, caught by B1)
* void_dedup_ret | bare `return;` of (i32|i32|void)| 5 | !5 [voidvar-
* | then match the void variant | | iantindex fold]
* void_pos0_ret | bare `return;` of (void|i32) | 5 | 5 (idx-0
* | | | boundary)
* void_pos2_ret | bare `return;` of (i32|str|void)| 5 | !5 [idx-2
* | | | teeth]
*
* void_{dedup,pos0,pos2}_ret pin voidvariantindex's idx counter at the
* three normalized positions (0/1/2): the first fold omitted `idx += 1`,
* so it returned 0 for ANY void position — only positions 1 and 2 are RED
* under that bug (position 0 is the one value it happened to get right, a
* boundary pin). void_dedup_ret is the gate-blind teeth for the cgenutil
* voidvariantindex
* fold: the bare-`return;` void tag must come off the NORMALIZED (deduped)
* variant chain, like construct/match already do. With the F1 checker dedup
* (i32|i32|void)->(i32|void) but a RAW-list voidvariantindex, bare return
* stores tag 2 while match expects void at tag 1 -> no arm fires, r stays 0
* (RED). The fold reads ti.params -> tag 1 -> the void arm yields 5.
*/
#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_exit;
};
static const struct row rows[] = {
/* (1) #1 — nullable `(*T|void)` folds to an 8B pointer slot, not a
* 16B tag+payload box. */
{ "nullable_size",
"package main;\n"
"type p = (*u8 | void);\n"
"export fn main() int = { return size(p): int; };\n",
8 },
/* (2) #1 — `never` drops, leaving a lone i32 → the union collapses to
* i32 (size 4), not an 8+8 box. */
{ "never_size",
"package main;\n"
"type q = (i32 | never);\n"
"export fn main() int = { return size(q): int; };\n",
4 },
/* (3) #1 — duplicate variants dedup to one i32 → collapse to i32. */
{ "dup_size",
"package main;\n"
"type d = (i32 | i32);\n"
"export fn main() int = { return size(d): int; };\n",
4 },
/* (4) #1 — align() rides the same normalization: the collapsed i32
* aligns to 4, not the tag word's 8. */
{ "never_align",
"package main;\n"
"type q = (i32 | never);\n"
"export fn main() int = { return align(q): int; };\n",
4 },
/* (5) #3 — a deduped `(i32|i32|str)` still constructs+matches its str
* variant correctly in BOTH stages (the str-arm yields 7). Pins
* cs==ww on the functional outcome; the per-stage tag-NUMBER (str=$1
* vs $2) divergence is rule-10 byte-id, proven by B1 self-compile. */
{ "dedup_match",
"package main;\n"
"type u = (i32 | i32 | str);\n"
"export fn main() int = {\n"
" let v: u = \"hi\";\n"
" let r: i64 = 0;\n"
" match (v) {\n"
" case let s: str => { r = 7; };\n"
" case let x: i32 => { r = 1; };\n"
" };\n"
" return r: int;\n"
"};\n",
7 },
/* (6) #3/F1-fold — a deduped `(i32|i32|void)` returned via bare
* `return;` must tag the void variant off the NORMALIZED chain (idx 1),
* matching the dedup'd construct/match numbering. A raw-list
* voidvariantindex tags it 2 -> no match arm fires -> r stays 0. The
* void arm yields 5. */
{ "void_dedup_ret",
"package main;\n"
"type u = (i32 | i32 | void);\n"
"fn f() u = { return; };\n"
"export fn main() int = {\n"
" let v: u = f();\n"
" let r: i64 = 0;\n"
" match (v) {\n"
" case void => { r = 5; };\n"
" case let x: i32 => { r = 9; };\n"
" };\n"
" return r: int;\n"
"};\n",
5 },
/* (7) #3/F1-fold — void at NORMALIZED index 0 pins the lower
* boundary of voidvariantindex's idx counter (the only position the
* missing `idx += 1` happened to get right). `(void|i32)` keeps void
* first; bare `return;` tags idx 0, the void arm yields 5. */
{ "void_pos0_ret",
"package main;\n"
"type u = (void | i32);\n"
"fn f() u = { return; };\n"
"export fn main() int = {\n"
" let v: u = f();\n"
" let r: i64 = 0;\n"
" match (v) {\n"
" case void => { r = 5; };\n"
" case let x: i32 => { r = 9; };\n"
" };\n"
" return r: int;\n"
"};\n",
5 },
/* (8) #3/F1-fold — void at NORMALIZED index 2 is the stronger teeth
* for the idx counter: a raw-list / non-incrementing voidvariantindex
* tags it 0 (the i32 arm) instead of 2, so r never reaches 5.
* `(i32|str|void)` has no dedup/drop, so void's normalized index IS
* its declaration index 2. */
{ "void_pos2_ret",
"package main;\n"
"type u = (i32 | str | void);\n"
"fn f() u = { return; };\n"
"export fn main() int = {\n"
" let v: u = f();\n"
" let r: i64 = 0;\n"
" match (v) {\n"
" case void => { r = 5; };\n"
" case let s: str => { r = 8; };\n"
" case let x: i32 => { r = 9; };\n"
" };\n"
" return r: int;\n"
"};\n",
5 },
};
/* run_build — build+run `src` via `driver`; returns the binary's exit
* code, or -1 on a build failure. */
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/tagnorm_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/tagnorm_%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;
}
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, "tagnorm_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
for (int i = 0; i < n; i++) {
total++;
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != rows[i].want_exit) {
fprintf(stderr, "tagnorm_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, rows[i].want_exit);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "tagnorm_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("tagnorm_run: %d/%d ok\n", total, total);
return 0;
}