From 5c28c908371beafb7eecc02c3ef084d0aabe8410 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Tue, 9 Jun 2026 19:08:12 +0900 Subject: [PATCH] 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. --- Makefile | 7 + selfhost/cmd/w6c/main.combined.ww | 17 +- selfhost/cmd/wcc/cgen.ww | 17 +- selfhost/cmd/wwdump/main.combined.ww | 17 +- test/wcc/838_global_tagged_castinit.c | 222 ++++++++++++++++++++++++++ 5 files changed, 277 insertions(+), 3 deletions(-) create mode 100644 test/wcc/838_global_tagged_castinit.c diff --git a/Makefile b/Makefile index 98a00b0d..4d854a1e 100644 --- a/Makefile +++ b/Makefile @@ -262,6 +262,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_nested_union_int_box \ $(BIN)/test_tagged_assignable_cluster \ $(BIN)/test_idcast_tagged_return \ + $(BIN)/test_global_tagged_castinit \ $(BIN)/test_inferred_array_global \ $(BIN)/test_slice_str_global_arg \ $(BIN)/test_slice_str_global_zero \ @@ -751,6 +752,12 @@ $(BIN)/test_idcast_tagged_return: test/wcc/837_idcast_tagged_return.c $(BIN)/ww $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_global_tagged_castinit: test/wcc/838_global_tagged_castinit.c $(BIN)/ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_inferred_array_global: test/wcc/833_inferred_array_global.c $(BIN)/ww \ $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 3d1f86cd..3ef2c9c8 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -41336,7 +41336,22 @@ fn emittaggeddata(c: *cgen, name: str, module: str, tt: *node, rhs: *node, sz: i let r: *node = rhs; for (r != nil && r.kind == nkind.N_CAST) { r = r.lhs; }; if (r == nil) { return false; }; - let tag: i32 = taggedvariantindex(c, tt, r); + // E8/#35: select the variant via flatvariantidx — the EXACT twin of + // cstage emit_tagged_data's cg_tag_for_variant (cmd/w6c/cgen.c:15472). + // taggedvariantindex adds a str/slice SHAPE fallback (cgenutil.ww:3054) + // that cstage does NOT run at this site: a same-type/subset CAST init + // (`let g: u = true: u;`) stamps the peeled literal's type_ as the + // union itself, so flatvariantidx finds no variant and returns -1, + // matching cstage's loud (caller emits "unsupported variant init", + // cgen.ww:2818). The shape fallback instead picked the first scalar + // variant (tag 0) -> SILENT miscompile (ran the int arm on a bool + // value, the S1 coincidence trap). A bare-literal init (`= true` / `= + // 7`) keeps its concrete/untyped type and still resolves via + // flatvariantidx pass 1 (byte-id with cstage); a str-literal CAST + // (`"hi": u`) keeps its str type and resolves to the str variant too. + // Faithful tag-remap for a cast-init static global = the deferred + // #23/#40 nominal widen feature. + let tag: i32 = flatvariantidx(c, tt, r); if (tag < 0) { return false; }; let wide: bool = nodeisstr(c, r) || nodeisslice(c, r); let i: i32 = 0; diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index 09c65a19..df06986c 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -2473,7 +2473,22 @@ fn emittaggeddata(c: *cgen, name: str, module: str, tt: *node, rhs: *node, sz: i let r: *node = rhs; for (r != nil && r.kind == nkind.N_CAST) { r = r.lhs; }; if (r == nil) { return false; }; - let tag: i32 = taggedvariantindex(c, tt, r); + // E8/#35: select the variant via flatvariantidx — the EXACT twin of + // cstage emit_tagged_data's cg_tag_for_variant (cmd/w6c/cgen.c:15472). + // taggedvariantindex adds a str/slice SHAPE fallback (cgenutil.ww:3054) + // that cstage does NOT run at this site: a same-type/subset CAST init + // (`let g: u = true: u;`) stamps the peeled literal's type_ as the + // union itself, so flatvariantidx finds no variant and returns -1, + // matching cstage's loud (caller emits "unsupported variant init", + // cgen.ww:2818). The shape fallback instead picked the first scalar + // variant (tag 0) -> SILENT miscompile (ran the int arm on a bool + // value, the S1 coincidence trap). A bare-literal init (`= true` / `= + // 7`) keeps its concrete/untyped type and still resolves via + // flatvariantidx pass 1 (byte-id with cstage); a str-literal CAST + // (`"hi": u`) keeps its str type and resolves to the str variant too. + // Faithful tag-remap for a cast-init static global = the deferred + // #23/#40 nominal widen feature. + let tag: i32 = flatvariantidx(c, tt, r); if (tag < 0) { return false; }; let wide: bool = nodeisstr(c, r) || nodeisslice(c, r); let i: i32 = 0; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 2a93d993..546f9c8c 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -41336,7 +41336,22 @@ fn emittaggeddata(c: *cgen, name: str, module: str, tt: *node, rhs: *node, sz: i let r: *node = rhs; for (r != nil && r.kind == nkind.N_CAST) { r = r.lhs; }; if (r == nil) { return false; }; - let tag: i32 = taggedvariantindex(c, tt, r); + // E8/#35: select the variant via flatvariantidx — the EXACT twin of + // cstage emit_tagged_data's cg_tag_for_variant (cmd/w6c/cgen.c:15472). + // taggedvariantindex adds a str/slice SHAPE fallback (cgenutil.ww:3054) + // that cstage does NOT run at this site: a same-type/subset CAST init + // (`let g: u = true: u;`) stamps the peeled literal's type_ as the + // union itself, so flatvariantidx finds no variant and returns -1, + // matching cstage's loud (caller emits "unsupported variant init", + // cgen.ww:2818). The shape fallback instead picked the first scalar + // variant (tag 0) -> SILENT miscompile (ran the int arm on a bool + // value, the S1 coincidence trap). A bare-literal init (`= true` / `= + // 7`) keeps its concrete/untyped type and still resolves via + // flatvariantidx pass 1 (byte-id with cstage); a str-literal CAST + // (`"hi": u`) keeps its str type and resolves to the str variant too. + // Faithful tag-remap for a cast-init static global = the deferred + // #23/#40 nominal widen feature. + let tag: i32 = flatvariantidx(c, tt, r); if (tag < 0) { return false; }; let wide: bool = nodeisstr(c, r) || nodeisslice(c, r); let i: i32 = 0; diff --git a/test/wcc/838_global_tagged_castinit.c b/test/wcc/838_global_tagged_castinit.c new file mode 100644 index 00000000..2d6c0746 --- /dev/null +++ b/test/wcc/838_global_tagged_castinit.c @@ -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 +#include +#include +#include +#include +#include + +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; +}