diff --git a/Makefile b/Makefile index 65040e7a..98a00b0d 100644 --- a/Makefile +++ b/Makefile @@ -261,6 +261,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_tagged_arr_variant \ $(BIN)/test_nested_union_int_box \ $(BIN)/test_tagged_assignable_cluster \ + $(BIN)/test_idcast_tagged_return \ $(BIN)/test_inferred_array_global \ $(BIN)/test_slice_str_global_arg \ $(BIN)/test_slice_str_global_zero \ @@ -744,6 +745,12 @@ $(BIN)/test_tagged_assignable_cluster: test/wcc/836_tagged_assignable_cluster.c $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_idcast_tagged_return: test/wcc/837_idcast_tagged_return.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 f83275d4..3d1f86cd 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -35057,6 +35057,24 @@ fn cgreturn(c: *cgen, n: *node) void = { if (ru1.kind == tykind.TY_TAGGED && ru1 != fu1) { needswiden = true; }; + // S3/#35: a SAME-TYPE tagged CAST (ru1 == fu1, + // rhs an N_CAST) routes through the widener too. + // cstage keeps the N_CAST out of the srcreg + // passthrough (kind != N_CALL/INDEX/DOT) and the + // widen branch peels the identity cast internally + // (cg_widen_tagged_store cg_tagged_castpeel, + // cgen.c:2553), so cs emits the scratch-widen (NOT + // passthrough) for `return v: u` over an ident/ + // call/dot source. ww's forwardtagged keys on + // rhs.kind (N_CAST uncovered), so the same-type + // cast fell to the scalar shuffle and synthesized + // tag 0 (silent). The N_CAST guard mirrors cstage's + // "N_CAST defeats srcreg"; peeling here instead + // would reroute a call/dot source to passthrough + // and break byte-id. + if (rhs.kind == nkind.N_CAST && ru1.kind == tykind.TY_TAGGED && ru1 == fu1) { + needswiden = true; + }; }; }; }; diff --git a/selfhost/cmd/wcc/cgenstmt.ww b/selfhost/cmd/wcc/cgenstmt.ww index 2d9f7428..52d5d043 100644 --- a/selfhost/cmd/wcc/cgenstmt.ww +++ b/selfhost/cmd/wcc/cgenstmt.ww @@ -1059,6 +1059,24 @@ fn cgreturn(c: *cgen, n: *node) void = { if (ru1.kind == tykind.TY_TAGGED && ru1 != fu1) { needswiden = true; }; + // S3/#35: a SAME-TYPE tagged CAST (ru1 == fu1, + // rhs an N_CAST) routes through the widener too. + // cstage keeps the N_CAST out of the srcreg + // passthrough (kind != N_CALL/INDEX/DOT) and the + // widen branch peels the identity cast internally + // (cg_widen_tagged_store cg_tagged_castpeel, + // cgen.c:2553), so cs emits the scratch-widen (NOT + // passthrough) for `return v: u` over an ident/ + // call/dot source. ww's forwardtagged keys on + // rhs.kind (N_CAST uncovered), so the same-type + // cast fell to the scalar shuffle and synthesized + // tag 0 (silent). The N_CAST guard mirrors cstage's + // "N_CAST defeats srcreg"; peeling here instead + // would reroute a call/dot source to passthrough + // and break byte-id. + if (rhs.kind == nkind.N_CAST && ru1.kind == tykind.TY_TAGGED && ru1 == fu1) { + needswiden = true; + }; }; }; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index d5d928b6..2a93d993 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -35057,6 +35057,24 @@ fn cgreturn(c: *cgen, n: *node) void = { if (ru1.kind == tykind.TY_TAGGED && ru1 != fu1) { needswiden = true; }; + // S3/#35: a SAME-TYPE tagged CAST (ru1 == fu1, + // rhs an N_CAST) routes through the widener too. + // cstage keeps the N_CAST out of the srcreg + // passthrough (kind != N_CALL/INDEX/DOT) and the + // widen branch peels the identity cast internally + // (cg_widen_tagged_store cg_tagged_castpeel, + // cgen.c:2553), so cs emits the scratch-widen (NOT + // passthrough) for `return v: u` over an ident/ + // call/dot source. ww's forwardtagged keys on + // rhs.kind (N_CAST uncovered), so the same-type + // cast fell to the scalar shuffle and synthesized + // tag 0 (silent). The N_CAST guard mirrors cstage's + // "N_CAST defeats srcreg"; peeling here instead + // would reroute a call/dot source to passthrough + // and break byte-id. + if (rhs.kind == nkind.N_CAST && ru1.kind == tykind.TY_TAGGED && ru1 == fu1) { + needswiden = true; + }; }; }; }; diff --git a/test/wcc/837_idcast_tagged_return.c b/test/wcc/837_idcast_tagged_return.c new file mode 100644 index 00000000..cdb806cd --- /dev/null +++ b/test/wcc/837_idcast_tagged_return.c @@ -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 +#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 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; +}