diff --git a/Makefile b/Makefile index 8e47db25..0f154be3 100644 --- a/Makefile +++ b/Makefile @@ -252,6 +252,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_catA_f2_reject \ $(BIN)/test_intoverflow_reject \ $(BIN)/test_unknowndecl_reject \ + $(BIN)/test_nullableglobal_reject \ $(BIN)/test_idxarg_run \ $(BIN)/test_chainidx_run \ $(BIN)/test_tupfieldsize_run \ @@ -721,6 +722,20 @@ $(BIN)/test_unknowndecl_reject: test/wcc/989_unknowndecl_reject.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +# 989_nullableglobal_reject (#45 silent->loud bridge, task #15 prereq): a +# module-level nullable `(*T | void)` GLOBAL has no storage path, so a +# match/is/as on it silently miscompiled (0(BP) read / undefined ref) on +# BOTH stages. The bridge dies LOUD at the size/storage layer; the full +# storage + read-class arc is deferred to #15 (CSP handle-singleton +# prereq). Needs the full cstage + wwstage tool sets plus libwwrt for the +# control links. +$(BIN)/test_nullableglobal_reject: test/wcc/989_nullableglobal_reject.c \ + $(BIN)/ww $(BIN)/ww_ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + # 989_idxarg_run (F7-c2, #45/#46): an indexed slice/str element passed as # a call arg must push its full multi-word header. Builds+runs each fixture # on BOTH the cstage `ww` and wwstage `ww_ww` drivers (rule-10), so it needs diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index 7338dba8..6552f3dd 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -1269,10 +1269,18 @@ let_emit_size(Type *t) * variant init via emit_tagged_data; match-scrutinee reads * resolve the box at name(SB). Pre-#87 the missing arm sized 0 * → no DATA, no letvar registration, and match read saved BP - * as the tag (SEGV). The nullable `(*T | void)` one-word fold - * stays 0 (handled, when const, by the 8B scalar arm in - * emit_lets — adding it here would re-route that path). */ - return u->nullable ? 0 : (int)u->size; + * as the tag (SEGV). + * #45 (silent→loud bridge, task #15): a nullable `(*T | void)` + * GLOBAL has no storage path. Returning 0 here made let_collect + * + emit_lets silently skip the decl (no DATA, no letvar reg), + * so a later match read 0(BP) and is/as emitted MOVQ name(SB) + * for an undefined symbol — a silent miscompile in the CSP + * handle-singleton substrate. Die loud at the size/storage layer + * so all three read paths hit one diagnostic; the full storage + + * read-class arc is task #15 (CSP-prereq). */ + if (u->nullable) + fatal("nullable-global storage unimplemented (task #15)"); + return (int)u->size; default: return 0; } diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 73c12b78..11189299 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -41821,11 +41821,22 @@ fn letemitsize(c: *cgen, d: *node) i32 = { return tsum; }; // #87: non-nullable tagged-union global — box size (tag word + - // max payload, mirror of the runtime local). Nullable stays 0 so - // the (*T|void) one-word fold keeps the 8B scalar arm in - // emitletdataw. Mirrors cstage let_emit_size TY_TAGGED. + // max payload, mirror of the runtime local). Mirrors cstage + // let_emit_size TY_TAGGED. if (t.kind == nkind.N_TTAGGED) { - if (isnullabletype(t)) { return 0; }; + // #45 (silent→loud bridge, task #15): a nullable (*T|void) + // GLOBAL has no storage path. Returning 0 here made + // letcollect + emitletdataw silently skip the decl (no DATA, + // no let-registration), so a later match/is/as resolved + // 0(BP) or an undefined symbol — a silent miscompile in the + // CSP handle-singleton substrate. Die loud at the size/ + // storage layer so all three read paths hit one diagnostic; + // the full storage + read-class arc is task #15 (CSP-prereq). + if (isnullabletype(t)) { + let mng: str = "nullable-global storage unimplemented (task #15)\n"; + os.write(2, mng.ptr, mng.len: u64); + os.exit(1); + }; return slotsize(c, t); }; if (t.kind != nkind.N_TNAME) { return 0; }; diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index 854ececd..7cb2911b 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -1047,11 +1047,22 @@ fn letemitsize(c: *cgen, d: *node) i32 = { return tsum; }; // #87: non-nullable tagged-union global — box size (tag word + - // max payload, mirror of the runtime local). Nullable stays 0 so - // the (*T|void) one-word fold keeps the 8B scalar arm in - // emitletdataw. Mirrors cstage let_emit_size TY_TAGGED. + // max payload, mirror of the runtime local). Mirrors cstage + // let_emit_size TY_TAGGED. if (t.kind == nkind.N_TTAGGED) { - if (isnullabletype(t)) { return 0; }; + // #45 (silent→loud bridge, task #15): a nullable (*T|void) + // GLOBAL has no storage path. Returning 0 here made + // letcollect + emitletdataw silently skip the decl (no DATA, + // no let-registration), so a later match/is/as resolved + // 0(BP) or an undefined symbol — a silent miscompile in the + // CSP handle-singleton substrate. Die loud at the size/ + // storage layer so all three read paths hit one diagnostic; + // the full storage + read-class arc is task #15 (CSP-prereq). + if (isnullabletype(t)) { + let mng: str = "nullable-global storage unimplemented (task #15)\n"; + os.write(2, mng.ptr, mng.len: u64); + os.exit(1); + }; return slotsize(c, t); }; if (t.kind != nkind.N_TNAME) { return 0; }; diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index d44e2ed7..25fd852f 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -41821,11 +41821,22 @@ fn letemitsize(c: *cgen, d: *node) i32 = { return tsum; }; // #87: non-nullable tagged-union global — box size (tag word + - // max payload, mirror of the runtime local). Nullable stays 0 so - // the (*T|void) one-word fold keeps the 8B scalar arm in - // emitletdataw. Mirrors cstage let_emit_size TY_TAGGED. + // max payload, mirror of the runtime local). Mirrors cstage + // let_emit_size TY_TAGGED. if (t.kind == nkind.N_TTAGGED) { - if (isnullabletype(t)) { return 0; }; + // #45 (silent→loud bridge, task #15): a nullable (*T|void) + // GLOBAL has no storage path. Returning 0 here made + // letcollect + emitletdataw silently skip the decl (no DATA, + // no let-registration), so a later match/is/as resolved + // 0(BP) or an undefined symbol — a silent miscompile in the + // CSP handle-singleton substrate. Die loud at the size/ + // storage layer so all three read paths hit one diagnostic; + // the full storage + read-class arc is task #15 (CSP-prereq). + if (isnullabletype(t)) { + let mng: str = "nullable-global storage unimplemented (task #15)\n"; + os.write(2, mng.ptr, mng.len: u64); + os.exit(1); + }; return slotsize(c, t); }; if (t.kind != nkind.N_TNAME) { return 0; }; diff --git a/test/wcc/989_nullableglobal_reject.c b/test/wcc/989_nullableglobal_reject.c new file mode 100644 index 00000000..dc5863bf --- /dev/null +++ b/test/wcc/989_nullableglobal_reject.c @@ -0,0 +1,277 @@ +/* + * 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; +}