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:
7
Makefile
7
Makefile
@@ -249,6 +249,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
|
||||
$(BIN)/test_tagged_call_arg \
|
||||
$(BIN)/test_tagged_call_arg_run \
|
||||
$(BIN)/test_tryprop_tag_remap_run \
|
||||
$(BIN)/test_nested_union_widen_run \
|
||||
$(BIN)/test_sret_struct_return \
|
||||
$(BIN)/test_sret_struct_return_run \
|
||||
$(BIN)/test_sret_narrow_field \
|
||||
@@ -829,6 +830,12 @@ $(BIN)/test_tryprop_tag_remap_run: test/wcc/925_tryprop_tag_remap_run.c \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_nested_union_widen_run: test/wcc/925_nested_union_widen_run.c \
|
||||
$(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
|
||||
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \
|
||||
$(LIB)/libwwrt.a | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
$(BIN)/test_sret_struct_return: test/wcc/721_sret_struct_return.c \
|
||||
$(BIN)/w6c $(BIN)/w6c_ww | $(BIN)
|
||||
$(CC) $(CFLAGS) -o $@ $<
|
||||
|
||||
@@ -574,10 +574,40 @@ cg_variant_match(Type *vt, Type *src)
|
||||
if (vt == NULL || src == NULL) return 0;
|
||||
if (type_isuntyped(src)) return type_assignable(vt, src);
|
||||
if (vt->kind == TY_NAMED && src->kind == TY_NAMED) return vt == src;
|
||||
if (vt->kind == TY_NAMED || src->kind == TY_NAMED) return 0;
|
||||
if (vt->kind == TY_NAMED || src->kind == TY_NAMED) {
|
||||
/* #218: nominal identity is lost when the source's stamped
|
||||
* type was collapsed to its unwrapped tagged (project
|
||||
* tinfo_lossy_nominal). A NAMED multi-variant union variant vs
|
||||
* an unwrapped-tagged source can still be THE nested variant —
|
||||
* fall back to structural equality of the two unwrapped tagged
|
||||
* unions so the outer widen tag (cg_tag_for_variant) computes.
|
||||
* Sound only while the model is nominal-lossy; the collision
|
||||
* guard at the widen site (cg_widen_tagged_store) enforces the
|
||||
* invariant for when #199b/B-full lands true nominal layout. */
|
||||
Type *vu = (vt->kind == TY_NAMED) ? vt->under : vt;
|
||||
Type *su = (src->kind == TY_NAMED) ? src->under : src;
|
||||
if (vu && su && vu->kind == TY_TAGGED && su->kind == TY_TAGGED)
|
||||
return type_eq(vu, su);
|
||||
return 0;
|
||||
}
|
||||
return type_eq(vt, src);
|
||||
}
|
||||
|
||||
/* cg_variant_struct_match — structural equality of two variants ignoring
|
||||
* nominal identity (peel NAMED, then type_eq). #218: the collision guard
|
||||
* at the nested-widen site counts how many du variants share the source's
|
||||
* *shape*; ≥2 means the structural fallback could not disambiguate them
|
||||
* once nominal identity is lost. cg_variant_match (pointer-id for both-
|
||||
* NAMED) would under-count here, so the guard needs the shape-only view. */
|
||||
static int
|
||||
cg_variant_struct_match(Type *vt, Type *src)
|
||||
{
|
||||
Type *vu = (vt && vt->kind == TY_NAMED) ? vt->under : vt;
|
||||
Type *su = (src && src->kind == TY_NAMED) ? src->under : src;
|
||||
if (vu == NULL || su == NULL) return 0;
|
||||
return type_eq(vu, su);
|
||||
}
|
||||
|
||||
/* cg_tagged_success_tag — index of the success variant in a tagged
|
||||
* union. Mirrors check.c tagged_success_type: explicit-flag mode
|
||||
* picks the first non-`!`-marked variant; legacy mode picks index 0. */
|
||||
@@ -1771,6 +1801,63 @@ cg_widen_tagged_store(Cg *c, Local **locals_p, Type *dst, Node *src,
|
||||
/* Tagged → tagged subset: copy slot words then tag-remap. */
|
||||
if (su && su->kind == TY_TAGGED) {
|
||||
int ssz = (int)su->size;
|
||||
/* #218: is the source itself a single NESTED variant of du
|
||||
* (its whole tagged type matches one du variant), rather than
|
||||
* a flattened SUBSET whose members spread into du? If so, the
|
||||
* inner tagged value is the payload: store it at slot+8 with
|
||||
* the outer tag at slot+0, exactly like the scalar/struct/str
|
||||
* single-variant arms below — NOT a copy-to-+0 + sub-variant
|
||||
* remap. cg_tag_for_variant's structural fallback (cgen.c
|
||||
* cg_variant_match) is what recovers the index after the
|
||||
* nominal-lossy collapse. */
|
||||
int nested = cg_tag_for_variant(du, st);
|
||||
if (nested >= 0) {
|
||||
/* drew collision guard: the structural fallback over-
|
||||
* matches if ≥2 nominally-distinct du 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. */
|
||||
int nmatch = 0;
|
||||
for (Tparam *p = du->params; p; p = p->next)
|
||||
if (cg_variant_struct_match(p->type, st))
|
||||
nmatch++;
|
||||
if (nmatch >= 2)
|
||||
fatal("cg_widen_tagged_store: structural fallback "
|
||||
"cannot disambiguate nominally-distinct same-"
|
||||
"shape variants without nominal layout "
|
||||
"(#218/#199b/B-full)");
|
||||
ins2(c, A_XORQ, areg(D_AX), areg(D_AX));
|
||||
for (int k = 0; k < sz; k += 8)
|
||||
ins2(c, A_MOVQ, areg(D_AX),
|
||||
amem(D_BP, write_off + k));
|
||||
if (src->kind == N_IDENT) {
|
||||
int soff = localfind(*locals_p, src->str);
|
||||
for (int k = 0; k < ssz; k += 8) {
|
||||
ins2(c, A_MOVQ, amem(D_BP, soff + k),
|
||||
areg(D_AX));
|
||||
ins2(c, A_MOVQ, areg(D_AX),
|
||||
amem(D_BP, write_off + 8 + k));
|
||||
}
|
||||
} else {
|
||||
cgexpr(c, src, *locals_p);
|
||||
ins2(c, A_MOVQ, areg(D_AX),
|
||||
amem(D_BP, write_off + 8));
|
||||
if (ssz > 8)
|
||||
ins2(c, A_MOVQ, areg(D_DX),
|
||||
amem(D_BP, write_off + 16));
|
||||
if (ssz > 16)
|
||||
ins2(c, A_MOVQ, areg(D_CX),
|
||||
amem(D_BP, write_off + 24));
|
||||
if (ssz > 24)
|
||||
ins2(c, A_MOVQ, areg(D_R8),
|
||||
amem(D_BP, write_off + 32));
|
||||
}
|
||||
ins2(c, A_MOVQ, aimm(nested),
|
||||
amem(D_BP, write_off + 0));
|
||||
if (via_outer) goto copy_out;
|
||||
return;
|
||||
}
|
||||
if (src->kind == N_IDENT) {
|
||||
int soff = localfind(*locals_p, src->str);
|
||||
for (int k = 0; k < ssz; k += 8) {
|
||||
|
||||
@@ -543,6 +543,26 @@ export fn typeeq(a: *tinfo, b: *tinfo) bool = {
|
||||
return true;
|
||||
};
|
||||
if (k == tykind.TY_NAMED) { return false; }; // nominal: only same ptr
|
||||
if (k == tykind.TY_TAGGED) {
|
||||
// Structural: variant lists match position-by-position, and
|
||||
// the nullable `(*T|void)` fold is part of identity. Mirrors
|
||||
// cstage type_eq's TY_TAGGED arm (cmd/wcc/type.c:271-284);
|
||||
// the missing branch let any two tagged unions compare equal
|
||||
// (fell through to the primitive `return true`), which
|
||||
// #218's cgvariantmatch structural fallback was the first
|
||||
// caller to exercise.
|
||||
if (a.nullable != b.nullable) { return false; };
|
||||
let pa: *tparam = a.params;
|
||||
let pb: *tparam = b.params;
|
||||
for (true) {
|
||||
if (pa == nil) { if (pb == nil) { return true; }; return false; };
|
||||
if (pb == nil) { return false; };
|
||||
if (!typeeq(pa.type_, pb.type_)) { return false; };
|
||||
pa = pa.tnext;
|
||||
pb = pb.tnext;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
if (k == tykind.TY_TUPLE) {
|
||||
let pa: *tparam = a.params;
|
||||
let pb: *tparam = b.params;
|
||||
|
||||
@@ -9734,6 +9734,26 @@ export fn typeeq(a: *tinfo, b: *tinfo) bool = {
|
||||
return true;
|
||||
};
|
||||
if (k == tykind.TY_NAMED) { return false; }; // nominal: only same ptr
|
||||
if (k == tykind.TY_TAGGED) {
|
||||
// Structural: variant lists match position-by-position, and
|
||||
// the nullable `(*T|void)` fold is part of identity. Mirrors
|
||||
// cstage type_eq's TY_TAGGED arm (cmd/wcc/type.c:271-284);
|
||||
// the missing branch let any two tagged unions compare equal
|
||||
// (fell through to the primitive `return true`), which
|
||||
// #218's cgvariantmatch structural fallback was the first
|
||||
// caller to exercise.
|
||||
if (a.nullable != b.nullable) { return false; };
|
||||
let pa: *tparam = a.params;
|
||||
let pb: *tparam = b.params;
|
||||
for (true) {
|
||||
if (pa == nil) { if (pb == nil) { return true; }; return false; };
|
||||
if (pb == nil) { return false; };
|
||||
if (!typeeq(pa.type_, pb.type_)) { return false; };
|
||||
pa = pa.tnext;
|
||||
pb = pb.tnext;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
if (k == tykind.TY_TUPLE) {
|
||||
let pa: *tparam = a.params;
|
||||
let pb: *tparam = b.params;
|
||||
@@ -16966,13 +16986,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
|
||||
@@ -17384,6 +17454,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).
|
||||
|
||||
@@ -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).
|
||||
|
||||
@@ -9734,6 +9734,26 @@ export fn typeeq(a: *tinfo, b: *tinfo) bool = {
|
||||
return true;
|
||||
};
|
||||
if (k == tykind.TY_NAMED) { return false; }; // nominal: only same ptr
|
||||
if (k == tykind.TY_TAGGED) {
|
||||
// Structural: variant lists match position-by-position, and
|
||||
// the nullable `(*T|void)` fold is part of identity. Mirrors
|
||||
// cstage type_eq's TY_TAGGED arm (cmd/wcc/type.c:271-284);
|
||||
// the missing branch let any two tagged unions compare equal
|
||||
// (fell through to the primitive `return true`), which
|
||||
// #218's cgvariantmatch structural fallback was the first
|
||||
// caller to exercise.
|
||||
if (a.nullable != b.nullable) { return false; };
|
||||
let pa: *tparam = a.params;
|
||||
let pb: *tparam = b.params;
|
||||
for (true) {
|
||||
if (pa == nil) { if (pb == nil) { return true; }; return false; };
|
||||
if (pb == nil) { return false; };
|
||||
if (!typeeq(pa.type_, pb.type_)) { return false; };
|
||||
pa = pa.tnext;
|
||||
pb = pb.tnext;
|
||||
};
|
||||
return true;
|
||||
};
|
||||
if (k == tykind.TY_TUPLE) {
|
||||
let pa: *tparam = a.params;
|
||||
let pb: *tparam = b.params;
|
||||
@@ -16966,13 +16986,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
|
||||
@@ -17384,6 +17454,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).
|
||||
|
||||
330
test/wcc/925_nested_union_widen_run.c
Normal file
330
test/wcc/925_nested_union_widen_run.c
Normal file
@@ -0,0 +1,330 @@
|
||||
/*
|
||||
* 925_nested_union_widen_run — widening a NAMED multi-variant union
|
||||
* value into an ENCLOSING union must tag the OUTER variant correctly
|
||||
* and lay the inner tagged value out as the payload at slot+8 (project
|
||||
* #218).
|
||||
*
|
||||
* Gate-blind hazard: the outer-widen of a NAMED multi-variant union
|
||||
* (`inner`) into `(size | inner)` took the tagged-SUBSET path — it
|
||||
* byte-copied the inner value at slot+0 and remapped its SUB-variants,
|
||||
* collapsing every inner sub-variant onto outer tag 0. The match
|
||||
* EXTRACT side already read the inner value from slot+8 (nested layout),
|
||||
* so store and extract disagreed and the outer arm mis-selected. The
|
||||
* byte-id (990-997) and cs==ww asm gates both passed because the
|
||||
* bootstrap never widens a multi-variant union into an enclosing one;
|
||||
* the #94 io eFinal error path (`let e: io.error = <leaf>; return e`)
|
||||
* is the first to exercise it. cstage cg_widen_tagged_store +
|
||||
* cg_variant_match (cmd/w6c/cgen.c) and the wwstage twin
|
||||
* (selfhost/cmd/wcc/cgenutil.ww cgwidentaggedstorebp + cgvariantmatch)
|
||||
* now detect the nested-variant case (the source's whole tagged type
|
||||
* matches one outer variant — recovered via a structural fallback after
|
||||
* the nominal-lossy collapse, project tinfo_lossy_nominal) and store
|
||||
* the inner value at slot+8 with the outer tag at slot+0.
|
||||
*
|
||||
* Two assertions per RUN row:
|
||||
* - RUNTIME: build with both `ww` (cstage) and `ww_ww` (wwstage),
|
||||
* run, compare the exit code. This is what was wrong pre-fix.
|
||||
* - ASM BYTE-ID: compile through `w6c` and `w6c_ww` and require
|
||||
* byte-identical .s (rule-10).
|
||||
*
|
||||
* ERR rows assert a COMPILE-ERROR on both stages (the drew collision
|
||||
* guard): when ≥2 outer variants are structurally identical, the
|
||||
* structural fallback cannot disambiguate them once nominal identity
|
||||
* is lost (#199b/B-full), so the compiler must STOP rather than silently
|
||||
* mis-tag. The over-match is unreachable under today's nominal-lossy
|
||||
* model but INVERTS when #199b lands — the guard pins the invariant now.
|
||||
*
|
||||
* Rows:
|
||||
* 1. outer_arm_select — let-widen `e: inner` into `(size | inner)`;
|
||||
* match selects the `inner` arm (the bug returned the `size` arm).
|
||||
* 2. destructure_return — `return e` widens through the tagged-return
|
||||
* ABI; the caller destructures the inner i32 payload (5) — proves
|
||||
* the payload survives the OUTER round-trip (the rule-7 gate: if
|
||||
* this fails it is the deep wrapped-slot-layout, not this fix).
|
||||
* 3. destructure_let — let-widen + nested match unwraps the inner i32
|
||||
* payload (7) — the payload survives a non-return widen too.
|
||||
* 4. single_variant — scalar widen into `(i32 | bool)`; the non-nested
|
||||
* single-variant path is unperturbed (control + byte-id witness).
|
||||
* 5. collision_guard — two structurally-identical NAMED unions as
|
||||
* sibling variants `(a | b)`; widening must COMPILE-ERROR (the
|
||||
* guard), not silently pick one.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
static int
|
||||
runwait(const char *cmd)
|
||||
{
|
||||
int rc = system(cmd);
|
||||
if (rc == -1) return -1;
|
||||
if (WIFEXITED(rc)) return WEXITSTATUS(rc);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* experr != 0: the source must FAIL to compile on both stages. */
|
||||
struct row { const char *label; const char *src; int want; int experr; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
{ "outer_arm_select",
|
||||
"type inner = !(i32|bool);\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let e: inner = (true: inner);\n"
|
||||
" let r: (size | inner) = e;\n"
|
||||
" match (r) {\n"
|
||||
" case let n: size => return 50;\n"
|
||||
" case let x: inner => return 0;\n"
|
||||
" };\n"
|
||||
"};\n",
|
||||
0, 0 },
|
||||
{ "destructure_return",
|
||||
"type inner = !(i32|bool);\n"
|
||||
"fn g() (size | inner) = {\n"
|
||||
" let e: inner = (5i32: inner);\n"
|
||||
" return e;\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" match (g()) {\n"
|
||||
" case let s: size => return 90;\n"
|
||||
" case let x: inner => match (x) {\n"
|
||||
" case let i: i32 => return i;\n"
|
||||
" case let b: bool => return 99;\n"
|
||||
" };\n"
|
||||
" };\n"
|
||||
"};\n",
|
||||
5, 0 },
|
||||
{ "destructure_let",
|
||||
"type inner = !(i32|bool);\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let e: inner = (7i32: inner);\n"
|
||||
" let r: (size | inner) = e;\n"
|
||||
" match (r) {\n"
|
||||
" case let n: size => return 50;\n"
|
||||
" case let x: inner => match (x) {\n"
|
||||
" case let i: i32 => return i;\n"
|
||||
" case let b: bool => return 88;\n"
|
||||
" };\n"
|
||||
" };\n"
|
||||
"};\n",
|
||||
7, 0 },
|
||||
{ "single_variant",
|
||||
"export fn main() i32 = {\n"
|
||||
" let r: (i32 | bool) = 7i32;\n"
|
||||
" match (r) {\n"
|
||||
" case let n: i32 => return n;\n"
|
||||
" case let b: bool => return 88;\n"
|
||||
" };\n"
|
||||
"};\n",
|
||||
7, 0 },
|
||||
{ "collision_guard",
|
||||
"type a = !(i32|bool);\n"
|
||||
"type b = !(i32|bool);\n"
|
||||
"export fn main() i32 = {\n"
|
||||
" let x: a = (1i32: a);\n"
|
||||
" let r: (a | b) = x;\n"
|
||||
" match (r) {\n"
|
||||
" case let p: a => return 1;\n"
|
||||
" case let q: b => return 2;\n"
|
||||
" };\n"
|
||||
"};\n",
|
||||
0, 1 },
|
||||
};
|
||||
|
||||
/* Write r->src to <dir>/<base>.ww; returns 0 on success. */
|
||||
static int
|
||||
write_src(const char *dir, const char *base, const struct row *r, char *out,
|
||||
size_t outsz)
|
||||
{
|
||||
snprintf(out, outsz, "%s/%s.ww", dir, base);
|
||||
FILE *f = fopen(out, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* Build src with `driver build` inside its own subdir, run the binary,
|
||||
* return its exit code (or -1 on a build/exec failure). */
|
||||
static int
|
||||
build_run(const char *driver, const char *src, const char *workdir)
|
||||
{
|
||||
char cmd[8192];
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s build %s > /dev/null 2>&1",
|
||||
workdir, driver, src);
|
||||
if (runwait(cmd) != 0) return -1;
|
||||
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
char outbin[1024];
|
||||
snprintf(outbin, sizeof outbin, "%s/%s", workdir, base);
|
||||
char *dot = strrchr(outbin, '.');
|
||||
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
||||
return runwait(outbin);
|
||||
}
|
||||
|
||||
static int
|
||||
files_equal(const char *a, const char *b)
|
||||
{
|
||||
FILE *fa = fopen(a, "rb");
|
||||
FILE *fb = fopen(b, "rb");
|
||||
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
|
||||
int ca, cb, eq = 1;
|
||||
do {
|
||||
ca = fgetc(fa);
|
||||
cb = fgetc(fb);
|
||||
if (ca != cb) { eq = 0; break; }
|
||||
} while (ca != EOF);
|
||||
fclose(fa);
|
||||
fclose(fb);
|
||||
return eq;
|
||||
}
|
||||
|
||||
int
|
||||
main(void)
|
||||
{
|
||||
const char *bin = getenv("BIN");
|
||||
if (!bin) bin = "out/bin";
|
||||
char absbin[512];
|
||||
if (bin[0] != '/') {
|
||||
char cwd[256];
|
||||
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
|
||||
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
|
||||
bin = absbin;
|
||||
}
|
||||
|
||||
char cdrv[640], wdrv[640], cw6[640], ww6[640];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
snprintf(cw6, sizeof cw6, "%s/w6c", bin);
|
||||
snprintf(ww6, sizeof ww6, "%s/w6c_ww", bin);
|
||||
|
||||
int have_ww = (access(wdrv, X_OK) == 0);
|
||||
int have_w6cww = (access(ww6, X_OK) == 0);
|
||||
|
||||
int n = (int)(sizeof rows / sizeof rows[0]);
|
||||
int total = 0, fail = 0;
|
||||
|
||||
for (int i = 0; i < n; i++) {
|
||||
const struct row *r = &rows[i];
|
||||
|
||||
char dir[] = "/tmp/nuw.XXXXXX";
|
||||
if (mkdtemp(dir) == NULL) {
|
||||
fprintf(stderr, "row[%s]: mkdtemp failed\n", r->label);
|
||||
fail++; total++;
|
||||
continue;
|
||||
}
|
||||
|
||||
char src[1024];
|
||||
if (write_src(dir, "p", r, src, sizeof src) != 0) {
|
||||
fprintf(stderr, "row[%s]: write src failed\n", r->label);
|
||||
fail++; total++;
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
if (r->experr) {
|
||||
/* Collision guard: w6c (and w6c_ww) must REFUSE to
|
||||
* emit. A successful emit is the silent mis-tag the
|
||||
* guard exists to stop. */
|
||||
char css[1024], wss[1024], cmd[8192];
|
||||
snprintf(css, sizeof css, "%s/cs.s", dir);
|
||||
snprintf(wss, sizeof wss, "%s/ww.s", dir);
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s > /dev/null 2>&1",
|
||||
cw6, css, src);
|
||||
total++;
|
||||
if (runwait(cmd) == 0) {
|
||||
fprintf(stderr,
|
||||
"row[%s][cstage]: emit SUCCEEDED, "
|
||||
"want compile-error (#218/#199b)\n",
|
||||
r->label);
|
||||
fail++;
|
||||
}
|
||||
if (have_w6cww) {
|
||||
snprintf(cmd, sizeof cmd,
|
||||
"%s -o %s %s > /dev/null 2>&1",
|
||||
ww6, wss, src);
|
||||
total++;
|
||||
if (runwait(cmd) == 0) {
|
||||
fprintf(stderr,
|
||||
"row[%s][wwstage]: emit SUCCEEDED, "
|
||||
"want compile-error (#218/#199b)\n",
|
||||
r->label);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
goto cleanup;
|
||||
}
|
||||
|
||||
/* ASM byte-id: w6c vs w6c_ww .s must be identical. */
|
||||
if (have_w6cww) {
|
||||
char css[1024], wss[1024], cmd[8192];
|
||||
snprintf(css, sizeof css, "%s/cs.s", dir);
|
||||
snprintf(wss, sizeof wss, "%s/ww.s", dir);
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s > /dev/null 2>&1",
|
||||
cw6, css, src);
|
||||
int rc1 = runwait(cmd);
|
||||
snprintf(cmd, sizeof cmd, "%s -o %s %s > /dev/null 2>&1",
|
||||
ww6, wss, src);
|
||||
int rc2 = runwait(cmd);
|
||||
total++;
|
||||
if (rc1 != 0 || rc2 != 0) {
|
||||
fprintf(stderr,
|
||||
"row[%s]: w6c/w6c_ww emit failed (%d/%d)\n",
|
||||
r->label, rc1, rc2);
|
||||
fail++;
|
||||
} else if (files_equal(css, wss) != 1) {
|
||||
fprintf(stderr,
|
||||
"row[%s]: cs.s != ww.s (rule-10 break)\n",
|
||||
r->label);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
/* RUNTIME: cstage. */
|
||||
{
|
||||
char wk[1024];
|
||||
snprintf(wk, sizeof wk, "%s/cs", dir);
|
||||
mkdir(wk, 0755);
|
||||
int got = build_run(cdrv, src, wk);
|
||||
total++;
|
||||
if (got != r->want) {
|
||||
fprintf(stderr,
|
||||
"row[%s][cstage]: exit=%d want=%d\n",
|
||||
r->label, got, r->want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
/* RUNTIME: wwstage (the second half of rule-10). */
|
||||
if (have_ww) {
|
||||
char wk[1024];
|
||||
snprintf(wk, sizeof wk, "%s/ww", dir);
|
||||
mkdir(wk, 0755);
|
||||
int got = build_run(wdrv, src, wk);
|
||||
total++;
|
||||
if (got != r->want) {
|
||||
fprintf(stderr,
|
||||
"row[%s][wwstage]: exit=%d want=%d\n",
|
||||
r->label, got, r->want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
|
||||
cleanup:
|
||||
{
|
||||
char rm[1100];
|
||||
snprintf(rm, sizeof rm, "rm -rf %s", dir);
|
||||
runwait(rm);
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr,
|
||||
"nested_union_widen_run: %d/%d checks failed\n", fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("nested_union_widen_run: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user