wcc/ww: widen of a module-global tagged ident copies the whole box

Widening a global tagged union into a wider tagged slot copied
nothing of the box; treat the global ident as a tagged source and
copy the full box from g(SB) through the nested arm. Both-wrong pair:
cstage spills frame garbage as the box (filed task #44, residual
non-deterministic so the rows assert divergence only). The non-nested
SUBSET-widen shape remains open as task #49 (site comment at the
fall-through). Review item #51.
This commit is contained in:
2026-06-12 09:21:16 +09:00
parent 32848194d7
commit e416885d96
5 changed files with 328 additions and 30 deletions

View File

@@ -0,0 +1,190 @@
/*
* 989_globtagwiden_run — F8-c8 (report-item #51, #263): widening a module-
* GLOBAL tagged-union ident into a wider union (`let x: (inner | str) = gi;`)
* must copy gi's whole box as the nested payload, not store its tag as the
* payload.
*
* THE BUG (cat-A silent miscompile, #263 BOTH-WRONG): cgwidentaggedstorebp's
* #218 nested-tagged-source arm is gated on rhstaggedident, which returns nil
* for a module-global tagged ident (local-only). So srctagged was false and a
* global tagged source fell to the scalar word0 widen arm — gi's TAG word
* landed as the box payload, the real value lost. cstage is ALSO wrong: it
* copies frame garbage (saved-BP / return-address), never gi(SB). Both wrong,
* divergent (#263); the cstage half is filed as task #44.
*
* THE WWSTAGE FIX (ww-runtime-correct): srctagged also recognises a global
* tagged ident, and the nested arm's N_IDENT copy grows a global branch —
* LEAQ gi(SB),SI via aggargsrcaddr, then copy the inner box (ssz bytes) into
* slot+8 with the outer tag at slot+0. Local sources keep the BP copy
* (byte-id). cstage stays wrong → cs≠ww residual.
*
* cstage's residual is FRAME GARBAGE (non-deterministic across stack
* layouts), so — unlike the deterministic #263 members — the cstage rows
* assert only that cstage DIVERGES (got != the runtime-correct value and the
* build succeeded), not a pinned value. The wwstage rows assert the exact
* runtime-correct value. (#44 closes the cs side → cstage will then match.)
* row | shape | ww | cs
* ----------+-----------------------------------------+----+--------
* nest_47 | gi:inner=47; (inner|str)=gi; match→v | 47 | != 47 (garbage)
* nest_99 | gi:inner=99; (inner|str)=gi; match→v | 99 | != 99 (garbage)
*/
#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_ww;
};
static const struct row rows[] = {
{ "nest_47",
"package main;\n"
"type inner = (i64 | bool);\n"
"let gi: inner = 47;\n"
"export fn main() int = {\n"
" let x: (inner | str) = gi;\n"
" match (x) {\n"
" case let n: inner => {\n"
" match (n) {\n"
" case let v: i64 => return v: int;\n"
" case bool => return 88;\n"
" };\n"
" };\n"
" case str => return 3;\n"
" };\n"
"};\n",
47 },
{ "nest_99",
"package main;\n"
"type inner = (i64 | bool);\n"
"let gi: inner = 99;\n"
"export fn main() int = {\n"
" let x: (inner | str) = gi;\n"
" match (x) {\n"
" case let n: inner => {\n"
" match (n) {\n"
" case let v: i64 => return v: int;\n"
" case bool => return 88;\n"
" };\n"
" };\n"
" case str => return 3;\n"
" };\n"
"};\n",
99 },
};
/* 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/gtw_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/gtw_%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, "globtagwiden_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
int is_ww = (d == 1);
for (int i = 0; i < n; i++) {
total++;
int got = run_build(drivers[d].drv, &rows[i], i);
if (is_ww) {
/* align-to-runtime-correct: exact value. */
if (got != rows[i].want_ww) {
fprintf(stderr, "globtagwiden_run[wwstage][%s]:"
" exit=%d want=%d\n", rows[i].label,
got, rows[i].want_ww);
fail++;
}
} else {
/* #263 residual: cstage built but diverges from the
* runtime-correct value (frame garbage, #44). */
if (got == -1 || got == rows[i].want_ww) {
fprintf(stderr, "globtagwiden_run[cstage][%s]:"
" exit=%d expected build-ok and !=%d "
"(#263 residual, task #44)\n",
rows[i].label, got, rows[i].want_ww);
fail++;
}
}
}
}
if (fail) {
fprintf(stderr, "globtagwiden_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("globtagwiden_run: %d/%d ok\n", total, total);
return 0;
}