wcc/cgen: route same-type tagged-cast return through the widener (wwstage align to cstage)

A same-type tagged cast return (`return x: u`, u == fnret) emitted tag=0
(a 2nd-variant value returned the wrong payload). Route it through the
widener (needswiden when N_CAST && ru1==fu1, beside S1's ru1!=fu1) which
peels the identity cast internally -> correct; same-type non-cast stays on
the passthrough. Byte-id-neutral. test/wcc/837.
This commit is contained in:
2026-06-09 19:07:57 +09:00
parent 08ccb734b2
commit 90452a8364
5 changed files with 251 additions and 0 deletions

View File

@@ -0,0 +1,190 @@
/*
* 837_idcast_tagged_return — S3 / #35: a SAME-TYPE tagged CAST in a RETURN
* (`fn f() u = { ...; return v: u; }` where v: u — an identity cast).
* ken2 spec .ai/ken2-family-spec.md (S3 section). Category A, CORRECTNESS
* (widen-route), proven byte-id with cstage.
*
* ww's cgreturn keyed the tagged passthrough on rhs.kind; an N_CAST was
* uncovered, so the same-type cast fell to the scalar-shuffle fast-path and
* synthesised tag 0 — SILENT on any non-variant-0 value (a bool boxed as the
* int variant: ran the int arm -> 70, want 11). cstage routes the N_CAST
* through the scratch-widener (the N_CAST defeats its srcreg passthrough),
* which peels the identity cast internally (cg_widen_tagged_store /
* cg_tagged_castpeel). The fix adds the same-type N_CAST guard to cgreturn's
* needswiden block so ww takes the widen route too — NOT a top-level peel,
* which would reroute a call/dot source to passthrough and break byte-id.
*
* row | shape | want
* -------------+----------------------------------------+------
* S3_idcast | return x: u (x: u, 2nd-variant bool) | 11
* S3_nocast | return x (same value, no cast) | 11
* S3_intcast | return x: u (x: u, variant-0 int=7) | 7
*
* The 2nd-variant (bool) value is mandatory: a variant-0 (int) value boxes to
* tag 0 coincidentally right (the S1 trap). S3_nocast pins the no-cast route
* unchanged; S3_intcast pins the cast route on a variant-0 value.
*
* BYTE-ID: NEUTRAL. The newly-covered shape (same-type tagged CAST in return)
* is non-bootstrap — had the bootstrap contained it the byte-id gate would
* already be RED (pre-fix ww emitted tag-0 scalar-shuffle, cstage the
* scratch-widen). 990-997 untouched.
*/
#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; };
static const struct row rows[] = {
/* S3 — same-type tagged CAST in return, 2nd-variant (bool) value.
* Pre-fix ww ran the int arm (70); now the widen route boxes tag 1. */
{ "S3_idcast",
"package main;\n"
"type u = (int | bool | str);\n"
"fn f() u = { let x: u = true; return x: u; };\n"
"export fn main() i32 = {\n"
"\tmatch (f()) {\n"
"\tcase let n: int => return 70;\n"
"\tcase let b: bool => return 11;\n"
"\tcase let s: str => return 22;\n"
"\t};\n"
"\treturn 99;\n"
"};\n",
11 },
/* S3 — no-cast control: identical value, plain return. Pins the
* forwardtagged/passthrough route unchanged. */
{ "S3_nocast",
"package main;\n"
"type u = (int | bool | str);\n"
"fn f() u = { let x: u = true; return x; };\n"
"export fn main() i32 = {\n"
"\tmatch (f()) {\n"
"\tcase let n: int => return 70;\n"
"\tcase let b: bool => return 11;\n"
"\tcase let s: str => return 22;\n"
"\t};\n"
"\treturn 99;\n"
"};\n",
11 },
/* S3 — cast route on a variant-0 (int) value: the widen route must
* box tag 0 and carry the payload (7), not just coincidentally land
* on tag 0. */
{ "S3_intcast",
"package main;\n"
"type u = (int | bool | str);\n"
"fn f() u = { let x: u = 7; return x: u; };\n"
"export fn main() i32 = {\n"
"\tmatch (f()) {\n"
"\tcase let n: int => return n: i32;\n"
"\tcase let b: bool => return 11;\n"
"\tcase let s: str => return 22;\n"
"\t};\n"
"\treturn 99;\n"
"};\n",
7 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char src[64], tmpdir[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/idc_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/idc_%d_d_%d", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
fputs(r->src, f);
fclose(f);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
unlink(src); rmdir(tmpdir);
return -1;
}
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 = runwait(outbin);
unlink(src); unlink(outbin); rmdir(tmpdir);
return got;
}
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];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char wdrv[1024];
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *path; int gated_on_existence; }
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_on_existence
&& access(drivers[d].path, X_OK) != 0) {
fprintf(stderr, "idcast_tagged_return: skip %s (no %s)\n",
drivers[d].name, drivers[d].path);
continue;
}
for (int i = 0; i < n; i++) {
int got = run_driver(drivers[d].path, &rows[i], i);
total++;
if (got != rows[i].want) {
fprintf(stderr,
"idcast_tagged_return[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr,
"idcast_tagged_return: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("idcast_tagged_return: %d/%d ok\n", total, total);
return 0;
}