wcc/cgen: loud-align static-global tagged cast-init (wwstage align to cstage)

A module-global tagged init via cast (`let g: u = true: u;`) silently
emitted tag=0; cstage louds. Route emittaggeddata through flatvariantidx
(the cstage cg_tag_for_variant twin) so an unsupported variant louds while
valid str/slice cast-inits still accept+run. test/wcc/838.
This commit is contained in:
2026-06-09 19:08:12 +09:00
parent 90452a8364
commit 5c28c90837
5 changed files with 277 additions and 3 deletions

View File

@@ -0,0 +1,222 @@
/*
* 838_global_tagged_castinit — E8: a module-level tagged global initialised
* by a same-type/subset CAST (`let g: u = true: u;`). ken2 spec
* .ai/ken2-family-spec.md (E8 section). Category A, SILENT-in-ww / cstage-
* LOUD, aligned DOWN to cstage's loud.
*
* ww's emittaggeddata selected the variant via taggedvariantindex, whose
* str/slice SHAPE fallback (cgenutil.ww) returns the first scalar variant
* (tag 0) when no variant typeeq's the source. For a CAST init the checker
* stamps the peeled literal's type as the union itself, so no variant matches
* and the fallback synthesised tag 0 in the DATA word — SILENT (a bool/int
* cast ran the int arm, payload mis-tagged). cstage's emit_tagged_data calls
* cg_tag_for_variant (NO shape fallback, cgen.c:15472) which returns -1 ->
* the caller loud-stops ("unsupported variant init"). The fix routes ww's
* emittaggeddata through flatvariantidx — the EXACT cg_tag_for_variant twin —
* so a cast-init that resolves to no variant louds, while every shape cstage
* accepts (bare literal, str-literal cast) still resolves via pass 1.
*
* neg row | shape | gate
* -------------+--------------------------------+------
* E8_boolcast | let g: u = true: u; | FAIL
* E8_intcast | let g: u = 7: u; | FAIL
*
* pos row | shape | want
* -------------+--------------------------------+------
* E8_barebool | let g: u = true; match bool | 11
* E8_bareint | let g: u = 7; match int -> n | 7
* E8_barestr | let g: u = "hi"; match str | 22
* E8_strcast | let g: u = "hi": u; (str keeps | 22
* | str type -> str variant) |
*
* The bare-literal forms are clean on both stages (byte-id with cstage); the
* str-literal CAST keeps its concrete str type and resolves to the str
* variant, so it must NOT be over-rejected (a naive "N_CAST onto tagged ->
* loud" predicate would wrongly reject it). u = (int | bool | str): int is
* variant 0, so bare-int landing on tag 0 is correct (and pins the payload).
*
* BYTE-ID: NEUTRAL. A new ww loud emits no asm; cstage already louds; the
* bootstrap cannot contain a shape cstage refuses. The accept-path tags are
* unchanged (flatvariantidx returns the same index as the old pass 1). 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 char *PROLOGUE =
"package main;\n"
"type u = (int | bool | str);\n";
static const char *EPILOGUE =
"export fn main() i32 = {\n"
"\tmatch (g) {\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";
static const struct row rows[] = {
{ "E8_barebool", "let g: u = true;\n", 11 },
{ "E8_bareint", "let g: u = 7;\n", 7 },
{ "E8_barestr", "let g: u = \"hi\";\n", 22 },
{ "E8_strcast", "let g: u = \"hi\": u;\n", 22 },
};
static const char *neg[] = {
"let g: u = true: u;\n",
"let g: u = 7: u;\n",
};
static void
write_src(const char *path, const char *mid)
{
FILE *f = fopen(path, "wb");
if (!f) return;
fputs(PROLOGUE, f);
fputs(mid, f);
fputs(EPILOGUE, f);
fclose(f);
}
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/gtc_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/gtc_%d_d_%d", getpid(), i);
write_src(src, r->src);
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;
}
static int
build_should_fail(const char *driver, const char *mid, int i)
{
char s[64], tmpdir[64], cmd[1024];
snprintf(s, sizeof s, "/tmp/gtcn_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/gtcn_%d_d_%d", getpid(), i);
write_src(s, mid);
mkdir(tmpdir, 0755);
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
tmpdir, driver, s);
int rc = runwait(cmd);
unlink(s);
const char *base = strrchr(s, '/');
base = base ? base + 1 : s;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
unlink(outbin);
rmdir(tmpdir);
return rc == 0 ? -1 : 0; /* build must NOT succeed */
}
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 nn = (int)(sizeof neg / sizeof neg[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, "global_tagged_castinit: 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,
"global_tagged_castinit[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
for (int i = 0; i < nn; i++) {
total++;
if (build_should_fail(drivers[d].path, neg[i],
100 + i) != 0) {
fprintf(stderr,
"global_tagged_castinit[%s][neg%d]: built ok, "
"expected a loud reject\n",
drivers[d].name, i);
fail++;
}
}
}
if (fail) {
fprintf(stderr,
"global_tagged_castinit: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("global_tagged_castinit: %d/%d ok\n", total, total);
return 0;
}