From e0df2adf479361da47cc0f056563019e3108f376 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Fri, 12 Jun 2026 07:03:45 +0900 Subject: [PATCH] wcc/ww: tagged-union normalization at tinfofornode (never-drop, dedup, collapse, nullable fold) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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). --- Makefile | 11 ++ selfhost/cmd/w6c/main.combined.ww | 195 ++++++++++++------- selfhost/cmd/wcc/cgenutil.ww | 30 ++- selfhost/cmd/wcc/check.ww | 165 ++++++++++------ selfhost/cmd/wwdump/main.combined.ww | 195 ++++++++++++------- test/wcc/989_tagnorm_run.c | 271 +++++++++++++++++++++++++++ 6 files changed, 666 insertions(+), 201 deletions(-) create mode 100644 test/wcc/989_tagnorm_run.c diff --git a/Makefile b/Makefile index 35a55411..4d947728 100644 --- a/Makefile +++ b/Makefile @@ -254,6 +254,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_gunsigned_run \ $(BIN)/test_taggedidx_run \ $(BIN)/test_fnptrcollide_run \ + $(BIN)/test_tagnorm_run \ $(BIN)/test_arr_ptr_global \ $(BIN)/test_def_arr_infer_len \ $(BIN)/test_def_arr_len \ @@ -690,6 +691,16 @@ $(BIN)/test_taggedidx_run: test/wcc/989_taggedidx_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_tagnorm_run (F1, #1/#3): tagged-union type-set normalization (never-drop, +# dedup, single-collapse, nullable fold) must be symmetric — size()/align() +# fold + tag numbering. wwstage gated. See the test header. +$(BIN)/test_tagnorm_run: test/wcc/989_tagnorm_run.c \ + $(BIN)/ww $(BIN)/ww_ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + # 989_fnptrcollide_run (F7-c7, #14): a value ident whose leaf collides with # a fn name must not be mis-folded into the fn's TEXT reloc. Build-must-fail # on BOTH driver twins (rule-10); wwstage gated. See the test header. diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 4f3e649f..8bc29570 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -11569,7 +11569,15 @@ fn astalign(c: *checker, t: *node) i64 = { if (k == nkind.N_TCHAN) { return 8i64; }; if (k == nkind.N_TFN) { return 8i64; }; if (k == nkind.N_TARRAY) { return astalign(c, t.lhs); }; - if (k == nkind.N_TTAGGED) { return 8i64; }; + if (k == nkind.N_TTAGGED) { + // Route through the normalization SSoT: a lone survivor + // collapses (align((i32|never))==align(i32)==4, not the tag + // word's 8), matching cstage align() reading resolve_type(...) + // ->align (cmd/wcc/check.c:1550). Same #1 family as astsize. + let ti: *tinfo = tinfofornode(c, t); + if (ti != nil) { return ti.align: i64; }; + return 8i64; + }; if (k == nkind.N_TTUPLE) { let m: i64 = 1i64; let p: *node = t.list; @@ -11652,16 +11660,16 @@ fn astsize(c: *checker, t: *node) i64 = { return (off + maxal - 1i64) & ~(maxal - 1i64); }; if (k == nkind.N_TTAGGED) { - // 8 (tag) + max variant payload, rounded up to 8. - let maxsz: i64 = 0i64; - let v: *node = t.list; - for (v != nil) { - let sz: i64 = astsize(c, v); - if (sz > maxsz) { maxsz = sz; }; - v = v.next; - }; - let pad: i64 = (maxsz + 7i64) & ~7i64; - return 8i64 + pad; + // Route through tinfofornode (the tagged-normalization SSoT) + // so the size() fold matches cgen's layout AND cstage: the raw + // 8+roundup8(max) here ignored never-drop / dedup / single- + // collapse / nullable, so size((*u8|void)) folded 16 not 8 and + // size((i32|never)) 16 not 4 (#1). Mirrors the N_TTUPLE arm + // above and cstage size() reading resolve_type(...)->size + // (cmd/wcc/check.c:1547-1550). + let ti: *tinfo = tinfofornode(c, t); + if (ti != nil) { return ti.size: i64; }; + return 0i64; }; if (k == nkind.N_TENUM) { if (t.lhs != nil) { return astsize(c, t.lhs); }; @@ -12287,6 +12295,24 @@ fn fieldslotsize(ft: *tinfo) u64 = { // computation tracks cstage natural sizes; cgen's slot-padding // contract (cmd/w6c/cgen.c let_emit_size:691-720 pads narrow scalars // to 8B) stays in slotsize's fallback walker. +// variantpresent — tagged-union dedup predicate. Mirrors cstage +// variant_present/variant_match (cmd/wcc/check.c:111-126): NAMED types +// are nominal (pointer-identical), everything else structural — exactly +// typeeq's contract (TY_NAMED → only same ptr, else structural; lib/ww/ +// typ.ww:581). nil guards match variant_match's `a==NULL||b==NULL → 0` +// so two unresolved variants never collapse. +fn variantpresent(head: *tparam, vt: *tinfo) bool = { + if (vt == nil) { return false; }; + let p: *tparam = head; + for (p != nil) { + if (p.type_ != nil) { + if (typeeq(p.type_, vt)) { return true; }; + }; + p = p.tnext; + }; + return false; +}; + fn tinfofornode(c: *checker, n: *node) *tinfo = { if (n == nil) { return nil; }; let cached: *tinfo = tinfocachelookup(c.tc, n); @@ -12583,29 +12609,31 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { }; r.slotsize = soff; case nkind.N_TTAGGED: - // Cstage cmd/wcc/check.c:347-435: 8B tag + max(variant) - // rounded up to 8. Pre-bind for cycle protection (recursive + // THE tagged-union normalization SSoT (astsize/astalign delegate + // here). Mirrors cstage resolve_type N_TTAGGED (cmd/wcc/check.c: + // 801-882): 8B tag + max(variant) rounded up to 8, AFTER type-set + // normalization — drop `never` (807/824), dedup variants by + // variantpresent==variant_match (837/825), collapse a lone + // survivor to that variant (847-848), fold a two-variant + // `(*T|void)` to an 8B nullable pointer (859-874). #1/#3: astsize + // (check.ww) AND this arm formerly sized the RAW declaration list + // (no drop/dedup/collapse), so size((i32|never)) folded 16 not 4 + // and (i32|i32|str) numbered str's tag 2 not 1 (cgen reads the + // deduped ti.params). Pre-bind for cycle protection (recursive // sum-type shapes through NAMED variants). r = newtype(tykind.TY_TAGGED); tinfocachebind(c.tc, n, r); // #50 / A.6.3f phase 1: populate ti.params as a tparam linked - // list (head=first source variant). #61a: flatten `...inner` - // tagged spreads into the chain and stamp each variant's - // iserror. Mirrors cstage check.c:366-389 — dealias one NAMED - // level, require TY_TAGGED, splice its (already-flattened) - // variants in declaration order; otherwise append the single - // variant. size/align stays accounted off the surface member - // (vt), so r.size is byte-identical to pre-#61a: the flatten + - // iserror are additive, with no #61a-stage readers (the variant - // machinery + cgwidentaggedstore/matchscrutt migrate onto the - // chain in #61b/c). Same shared Tparam shape ww reuses across - // struct-fields / tuple-fields / fn-params / tagged-variants - // (sea-of-stars per rule 12). Phase 2 (#50b) retired cgenutil's - // AST-keyed nullableptrtag onto the chain. + // list (head=first surviving variant). #61a: flatten `...inner` + // tagged spreads into the chain and stamp each variant's iserror. + // Same shared Tparam shape ww reuses across struct-fields / + // tuple-fields / fn-params / tagged-variants (sea-of-stars per + // rule 12). let head: *tparam = nil; let tail: *tparam = nil; let maxsz: u64 = 0u64; let al: u64 = 8u64; + let nv: i32 = 0; let v: *node = n.list; for (v != nil) { let vt: *tinfo = tinfofornode(c, v); @@ -12621,15 +12649,21 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { if (isspread && vu != nil && vu.kind == tykind.TY_TAGGED) { // #209: a `...inner` spread's PAYLOAD is its // members, not the whole inner union — size/align - // off each spliced member (cstage check.c:660-667 - // st->size), NOT the surface member vt (which would - // over-size by the inner union's own 8B tag word and - // desync the `field` slot from cstage's). Spliced + // off each surviving spliced member (cstage check.c: + // 822-834 st->size), NOT the surface member vt (which + // would over-size by the inner union's own 8B tag word + // and desync the `field` slot from cstage's). Spliced // variants carry the inner union's already-stamped // iserror; no re-derivation. let src: *tparam = vu.params; for (src != nil) { let st: *tinfo = src.type_; + if (st != nil && st.kind == tykind.TY_NEVER) { + src = src.tnext; continue; + }; + if (variantpresent(head, st)) { + src = src.tnext; continue; + }; if (st != nil) { if (st.size > maxsz) { maxsz = st.size; }; if (st.align > al) { al = st.align; }; @@ -12637,9 +12671,16 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { let tp: *tparam = alloc(tparam{name="", type_=src.type_, iserror=src.iserror, tnext=nil})!; if (head == nil) { head = tp; } else { tail.tnext = tp; }; tail = tp; + nv += 1; src = src.tnext; }; } else { + if (vt != nil && vt.kind == tykind.TY_NEVER) { + v = v.next; continue; + }; + if (variantpresent(head, vt)) { + v = v.next; continue; + }; if (vt != nil) { if (vt.size > maxsz) { maxsz = vt.size; }; if (vt.align > al) { al = vt.align; }; @@ -12648,42 +12689,48 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { let tp: *tparam = alloc(tparam{name="", type_=vt, iserror=ve, tnext=nil})!; if (head == nil) { head = tp; } else { tail.tnext = tp; }; tail = tp; + nv += 1; }; v = v.next; }; - r.params = head; - // #61 A.3 nullable fold: `(*T | void)` collapses to a single - // 8B pointer slot, null is the void variant. Mirrors - // cmd/wcc/check.c:412-426 — bare TNAME("void"), not `!void`, - // and not NAMED. AST-kind discrimination retained: wwstage - // tinfo carries no `iserror` field, so cstage's tinfo-level - // (kind==TY_VOID && !iserror) check doesn't port symmetrically. - let a: *node = n.list; - if (a != nil) { - let b: *node = a.next; - if (b != nil && b.next == nil) { - let aptr: bool = (a.kind == nkind.N_TPTR); - let bptr: bool = (b.kind == nkind.N_TPTR); - let avoid: bool = (a.kind == nkind.N_TNAME); - if (avoid) { avoid = streq(a.str, "void"); }; - let bvoid: bool = (b.kind == nkind.N_TNAME); - if (bvoid) { bvoid = streq(b.str, "void"); }; - let isnull: bool = false; + // Collapse on the NORMALIZED survivor count (cstage check.c: + // 847-882). The trailing tinfocachebind below re-binds n→r, so + // a collapsed `r` shadows the pre-bound tagged placeholder. + if (nv == 0) { + r = c.tc.tynever; + } else if (nv == 1) { + r = head.type_; + } else { + r.params = head; + // #61 A.3 nullable fold on the normalized pair so + // `(*T|void|never)` and dup'd shapes fold too. Mirrors + // cmd/wcc/check.c:859-874 — bare TY_VOID (NOT `!void`, + // carried by the tparam iserror flag), not NAMED. The + // per-variant iserror (varianterr) now lets this port at + // the tinfo layer; pre-#1 it AST-keyed n.list instead. + let isnull: bool = false; + if (nv == 2) { + let pa: *tparam = head; + let pb: *tparam = head.tnext; + let aptr: bool = (pa.type_ != nil && pa.type_.kind == tykind.TY_PTR); + let bptr: bool = (pb.type_ != nil && pb.type_.kind == tykind.TY_PTR); + let avoid: bool = (pa.type_ != nil && pa.type_.kind == tykind.TY_VOID && !pa.iserror); + let bvoid: bool = (pb.type_ != nil && pb.type_.kind == tykind.TY_VOID && !pb.iserror); if (aptr) { if (bvoid) { isnull = true; }; }; if (avoid) { if (bptr) { isnull = true; }; }; - if (isnull) { - r.size = 8u64; - r.align = 8u64; - r.nullable = 1; - r.slotsize = 8u64; - return r; - }; + }; + if (isnull) { + r.size = 8u64; + r.align = 8u64; + r.nullable = 1; + r.slotsize = 8u64; + } else { + let pad: u64 = (maxsz + 7u64) & ~7u64; + r.size = 8u64 + pad; + r.align = al; + r.slotsize = 8u64 + pad; }; }; - let pad: u64 = (maxsz + 7u64) & ~7u64; - r.size = 8u64 + pad; - r.align = al; - r.slotsize = 8u64 + pad; }; if (r != nil) { // #61 A.5: any arm that didn't set slotsize gets ti.size as @@ -20340,18 +20387,32 @@ export fn nullableptrtag(t: *node) i32 = { }; // voidvariantindex — find the 0-based index of the `void` variant in a -// tagged-union type expr, -1 if absent. Used by cgreturn to map bare -// `return;` in a tagged-union-returning fn to the void variant's tag. +// tagged-union type, -1 if absent. Used by cgreturn to map bare `return;` +// in a tagged-union-returning fn to the void variant's tag. +// +// #1/#3 (F1 fold): reads the NORMALIZED variant chain (ti.params, which +// tinfofornode now never-drops + dedups), NOT the raw AST tagged.list. The +// construct/match tag numbering already rides ti.params, so once F1 dedups +// it, a deduped union with a void variant (e.g. `(i32|i32|void)`) would +// desync its bare-`return;` void tag from match's if this stayed AST-keyed. +// Mirrors cstage cg_tag_for_variant(rt, ty_void) (cmd/w6c/cgen.c:900-911): +// chase NAMED, scan params, match bare TY_VOID (not `!void`, carried by the +// tparam iserror flag). fn voidvariantindex(tagged: *node) i32 = { if (tagged == nil) { return -1; }; - if (tagged.kind != nkind.N_TTAGGED) { return -1; }; - let v: *node = tagged.list; + let ti: *tinfo = tagged.type_: *tinfo; + if (ti == nil) { return -1; }; + ti = tichase(ti); + if (ti == nil) { return -1; }; + if (ti.kind != tykind.TY_TAGGED) { return -1; }; + let p: *tparam = ti.params; let idx: i32 = 0; - for (v != nil) { - if (v.kind == nkind.N_TNAME) { - if (streq(v.str, "void")) { return idx; }; + for (p != nil) { + let vt: *tinfo = p.type_; + if (vt != nil && vt.kind == tykind.TY_VOID && !p.iserror) { + return idx; }; - v = v.next; + p = p.tnext; idx += 1; }; return -1; diff --git a/selfhost/cmd/wcc/cgenutil.ww b/selfhost/cmd/wcc/cgenutil.ww index 681b2b4b..263c5709 100644 --- a/selfhost/cmd/wcc/cgenutil.ww +++ b/selfhost/cmd/wcc/cgenutil.ww @@ -2967,18 +2967,32 @@ export fn nullableptrtag(t: *node) i32 = { }; // voidvariantindex — find the 0-based index of the `void` variant in a -// tagged-union type expr, -1 if absent. Used by cgreturn to map bare -// `return;` in a tagged-union-returning fn to the void variant's tag. +// tagged-union type, -1 if absent. Used by cgreturn to map bare `return;` +// in a tagged-union-returning fn to the void variant's tag. +// +// #1/#3 (F1 fold): reads the NORMALIZED variant chain (ti.params, which +// tinfofornode now never-drops + dedups), NOT the raw AST tagged.list. The +// construct/match tag numbering already rides ti.params, so once F1 dedups +// it, a deduped union with a void variant (e.g. `(i32|i32|void)`) would +// desync its bare-`return;` void tag from match's if this stayed AST-keyed. +// Mirrors cstage cg_tag_for_variant(rt, ty_void) (cmd/w6c/cgen.c:900-911): +// chase NAMED, scan params, match bare TY_VOID (not `!void`, carried by the +// tparam iserror flag). fn voidvariantindex(tagged: *node) i32 = { if (tagged == nil) { return -1; }; - if (tagged.kind != nkind.N_TTAGGED) { return -1; }; - let v: *node = tagged.list; + let ti: *tinfo = tagged.type_: *tinfo; + if (ti == nil) { return -1; }; + ti = tichase(ti); + if (ti == nil) { return -1; }; + if (ti.kind != tykind.TY_TAGGED) { return -1; }; + let p: *tparam = ti.params; let idx: i32 = 0; - for (v != nil) { - if (v.kind == nkind.N_TNAME) { - if (streq(v.str, "void")) { return idx; }; + for (p != nil) { + let vt: *tinfo = p.type_; + if (vt != nil && vt.kind == tykind.TY_VOID && !p.iserror) { + return idx; }; - v = v.next; + p = p.tnext; idx += 1; }; return -1; diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index 5efb0b5c..7996901f 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -1149,7 +1149,15 @@ fn astalign(c: *checker, t: *node) i64 = { if (k == nkind.N_TCHAN) { return 8i64; }; if (k == nkind.N_TFN) { return 8i64; }; if (k == nkind.N_TARRAY) { return astalign(c, t.lhs); }; - if (k == nkind.N_TTAGGED) { return 8i64; }; + if (k == nkind.N_TTAGGED) { + // Route through the normalization SSoT: a lone survivor + // collapses (align((i32|never))==align(i32)==4, not the tag + // word's 8), matching cstage align() reading resolve_type(...) + // ->align (cmd/wcc/check.c:1550). Same #1 family as astsize. + let ti: *tinfo = tinfofornode(c, t); + if (ti != nil) { return ti.align: i64; }; + return 8i64; + }; if (k == nkind.N_TTUPLE) { let m: i64 = 1i64; let p: *node = t.list; @@ -1232,16 +1240,16 @@ fn astsize(c: *checker, t: *node) i64 = { return (off + maxal - 1i64) & ~(maxal - 1i64); }; if (k == nkind.N_TTAGGED) { - // 8 (tag) + max variant payload, rounded up to 8. - let maxsz: i64 = 0i64; - let v: *node = t.list; - for (v != nil) { - let sz: i64 = astsize(c, v); - if (sz > maxsz) { maxsz = sz; }; - v = v.next; - }; - let pad: i64 = (maxsz + 7i64) & ~7i64; - return 8i64 + pad; + // Route through tinfofornode (the tagged-normalization SSoT) + // so the size() fold matches cgen's layout AND cstage: the raw + // 8+roundup8(max) here ignored never-drop / dedup / single- + // collapse / nullable, so size((*u8|void)) folded 16 not 8 and + // size((i32|never)) 16 not 4 (#1). Mirrors the N_TTUPLE arm + // above and cstage size() reading resolve_type(...)->size + // (cmd/wcc/check.c:1547-1550). + let ti: *tinfo = tinfofornode(c, t); + if (ti != nil) { return ti.size: i64; }; + return 0i64; }; if (k == nkind.N_TENUM) { if (t.lhs != nil) { return astsize(c, t.lhs); }; @@ -1867,6 +1875,24 @@ fn fieldslotsize(ft: *tinfo) u64 = { // computation tracks cstage natural sizes; cgen's slot-padding // contract (cmd/w6c/cgen.c let_emit_size:691-720 pads narrow scalars // to 8B) stays in slotsize's fallback walker. +// variantpresent — tagged-union dedup predicate. Mirrors cstage +// variant_present/variant_match (cmd/wcc/check.c:111-126): NAMED types +// are nominal (pointer-identical), everything else structural — exactly +// typeeq's contract (TY_NAMED → only same ptr, else structural; lib/ww/ +// typ.ww:581). nil guards match variant_match's `a==NULL||b==NULL → 0` +// so two unresolved variants never collapse. +fn variantpresent(head: *tparam, vt: *tinfo) bool = { + if (vt == nil) { return false; }; + let p: *tparam = head; + for (p != nil) { + if (p.type_ != nil) { + if (typeeq(p.type_, vt)) { return true; }; + }; + p = p.tnext; + }; + return false; +}; + fn tinfofornode(c: *checker, n: *node) *tinfo = { if (n == nil) { return nil; }; let cached: *tinfo = tinfocachelookup(c.tc, n); @@ -2163,29 +2189,31 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { }; r.slotsize = soff; case nkind.N_TTAGGED: - // Cstage cmd/wcc/check.c:347-435: 8B tag + max(variant) - // rounded up to 8. Pre-bind for cycle protection (recursive + // THE tagged-union normalization SSoT (astsize/astalign delegate + // here). Mirrors cstage resolve_type N_TTAGGED (cmd/wcc/check.c: + // 801-882): 8B tag + max(variant) rounded up to 8, AFTER type-set + // normalization — drop `never` (807/824), dedup variants by + // variantpresent==variant_match (837/825), collapse a lone + // survivor to that variant (847-848), fold a two-variant + // `(*T|void)` to an 8B nullable pointer (859-874). #1/#3: astsize + // (check.ww) AND this arm formerly sized the RAW declaration list + // (no drop/dedup/collapse), so size((i32|never)) folded 16 not 4 + // and (i32|i32|str) numbered str's tag 2 not 1 (cgen reads the + // deduped ti.params). Pre-bind for cycle protection (recursive // sum-type shapes through NAMED variants). r = newtype(tykind.TY_TAGGED); tinfocachebind(c.tc, n, r); // #50 / A.6.3f phase 1: populate ti.params as a tparam linked - // list (head=first source variant). #61a: flatten `...inner` - // tagged spreads into the chain and stamp each variant's - // iserror. Mirrors cstage check.c:366-389 — dealias one NAMED - // level, require TY_TAGGED, splice its (already-flattened) - // variants in declaration order; otherwise append the single - // variant. size/align stays accounted off the surface member - // (vt), so r.size is byte-identical to pre-#61a: the flatten + - // iserror are additive, with no #61a-stage readers (the variant - // machinery + cgwidentaggedstore/matchscrutt migrate onto the - // chain in #61b/c). Same shared Tparam shape ww reuses across - // struct-fields / tuple-fields / fn-params / tagged-variants - // (sea-of-stars per rule 12). Phase 2 (#50b) retired cgenutil's - // AST-keyed nullableptrtag onto the chain. + // list (head=first surviving variant). #61a: flatten `...inner` + // tagged spreads into the chain and stamp each variant's iserror. + // Same shared Tparam shape ww reuses across struct-fields / + // tuple-fields / fn-params / tagged-variants (sea-of-stars per + // rule 12). let head: *tparam = nil; let tail: *tparam = nil; let maxsz: u64 = 0u64; let al: u64 = 8u64; + let nv: i32 = 0; let v: *node = n.list; for (v != nil) { let vt: *tinfo = tinfofornode(c, v); @@ -2201,15 +2229,21 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { if (isspread && vu != nil && vu.kind == tykind.TY_TAGGED) { // #209: a `...inner` spread's PAYLOAD is its // members, not the whole inner union — size/align - // off each spliced member (cstage check.c:660-667 - // st->size), NOT the surface member vt (which would - // over-size by the inner union's own 8B tag word and - // desync the `field` slot from cstage's). Spliced + // off each surviving spliced member (cstage check.c: + // 822-834 st->size), NOT the surface member vt (which + // would over-size by the inner union's own 8B tag word + // and desync the `field` slot from cstage's). Spliced // variants carry the inner union's already-stamped // iserror; no re-derivation. let src: *tparam = vu.params; for (src != nil) { let st: *tinfo = src.type_; + if (st != nil && st.kind == tykind.TY_NEVER) { + src = src.tnext; continue; + }; + if (variantpresent(head, st)) { + src = src.tnext; continue; + }; if (st != nil) { if (st.size > maxsz) { maxsz = st.size; }; if (st.align > al) { al = st.align; }; @@ -2217,9 +2251,16 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { let tp: *tparam = alloc(tparam{name="", type_=src.type_, iserror=src.iserror, tnext=nil})!; if (head == nil) { head = tp; } else { tail.tnext = tp; }; tail = tp; + nv += 1; src = src.tnext; }; } else { + if (vt != nil && vt.kind == tykind.TY_NEVER) { + v = v.next; continue; + }; + if (variantpresent(head, vt)) { + v = v.next; continue; + }; if (vt != nil) { if (vt.size > maxsz) { maxsz = vt.size; }; if (vt.align > al) { al = vt.align; }; @@ -2228,42 +2269,48 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { let tp: *tparam = alloc(tparam{name="", type_=vt, iserror=ve, tnext=nil})!; if (head == nil) { head = tp; } else { tail.tnext = tp; }; tail = tp; + nv += 1; }; v = v.next; }; - r.params = head; - // #61 A.3 nullable fold: `(*T | void)` collapses to a single - // 8B pointer slot, null is the void variant. Mirrors - // cmd/wcc/check.c:412-426 — bare TNAME("void"), not `!void`, - // and not NAMED. AST-kind discrimination retained: wwstage - // tinfo carries no `iserror` field, so cstage's tinfo-level - // (kind==TY_VOID && !iserror) check doesn't port symmetrically. - let a: *node = n.list; - if (a != nil) { - let b: *node = a.next; - if (b != nil && b.next == nil) { - let aptr: bool = (a.kind == nkind.N_TPTR); - let bptr: bool = (b.kind == nkind.N_TPTR); - let avoid: bool = (a.kind == nkind.N_TNAME); - if (avoid) { avoid = streq(a.str, "void"); }; - let bvoid: bool = (b.kind == nkind.N_TNAME); - if (bvoid) { bvoid = streq(b.str, "void"); }; - let isnull: bool = false; + // Collapse on the NORMALIZED survivor count (cstage check.c: + // 847-882). The trailing tinfocachebind below re-binds n→r, so + // a collapsed `r` shadows the pre-bound tagged placeholder. + if (nv == 0) { + r = c.tc.tynever; + } else if (nv == 1) { + r = head.type_; + } else { + r.params = head; + // #61 A.3 nullable fold on the normalized pair so + // `(*T|void|never)` and dup'd shapes fold too. Mirrors + // cmd/wcc/check.c:859-874 — bare TY_VOID (NOT `!void`, + // carried by the tparam iserror flag), not NAMED. The + // per-variant iserror (varianterr) now lets this port at + // the tinfo layer; pre-#1 it AST-keyed n.list instead. + let isnull: bool = false; + if (nv == 2) { + let pa: *tparam = head; + let pb: *tparam = head.tnext; + let aptr: bool = (pa.type_ != nil && pa.type_.kind == tykind.TY_PTR); + let bptr: bool = (pb.type_ != nil && pb.type_.kind == tykind.TY_PTR); + let avoid: bool = (pa.type_ != nil && pa.type_.kind == tykind.TY_VOID && !pa.iserror); + let bvoid: bool = (pb.type_ != nil && pb.type_.kind == tykind.TY_VOID && !pb.iserror); if (aptr) { if (bvoid) { isnull = true; }; }; if (avoid) { if (bptr) { isnull = true; }; }; - if (isnull) { - r.size = 8u64; - r.align = 8u64; - r.nullable = 1; - r.slotsize = 8u64; - return r; - }; + }; + if (isnull) { + r.size = 8u64; + r.align = 8u64; + r.nullable = 1; + r.slotsize = 8u64; + } else { + let pad: u64 = (maxsz + 7u64) & ~7u64; + r.size = 8u64 + pad; + r.align = al; + r.slotsize = 8u64 + pad; }; }; - let pad: u64 = (maxsz + 7u64) & ~7u64; - r.size = 8u64 + pad; - r.align = al; - r.slotsize = 8u64 + pad; }; if (r != nil) { // #61 A.5: any arm that didn't set slotsize gets ti.size as diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index a358d4db..2459f399 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -11569,7 +11569,15 @@ fn astalign(c: *checker, t: *node) i64 = { if (k == nkind.N_TCHAN) { return 8i64; }; if (k == nkind.N_TFN) { return 8i64; }; if (k == nkind.N_TARRAY) { return astalign(c, t.lhs); }; - if (k == nkind.N_TTAGGED) { return 8i64; }; + if (k == nkind.N_TTAGGED) { + // Route through the normalization SSoT: a lone survivor + // collapses (align((i32|never))==align(i32)==4, not the tag + // word's 8), matching cstage align() reading resolve_type(...) + // ->align (cmd/wcc/check.c:1550). Same #1 family as astsize. + let ti: *tinfo = tinfofornode(c, t); + if (ti != nil) { return ti.align: i64; }; + return 8i64; + }; if (k == nkind.N_TTUPLE) { let m: i64 = 1i64; let p: *node = t.list; @@ -11652,16 +11660,16 @@ fn astsize(c: *checker, t: *node) i64 = { return (off + maxal - 1i64) & ~(maxal - 1i64); }; if (k == nkind.N_TTAGGED) { - // 8 (tag) + max variant payload, rounded up to 8. - let maxsz: i64 = 0i64; - let v: *node = t.list; - for (v != nil) { - let sz: i64 = astsize(c, v); - if (sz > maxsz) { maxsz = sz; }; - v = v.next; - }; - let pad: i64 = (maxsz + 7i64) & ~7i64; - return 8i64 + pad; + // Route through tinfofornode (the tagged-normalization SSoT) + // so the size() fold matches cgen's layout AND cstage: the raw + // 8+roundup8(max) here ignored never-drop / dedup / single- + // collapse / nullable, so size((*u8|void)) folded 16 not 8 and + // size((i32|never)) 16 not 4 (#1). Mirrors the N_TTUPLE arm + // above and cstage size() reading resolve_type(...)->size + // (cmd/wcc/check.c:1547-1550). + let ti: *tinfo = tinfofornode(c, t); + if (ti != nil) { return ti.size: i64; }; + return 0i64; }; if (k == nkind.N_TENUM) { if (t.lhs != nil) { return astsize(c, t.lhs); }; @@ -12287,6 +12295,24 @@ fn fieldslotsize(ft: *tinfo) u64 = { // computation tracks cstage natural sizes; cgen's slot-padding // contract (cmd/w6c/cgen.c let_emit_size:691-720 pads narrow scalars // to 8B) stays in slotsize's fallback walker. +// variantpresent — tagged-union dedup predicate. Mirrors cstage +// variant_present/variant_match (cmd/wcc/check.c:111-126): NAMED types +// are nominal (pointer-identical), everything else structural — exactly +// typeeq's contract (TY_NAMED → only same ptr, else structural; lib/ww/ +// typ.ww:581). nil guards match variant_match's `a==NULL||b==NULL → 0` +// so two unresolved variants never collapse. +fn variantpresent(head: *tparam, vt: *tinfo) bool = { + if (vt == nil) { return false; }; + let p: *tparam = head; + for (p != nil) { + if (p.type_ != nil) { + if (typeeq(p.type_, vt)) { return true; }; + }; + p = p.tnext; + }; + return false; +}; + fn tinfofornode(c: *checker, n: *node) *tinfo = { if (n == nil) { return nil; }; let cached: *tinfo = tinfocachelookup(c.tc, n); @@ -12583,29 +12609,31 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { }; r.slotsize = soff; case nkind.N_TTAGGED: - // Cstage cmd/wcc/check.c:347-435: 8B tag + max(variant) - // rounded up to 8. Pre-bind for cycle protection (recursive + // THE tagged-union normalization SSoT (astsize/astalign delegate + // here). Mirrors cstage resolve_type N_TTAGGED (cmd/wcc/check.c: + // 801-882): 8B tag + max(variant) rounded up to 8, AFTER type-set + // normalization — drop `never` (807/824), dedup variants by + // variantpresent==variant_match (837/825), collapse a lone + // survivor to that variant (847-848), fold a two-variant + // `(*T|void)` to an 8B nullable pointer (859-874). #1/#3: astsize + // (check.ww) AND this arm formerly sized the RAW declaration list + // (no drop/dedup/collapse), so size((i32|never)) folded 16 not 4 + // and (i32|i32|str) numbered str's tag 2 not 1 (cgen reads the + // deduped ti.params). Pre-bind for cycle protection (recursive // sum-type shapes through NAMED variants). r = newtype(tykind.TY_TAGGED); tinfocachebind(c.tc, n, r); // #50 / A.6.3f phase 1: populate ti.params as a tparam linked - // list (head=first source variant). #61a: flatten `...inner` - // tagged spreads into the chain and stamp each variant's - // iserror. Mirrors cstage check.c:366-389 — dealias one NAMED - // level, require TY_TAGGED, splice its (already-flattened) - // variants in declaration order; otherwise append the single - // variant. size/align stays accounted off the surface member - // (vt), so r.size is byte-identical to pre-#61a: the flatten + - // iserror are additive, with no #61a-stage readers (the variant - // machinery + cgwidentaggedstore/matchscrutt migrate onto the - // chain in #61b/c). Same shared Tparam shape ww reuses across - // struct-fields / tuple-fields / fn-params / tagged-variants - // (sea-of-stars per rule 12). Phase 2 (#50b) retired cgenutil's - // AST-keyed nullableptrtag onto the chain. + // list (head=first surviving variant). #61a: flatten `...inner` + // tagged spreads into the chain and stamp each variant's iserror. + // Same shared Tparam shape ww reuses across struct-fields / + // tuple-fields / fn-params / tagged-variants (sea-of-stars per + // rule 12). let head: *tparam = nil; let tail: *tparam = nil; let maxsz: u64 = 0u64; let al: u64 = 8u64; + let nv: i32 = 0; let v: *node = n.list; for (v != nil) { let vt: *tinfo = tinfofornode(c, v); @@ -12621,15 +12649,21 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { if (isspread && vu != nil && vu.kind == tykind.TY_TAGGED) { // #209: a `...inner` spread's PAYLOAD is its // members, not the whole inner union — size/align - // off each spliced member (cstage check.c:660-667 - // st->size), NOT the surface member vt (which would - // over-size by the inner union's own 8B tag word and - // desync the `field` slot from cstage's). Spliced + // off each surviving spliced member (cstage check.c: + // 822-834 st->size), NOT the surface member vt (which + // would over-size by the inner union's own 8B tag word + // and desync the `field` slot from cstage's). Spliced // variants carry the inner union's already-stamped // iserror; no re-derivation. let src: *tparam = vu.params; for (src != nil) { let st: *tinfo = src.type_; + if (st != nil && st.kind == tykind.TY_NEVER) { + src = src.tnext; continue; + }; + if (variantpresent(head, st)) { + src = src.tnext; continue; + }; if (st != nil) { if (st.size > maxsz) { maxsz = st.size; }; if (st.align > al) { al = st.align; }; @@ -12637,9 +12671,16 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { let tp: *tparam = alloc(tparam{name="", type_=src.type_, iserror=src.iserror, tnext=nil})!; if (head == nil) { head = tp; } else { tail.tnext = tp; }; tail = tp; + nv += 1; src = src.tnext; }; } else { + if (vt != nil && vt.kind == tykind.TY_NEVER) { + v = v.next; continue; + }; + if (variantpresent(head, vt)) { + v = v.next; continue; + }; if (vt != nil) { if (vt.size > maxsz) { maxsz = vt.size; }; if (vt.align > al) { al = vt.align; }; @@ -12648,42 +12689,48 @@ fn tinfofornode(c: *checker, n: *node) *tinfo = { let tp: *tparam = alloc(tparam{name="", type_=vt, iserror=ve, tnext=nil})!; if (head == nil) { head = tp; } else { tail.tnext = tp; }; tail = tp; + nv += 1; }; v = v.next; }; - r.params = head; - // #61 A.3 nullable fold: `(*T | void)` collapses to a single - // 8B pointer slot, null is the void variant. Mirrors - // cmd/wcc/check.c:412-426 — bare TNAME("void"), not `!void`, - // and not NAMED. AST-kind discrimination retained: wwstage - // tinfo carries no `iserror` field, so cstage's tinfo-level - // (kind==TY_VOID && !iserror) check doesn't port symmetrically. - let a: *node = n.list; - if (a != nil) { - let b: *node = a.next; - if (b != nil && b.next == nil) { - let aptr: bool = (a.kind == nkind.N_TPTR); - let bptr: bool = (b.kind == nkind.N_TPTR); - let avoid: bool = (a.kind == nkind.N_TNAME); - if (avoid) { avoid = streq(a.str, "void"); }; - let bvoid: bool = (b.kind == nkind.N_TNAME); - if (bvoid) { bvoid = streq(b.str, "void"); }; - let isnull: bool = false; + // Collapse on the NORMALIZED survivor count (cstage check.c: + // 847-882). The trailing tinfocachebind below re-binds n→r, so + // a collapsed `r` shadows the pre-bound tagged placeholder. + if (nv == 0) { + r = c.tc.tynever; + } else if (nv == 1) { + r = head.type_; + } else { + r.params = head; + // #61 A.3 nullable fold on the normalized pair so + // `(*T|void|never)` and dup'd shapes fold too. Mirrors + // cmd/wcc/check.c:859-874 — bare TY_VOID (NOT `!void`, + // carried by the tparam iserror flag), not NAMED. The + // per-variant iserror (varianterr) now lets this port at + // the tinfo layer; pre-#1 it AST-keyed n.list instead. + let isnull: bool = false; + if (nv == 2) { + let pa: *tparam = head; + let pb: *tparam = head.tnext; + let aptr: bool = (pa.type_ != nil && pa.type_.kind == tykind.TY_PTR); + let bptr: bool = (pb.type_ != nil && pb.type_.kind == tykind.TY_PTR); + let avoid: bool = (pa.type_ != nil && pa.type_.kind == tykind.TY_VOID && !pa.iserror); + let bvoid: bool = (pb.type_ != nil && pb.type_.kind == tykind.TY_VOID && !pb.iserror); if (aptr) { if (bvoid) { isnull = true; }; }; if (avoid) { if (bptr) { isnull = true; }; }; - if (isnull) { - r.size = 8u64; - r.align = 8u64; - r.nullable = 1; - r.slotsize = 8u64; - return r; - }; + }; + if (isnull) { + r.size = 8u64; + r.align = 8u64; + r.nullable = 1; + r.slotsize = 8u64; + } else { + let pad: u64 = (maxsz + 7u64) & ~7u64; + r.size = 8u64 + pad; + r.align = al; + r.slotsize = 8u64 + pad; }; }; - let pad: u64 = (maxsz + 7u64) & ~7u64; - r.size = 8u64 + pad; - r.align = al; - r.slotsize = 8u64 + pad; }; if (r != nil) { // #61 A.5: any arm that didn't set slotsize gets ti.size as @@ -20340,18 +20387,32 @@ export fn nullableptrtag(t: *node) i32 = { }; // voidvariantindex — find the 0-based index of the `void` variant in a -// tagged-union type expr, -1 if absent. Used by cgreturn to map bare -// `return;` in a tagged-union-returning fn to the void variant's tag. +// tagged-union type, -1 if absent. Used by cgreturn to map bare `return;` +// in a tagged-union-returning fn to the void variant's tag. +// +// #1/#3 (F1 fold): reads the NORMALIZED variant chain (ti.params, which +// tinfofornode now never-drops + dedups), NOT the raw AST tagged.list. The +// construct/match tag numbering already rides ti.params, so once F1 dedups +// it, a deduped union with a void variant (e.g. `(i32|i32|void)`) would +// desync its bare-`return;` void tag from match's if this stayed AST-keyed. +// Mirrors cstage cg_tag_for_variant(rt, ty_void) (cmd/w6c/cgen.c:900-911): +// chase NAMED, scan params, match bare TY_VOID (not `!void`, carried by the +// tparam iserror flag). fn voidvariantindex(tagged: *node) i32 = { if (tagged == nil) { return -1; }; - if (tagged.kind != nkind.N_TTAGGED) { return -1; }; - let v: *node = tagged.list; + let ti: *tinfo = tagged.type_: *tinfo; + if (ti == nil) { return -1; }; + ti = tichase(ti); + if (ti == nil) { return -1; }; + if (ti.kind != tykind.TY_TAGGED) { return -1; }; + let p: *tparam = ti.params; let idx: i32 = 0; - for (v != nil) { - if (v.kind == nkind.N_TNAME) { - if (streq(v.str, "void")) { return idx; }; + for (p != nil) { + let vt: *tinfo = p.type_; + if (vt != nil && vt.kind == tykind.TY_VOID && !p.iserror) { + return idx; }; - v = v.next; + p = p.tnext; idx += 1; }; return -1; diff --git a/test/wcc/989_tagnorm_run.c b/test/wcc/989_tagnorm_run.c new file mode 100644 index 00000000..c8050e9e --- /dev/null +++ b/test/wcc/989_tagnorm_run.c @@ -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 +#include +#include +#include +#include +#include + +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; +}