/* * 823_inferred_global_let — an inferred-type (annotation-less) module-global * `let g = ;` must type-resolve at every downstream read. cstage's * module N_LET pass-2 (cmd/wcc/check.c) stamped `d->type` from the rhs but * never repointed the Sym that pass-1 installed with the annotation-less NULL * type — so every later N_IDENT read of the global resolved its type as nil: * `let n = 5; return n + 0` → `arithmetic on non-numeric type` * `let g = pt{..}; g.a + g.b` → `arithmetic on non-numeric type` * `let g = pt{..}; take(g)` → `argument type not assignable to pt` * * The fix (cmd/w6c/cgen.c sibling check.c) mirrors the #11 [_]-array Sym * repoint a few lines up: after `d->type = type_default(rt)`, repoint the * Sym at the stamped type. cstage-ONLY — wwstage's single-pass resolve * already stamps the inferred global (all three rows built clean pre-fix), * so this LIFTS cstage from loud-reject to accept, converging acceptance. * No asm moves for code that already built; the inferred form now reaches * the same cgen as the explicit-typed form, so cstage==wwstage byte-id. * * row | shape | want * --------------+---------------------------------------------+----- * scalar | let n = 5; return n + 0 (broadest case) | 5 * struct_field | let g = pt{a=7,b=9}; g.a + g.b | 16 * struct_arg | let g = pt{a=5,b=9}; take(g) (rides #150-A) | 14 * * All three are mutation-sane: pre-fix cstage loud-rejects each (the build * fails), so a regression that drops the repoint re-louds and fails the row. * The byte-id rows pin cstage==wwstage `.s` for the now-accepted inferred * source. */ #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[] = { /* scalar — the broadest case: an inferred scalar global read in * arithmetic. The read must flow through the repointed Sym's numeric * type; pre-fix cstage `arithmetic on non-numeric type` (a bare * `return n: i32` would NOT loud — the cast's target type masks the * nil Sym — so `+ 0` is the mutation-sane shape). */ { "scalar", "package main;\n" "let n = 5;\n" "export fn main() i32 = {\n" "\treturn (n + 0): i32;\n" "};\n", 5 }, /* struct_field — inferred struct global, both fields read. pre-fix * cstage `arithmetic on non-numeric type`. */ { "struct_field", "package main;\n" "type pt = struct { a: int, b: int };\n" "let g = pt { a = 7, b = 9 };\n" "export fn main() i32 = {\n" "\treturn (g.a + g.b): i32;\n" "};\n", 16 }, /* struct_arg — inferred struct global passed BY VALUE; rides the * #150 Commit A cgen marshalling. pre-fix cstage `argument type * not assignable to pt`. */ { "struct_arg", "package main;\n" "type pt = struct { a: int, b: int };\n" "let g = pt { a = 5, b = 9 };\n" "fn take(x: pt) int = {\n" "\treturn x.a + x.b;\n" "};\n" "export fn main() i32 = {\n" "\treturn take(g): i32;\n" "};\n", 14 }, }; 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/igl_%d_d_%d", getpid(), i); mkdir(tmpdir, 0755); snprintf(src, sizeof src, "%s/igl_%d_%d.ww", tmpdir, getpid(), i); snprintf(outbin, sizeof outbin, "%s/igl_%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/igl_asm_%d_%d.ww", getpid(), i); snprintf(cs, sizeof cs, "/tmp/igl_asm_%d_%d_c.s", getpid(), i); snprintf(ws, sizeof ws, "/tmp/igl_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_global_let: 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_global_let[%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_global_let: %d/%d fixtures failed\n", fail, total); return 1; } printf("inferred_global_let: %d/%d ok\n", total, total); return 0; }