selfhost+test: route N_TSLICE variant through shape-aware index helper (#19)
Class A wwstage cgen miscompile, silent until wwstage path engaged.
Pre-fix wwstage's name-keyed flatvariantidx returned -1 for `[]T`
variants (pat.str empty on N_TSLICE), so cgmatch and
cgtagvariantidx collapsed every `(scalar | []T)` arm to tag 0.
Internally consistent within wwstage; cstage's structural
`type_eq` (cmd/w6c/cgen.c:466 cg_tag_for_variant) matched
correctly. Bootstrap stayed green because no selfhost-corpus path
exercises `(scalar | []T)` until lib/bytes / lib/strings landing
pulls bytes.index through wwstage compilation — 967_bytes_run
uses `ww run` (cstage only), so the wwstage path was never
exercised.
Polarity catalog entry: wwstage UNDER (missing N_TSLICE dispatch
arm in variantindex lookup), not REVERSE — worker's deeper read
corrected rob's initial diagnosis. cstage's structural type-eq is
the leaner-correct side; wwstage converges to it per rule 10.
Fix: new `flatslicevariantidx` helper in cgenutil.ww keyed on
N_TSLICE shape walking pat.lhs against vt.lhs alongside the existing
name-keyed flatvariantidx; extend `taggedvariantindex` shape-fallback
with a `wantslice == ivisslice` axis alongside the existing str
axis; route N_TSLICE in cgenexpr.ww's cgtagvariantidx (is/as)
and cgmatch (case) through the helper. No edits to cgenmatch's
dispatch codegen (CMPQ/JNE/spill) — that's symptom, the bug is
in the variantindex lookup.
Surfaced the 7th corpus-coverage-blind unmask of session 5 (sister
shape to STATUS-4 #11 / #14 / #21 wwstage UNDER family). Latent
within lib/bytes (a6abac2) since landing today; 967_bytes_run's
cstage-only `ww run` driver kept it dormant.
Tests:
- 722_match_slice_variant pins cmp -s byte-id between stages
for the canonical (u8|[]u8), reverse-order ([]u8|u8), and
three-arm (u8|[]u8|str) shapes.
- 926_match_slice_variant_run runtime-pins 7 rows × 2 stages
(cstage + wwstage drivers): canonical, reverse-order, and
other scalar-vs-slice-of-same-primitive matrices (i8|[]i8,
i32|[]i32, u64|[]u64, rune|[]rune), three-arm with str.
Verifies both arms reachable and payload survives.
Filed follow-ups (latent, NOT in this commit's scope):
- flatslicevariantidx falls back to first slice slot when no
element-name matches; `([]u8 | []i32)` would mis-route. No
in-tree consumer.
- 926 missing nested ((u8|[]u8) | i32) row per rob's spec.
- 3-arm 32B tagged sequential-push payload corruption (both
stages, asm byte-id passes, only 9xx runtime catches).
- Chained inline pick() over 32B 3-arm slot (both stages,
bind-to-let workaround documented at 926 row).
93/93 ok. 995_self_rebuild stays green (ww2==ww3==ww4 byte-id).
This commit is contained in:
@@ -2414,10 +2414,16 @@ fn taggedvariantindex(c: *cgen, tagged: *node, rhs: *node) i32 = {
|
||||
let r: i32 = flatvariantidx(c, tagged, wantname);
|
||||
if (r >= 0) { return r; };
|
||||
};
|
||||
// Fallback: by str-shape (resolves aliases). Walks the
|
||||
// spread-flattened variant list so a `(...inner | str)` outer
|
||||
// agrees with the (i32 | str) inner's str position.
|
||||
// Shape fallback: classify rhs as (str, slice, scalar/other) and
|
||||
// pick the first variant of matching shape. Cstage's type_eq
|
||||
// distinguishes a `[]u8` arm from a `u8` arm at type-build; the
|
||||
// name-only flatvariantidx pass above can't see `[]T`, so without
|
||||
// the slice axis a (u8 | []u8) widen / match collapses every
|
||||
// non-str rhs onto the leading scalar variant (task #19). Walks
|
||||
// the spread-flattened list so a `(...inner | str)` outer agrees
|
||||
// with the inner's str / slice positions.
|
||||
let wantstr: bool = nodeisstr(c, rhs);
|
||||
let wantslice: bool = nodeisslice(c, rhs);
|
||||
let v: *node = tagged.list;
|
||||
let idx: i32 = 0;
|
||||
for (v != nil) {
|
||||
@@ -2432,11 +2438,9 @@ fn taggedvariantindex(c: *cgen, tagged: *node, rhs: *node) i32 = {
|
||||
if (inner.kind == nkind.N_TTAGGED) {
|
||||
let iv: *node = inner.list;
|
||||
for (iv != nil) {
|
||||
let ivisstr: bool = false;
|
||||
if (iv.kind == nkind.N_TNAME) {
|
||||
if (isstrtype(c, iv)) { ivisstr = true; };
|
||||
};
|
||||
if (ivisstr == wantstr) { return idx; };
|
||||
let ivisstr: bool = isstrtype(c, iv);
|
||||
let ivisslice: bool = isslicetype(c, iv);
|
||||
if (ivisstr == wantstr && ivisslice == wantslice) { return idx; };
|
||||
iv = iv.next;
|
||||
idx += 1;
|
||||
};
|
||||
@@ -2445,11 +2449,9 @@ fn taggedvariantindex(c: *cgen, tagged: *node, rhs: *node) i32 = {
|
||||
};
|
||||
};
|
||||
};
|
||||
let visstr: bool = false;
|
||||
if (v.kind == nkind.N_TNAME) {
|
||||
if (isstrtype(c, v)) { visstr = true; };
|
||||
};
|
||||
if (visstr == wantstr) { return idx; };
|
||||
let visstr: bool = isstrtype(c, v);
|
||||
let visslice: bool = isslicetype(c, v);
|
||||
if (visstr == wantstr && visslice == wantslice) { return idx; };
|
||||
v = v.next;
|
||||
idx += 1;
|
||||
};
|
||||
@@ -2503,6 +2505,76 @@ fn flatvariantidx(c: *cgen, tagged: *node, want: str) i32 = {
|
||||
return -1;
|
||||
};
|
||||
|
||||
// flatslicevariantidx — flat 0-based index of the first slice-shape
|
||||
// variant in `tagged` (`...inner` spread expanded). When `elem` is an
|
||||
// N_TNAME, prefer a `[]<elem.str>` variant; falls back to the first
|
||||
// slice slot if no element match is found. Cstage walks resolved
|
||||
// Type pointers and dispatches via cg_tag_for_variant / type_eq;
|
||||
// wwstage's name-keyed flatvariantidx can't see a `[]u8` variant
|
||||
// (pat.str == ""), collapsing every (scalar | []T) match arm and
|
||||
// widen-to-tagged call onto tag 0. Task #19. Returns -1 when no
|
||||
// slice variant exists.
|
||||
fn flatslicevariantidx(c: *cgen, tagged: *node, elem: *node) i32 = {
|
||||
if (tagged == nil) { return -1; };
|
||||
if (tagged.kind != nkind.N_TTAGGED) { return -1; };
|
||||
let elemname: str;
|
||||
elemname.ptr = nil; elemname.len = 0;
|
||||
if (elem != nil) {
|
||||
if (elem.kind == nkind.N_TNAME) { elemname = elem.str; };
|
||||
};
|
||||
let fallback: i32 = -1;
|
||||
let v: *node = tagged.list;
|
||||
let idx: i32 = 0;
|
||||
for (v != nil) {
|
||||
let isspread: bool = (v.op == tkind.TK_ELLIPSIS);
|
||||
if (isspread) {
|
||||
let inner: *node = v;
|
||||
if (inner.kind == nkind.N_TNAME) {
|
||||
let a: *node = aliaslookup(c, inner.str);
|
||||
if (a != nil) { inner = a; };
|
||||
};
|
||||
if (inner != nil) {
|
||||
if (inner.kind == nkind.N_TTAGGED) {
|
||||
let iv: *node = inner.list;
|
||||
for (iv != nil) {
|
||||
if (isslicetype(c, iv)) {
|
||||
if (fallback < 0) { fallback = idx; };
|
||||
if (elemname.len > 0) {
|
||||
if (iv.kind == nkind.N_TSLICE) {
|
||||
if (iv.lhs != nil) {
|
||||
if (iv.lhs.kind == nkind.N_TNAME) {
|
||||
if (variantnamematch(iv.lhs.str, elemname)) { return idx; };
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
iv = iv.next;
|
||||
idx += 1;
|
||||
};
|
||||
v = v.next;
|
||||
continue;
|
||||
};
|
||||
};
|
||||
};
|
||||
if (isslicetype(c, v)) {
|
||||
if (fallback < 0) { fallback = idx; };
|
||||
if (elemname.len > 0) {
|
||||
if (v.kind == nkind.N_TSLICE) {
|
||||
if (v.lhs != nil) {
|
||||
if (v.lhs.kind == nkind.N_TNAME) {
|
||||
if (variantnamematch(v.lhs.str, elemname)) { return idx; };
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
};
|
||||
v = v.next;
|
||||
idx += 1;
|
||||
};
|
||||
return fallback;
|
||||
};
|
||||
|
||||
// cgwidentagremap — when widening from one tagged union to a wider one,
|
||||
// rewrite the source's variant tag at slot_off+0 to use the destination's
|
||||
// variant indices. No-op when src and dst index orders coincide.
|
||||
|
||||
Reference in New Issue
Block a user