w6c+wwstage: tag the outer widen of a nested multi-variant union (#218)
The outer widen of a NAMED multi-variant union value into an enclosing union mis-tagged: the store took the tagged-subset path (inner value at slot+0 plus a sub-variant remap, collapsing every inner sub-variant onto outer tag 0), while the match-extract reads the nested layout (outer tag at +0, inner 16B value at +8). Store and extract disagreed, so the match selected the first arm. Pre-existing silent miscompile, latent because error-origination sites (`let e: io.error = <leaf>; return e`) were gate-blind — no test discriminated a freshly-originated error at a branched caller; the io vstream surface is the first to do so. Fix, both stages, byte-identical: cg_variant_match (cmd/w6c/cgen.c) and its wwstage mirror cgvariantmatch (cgenutil.ww) fall back to structural equality of the unwrapped tagged unions when the alias collapse loses nominal identity (a NAMED outer variant vs an unwrapped-tagged source); the widen store now writes the inner value at slot+8 and the outer tag at +0, matching the extract. The inner union's build/payload/extract already worked (a destructure through the outer round-trip recovers the inner payload) — only the outer-widen store was wrong. Collision guard (the fallback is unsound without it): structural matching cannot disambiguate two nominally-distinct same-shape variants in one outer union. That is unreachable under today's nominal-lossy collapse but inverts the moment #199b lands the nominal layer, so if >=2 outer variants structurally match the source we hard-error at compile time citing #199b — both stages, an enforced invariant rather than a "rare, trust it" assumption. Folds #219: the wwstage tinfo typeeq (lib/ww/typ.ww) had no TY_TAGGED branch and fell through to `return true` (any two tagged unions compared equal); cstage type_eq (type.c:269) has the structural branch. The structural fallback above is the first and only caller to compare two bare tagged unions, so #219 is unexercised — and therefore ungateable — in isolation; it folds here per the rule-11 couldn't-split carve-out (same structural reason as #206's N_TTUPLE fold). The added branch mirrors cstage type_eq, tightening wwstage into alignment. test/wcc/925_nested_union_widen_run: outer-arm select, destructure-after- propagation (payload survives the round-trip), destructure-let, single-variant control, and the collision-guard compile-error, each with a cstage==wwstage byte-id check (the path is gate-blind). Interim until #199b/B-full lands the true nominal wrapped-slot layout.
This commit is contained in:
@@ -2225,13 +2225,63 @@ fn flatvariantidxt(tagged: *tinfo, want: *tinfo) i32 = {
|
||||
let p: *tparam = ti.params;
|
||||
let idx: i32 = 0;
|
||||
for (p != nil) {
|
||||
if (typeeq(p.type_, want)) { return idx; };
|
||||
if (cgvariantmatch(p.type_, want)) { return idx; };
|
||||
p = p.tnext;
|
||||
idx += 1;
|
||||
};
|
||||
return -1;
|
||||
};
|
||||
|
||||
// cgvariantmatch — does a source value of type `want` tag as variant
|
||||
// `vt` in a tagged-union dispatch? Mirrors cstage cg_variant_match
|
||||
// (cmd/w6c/cgen.c):
|
||||
// - both NAMED → nominal ptr-id (typeeq line 545 = same ptr only)
|
||||
// - exactly one NAMED → #218 nominal lost: fall back to structural
|
||||
// equality of the two unwrapped tagged unions, so an outer widen of
|
||||
// a NAMED multi-variant union into an enclosing union computes its
|
||||
// tag (project tinfo_lossy_nominal). Sound only while the model is
|
||||
// nominal-lossy; the collision guard in cgwidentaggedstorebp enforces
|
||||
// the invariant for when #199b/B-full lands true nominal layout.
|
||||
// - neither NAMED → structural typeeq
|
||||
// The untyped/loose arm (cstage's type_assignable) is NOT mirrored here —
|
||||
// ww has no type_assignable, so it lives in taggedvariantindext's str/
|
||||
// slice shape fallback (the existing documented divergence).
|
||||
fn cgvariantmatch(vt: *tinfo, want: *tinfo) bool = {
|
||||
if (vt == nil) { return false; };
|
||||
if (want == nil) { return false; };
|
||||
if (vt.kind == tykind.TY_NAMED && want.kind == tykind.TY_NAMED) {
|
||||
return typeeq(vt, want);
|
||||
};
|
||||
if (vt.kind == tykind.TY_NAMED || want.kind == tykind.TY_NAMED) {
|
||||
let vu: *tinfo = vt;
|
||||
for (vu != nil && vu.kind == tykind.TY_NAMED) { vu = vu.under; };
|
||||
let wu: *tinfo = want;
|
||||
for (wu != nil && wu.kind == tykind.TY_NAMED) { wu = wu.under; };
|
||||
if (vu != nil && wu != nil
|
||||
&& vu.kind == tykind.TY_TAGGED
|
||||
&& wu.kind == tykind.TY_TAGGED) {
|
||||
return typeeq(vu, wu);
|
||||
};
|
||||
return false;
|
||||
};
|
||||
return typeeq(vt, want);
|
||||
};
|
||||
|
||||
// cgvariantstructmatch — structural equality ignoring nominal identity
|
||||
// (peel NAMED, then typeeq). #218 collision guard: counts how many dst
|
||||
// variants share the source's *shape*; ≥2 means the structural fallback
|
||||
// could not disambiguate them once nominal identity is lost. Mirrors
|
||||
// cstage cg_variant_struct_match (cmd/w6c/cgen.c).
|
||||
fn cgvariantstructmatch(vt: *tinfo, want: *tinfo) bool = {
|
||||
let vu: *tinfo = vt;
|
||||
for (vu != nil && vu.kind == tykind.TY_NAMED) { vu = vu.under; };
|
||||
let wu: *tinfo = want;
|
||||
for (wu != nil && wu.kind == tykind.TY_NAMED) { wu = wu.under; };
|
||||
if (vu == nil) { return false; };
|
||||
if (wu == nil) { return false; };
|
||||
return typeeq(vu, wu);
|
||||
};
|
||||
|
||||
// flatslicevariantidx — flat 0-based index of a slice-shape variant in
|
||||
// `tagged`. Prefers the variant whose element typeeq's the pattern
|
||||
// element `elem`; falls back to the first slice-shape slot when no exact
|
||||
@@ -2643,6 +2693,96 @@ fn cgwidentaggedstorebp(c: *cgen, dst: *tinfo, src: *node, slot_off: i32, slot_s
|
||||
};
|
||||
};
|
||||
};
|
||||
// #218: is the source itself a single NESTED variant of dt (its
|
||||
// whole tagged type matches one dt variant), rather than a flattened
|
||||
// SUBSET whose members spread into dt? If so, the inner tagged value
|
||||
// is the payload: store it at slot_off+8 with the outer tag at
|
||||
// slot_off+0, mirroring the scalar/struct/str single-variant arms —
|
||||
// NOT a copy-to-+0 + sub-variant remap. flatvariantidxt's structural
|
||||
// fallback (cgvariantmatch) recovers the index after the nominal-
|
||||
// lossy collapse. Gated on a tagged source so scalar/str/struct
|
||||
// sources keep their existing arms. Mirrors cstage
|
||||
// cg_widen_tagged_store's nested arm (cmd/w6c/cgen.c).
|
||||
let srctagged: bool = (rhstaggedident(c, src) != nil)
|
||||
|| rhstaggedabicall(c, src);
|
||||
if (srctagged) {
|
||||
let nested: i32 = flatvariantidxt(dt, src.type_: *tinfo);
|
||||
if (nested >= 0) {
|
||||
// drew collision guard: the structural fallback over-
|
||||
// matches if ≥2 nominally-distinct dt variants share the
|
||||
// source's shape. Unreachable under today's nominal-lossy
|
||||
// model, but INVERTS when #199b/B-full lands the nominal
|
||||
// layer — hard-error NOW so a future collision STOPS the
|
||||
// compiler instead of silently mis-tagging.
|
||||
let nmatch: i32 = 0;
|
||||
let gp: *tparam = dt.params;
|
||||
for (gp != nil) {
|
||||
if (cgvariantstructmatch(gp.type_,
|
||||
src.type_: *tinfo)) {
|
||||
nmatch += 1;
|
||||
};
|
||||
gp = gp.tnext;
|
||||
};
|
||||
if (nmatch >= 2) {
|
||||
let msg: str = "cgwidentaggedstore: structural fallback cannot disambiguate nominally-distinct same-shape variants without nominal layout (#218/#199b/B-full)\n";
|
||||
os.write(2, msg.ptr, msg.len: u64);
|
||||
os.exit(1);
|
||||
};
|
||||
let su: *tinfo = src.type_: *tinfo;
|
||||
for (su != nil && su.kind == tykind.TY_NAMED) {
|
||||
su = su.under;
|
||||
};
|
||||
let ssz: i32 = su.size: i32;
|
||||
emitline("\tXORQ\tAX, AX\n");
|
||||
let zk: i32 = 0;
|
||||
for (zk < slot_sz) {
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((slot_off + zk): i64);
|
||||
emitline("(BP)\n");
|
||||
zk += 8;
|
||||
};
|
||||
if (src.kind == nkind.N_IDENT) {
|
||||
let lc: *local = localfindnode(c, src.str);
|
||||
let soff: i32 = lc.off;
|
||||
let ck: i32 = 0;
|
||||
for (ck < ssz) {
|
||||
emitline("\tMOVQ\t");
|
||||
emitoff((soff + ck): i64);
|
||||
emitline("(BP), AX\n");
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((slot_off + 8 + ck): i64);
|
||||
emitline("(BP)\n");
|
||||
ck += 8;
|
||||
};
|
||||
} else {
|
||||
cgexpr(c, src);
|
||||
emitline("\tMOVQ\tAX, ");
|
||||
emitoff((slot_off + 8): i64);
|
||||
emitline("(BP)\n");
|
||||
if (ssz > 8) {
|
||||
emitline("\tMOVQ\tDX, ");
|
||||
emitoff((slot_off + 16): i64);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
if (ssz > 16) {
|
||||
emitline("\tMOVQ\tCX, ");
|
||||
emitoff((slot_off + 24): i64);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
if (ssz > 24) {
|
||||
emitline("\tMOVQ\tR8, ");
|
||||
emitoff((slot_off + 32): i64);
|
||||
emitline("(BP)\n");
|
||||
};
|
||||
};
|
||||
emitline("\tMOVQ\t$");
|
||||
emitint(nested: i64);
|
||||
emitline(", ");
|
||||
emitoff(slot_off: i64);
|
||||
emitline("(BP)\n");
|
||||
return;
|
||||
};
|
||||
};
|
||||
// Tagged source ident: byte-copy slot words then tag-remap.
|
||||
// rhstaggedident gates "src is a tagged-typed local ident"; the
|
||||
// remap reads the source tagged tinfo off the local's tnode (#68).
|
||||
|
||||
Reference in New Issue
Block a user