selfhost/cmd/wcc: type-key tagged variant match (#66, Phase-N step 3)

The user-ruled B-full semantic change: flip tagged-union variant matching
from surface-NAME to TYPE-identity (typeeq over tinfo.params), mirroring
cstage cg_variant_match (cmd/w6c/cgen.c:451). A cross-module `a.T` != `b.T`
and `type linerr=!str` != str are now distinguished by the per-decl TY_NAMED
pointer (Phase-N #64). ww has no type_assignable, so the untyped/loose arm
keeps the str/slice shape fallback (rule-10 align-down). The 5 helpers
(flatvariantidx, flatslicevariantidx, taggedvariantindex, cgtagvariantidx,
cgmatch dispatch) flip; nomem propagation (NAMED-name scan, no source value)
and the f64 widen arm (float-kind classification, no pattern node) are not
arm-by-value discrimination and stay name/kind-keyed.

The flip requires value nodes to carry nominal identity. exprtype's
N_STRUCTLIT arm stamped the flattened body, so `overflow{}` (overflow=!void)
got TY_VOID and missed its variant -- fixed to stamp the per-decl NAMED
(mktname(lhs.str) -> tinfofornode reuses the #64 NAMED build/cache, same ptr
the union variant resolved to), mirroring the N_CAST/N_IDENT arms + cstage.
Returns the body node unchanged (only e.type_ rides NAMED); struct-lit layout
is unaffected -- cgstructlitfill is structlookup(name)-keyed, never reads
NAMED.fields. The fix now hits all `T{}` stamps, kept byte-id by the #63/#65
structural-walker peels.

931_variant_typekey_run: table-driven, both stages, /tmp-isolated. Two rows
widen an alias-FIRST variant from a call (no surface name): `(linerr|str)`
str-via-call -> idx 1, `(ec|i32)` i32-via-call -> idx 1. Empirically
discriminating: FAILS pre-flip (wwstage falls to the leading-shape variant,
exit 10; cstage exit 0) and PASSES post-flip -- locking in the capability
byte-id can't reach (the corpus has no name-key/type-key-disagreeing
co-variant, which is why name-keying survived).

make test 134/134 (byte-id 990-997 green; 995 self-rebuild green).
This commit is contained in:
2026-05-23 21:22:24 +09:00
parent 0fde4beeb4
commit 3036ba766d
8 changed files with 634 additions and 369 deletions

View File

@@ -117,17 +117,16 @@ fn cgtagvariantidx(c: *cgen, tagged: *node, vt: *node) i32 = {
if (tagged == nil) { return -1; };
if (vt == nil) { return -1; };
if (tagged.kind != nkind.N_TTAGGED) { return -1; };
// `is []T` / `as []T` — slice-shape variant lookup routes through
// the shape-aware helper so non-N_TNAME variant nodes (which
// flatvariantidx's name key can't see) resolve. Task #19.
// `is []T` / `as []T` — slice-shape lookup routes through the
// element-aware helper, which carries the loose first-slice-shape
// fallback (cstage type_assignable stand-in) that flatvariantidx's
// strict typeeq below doesn't. Task #19; #66 refresh.
if (vt.kind == nkind.N_TSLICE) {
return flatslicevariantidx(c, tagged, vt.lhs);
};
let want: str;
want.ptr = nil; want.len = 0;
if (vt.kind == nkind.N_TNAME) { want = vt.str; };
if (want.len == 0) { return -1; };
return flatvariantidx(c, tagged, want);
// #66 Phase-N step 3: match by typeeq on vt's stamped tinfo
// (flatvariantidx), not vt's surface name.
return flatvariantidx(c, tagged, vt);
};
// cgtryprop — `e?` propagates the error variant up the stack.
@@ -1120,7 +1119,7 @@ fn cgmatch(c: *cgen, n: *node) void = {
if (scrutt.kind == nkind.N_TTAGGED) {
let r: i32 = -1;
if (pat.kind == nkind.N_TNAME) {
r = flatvariantidx(c, scrutt, pat.str);
r = flatvariantidx(c, scrutt, pat);
} else { if (pat.kind == nkind.N_TSLICE) {
// `case let s: []T =>` — pat.str is empty
// because the variant is a composite, so