From 46e8354056de8e6dedc32920883820962156c448 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Tue, 9 Jun 2026 02:59:52 +0900 Subject: [PATCH] wcc/check: #6 stamp inferred-type array global so wwstage compiles it (was asserttyped exit 1) An inferred-type array global -- let xs = [1,2,3]; -- hard-failed wwstage with 'asserttyped: int' exit 1, while cstage compiled+ran it. The array-twin of the inferred-global family (#135 inferred-float, #150-B inferred-Sym-repoint). exprtype's N_ARRLIT arm synthesizes the array type for an unannotated literal but left two synthesized child nodes unstamped: the count literal (asserttyped trips on it -> the loud failure) and the element TNAME (cgen then drops a non-scalar element's header load -> the silent miscompile that merely accepting on alone would introduce: an inferred str-array's xs[1].len read 24 not 3). Both are now stamped at the synthesis site: cn.type_ (asserttyped facet) and elt.type_ (cgen facet). Inferred int / u8 / str / struct / multi-dim array globals + locals + args now compile byte-identically to the explicit-typed form (== cstage). cstage unchanged (w6c md5 unchanged); selfhost has no inferred array globals so byte-id 990-997 8/8, no lib pin flips. test/wcc/833. --- Makefile | 7 + selfhost/cmd/w6c/main.combined.ww | 20 +++ selfhost/cmd/wcc/check.ww | 20 +++ selfhost/cmd/wwdump/main.combined.ww | 20 +++ test/wcc/833_inferred_array_global.c | 243 +++++++++++++++++++++++++++ 5 files changed, 310 insertions(+) create mode 100644 test/wcc/833_inferred_array_global.c diff --git a/Makefile b/Makefile index 5410972b..724739a3 100644 --- a/Makefile +++ b/Makefile @@ -258,6 +258,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_str_eq \ $(BIN)/test_overlong_arrlit \ $(BIN)/test_tuple_elem_overlong \ + $(BIN)/test_inferred_array_global \ $(BIN)/test_slice_str_global_arg \ $(BIN)/test_slice_str_global_zero \ $(BIN)/test_slice_literal_global \ @@ -722,6 +723,12 @@ $(BIN)/test_tuple_elem_overlong: test/wcc/832_tuple_elem_overlong.c $(BIN)/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 \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_slice_str_global_arg: test/wcc/829_slice_str_global_arg.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 1fdfb66d..26d91f79 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -13651,9 +13651,29 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = { // check.c:1859 empty-fallback ty_int (#103). Was "i32". if (elt == nil) { elt = mktname(c, "int"); }; let arr: *node = newnode(nkind.N_TARRAY, "", 0, 0); + // #6(niche): stamp the SYNTHESIZED element TNAME so the inferred + // array's cgen is IDENTICAL to an explicit [N]T's (whose element is + // resolvewalk-stamped). For a scalar element (int/u8) cgen's + // fallback reads correctly with a nil elt.type_ (byte-id either + // way), but a multi-word element (str, 24B) needs the resolved + // element tinfo to emit the 3-word header element load — without it + // cgen drops to the 8B-scalar path and `xs[i].len` reads the slot + // stride, not the length (silent cs!=ww). Idempotent: elt may be a + // real exprtype result whose type_ is already set. + if (elt.type_ == nil) { elt.type_ = tinfofornode(c, elt): *void; }; arr.lhs = elt; let cn: *node = newnode(nkind.N_INTLIT, "", 0, 0); cn.uval = count; + // #6(niche): stamp the SYNTHESIZED count literal. When this arr is + // planted on an inferred array global's n.lhs (the array twin of + // #135/#150-B), asserttyped walks arr.rhs (this N_INTLIT, an expr + // node) and trips `asserttyped: int` — an explicit [N]T's count is + // resolvewalk-stamped, the synthesized inferred one wasn't. The + // element TNAME (arr.lhs) needs no stamp: N_TNAME is not an + // asserttyped expr kind. tinfofornode only resolves type-kind + // nodes, so type the count off a fresh `int` TNAME; cgen reads + // arr.rhs.uval, so this stamp is byte-id-inert. + cn.type_ = tinfofornode(c, mktname(c, "int")): *void; arr.rhs = cn; e.type_ = tinfofornode(c, arr): *void; // #103: stamp the synthesized array NODE too. checkletassign diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index 8e37cbeb..05eb7299 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -3370,9 +3370,29 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = { // check.c:1859 empty-fallback ty_int (#103). Was "i32". if (elt == nil) { elt = mktname(c, "int"); }; let arr: *node = newnode(nkind.N_TARRAY, "", 0, 0); + // #6(niche): stamp the SYNTHESIZED element TNAME so the inferred + // array's cgen is IDENTICAL to an explicit [N]T's (whose element is + // resolvewalk-stamped). For a scalar element (int/u8) cgen's + // fallback reads correctly with a nil elt.type_ (byte-id either + // way), but a multi-word element (str, 24B) needs the resolved + // element tinfo to emit the 3-word header element load — without it + // cgen drops to the 8B-scalar path and `xs[i].len` reads the slot + // stride, not the length (silent cs!=ww). Idempotent: elt may be a + // real exprtype result whose type_ is already set. + if (elt.type_ == nil) { elt.type_ = tinfofornode(c, elt): *void; }; arr.lhs = elt; let cn: *node = newnode(nkind.N_INTLIT, "", 0, 0); cn.uval = count; + // #6(niche): stamp the SYNTHESIZED count literal. When this arr is + // planted on an inferred array global's n.lhs (the array twin of + // #135/#150-B), asserttyped walks arr.rhs (this N_INTLIT, an expr + // node) and trips `asserttyped: int` — an explicit [N]T's count is + // resolvewalk-stamped, the synthesized inferred one wasn't. The + // element TNAME (arr.lhs) needs no stamp: N_TNAME is not an + // asserttyped expr kind. tinfofornode only resolves type-kind + // nodes, so type the count off a fresh `int` TNAME; cgen reads + // arr.rhs.uval, so this stamp is byte-id-inert. + cn.type_ = tinfofornode(c, mktname(c, "int")): *void; arr.rhs = cn; e.type_ = tinfofornode(c, arr): *void; // #103: stamp the synthesized array NODE too. checkletassign diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 36eba55d..67717181 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -13651,9 +13651,29 @@ fn exprtype(c: *checker, e: *node, hint: *node) *node = { // check.c:1859 empty-fallback ty_int (#103). Was "i32". if (elt == nil) { elt = mktname(c, "int"); }; let arr: *node = newnode(nkind.N_TARRAY, "", 0, 0); + // #6(niche): stamp the SYNTHESIZED element TNAME so the inferred + // array's cgen is IDENTICAL to an explicit [N]T's (whose element is + // resolvewalk-stamped). For a scalar element (int/u8) cgen's + // fallback reads correctly with a nil elt.type_ (byte-id either + // way), but a multi-word element (str, 24B) needs the resolved + // element tinfo to emit the 3-word header element load — without it + // cgen drops to the 8B-scalar path and `xs[i].len` reads the slot + // stride, not the length (silent cs!=ww). Idempotent: elt may be a + // real exprtype result whose type_ is already set. + if (elt.type_ == nil) { elt.type_ = tinfofornode(c, elt): *void; }; arr.lhs = elt; let cn: *node = newnode(nkind.N_INTLIT, "", 0, 0); cn.uval = count; + // #6(niche): stamp the SYNTHESIZED count literal. When this arr is + // planted on an inferred array global's n.lhs (the array twin of + // #135/#150-B), asserttyped walks arr.rhs (this N_INTLIT, an expr + // node) and trips `asserttyped: int` — an explicit [N]T's count is + // resolvewalk-stamped, the synthesized inferred one wasn't. The + // element TNAME (arr.lhs) needs no stamp: N_TNAME is not an + // asserttyped expr kind. tinfofornode only resolves type-kind + // nodes, so type the count off a fresh `int` TNAME; cgen reads + // arr.rhs.uval, so this stamp is byte-id-inert. + cn.type_ = tinfofornode(c, mktname(c, "int")): *void; arr.rhs = cn; e.type_ = tinfofornode(c, arr): *void; // #103: stamp the synthesized array NODE too. checkletassign diff --git a/test/wcc/833_inferred_array_global.c b/test/wcc/833_inferred_array_global.c new file mode 100644 index 00000000..add9fb96 --- /dev/null +++ b/test/wcc/833_inferred_array_global.c @@ -0,0 +1,243 @@ +/* + * 833_inferred_array_global — an inferred-type (annotation-less) module-global + * `let xs = [10, 20, 30];` whose init is an ARRAY literal must compile and run + * exactly as the explicit-typed `let xs: [3]int = [10, 20, 30]` does. The + * array twin of #135/#150-B (inferred float / inferred scalar global). + * WWSTAGE-ONLY bug (cstage already correct post-#150-B). + * + * Root (selfhost/cmd/wcc/check.ww exprtype N_ARRLIT, ~3294): for an inferred + * array let, exprtype synthesizes the array type — an N_TARRAY whose lhs is + * the element TNAME and whose rhs is a fresh N_INTLIT count node — and stamps + * e.type_ and arr.type_, but NEVER the count node's type_. checkletassign + * plants this synthesized arr on the inferred let's n.lhs; the pass-3 + * asserttyped walker then descends arr.rhs (the count N_INTLIT, an expr kind) + * with type_ == nil and trips `asserttyped: int` — a HARD loud build failure. + * wwstage could not compile an inferred array global at all. An explicit + * `[3]int` works because resolvewalk stamps its parser-built count node. + * + * The fix (check.ww, checker-only): stamp the synthesized count node's type_. + * The element TNAME needs no stamp (N_TNAME is not an asserttyped expr kind). + * cgen reads arr.rhs.uval, so the stamp is byte-id-inert: once the checker + * accepts the inferred array identically to an explicit one, the existing + * [N]T-global cgen path emits the IDENTICAL `.s`. cstage is UNTOUCHED. + * + * row | shape | want + * -----------+---------------------------------------------+----- + * int_idx | let xs = [10,20,30]; xs[1] (the bug) | 20 + * int_len | let xs = [10,20,30]; xs.len | 3 + * u8_elem | let bs = [1u8,2u8,3u8]; bs[2] (narrow elem) | 3 + * ctrl_expl | let xs: [3]int = [10,20,30]; xs[1] (explicit)| 20 + * + * Pre-fix the inferred rows ran wwstage=LOUD (asserttyped exit 1) and + * cstage=correct (cs≠ww); post-fix all four run in both stages and the + * inferred rows' `.s` is byte-identical between stages (and to the explicit + * form). ctrl_expl guards the explicit-[N]T path didn't regress and is the + * byte-id reference the inferred rows must match. + */ +#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[] = { + /* int_idx — the bug: inferred array global indexed, cast to int. + * Pre-fix wwstage tripped asserttyped (exit 1); cstage 20. */ + { "int_idx", + "package main;\n" + "let xs = [10, 20, 30];\n" + "export fn main() i32 = {\n" + "\treturn xs[1]: i32;\n" + "};\n", + 20 }, + + /* int_len — inferred array global .len pseudo-field read. */ + { "int_len", + "package main;\n" + "let xs = [10, 20, 30];\n" + "export fn main() i32 = {\n" + "\treturn xs.len: i32;\n" + "};\n", + 3 }, + + /* u8_elem — narrow (u8-default) element; the synthesized element + * type defaults to u8 here, exercising a non-int stride. */ + { "u8_elem", + "package main;\n" + "let bs = [1u8, 2u8, 3u8];\n" + "export fn main() i32 = {\n" + "\treturn bs[2]: i32;\n" + "};\n", + 3 }, + + /* ctrl_expl — explicit [3]int annotation; already worked. The + * byte-id reference the inferred int rows must match. No-regress. */ + { "ctrl_expl", + "package main;\n" + "let xs: [3]int = [10, 20, 30];\n" + "export fn main() i32 = {\n" + "\treturn xs[1]: i32;\n" + "};\n", + 20 }, +}; + +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/iag_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/iag_%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; +} + +/* asm_byte_identical — w6c vs w6c_ww .s for the same source must match. */ +static int +asm_byte_identical(const char *bin, const struct row *r, int i) +{ + char src[64], cs[64], ws[64], cmd[1024]; + snprintf(src, sizeof src, "/tmp/iag_asm_%d_%d.ww", getpid(), i); + snprintf(cs, sizeof cs, "/tmp/iag_asm_%d_%d_c.s", getpid(), i); + snprintf(ws, sizeof ws, "/tmp/iag_asm_%d_%d_w.s", getpid(), i); + + FILE *f = fopen(src, "wb"); + if (!f) return -1; + fputs(r->src, f); + fclose(f); + + snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c errored\n", r->label); + unlink(src); + return -1; + } + snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null", + bin, ws, src); + if (runwait(cmd) != 0) { + fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label); + unlink(src); unlink(cs); + return -1; + } + + FILE *fc = fopen(cs, "rb"); + FILE *fw = fopen(ws, "rb"); + int rc = 0; + if (!fc || !fw) { + rc = -1; + } else { + for (;;) { + int a = fgetc(fc); + int b = fgetc(fw); + if (a != b) { rc = -1; break; } + if (a == EOF) break; + } + } + if (fc) fclose(fc); + if (fw) fclose(fw); + if (rc != 0) + fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n", + r->label); + unlink(src); unlink(cs); unlink(ws); + return rc; +} + +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, "inferred_array_global: 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, + "inferred_array_global[%s][%s]: exit=%d want=%d\n", + drivers[d].name, rows[i].label, + got, rows[i].want); + fail++; + } + } + } + + if (access(wdrv, X_OK) == 0) { + for (int i = 0; i < n; i++) { + total++; + if (asm_byte_identical(bin, &rows[i], i) != 0) + fail++; + } + } + + if (fail) { + fprintf(stderr, + "inferred_array_global: %d/%d fixtures failed\n", fail, total); + return 1; + } + printf("inferred_array_global: %d/%d ok\n", total, total); + return 0; +}