Files
ww/test/wcc/722_match_slice_variant.c
Hojun-Cho 53c9e46c21 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).
2026-05-18 01:51:57 +09:00

182 lines
5.1 KiB
C

/*
* 722_match_slice_variant — Class A asm-presence + byte-id sentinel
* for task #19. Pins that the variant tag chosen for a `[]T` arm in a
* `(scalar | []T)` tagged union matches between cstage and wwstage at
* BOTH dispatch (cgmatch's `CMPQ $K, AX`) and call-site (the
* `MOVQ $K, AX` widening that materialises the tag for the slice
* payload).
*
* Pre-fix wwstage's name-keyed flatvariantidx / str-shape fallback
* landed on tag 0 for any `[]T` lookup (pat.str == "" because the
* variant node is N_TSLICE, not N_TNAME). Both the case arm and the
* call-site emitted `$0` — internally consistent inside wwstage, but
* the cstage-vs-wwstage cmp -s diverges from cstage's correct `$1`.
* Class A: silent until wwstage path engaged. 967_bytes_run was
* compiled through cstage only and never tripped; lib/strings landing
* pulls bytes through wwstage and surfaces the dispatch miscompile
* via 995_self_rebuild.
*
* Asserts cmp -s byte-id on the canonical (u8 | []u8) probe plus
* other (scalar | []scalar) shapes from rob's matrix.
*/
#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;
}
struct row {
const char *label;
const char *src;
};
static const struct row rows[] = {
/* The canonical (u8 | []u8) repro from bytes.index. */
{ "u8_slice_u8",
"fn pick(n: (u8 | []u8)) i32 = {\n"
" match (n) {\n"
" case let c: u8 => return 1;\n"
" case let s: []u8 => return 2;\n"
" };\n"
" return 0;\n"
"};\n"
"export fn main() i32 = {\n"
" let r1: i32 = pick(7u8);\n"
" let buf: [2]u8;\n"
" let r2: i32 = pick(buf[0:2]);\n"
" if (r1 != 1) { return 11; };\n"
" if (r2 != 2) { return 12; };\n"
" return 0;\n"
"};\n" },
/* Reverse-order shape (slice arm first): the same shape-fallback
* that lost (u8 | []u8) must also resolve ([]u8 | u8) without
* accidentally inverting via the str-shape collapse. */
{ "slice_u8_then_u8",
"fn pick(n: ([]u8 | u8)) i32 = {\n"
" match (n) {\n"
" case let s: []u8 => return 1;\n"
" case let c: u8 => return 2;\n"
" };\n"
" return 0;\n"
"};\n"
"export fn main() i32 = {\n"
" let buf: [2]u8;\n"
" let r1: i32 = pick(buf[0:2]);\n"
" let r2: i32 = pick(7u8);\n"
" if (r1 != 1) { return 11; };\n"
" if (r2 != 2) { return 12; };\n"
" return 0;\n"
"};\n" },
/* Three-arm (u8 | []u8 | str): proves the slice axis composes with
* the existing str axis (str arm at idx 2 stays at idx 2). */
{ "u8_slice_u8_str",
"fn pick(n: (u8 | []u8 | str)) i32 = {\n"
" match (n) {\n"
" case let c: u8 => return 1;\n"
" case let s: []u8 => return 2;\n"
" case let t: str => return 3;\n"
" };\n"
" return 0;\n"
"};\n"
"export fn main() i32 = {\n"
" let buf: [2]u8;\n"
" if (pick(7u8) != 1) { return 11; };\n"
" if (pick(buf[0:2]) != 2) { return 12; };\n"
" if (pick(\"hi\") != 3) { return 13; };\n"
" return 0;\n"
"};\n" },
};
static int
emit_s(const char *w6c, const struct row *r, int i, char *out_s, size_t cap)
{
char src[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/msv_asm_%d_%d.ww", getpid(), i);
snprintf(out_s, cap, "/tmp/msv_asm_%d_%d_%s.s",
getpid(), i, w6c[strlen(w6c) - 1] == 'w' ? "ww" : "c");
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, out_s, src);
int rc = runwait(cmd);
unlink(src);
return rc;
}
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 w6c[640], w6c_ww[640];
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
int have_ww = (access(w6c_ww, X_OK) == 0);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
char cs_path[128], ws_path[128];
if (emit_s(w6c, &rows[i], i, cs_path, sizeof cs_path) != 0) {
fprintf(stderr,
"match_slice_variant[cstage][%s]: w6c failed\n",
rows[i].label);
fail++; total++; continue;
}
if (!have_ww) { unlink(cs_path); continue; }
if (emit_s(w6c_ww, &rows[i], i, ws_path, sizeof ws_path) != 0) {
fprintf(stderr,
"match_slice_variant[wwstage][%s]: w6c_ww failed\n",
rows[i].label);
fail++; total++;
unlink(cs_path);
continue;
}
total++;
char cmd[512];
snprintf(cmd, sizeof cmd, "cmp -s %s %s", cs_path, ws_path);
if (runwait(cmd) != 0) {
fprintf(stderr,
"match_slice_variant[%s]: cstage vs wwstage asm differs\n",
rows[i].label);
fail++;
}
unlink(cs_path); unlink(ws_path);
}
if (fail) {
fprintf(stderr,
"match_slice_variant: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("match_slice_variant: %d/%d ok\n", total, total);
return 0;
}