/* * 989_nullableglobal_reject — #45 silent->loud bridge (ww-core task #15 * prereq). A module-level nullable `(*T | void)` GLOBAL has no storage * path in either stage: letemitsize / let_emit_size returned 0 for the * nullable TY_TAGGED, so let_collect skipped registration and emit_lets * skipped DATA. The three READ paths then miscompiled SILENTLY: * - match (g) : cgmatch's #87 arm is let_islet-gated; with no * registration it fell to localfind -> read 0(BP) = * saved BP (garbage tag). * - g is *T : cgtypetest's nullable arm emitted MOVQ name(SB),AX * for a symbol with no DATA -> w6l undefined-reference. * - g as *T : cgtypeassert, same undefined-reference. * cstage's #87 match arm was ALSO `!is_nullable`-gated, so BOTH stages * were wrong (a #263-class both-wrong gap, not an align-up). * * THE BRIDGE (this commit): die LOUD at the size/storage layer * (let_emit_size:1278 / letemitsize cgen.ww:1054) the instant a nullable * global is declared, so all three read paths hit one diagnostic instead * of a silent miscompile. The full storage + read-class arc (real DATA, * nil/void/address-of init, let-registration, the three SB-resolution * read arms) is deferred to task #15 — it is the CSP handle-singleton * prereq (`let c: *Chan | void`), and a silent gap there is exactly what * "stable before CSP" forbids. Byte-id-neutral: the corpus declares zero * nullable globals (verified by grep), so the loud path is unreached in * self-compile and the emitted asm is zero-move. * * The diagnostic core text is identical on both stages * ("nullable-global storage unimplemented (task #15)"); cstage's fatal() * adds the harness-wide "ww: " prefix (err.c) that err.ww does not — the * same per-stage prefix asymmetry every existing both-stage reject * carries (e.g. the #37 / #48 fatals). The matrix asserts rc!=0 AND the * shared core substring on each driver. * * Reject-matrix idiom (sibling 989_catA_f2_reject.c): a REJECT row must * FAIL to build with the diagnostic on every driver; an ACCEPT control * must build + run to its expected exit. Rows run on cstage `ww` and, * when present, wwstage `ww_ww`; both must agree. */ #include #include #include #include #include #include static const char *DIAG = "nullable-global storage unimplemented (task #15)"; 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 expect_build; /* 1 = build+run to want_exit; 0 = must REJECT */ int want_exit; }; #define NP "type np = (*i64 | void);\nlet gv: i64 = 7;\n" static const struct row rows[] = { /* match on a nullable global -> loud reject (was: read 0(BP)). */ { "match_nullable_global", "package main;\n" NP "let g: np = &gv;\n" "fn ck() int = { match (g) { case let p: *i64 => return (*p): int; " "case void => return 9; }; };\n" "export fn main() int = { return ck(); };\n", 0, 0 }, /* `g is *T` on a nullable global -> loud reject (was: undefined ref). */ { "is_nullable_global", "package main;\n" NP "let g: np = &gv;\n" "fn ck() int = { if (g is *i64) { return 0; }; return 12; };\n" "export fn main() int = { return ck(); };\n", 0, 0 }, /* `g as *T` on a nullable global -> loud reject (was: undefined ref). */ { "as_nullable_global", "package main;\n" NP "let g: np = &gv;\n" "fn ck() int = { let p: *i64 = g as *i64; return (*p): int; };\n" "export fn main() int = { return ck(); };\n", 0, 0 }, /* A void-initialised nullable global rejects too — storage is * unimplemented regardless of the initialiser (the full arc handles * nil/void as 8 zero bytes; that is task #15). */ { "void_init_nullable_global", "package main;\n" NP "let g: np = void;\n" "fn ck() int = { match (g) { case let p: *i64 => return (*p): int; " "case void => return 9; }; };\n" "export fn main() int = { return ck(); };\n", 0, 0 }, /* A nil-initialised nullable global rejects too — init-invariant, * same as void (the storage gate keys on the nullable TY_TAGGED, not * the initialiser; nil is the silent-match case the reconcile flagged * alongside void/&gv). */ { "nil_init_nullable_global", "package main;\n" NP "let g: np = nil;\n" "fn ck() int = { match (g) { case let p: *i64 => return (*p): int; " "case void => return 9; }; };\n" "export fn main() int = { return ck(); };\n", 0, 0 }, /* The INLINE nullable form (no alias) rejects identically — exercises * the storage gate's direct N_TTAGGED arm rather than the * alias-resolution hop the other rows take, proving form-invariance. */ { "inline_nullable_global", "package main;\nlet gv: i64 = 7;\nlet g: (*i64 | void) = &gv;\n" "fn ck() int = { match (g) { case let p: *i64 => return (*p): int; " "case void => return 9; }; };\n" "export fn main() int = { return ck(); };\n", 0, 0 }, /* CONTROL — a NON-nullable tagged global still builds + runs (the * #87 storage path is untouched). is i64 on g=5 -> 0. */ { "nonnull_tagged_global_ok", "package main;\n" "let g: (i64 | bool) = 5;\n" "fn ck() int = { if (g is i64) { return 0; }; return 12; };\n" "export fn main() int = { return ck(); };\n", 1, 0 }, /* CONTROL — a PLAIN `*T` global (not a nullable tagged) still builds + * runs; the reject is keyed on the nullable TY_TAGGED, not on any * pointer. nil-init (an address-of init is a separate #48-reloc gap, * out of scope here). */ { "plain_ptr_global_ok", "package main;\n" "let p: *i64 = nil;\n" "export fn main() int = { if (p == nil) { return 5; }; return 9; };\n", 1, 5 }, }; static int run_build(const char *driver, const struct row *r, int i) { char src[64], tmpdir[64], cmd[1024]; snprintf(src, sizeof src, "/tmp/nbg_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/nbg_%d_d_%d", getpid(), i); FILE *f = fopen(src, "wb"); if (!f) return -2; fputs(r->src, f); fclose(f); mkdir(tmpdir, 0755); snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null", tmpdir, driver, src); int brc = runwait(cmd); 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 = -1; if (brc == 0) got = runwait(outbin); unlink(src); unlink(outbin); rmdir(tmpdir); return brc == 0 ? got : -1; } /* A reject row must (a) fail to build and (b) emit the shared diagnostic * core text. Returns 0 on the expected reject, -1 otherwise. */ static int build_should_reject(const char *driver, const char *src, int i) { char s[64], tmpdir[64], errf[80], cmd[1280]; snprintf(s, sizeof s, "/tmp/nbn_%d_%d.ww", getpid(), i); snprintf(tmpdir, sizeof tmpdir, "/tmp/nbn_%d_d_%d", getpid(), i); snprintf(errf, sizeof errf, "/tmp/nbn_%d_%d.err", getpid(), i); FILE *f = fopen(s, "wb"); if (!f) return -1; fputs(src, f); fclose(f); mkdir(tmpdir, 0755); snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>%s", tmpdir, driver, s, errf); int rc = runwait(cmd); int have_diag = 0; FILE *e = fopen(errf, "rb"); if (e) { char buf[4096]; size_t n = fread(buf, 1, sizeof buf - 1, e); buf[n] = '\0'; fclose(e); have_diag = (strstr(buf, DIAG) != NULL); } unlink(s); unlink(errf); 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); /* build must NOT succeed AND the shared diagnostic must appear. */ return (rc != 0 && have_diag) ? 0 : -1; } 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], wdrv[1024]; snprintf(cdrv, sizeof cdrv, "%s/ww", bin); snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); struct { const char *name; const char *drv; int gated; } 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 && access(drivers[d].drv, X_OK) != 0) { fprintf(stderr, "nullableglobal_reject: skip %s (no %s)\n", drivers[d].name, drivers[d].drv); continue; } for (int i = 0; i < n; i++) { total++; if (rows[i].expect_build) { int got = run_build(drivers[d].drv, &rows[i], i); if (got != rows[i].want_exit) { fprintf(stderr, "nullableglobal_reject[%s][%s]: " "exit=%d want=%d\n", drivers[d].name, rows[i].label, got, rows[i].want_exit); fail++; } } else { if (build_should_reject(drivers[d].drv, rows[i].src, 100 + i) != 0) { fprintf(stderr, "nullableglobal_reject[%s][%s]: " "expected a loud reject with \"%s\"\n", drivers[d].name, rows[i].label, DIAG); fail++; } } } } if (fail) { fprintf(stderr, "nullableglobal_reject: %d/%d fixtures failed\n", fail, total); return 1; } printf("nullableglobal_reject: %d/%d ok\n", total, total); return 0; }