/* * 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 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 tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024]; snprintf(tmpdir, sizeof tmpdir, "/tmp/iag_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); snprintf(src, sizeof src, "%s/iag_%d_%d.ww", tmpdir, getpid(), i); snprintf(outbin, sizeof outbin, "%s/iag_%d_%d", tmpdir, getpid(), i); snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir); FILE *f = fopen(src, "wb"); if (!f) { runwait(rmcmd); return -1; } fputs(r->src, f); fclose(f); snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null", driver, outbin, src); if (runwait(cmd) != 0) { fprintf(stderr, "row[%s]: build via %s failed\n", r->label, driver); runwait(rmcmd); return -1; } int got = runwait(outbin); runwait(rmcmd); 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; }