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

View File

@@ -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;

View File

@@ -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;

View File

@@ -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

View File

@@ -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;