/* * 783_amp_fn_assign_run — project #206 close. Pins that a bare * `&fn_name` is assignable into a `*` slot and a * `(* | void)` tagged slot (the io vstream vtable shape) * WITHOUT the explicit `(&fn): *alias` cast that the lib/{io,memio, * fmt,bufio,log} vstream surfaces currently carry. #94 fold-eFinal * drops those casts wholesale once this is green. * * THE FIX (Option C, both stages, checker-only — cgen is a no-op * fn-pointer reinterpret): type_assignable / isassignable stay fully * NOMINAL (a materialized `*fn` value laundered into a `*alias` is * rejected, mirror of harec ref/harec/src/types.c:1039-1066). A * caller-site gate `assignable_addrfn` (cmd/wcc/check.c) / * `assignableaddrfn` (selfhost/cmd/wcc/check.ww) admits ONLY a DIRECT * `&`-of-fn-ident whose signature structurally matches the * destination's pointed-to fn alias, or — for a tagged dst — the * single matching ptr-to-fn variant (>=2 same-sig variants is * ambiguous → reject). This is harec's adopt-the-alias-at-the-`&`-site * rule (ref/harec/src/check.c:3594-3626) without threading a type * hint through the bottom-up expression checker. * * ROW POLARITY: * POSITIVE execution assertions now live in the declarative compiler * corpus. Their residual rows here compile BOTH stages and assert * cs.s == ww.s (rule-10). `let_call` and `structlit_build` pin the * let-binding and struct-literal-field-init gate sites. * `fieldstore_dispatch` pins the eFinal SHIPPING shape: a bare &fn * field-STORE into one slot of the three-slot io.vtable * (reader/writer/closer), then a dispatcher match on a *vtable * POINTER-param. It remains byte-id-clean: the * single-slot local-composite zero-init divergence (project #213, * reproduces with the cast form and a pure `(i32|void)` field — NOT a * #206 regression) is a single-field artifact the real three-slot * io.vtable does not hit. * * The symmetric non-tagged rejection rows (`neg_launder`, * `neg_samesig`) live in the declarative compiler corpus. Both checker * paths report the common diagnostic fragment "not assignable". * * NEGATIVE tagged-slot rows (`neg_ambiguous_tagged`, * `neg_launder_tagged`) are pinned CSTAGE-ONLY. cstage REJECTS both. * wwstage WRONGLY ACCEPTS them: its `(X | void)` tagged-assignability * leniently matches any `*fn` against the `void` variant (project * #214, the void-variant OVER-acceptance). #214 is an accept-INVALID * hole and its close is REQUIRED BEFORE wwstage can become the * authoritative selfhost checker; these rows graduate to STAGE_WW * when #214 closes. Mirrors the 777/780/781/782 STAGE_CS carve-out. * * GATE POLARITY: must stay GREEN. Red means the #206 gate over- or * under-accepts or the stage output diverged. */ #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; } #define STAGE_CS 1 #define STAGE_WW 2 struct row { const char *label; const char *src; /* local source for asymmetric controls */ const char *fixture; /* declarative corpus source for byte rows */ int stage_mask; int byte_id; /* assert cs.s == ww.s */ int expect_fail; /* 1 = build MUST fail */ }; static const struct row rows[] = { /* POSITIVE byte-id: bare &fn into a * let, then call. */ { "let_call", NULL, "test/wcc/data/r78_amp_fn_assign_let_call/case.ww", STAGE_CS | STAGE_WW, 1, 0 }, /* POSITIVE byte-id: struct-literal field-init of a (*alias|void) * slot with bare &fn (the Hare `vtable{reader=&fn}` shape). Build * only — no match (project #212 blocks struct-lit-tagged + match * on wwstage cgen); the checker gate acceptance is what this pins. */ { "structlit_build", NULL, "test/wcc/data/r78_amp_fn_assign_structlit/case.ww", STAGE_CS | STAGE_WW, 1, 0 }, /* POSITIVE byte-id: bare &fn FIELD-STORE into a (*reader|void) slot * of the THREE-slot vtable (the exact io.vtable reader/writer/closer * eFinal shape, test 775), then dispatcher match on a *vtable * POINTER-param. The live-code-address call-through AND the void-arm * discrimination are exercised, and cs.s == ww.s (rule-10). This is * the eFinal SHIPPING path, so it MUST be byte-id-clean: the single- * slot local-composite zero-init divergence (#213) is a single-field * artifact that the real three-slot io.vtable does not hit. */ { "fieldstore_dispatch", NULL, "test/wcc/data/r78_amp_fn_assign_fieldstore_dispatch/case.ww", STAGE_CS | STAGE_WW, 1, 0 }, /* NEGATIVE cstage-only (#214 wwstage void-variant over-acceptance): * two same-signature ptr-to-fn variants in the tagged dst — a direct * &fn is ambiguous and must be rejected, never silently bound to one. */ { "neg_ambiguous_tagged", "package main;\n" "type reader = fn(x: i32) i32;\n" "type writer = fn(x: i32) i32;\n" "fn myfn(x: i32) i32 = { return x; };\n" "fn main() i32 = {\n" " let x: (*reader | *writer | void) = &myfn;\n" " return 0;\n" "};\n", NULL, STAGE_CS, 0, 1 }, /* NEGATIVE cstage-only (#214): laundering a materialized *fn into a * (*reader|void) tagged slot. cstage rejects nominally; wwstage's * void-variant leniency wrongly accepts. */ { "neg_launder_tagged", "package main;\n" "type reader = fn(x: i32) i32;\n" "fn rd(x: i32) i32 = { return x + 1; };\n" "fn main() i32 = {\n" " let p = &rd;\n" " let s: (*reader | void) = p;\n" " return 0;\n" "};\n", NULL, STAGE_CS, 0, 1 }, }; static int cleanup_tmp(const char *tmpdir, const char *base) { char p[1024]; int rc = 0; snprintf(p, sizeof p, "%s/%s", tmpdir, base); if (unlink(p) != 0 && errno != ENOENT) rc = -1; snprintf(p, sizeof p, "%s/%s.ww", tmpdir, base); if (unlink(p) != 0 && errno != ENOENT) rc = -1; /* #93: the sep scratch dir + the tmpdir-owned outputs. */ snprintf(p, sizeof p, "rm -rf %s/%s.sepwork", tmpdir, base); if (runwait(p) != 0) rc = -1; if (rmdir(tmpdir) != 0) rc = -1; return rc; } static int write_source(const char *path, const struct row *r) { FILE *in = NULL; if (r->fixture != NULL) { in = fopen(r->fixture, "rb"); if (in == NULL) return -1; } FILE *f = fopen(path, "wb"); if (!f) { if (in != NULL) fclose(in); return -1; } if (in != NULL) { int ioerr = 0; for (;;) { int ch = fgetc(in); if (ch == EOF) break; if (fputc(ch, f) == EOF) { ioerr = 1; break; } } if (ferror(in)) ioerr = 1; if (fclose(in) != 0) ioerr = 1; if (fclose(f) != 0) ioerr = 1; return ioerr ? -1 : 0; } else { if (fputs(r->src, f) == EOF) { fclose(f); return -1; } } return fclose(f) == 0 ? 0 : -1; } static int build_via_driver(const char *driver, const char *tmpdir, const char *src) { char cmd[2048]; /* #93 sep layout: `-o ` relocates artifacts to * .sepwork/ under tmpdir. expect_fail rows still fail: a checker * error makes w6c return nonzero regardless of artifact placement. */ snprintf(cmd, sizeof cmd, "cd %s && b='%s'; timeout 180 %s build " "-o \"${b%%.ww}\" \"$b\" 2>/dev/null", tmpdir, src, driver); return runwait(cmd); } /* build_ok — 1 iff the build succeeds (used by expect_fail rows). */ static int build_ok(const char *driver, const struct row *r, int seq) { (void)seq; char tmpdir[] = "/tmp/afa_nf_XXXXXX"; char src[512], base[64]; if (mkdtemp(tmpdir) == NULL) return -1; snprintf(base, sizeof base, "main783"); snprintf(src, sizeof src, "%s/%s.ww", tmpdir, base); if (write_source(src, r) != 0) { if (cleanup_tmp(tmpdir, base) != 0) fprintf(stderr, "amp_fn_assign[%s]: setup cleanup failed\n", r->label); return -1; } int br = build_via_driver(driver, tmpdir, src); if (cleanup_tmp(tmpdir, base) != 0) { fprintf(stderr, "amp_fn_assign[%s]: cleanup failed\n", r->label); return -1; } return br == 0; } /* asm_byte_identical — diff cstage vs wwstage .s. Parallel owned trees * keep each driver's retained .sepwork/__root.s distinct. */ static int asm_byte_identical(const char *cdrv, const char *wdrv, const struct row *r, int seq) { (void)seq; char src[512], base[64], cs[512], ws[512]; char tdc[] = "/tmp/afa_c_XXXXXX"; char tdw[] = "/tmp/afa_w_XXXXXX"; snprintf(base, sizeof base, "main783"); if (mkdtemp(tdc) == NULL) return -1; if (mkdtemp(tdw) == NULL) { if (cleanup_tmp(tdc, base) != 0) fprintf(stderr, "amp_fn_assign[%s]: setup cleanup failed\n", r->label); return -1; } snprintf(src, sizeof src, "%s/%s.ww", tdc, base); int rc = -1; if (write_source(src, r) != 0) goto out; if (build_via_driver(cdrv, tdc, src) != 0) goto out; snprintf(cs, sizeof cs, "%s/%s.sepwork/__root.s", tdc, base); snprintf(src, sizeof src, "%s/%s.ww", tdw, base); if (write_source(src, r) != 0) goto out; if (build_via_driver(wdrv, tdw, src) != 0) goto out; snprintf(ws, sizeof ws, "%s/%s.sepwork/__root.s", tdw, base); FILE *fc = fopen(cs, "rb"); FILE *fw = fopen(ws, "rb"); if (fc && fw) { rc = 0; 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); out: if (cleanup_tmp(tdc, base) != 0) { fprintf(stderr, "amp_fn_assign[%s]: c cleanup failed\n", r->label); rc = -1; } if (cleanup_tmp(tdw, base) != 0) { fprintf(stderr, "amp_fn_assign[%s]: ww cleanup failed\n", r->label); rc = -1; } return rc; } int main(void) { const char *bin = getenv("BIN"); if (!bin) bin = "out/bin"; char cwd[256]; if (getcwd(cwd, sizeof cwd) == NULL) return 1; char absbin[512]; if (bin[0] != '/') { snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin); bin = absbin; } char cdrv[640], wdrv[640]; snprintf(cdrv, sizeof cdrv, "%s/ww", bin); snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin); int n = (int)(sizeof rows / sizeof rows[0]); int total = 0, fail = 0, seq = 0; int wwpresent = (access(wdrv, X_OK) == 0); for (int i = 0; i < n; i++) { const struct row *r = &rows[i]; if ((r->stage_mask & STAGE_CS) && r->expect_fail) { total++; int got = build_ok(cdrv, r, seq++); if (got != 0) { if (got > 0) fprintf(stderr, "amp_fn_assign[cs][%s]: built but expected reject\n", r->label); else fprintf(stderr, "amp_fn_assign[cs][%s]: setup or cleanup failed\n", r->label); fail++; } } if (wwpresent && (r->stage_mask & STAGE_WW)) { if (r->expect_fail) { total++; int got = build_ok(wdrv, r, seq++); if (got != 0) { if (got > 0) fprintf(stderr, "amp_fn_assign[ww][%s]: built but expected reject\n", r->label); else fprintf(stderr, "amp_fn_assign[ww][%s]: setup or cleanup failed\n", r->label); fail++; } } else if (r->byte_id) { total++; if (asm_byte_identical(cdrv, wdrv, r, seq++) != 0) { fprintf(stderr, "amp_fn_assign[byte-id][%s]: cstage vs wwstage asm differs\n", r->label); fail++; } } } } if (fail) { fprintf(stderr, "amp_fn_assign: %d/%d checks failed\n", fail, total); return 1; } printf("amp_fn_assign: %d/%d ok\n", total, total); return 0; }