w6c+wwstage: tag a bare value widened into a NAMED-alias union variant via structural fallback (#15)

Variant selection (cg_tag_for_variant / flatvariantidxt) matched union variants by exact type only, so widening a bare value (e.g. *vtable) into a union with a NAMED ptr-alias variant (stream = *vtable, in handle = (file | stream)) found no match and the tag defaulted to 0 -- the wrong variant. In the compiler this hit emitbytes' io.write(&cgoutstream.vt) once io.write took a handle, writing the asm to a garbage fd -> empty .s -> w6c_ww miscompiled everything. Add a second selection pass: when the exact pass finds no variant, structurally compare the bare source against each NAMED-alias variant's unwrapped type; exact-match still wins in pass 1 (so a bare i64 stays the i64 variant, not oserror=!i64, which kept the os/errno union building). A >=2-structural-match collision guard (extending #218's) hard-errors LOUDLY on genuine nominal ambiguity (two ptr-aliases to the same struct) instead of silently first-picking, citing #199b/#10. Symmetric across cstage (cmd/w6c/cgen.c) and wwstage (selfhost/cmd/wcc/cgenutil.ww). One-level NAMED unwrap (chained ptr-aliases unmatched, unexercised -> #17). Adds test/wcc/789 (positive widen byte-id+runtime + degenerate-ambiguity reject guard, both stages). Unblocks post-eFinal #5's handle surface. rule-10 fix-up.
This commit is contained in:
2026-05-30 09:04:33 +09:00
parent 419c896ad0
commit 45e5d74047
6 changed files with 453 additions and 0 deletions

View File

@@ -17010,6 +17010,9 @@ fn flatvariantidxt(tagged: *tinfo, want: *tinfo) i32 = {
for (ti != nil && ti.kind == tykind.TY_NAMED) { ti = ti.under; };
if (ti == nil) { return -1; };
if (ti.kind != tykind.TY_TAGGED) { return -1; };
// Pass 1: exact match (NAMED-vs-NAMED typeeq, tagged-vs-tagged, bare
// typeeq). Exact matches take precedence and need no guard — distinct
// variants don't exact-match the same source.
let p: *tparam = ti.params;
let idx: i32 = 0;
for (p != nil) {
@@ -17017,6 +17020,42 @@ fn flatvariantidxt(tagged: *tinfo, want: *tinfo) i32 = {
p = p.tnext;
idx += 1;
};
// Pass 2 (#15): no exact variant matched — structurally match a BARE
// source against a NAMED-alias variant (bare *vtable into the
// `stream` (= *vtable) variant of `(file | stream)`). The bare side
// has no nominal identity, so structure is the only discriminator;
// without this the widen found no variant and defaulted to tag 0,
// miscompiling emitbytes' io.write(&cgoutstream.vt). Exact-first
// (pass 1) keeps a bare `i64` into `(i64 | oserror)` binding the
// exact `i64`. drew's proviso: guard the structural fallback like the
// #218 nested-widen site — a bare source matching >=2 NAMED variants
// needs nominal layout to disambiguate, so hard-error.
if (want.kind != tykind.TY_NAMED) {
let q: *tparam = ti.params;
let qi: i32 = 0;
let found: i32 = -1;
let n: i32 = 0;
for (q != nil) {
// One-level NAMED unwrap: a chained ptr-alias variant
// (type a=*X; type b=a) isn't reached here, so it would
// silently mis-tag — unexercised (zero in corpus), see
// task #17.
let pu: *tinfo = q.type_;
if (pu != nil && pu.kind == tykind.TY_NAMED
&& pu.under != nil && typeeq(pu.under, want)) {
if (found < 0) { found = qi; };
n += 1;
};
q = q.tnext;
qi += 1;
};
if (n >= 2) {
let msg: str = "flatvariantidxt: bare source structurally matches >=2 NAMED variants — ambiguous without nominal layout (#15/#218/#199b/#10)\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
return found;
};
return -1;
};

View File

@@ -2253,6 +2253,9 @@ fn flatvariantidxt(tagged: *tinfo, want: *tinfo) i32 = {
for (ti != nil && ti.kind == tykind.TY_NAMED) { ti = ti.under; };
if (ti == nil) { return -1; };
if (ti.kind != tykind.TY_TAGGED) { return -1; };
// Pass 1: exact match (NAMED-vs-NAMED typeeq, tagged-vs-tagged, bare
// typeeq). Exact matches take precedence and need no guard — distinct
// variants don't exact-match the same source.
let p: *tparam = ti.params;
let idx: i32 = 0;
for (p != nil) {
@@ -2260,6 +2263,42 @@ fn flatvariantidxt(tagged: *tinfo, want: *tinfo) i32 = {
p = p.tnext;
idx += 1;
};
// Pass 2 (#15): no exact variant matched — structurally match a BARE
// source against a NAMED-alias variant (bare *vtable into the
// `stream` (= *vtable) variant of `(file | stream)`). The bare side
// has no nominal identity, so structure is the only discriminator;
// without this the widen found no variant and defaulted to tag 0,
// miscompiling emitbytes' io.write(&cgoutstream.vt). Exact-first
// (pass 1) keeps a bare `i64` into `(i64 | oserror)` binding the
// exact `i64`. drew's proviso: guard the structural fallback like the
// #218 nested-widen site — a bare source matching >=2 NAMED variants
// needs nominal layout to disambiguate, so hard-error.
if (want.kind != tykind.TY_NAMED) {
let q: *tparam = ti.params;
let qi: i32 = 0;
let found: i32 = -1;
let n: i32 = 0;
for (q != nil) {
// One-level NAMED unwrap: a chained ptr-alias variant
// (type a=*X; type b=a) isn't reached here, so it would
// silently mis-tag — unexercised (zero in corpus), see
// task #17.
let pu: *tinfo = q.type_;
if (pu != nil && pu.kind == tykind.TY_NAMED
&& pu.under != nil && typeeq(pu.under, want)) {
if (found < 0) { found = qi; };
n += 1;
};
q = q.tnext;
qi += 1;
};
if (n >= 2) {
let msg: str = "flatvariantidxt: bare source structurally matches >=2 NAMED variants — ambiguous without nominal layout (#15/#218/#199b/#10)\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
return found;
};
return -1;
};

View File

@@ -17010,6 +17010,9 @@ fn flatvariantidxt(tagged: *tinfo, want: *tinfo) i32 = {
for (ti != nil && ti.kind == tykind.TY_NAMED) { ti = ti.under; };
if (ti == nil) { return -1; };
if (ti.kind != tykind.TY_TAGGED) { return -1; };
// Pass 1: exact match (NAMED-vs-NAMED typeeq, tagged-vs-tagged, bare
// typeeq). Exact matches take precedence and need no guard — distinct
// variants don't exact-match the same source.
let p: *tparam = ti.params;
let idx: i32 = 0;
for (p != nil) {
@@ -17017,6 +17020,42 @@ fn flatvariantidxt(tagged: *tinfo, want: *tinfo) i32 = {
p = p.tnext;
idx += 1;
};
// Pass 2 (#15): no exact variant matched — structurally match a BARE
// source against a NAMED-alias variant (bare *vtable into the
// `stream` (= *vtable) variant of `(file | stream)`). The bare side
// has no nominal identity, so structure is the only discriminator;
// without this the widen found no variant and defaulted to tag 0,
// miscompiling emitbytes' io.write(&cgoutstream.vt). Exact-first
// (pass 1) keeps a bare `i64` into `(i64 | oserror)` binding the
// exact `i64`. drew's proviso: guard the structural fallback like the
// #218 nested-widen site — a bare source matching >=2 NAMED variants
// needs nominal layout to disambiguate, so hard-error.
if (want.kind != tykind.TY_NAMED) {
let q: *tparam = ti.params;
let qi: i32 = 0;
let found: i32 = -1;
let n: i32 = 0;
for (q != nil) {
// One-level NAMED unwrap: a chained ptr-alias variant
// (type a=*X; type b=a) isn't reached here, so it would
// silently mis-tag — unexercised (zero in corpus), see
// task #17.
let pu: *tinfo = q.type_;
if (pu != nil && pu.kind == tykind.TY_NAMED
&& pu.under != nil && typeeq(pu.under, want)) {
if (found < 0) { found = qi; };
n += 1;
};
q = q.tnext;
qi += 1;
};
if (n >= 2) {
let msg: str = "flatvariantidxt: bare source structurally matches >=2 NAMED variants — ambiguous without nominal layout (#15/#218/#199b/#10)\n";
os.write(2, msg.ptr, msg.len: u64);
os.exit(1);
};
return found;
};
return -1;
};