wcc/ww: tagged-element classify keys on the stamp, not a base whitelist

cgindex's tagged-element classification whitelisted base node kinds;
call- and slice-based tagged elements fell off the list and dropped
the payload words (review finding #23). Classify by the stamped
element type. 989_taggedidx_run pins cs==ww (red 2/6 pre-fix).
This commit is contained in:
2026-06-12 00:17:55 +09:00
parent 0625af1309
commit 47b971330b
5 changed files with 256 additions and 36 deletions

View File

@@ -0,0 +1,197 @@
/*
* 989_taggedidx_run — F7-c6 (#23): a tagged-union element indexed off a
* CALL or SLICE base must load the full tag+payload cursor, both stages.
*
* THE BUG (cat-A silent miscompile, gate-blind): cgindex
* (selfhost/cmd/wcc/cgenexpr.ww) classifies an indexed element as tagged
* so it loads the (tag, val0, val1) slot. The non-ident-base arm gated
* that classify on a base-kind WHITELIST (N_DOT / N_INDEX / N_UN-deref),
* so a tagged element indexed off an N_CALL base (`f()[i]`) or N_SLICE
* base (`s[a:b][i]`) fell through to the scalar load — a lone
* `MOVQ (AX),AX` (tag word only), dropping the payload. cstage classifies
* for ANY base off the stamped element tinfo (`esubu->kind == TY_TAGGED`,
* cmd/w6c/cgen.c:8101), so it loaded the full cursor and ran correct
* (cs rc=50 / ww rc=40 in the review repro — the cat-A divergence).
* 990-997 stay green because the corpus never indexes a tagged element
* off a call/slice base; only a runtime row catches it. THE FIX: drop the
* base-kind whitelist — classify the tagged element off n.type_ for any
* non-N_IDENT base, aligning wwstage UP.
*
* Arrays of tagged are built by per-element store; box = (i64|bool).
*
* Rows (cstage `ww` + gated wwstage `ww_ww`; rule-10 + absolute value):
* row | shape | want
* -----------------+--------------------------------+------
* slice_tagged_idx | arr[1:][0] (N_SLICE base) | 40 [#23 bug]
* call_tagged_idx | getrows()[1] (N_CALL base) | 50 [#23 bug]
* ident_tagged_idx | arr[1] (N_IDENT base) | 40 (control: the
* | first arm already handled it — no regression)
*/
#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;
int want_exit;
};
static const struct row rows[] = {
/* (1) #23 — tagged element off a SLICE base `arr[1:][0]`. arr[1] is the
* i64 variant 40; the bug loaded only the tag word → garbage match. */
{ "slice_tagged_idx",
"package main;\n"
"type box = (i64 | bool);\n"
"export fn main() int = {\n"
" let arr: [3]box;\n"
" arr[0] = 10; arr[1] = 40; arr[2] = 70;\n"
" let v: box = arr[1:][0];\n"
" let r: i64 = 0;\n"
" match (v) {\n"
" case let x: i64 => { r = x; };\n"
" case let b: bool => { r = 99; };\n"
" };\n"
" return r: int;\n"
"};\n",
40 },
/* (2) #23 — tagged element off a CALL base `getrows()[1]`. The fn
* returns []box; element 1 is the i64 variant 50. */
{ "call_tagged_idx",
"package main;\n"
"type box = (i64 | bool);\n"
"fn getrows() []box = {\n"
" let arr: [3]box;\n"
" arr[0] = 10; arr[1] = 50; arr[2] = 70;\n"
" return arr[0:];\n"
"};\n"
"export fn main() int = {\n"
" let v: box = getrows()[1];\n"
" let r: i64 = 0;\n"
" match (v) {\n"
" case let x: i64 => { r = x; };\n"
" case let b: bool => { r = 99; };\n"
" };\n"
" return r: int;\n"
"};\n",
50 },
/* (3) control — tagged element off an N_IDENT base `arr[1]` (the first
* cgindex arm already handled it via istaggedtype on the base tnode):
* c6 must not regress it. arr[1] == 40. */
{ "ident_tagged_idx",
"package main;\n"
"type box = (i64 | bool);\n"
"export fn main() int = {\n"
" let arr: [3]box;\n"
" arr[0] = 10; arr[1] = 40; arr[2] = 70;\n"
" let v: box = arr[1];\n"
" let r: i64 = 0;\n"
" match (v) {\n"
" case let x: i64 => { r = x; };\n"
" case let b: bool => { r = 99; };\n"
" };\n"
" return r: int;\n"
"};\n",
40 },
};
/* run_build — build+run `src` via `driver`; returns the binary's exit
* code, or -1 on a build failure. */
static int
run_build(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/tagidx_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/tagidx_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -2;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
int brc = runwait(cmd);
const char *base = strrchr(src, '/');
base = base ? base + 1 : src;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = -1;
if (brc == 0) got = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return brc == 0 ? got : -1;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[1024];
if (bin[0] != '/') {
char cwd[1024];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[1024], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *drv; int gated; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) {
fprintf(stderr, "taggedidx_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
for (int i = 0; i < n; i++) {
total++;
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != rows[i].want_exit) {
fprintf(stderr, "taggedidx_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, rows[i].want_exit);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "taggedidx_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("taggedidx_run: %d/%d ok\n", total, total);
return 0;
}