From 66d69537a55974629b8b6608a163c8533b7fde85 Mon Sep 17 00:00:00 2001 From: Hojun-Cho Date: Sat, 6 Jun 2026 23:34:46 +0900 Subject: [PATCH] =?UTF-8?q?wcc/cgen:=20#124=20cross-module=20&fn=20in=20a?= =?UTF-8?q?=20const=20=E2=80=94=20N=5FDOT=20reloc=20+=20checker=20accept?= =?UTF-8?q?=20(both-stage)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A cross-module `&module.fn` in a const emitted no static reloc (the const was never defined -> w6l undefined-reference, both stages) and wwstage's checker rejected the const fn-table. #117/#119 wired the &fn->DATAR const-data reloc for SAME-module &fn only; charclass_map (fold-6) needs cross-module (12x &ascii.isXXX). cgen: add the N_DOT arm to the &fn->symbol helper (node_fnptr_sym / nodefnptr + the two ww emit sites), emitting mafn(leaf, module-ident) -- exactly the symbol a runtime &mod.fn or a direct cross-module call already emits. The helper is the SSoT for both the scalar (#119) and tuple-row (#117) const-data paths, so one arm closes both. checker: type a cross-module `&mod.fn` as `*fn(...)` in the TK_AMP arm (the N_DOT twin of #206's N_IDENT fn-ptr synthesis, gated on a resolved SK_FN/N_FNDECL leaf), so isassignable affirmatively accepts the const table -- aligning wwstage UP to cstage's actual acceptance reason rather than by abdication. The SK_FN gate keeps a non-fn `&mod.var` from synthesizing a fn type (the one pre-existing nonfn-scalar cs!=ww slip is N_IDENT-base, untouched and reproduces same-module). One consumer-coupled commit (the checker accept gates wwstage cgen, so neither half is independently testable). Narrow: slice-row + scalar only; fixed-array (#118) and struct-field (#129) stay separate. Both stages emit the correct cross-module symbols at the right tuple-slot offsets -> byte-identical (990-997 green). Pin 949_xmod_fnptr_const_run (distinct fns so a wrong reloc is caught + the SK_FN-gate axis). This was the last fold-6 cgen blocker; charclass_map is now unblocked. --- Makefile | 7 + cmd/w6c/cgen.c | 13 ++ selfhost/cmd/w6c/main.combined.ww | 61 +++++- selfhost/cmd/wcc/cgen.ww | 34 ++- selfhost/cmd/wcc/check.ww | 27 +++ selfhost/cmd/wwdump/main.combined.ww | 61 +++++- test/wcc/949_xmod_fnptr_const_run.c | 316 +++++++++++++++++++++++++++ 7 files changed, 513 insertions(+), 6 deletions(-) create mode 100644 test/wcc/949_xmod_fnptr_const_run.c diff --git a/Makefile b/Makefile index a8bc1b6f..81747c34 100644 --- a/Makefile +++ b/Makefile @@ -327,6 +327,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_nonlit_tuple_widen_run \ $(BIN)/test_const_slice_aggregate_run \ $(BIN)/test_tuple_index_read_run \ + $(BIN)/test_xmod_fnptr_const_run \ $(BIN)/test_tuple_slot_layout_run \ $(BIN)/test_tagged_tuple_widen_run \ $(BIN)/test_tagged_structlit_payload_run \ @@ -1643,6 +1644,12 @@ $(BIN)/test_tuple_index_read_run: test/wcc/947_tuple_index_read_run.c \ $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_xmod_fnptr_const_run: test/wcc/949_xmod_fnptr_const_run.c \ + $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_tagged_tuple_widen_run: test/wcc/936_tagged_tuple_widen_run.c \ $(BIN)/ww $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/cmd/w6c/cgen.c b/cmd/w6c/cgen.c index 5371fad9..1e156962 100644 --- a/cmd/w6c/cgen.c +++ b/cmd/w6c/cgen.c @@ -15217,6 +15217,19 @@ node_fnptr_sym(Cg *c, Node *ev) { if (ev == NULL || ev->kind != N_UN || ev->op != TK_AMP) return NULL; Node *opnd = ev->lhs; + /* #124: a cross-module `&mod.fn` — opnd is an N_DOT whose base is an + * untyped SK_USE module ident (NULL/ty_err); the leaf chases TY_FN. + * Mangle the leaf with the MODULE ident (not cur_mod) so the reloc + * targets the same TEXT symbol the runtime &mod.fn / direct call emit + * (cgen.c:4123-4144). The N_DOT arm of the #117/#119 reloc helper. */ + if (opnd && opnd->kind == N_DOT) { + if (opnd->lhs == NULL || opnd->lhs->kind != N_IDENT + || (opnd->lhs->type != NULL && opnd->lhs->type != ty_err)) + return NULL; + Type *du = type_chase_named(opnd->type); + if (du == NULL || du->kind != TY_FN) return NULL; + return mod_mangle_fn(c, opnd->str, opnd->lhs->str); + } if (opnd == NULL || opnd->kind != N_IDENT) return NULL; Type *ou = type_chase_named(opnd->type); if (ou == NULL || ou->kind != TY_FN) return NULL; diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index d048acda..914f3d42 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -12640,6 +12640,33 @@ fn unoptype(c: *checker, e: *node) *node = { }; }; }; + // #124: a cross-module `&mod.fn` — same synthesis as the + // N_IDENT arm, but the fn decl lives in the qualifier module + // (scopelookupinmodule). Without this the address-of falls to + // the generic `*opt` path and a const `[](str,*fn)` table's + // nested cross-module &fn element fails the isassignable + // typeeq → confident reject; cstage types `&mod.fn` as `*fn` + // natively (a dotted fn ref is TY_FN), so this aligns ww UP to + // cstage's actual acceptance reason. + if (e.lhs.kind == nkind.N_DOT) { + if (e.lhs.lhs != nil && e.lhs.lhs.kind == nkind.N_IDENT) { + let fs: *sym = scopelookupinmodule(c.cur, e.lhs.lhs.str, e.lhs.str); + if (fs != nil) { + if (fs.skind == skind.SK_FN) { + if (fs.decl != nil) { + if (fs.decl.kind == nkind.N_FNDECL) { + let synth: *node = newnode(nkind.N_TFN, "", 0, 0); + synth.lhs = fs.decl.lhs; + synth.list = fs.decl.list; + let pf: *node = newnode(nkind.N_TPTR, "", 0, 0); + pf.lhs = synth; + return pf; + }; + }; + }; + }; + }; + }; }; // opt nil → propagation from inherent-IDENT bail (5-lite-b // #34). Generic &expr widens to *opt; without opt we can't @@ -40228,6 +40255,22 @@ fn nodefnptr(c: *cgen, ev: *node) bool = { if (ev.op != tkind.TK_AMP) { return false; }; let opnd: *node = ev.lhs; if (opnd == nil) { return false; }; + // #124: a cross-module `&mod.fn` — opnd is an N_DOT whose base is an + // SK_USE module qualifier (not a local / let / def), and whose leaf + // resolves to a fn in that module. Mangle via the module ident (not + // curmod) at the emit sites so the reloc targets the same TEXT symbol + // the runtime `&mod.fn` emits (cgenexpr.ww N_DOT addr-of arm). The + // N_DOT arm of the #117/#119 reloc helper. + if (opnd.kind == nkind.N_DOT) { + if (opnd.lhs == nil) { return false; }; + if (opnd.lhs.kind != nkind.N_IDENT) { return false; }; + let basenm: str = opnd.lhs.str; + if (localfindnode(c, basenm) != nil) { return false; }; + if (isletvar(c, basenm)) { return false; }; + if (deflookup(c, basenm)) { return false; }; + if (fnretlookupmod(c, opnd.str, basenm) == nil) { return false; }; + return true; + }; if (opnd.kind != nkind.N_IDENT) { return false; }; if (fnretlookup(c, opnd.str) == nil) { return false; }; return true; @@ -40365,7 +40408,14 @@ fn emittuplerowrelocs(c: *cgen, name: str, module: str, backing: bool, rowoff: i emitline("+"); emitint(foff: i64); emitline("(SB),"); - emitfnname(c, ev.lhs.str, c.curmod); + // #124: a cross-module `&mod.fn` operand mangles + // the leaf with the MODULE ident; same-module `&fn` + // stays on curmod. + if (ev.lhs.kind == nkind.N_DOT) { + emitfnname(c, ev.lhs.str, ev.lhs.lhs.str); + } else { + emitfnname(c, ev.lhs.str, c.curmod); + }; emitline("(SB)\n"); }; // #22: slot stride via the accessor (tagged is @@ -40545,7 +40595,14 @@ fn emitletdataw(c: *cgen, file: *node) void = { emitline("DATAR "); emitsymnamehint(c, nm, d.nmod); emitline("+0(SB),"); - emitfnname(c, r.lhs.str, c.curmod); + // #124: cross-module `&mod.fn` mangles the + // leaf with the MODULE ident; same-module `&fn` + // stays on curmod. + if (r.lhs.kind == nkind.N_DOT) { + emitfnname(c, r.lhs.str, r.lhs.lhs.str); + } else { + emitfnname(c, r.lhs.str, c.curmod); + }; emitline("(SB)\n"); } else if (ok) { emitline("DATAW "); diff --git a/selfhost/cmd/wcc/cgen.ww b/selfhost/cmd/wcc/cgen.ww index a049a523..c6663d2b 100644 --- a/selfhost/cmd/wcc/cgen.ww +++ b/selfhost/cmd/wcc/cgen.ww @@ -2404,6 +2404,22 @@ fn nodefnptr(c: *cgen, ev: *node) bool = { if (ev.op != tkind.TK_AMP) { return false; }; let opnd: *node = ev.lhs; if (opnd == nil) { return false; }; + // #124: a cross-module `&mod.fn` — opnd is an N_DOT whose base is an + // SK_USE module qualifier (not a local / let / def), and whose leaf + // resolves to a fn in that module. Mangle via the module ident (not + // curmod) at the emit sites so the reloc targets the same TEXT symbol + // the runtime `&mod.fn` emits (cgenexpr.ww N_DOT addr-of arm). The + // N_DOT arm of the #117/#119 reloc helper. + if (opnd.kind == nkind.N_DOT) { + if (opnd.lhs == nil) { return false; }; + if (opnd.lhs.kind != nkind.N_IDENT) { return false; }; + let basenm: str = opnd.lhs.str; + if (localfindnode(c, basenm) != nil) { return false; }; + if (isletvar(c, basenm)) { return false; }; + if (deflookup(c, basenm)) { return false; }; + if (fnretlookupmod(c, opnd.str, basenm) == nil) { return false; }; + return true; + }; if (opnd.kind != nkind.N_IDENT) { return false; }; if (fnretlookup(c, opnd.str) == nil) { return false; }; return true; @@ -2541,7 +2557,14 @@ fn emittuplerowrelocs(c: *cgen, name: str, module: str, backing: bool, rowoff: i emitline("+"); emitint(foff: i64); emitline("(SB),"); - emitfnname(c, ev.lhs.str, c.curmod); + // #124: a cross-module `&mod.fn` operand mangles + // the leaf with the MODULE ident; same-module `&fn` + // stays on curmod. + if (ev.lhs.kind == nkind.N_DOT) { + emitfnname(c, ev.lhs.str, ev.lhs.lhs.str); + } else { + emitfnname(c, ev.lhs.str, c.curmod); + }; emitline("(SB)\n"); }; // #22: slot stride via the accessor (tagged is @@ -2721,7 +2744,14 @@ fn emitletdataw(c: *cgen, file: *node) void = { emitline("DATAR "); emitsymnamehint(c, nm, d.nmod); emitline("+0(SB),"); - emitfnname(c, r.lhs.str, c.curmod); + // #124: cross-module `&mod.fn` mangles the + // leaf with the MODULE ident; same-module `&fn` + // stays on curmod. + if (r.lhs.kind == nkind.N_DOT) { + emitfnname(c, r.lhs.str, r.lhs.lhs.str); + } else { + emitfnname(c, r.lhs.str, c.curmod); + }; emitline("(SB)\n"); } else if (ok) { emitline("DATAW "); diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index cfc8bfcb..3a02c2ab 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -2359,6 +2359,33 @@ fn unoptype(c: *checker, e: *node) *node = { }; }; }; + // #124: a cross-module `&mod.fn` — same synthesis as the + // N_IDENT arm, but the fn decl lives in the qualifier module + // (scopelookupinmodule). Without this the address-of falls to + // the generic `*opt` path and a const `[](str,*fn)` table's + // nested cross-module &fn element fails the isassignable + // typeeq → confident reject; cstage types `&mod.fn` as `*fn` + // natively (a dotted fn ref is TY_FN), so this aligns ww UP to + // cstage's actual acceptance reason. + if (e.lhs.kind == nkind.N_DOT) { + if (e.lhs.lhs != nil && e.lhs.lhs.kind == nkind.N_IDENT) { + let fs: *sym = scopelookupinmodule(c.cur, e.lhs.lhs.str, e.lhs.str); + if (fs != nil) { + if (fs.skind == skind.SK_FN) { + if (fs.decl != nil) { + if (fs.decl.kind == nkind.N_FNDECL) { + let synth: *node = newnode(nkind.N_TFN, "", 0, 0); + synth.lhs = fs.decl.lhs; + synth.list = fs.decl.list; + let pf: *node = newnode(nkind.N_TPTR, "", 0, 0); + pf.lhs = synth; + return pf; + }; + }; + }; + }; + }; + }; }; // opt nil → propagation from inherent-IDENT bail (5-lite-b // #34). Generic &expr widens to *opt; without opt we can't diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index fd807868..4457e611 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -12640,6 +12640,33 @@ fn unoptype(c: *checker, e: *node) *node = { }; }; }; + // #124: a cross-module `&mod.fn` — same synthesis as the + // N_IDENT arm, but the fn decl lives in the qualifier module + // (scopelookupinmodule). Without this the address-of falls to + // the generic `*opt` path and a const `[](str,*fn)` table's + // nested cross-module &fn element fails the isassignable + // typeeq → confident reject; cstage types `&mod.fn` as `*fn` + // natively (a dotted fn ref is TY_FN), so this aligns ww UP to + // cstage's actual acceptance reason. + if (e.lhs.kind == nkind.N_DOT) { + if (e.lhs.lhs != nil && e.lhs.lhs.kind == nkind.N_IDENT) { + let fs: *sym = scopelookupinmodule(c.cur, e.lhs.lhs.str, e.lhs.str); + if (fs != nil) { + if (fs.skind == skind.SK_FN) { + if (fs.decl != nil) { + if (fs.decl.kind == nkind.N_FNDECL) { + let synth: *node = newnode(nkind.N_TFN, "", 0, 0); + synth.lhs = fs.decl.lhs; + synth.list = fs.decl.list; + let pf: *node = newnode(nkind.N_TPTR, "", 0, 0); + pf.lhs = synth; + return pf; + }; + }; + }; + }; + }; + }; }; // opt nil → propagation from inherent-IDENT bail (5-lite-b // #34). Generic &expr widens to *opt; without opt we can't @@ -40228,6 +40255,22 @@ fn nodefnptr(c: *cgen, ev: *node) bool = { if (ev.op != tkind.TK_AMP) { return false; }; let opnd: *node = ev.lhs; if (opnd == nil) { return false; }; + // #124: a cross-module `&mod.fn` — opnd is an N_DOT whose base is an + // SK_USE module qualifier (not a local / let / def), and whose leaf + // resolves to a fn in that module. Mangle via the module ident (not + // curmod) at the emit sites so the reloc targets the same TEXT symbol + // the runtime `&mod.fn` emits (cgenexpr.ww N_DOT addr-of arm). The + // N_DOT arm of the #117/#119 reloc helper. + if (opnd.kind == nkind.N_DOT) { + if (opnd.lhs == nil) { return false; }; + if (opnd.lhs.kind != nkind.N_IDENT) { return false; }; + let basenm: str = opnd.lhs.str; + if (localfindnode(c, basenm) != nil) { return false; }; + if (isletvar(c, basenm)) { return false; }; + if (deflookup(c, basenm)) { return false; }; + if (fnretlookupmod(c, opnd.str, basenm) == nil) { return false; }; + return true; + }; if (opnd.kind != nkind.N_IDENT) { return false; }; if (fnretlookup(c, opnd.str) == nil) { return false; }; return true; @@ -40365,7 +40408,14 @@ fn emittuplerowrelocs(c: *cgen, name: str, module: str, backing: bool, rowoff: i emitline("+"); emitint(foff: i64); emitline("(SB),"); - emitfnname(c, ev.lhs.str, c.curmod); + // #124: a cross-module `&mod.fn` operand mangles + // the leaf with the MODULE ident; same-module `&fn` + // stays on curmod. + if (ev.lhs.kind == nkind.N_DOT) { + emitfnname(c, ev.lhs.str, ev.lhs.lhs.str); + } else { + emitfnname(c, ev.lhs.str, c.curmod); + }; emitline("(SB)\n"); }; // #22: slot stride via the accessor (tagged is @@ -40545,7 +40595,14 @@ fn emitletdataw(c: *cgen, file: *node) void = { emitline("DATAR "); emitsymnamehint(c, nm, d.nmod); emitline("+0(SB),"); - emitfnname(c, r.lhs.str, c.curmod); + // #124: cross-module `&mod.fn` mangles the + // leaf with the MODULE ident; same-module `&fn` + // stays on curmod. + if (r.lhs.kind == nkind.N_DOT) { + emitfnname(c, r.lhs.str, r.lhs.lhs.str); + } else { + emitfnname(c, r.lhs.str, c.curmod); + }; emitline("(SB)\n"); } else if (ok) { emitline("DATAW "); diff --git a/test/wcc/949_xmod_fnptr_const_run.c b/test/wcc/949_xmod_fnptr_const_run.c new file mode 100644 index 00000000..3fffde26 --- /dev/null +++ b/test/wcc/949_xmod_fnptr_const_run.c @@ -0,0 +1,316 @@ +/* + * 949_xmod_fnptr_const_run — #124: a CROSS-MODULE `&module.fn` inside a + * module-level const emits its &fn->DATAR reloc + the wwstage checker + * accepts the const table. + * + * #117/#119 wired the &fn->DATAR reloc for SAME-module `&fn` only. The + * cgen detect helper (node_fnptr_sym cgen.c / nodefnptr cgen.ww) was + * N_IDENT-only and mangled the leaf with cur_mod — so a cross-module + * `&cc.isa` (operand is N_DOT) returned NULL → no reloc → no DATA → + * w6l undefined-reference, BOTH stages. fold-6's charclass_map needs + * 12x cross-module `&ascii.isXXX` inside a const `[](str, *fn(rune)bool)`. + * + * The #124 fix is ONE both-stage, consumer-coupled commit: + * - cgen reloc (BOTH stages): node_fnptr_sym / nodefnptr grow an N_DOT + * arm forming mafn(leaf, MODULE-ident) — the same TEXT symbol the + * working runtime `&mod.fn` / direct call already emit. Shared by the + * scalar (#119) and tuple-row (#117) callers (one helper = SSoT). + * - wwstage checker (ww-only, align-UP): exprtype's TK_AMP arm now + * types a cross-module `&mod.fn` as `*fn(...)` (the N_DOT twin of the + * #206 N_IDENT synthesis, via scopelookupinmodule) — so the const + * table's nested cross-module &fn element typeeqs its `*fn` slot and + * isassignable AFFIRMATIVELY accepts (cstage types `&mod.fn` as `*fn` + * natively → ww aligns up to cstage's actual acceptance reason). The + * SK_FN gate keeps the accept narrow to genuine fn-pointers. + * + * POLARITY: align-BOTH — both stages were LOUD at base 754944a (cstage + * link-fail, wwstage checker reject). byte-id is the wwstage-correctness + * net (#263); the fn-reloc is pinned at runtime with DISTINCT fns so a + * wrong reloc (or swapped per-row offset) is caught, not masked. + * + * row | shape | exit | byte-id + * ----------------+------------------------------------+------+-------- + * xmod_table_scalar| const tbl:[](str,*fn) of (&cc.isa,| 0 | cs==ww + * | &cc.isz) DISTINCT + scalar &cc.isa| | + * | — index tbl[i].1, call each back | | + * wrong_sig_table | const t:[](str,*fn(i32)i32) = | LOUD | (n/a) + * | [(..,&cc.isa)] — sig mismatch must | | + * | reject BOTH (pins the *fn typeeq | | + * | is real, not accept-everything) | | + * nonfn_dotted_ | const t:[](str,*fn) =[(..,&cc.kval)| LOUD | (n/a) + * table | )] — & must reject | | + * | BOTH (pins the SK_FN/N_FNDECL gate | | + * | — the #117 over-accept hazard) | | + * + * Cross-module via OWN 2-module fixtures in a PRIVATE mktemp dir (784 + * model) — NOT the real lib/ascii: the driver writes intermediates next + * to traversed sources, so importing lib/ would race; a synthetic module + * `cc` exercises the IDENTICAL `&mod.fn` mangle (mafn(leaf, module)). + * NNN<950 so test-unit runs it; the private mktemp dir makes the ww_ww + * driver race-free (946 ww_ww-driver precedent). + */ +#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; +} + +static int +slurp_eq(const char *a, const char *b) +{ + FILE *fa = fopen(a, "rb"); + FILE *fb = fopen(b, "rb"); + if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; } + int rc = 0; + for (;;) { + int ca = fgetc(fa); + int cb = fgetc(fb); + if (ca != cb) { rc = -1; break; } + if (ca == EOF) break; + } + fclose(fa); fclose(fb); + return rc; +} + +#define K_RUN 0 /* build+run BOTH drivers, exit==want, + cs==ww byte-id */ +#define K_BUILDERR 1 /* build must FAIL on BOTH drivers (rule 7 loud) */ + +struct file { const char *name; const char *src; }; + +struct scenario { + const char *label; + const struct file *files; /* name==NULL terminates */ + int kind; + int want_exit; +}; + +/* The fold-6 consumer shape: a const cross-module `[](str,*fn(rune)bool)` + * + a scalar `*fn` global, both initialised with `&cc.`. DISTINCT + * predicates per row so a wrong reloc routes the wrong fn and a call-back + * catches it. The tuple's *fn is element .1 (the consumer ordering). */ +static const struct file table_files[] = { + { "cc.ww", + "package cc;\n" + "export fn isa(c: rune) bool = { return c == 'a'; };\n" + "export fn isz(c: rune) bool = { return c == 'z'; };\n" }, + { "main.ww", + "package main;\n" + "import cc;\n" + "const tbl: [](str, *fn(c: rune) bool) =" + " [(\":a\", &cc.isa), (\":z\", &cc.isz)];\n" + "const pf: *fn(c: rune) bool = &cc.isa;\n" + "export fn main() i32 = {\n" + " if (len(tbl) != 2) { return 10; };\n" + " let e0 = tbl[0];\n" + " let e1 = tbl[1];\n" + " let f0 = e0.1;\n" + " let f1 = e1.1;\n" + " if (!(*f0)('a')) { return 1; };\n" + " if ((*f0)('z')) { return 2; };\n" + " if (!(*f1)('z')) { return 3; };\n" + " if ((*f1)('a')) { return 4; };\n" + " if (!(*pf)('a')) { return 5; };\n" + " if ((*pf)('z')) { return 6; };\n" + " return 0;\n" + "};\n" }, + { NULL, NULL } +}; + +/* The SK_FN-gated bound: a cross-module &fn whose SIGNATURE doesn't match + * the declared `*fn` slot must still reject on BOTH stages (the *fn typeeq + * is real — the lever is affirmative recognition, not accept-everything). */ +static const struct file wrong_sig_files[] = { + { "cc.ww", + "package cc;\n" + "export fn isa(c: rune) bool = { return c == 'a'; };\n" }, + { "main.ww", + "package main;\n" + "import cc;\n" + "const t: [](str, *fn(x: i32) i32) = [(\":a\", &cc.isa)];\n" + "export fn main() i32 = { return len(t): i32; };\n" }, + { NULL, NULL } +}; + +/* The SK_FN/N_FNDECL-gated bound (distinct axis from wrong_sig_files): + * a cross-module dotted address-of a NON-fn export (`&cc.kval`, kval an + * exported var) must NOT synthesize a `*fn` and must reject the table on + * BOTH stages. This locks the lever's narrowness — the over-accept hazard + * reviewer-117 caught — so a future widening of the exprtype N_DOT arm + * past genuine fn-pointers is a loud test failure, not a silent accept. */ +static const struct file nonfn_dotted_files[] = { + { "cc.ww", + "package cc;\n" + "export let kval: int = 7;\n" }, + { "main.ww", + "package main;\n" + "import cc;\n" + "const t: [](str, *fn(c: rune) bool) = [(\":k\", &cc.kval)];\n" + "export fn main() i32 = { return len(t): i32; };\n" }, + { NULL, NULL } +}; + +static const struct scenario scenarios[] = { + { "xmod_table_scalar", table_files, K_RUN, 0 }, + { "wrong_sig_table", wrong_sig_files, K_BUILDERR, 0 }, + { "nonfn_dotted_table", nonfn_dotted_files, K_BUILDERR, 0 }, +}; + +/* Build via `driver` in the fixture dir; returns the driver's build rc and + * (for K_RUN) runs the produced binary, comparing exit to want_exit. */ +static int +build_run(const char *dir, const char *driver, const struct scenario *sc) +{ + char cmd[4096], path[1024]; + snprintf(cmd, sizeof cmd, + "cd %s && rm -f main main.s main.o main.combined.ww && " + "%s build -I %s %s/main.ww >/dev/null 2>%s/err", + dir, driver, dir, dir, dir); + int brc = runwait(cmd); + if (sc->kind == K_BUILDERR) { + if (brc == 0) { + fprintf(stderr, "949[%s]: %s built but expected LOUD " + "reject\n", sc->label, driver); + return -1; + } + return 0; + } + if (brc != 0) { + fprintf(stderr, "949[%s]: %s build failed\n", sc->label, driver); + return -1; + } + snprintf(path, sizeof path, "%s/main", dir); + int got = runwait(path); + if (got != sc->want_exit) { + fprintf(stderr, "949[%s]: %s exit %d, want %d\n", + sc->label, driver, got, sc->want_exit); + return -1; + } + return 0; +} + +static int +run_scenario(const char *bin, const char *w6c, const char *w6c_ww, + const struct scenario *sc) +{ + char dir[] = "/tmp/ww949_XXXXXX"; + if (mkdtemp(dir) == NULL) { + fprintf(stderr, "949[%s]: mkdtemp failed\n", sc->label); + return -1; + } + + char path[1024], cmd[4096], wwdrv[2200]; + int rc = 0; + + for (int i = 0; sc->files[i].name; i++) { + snprintf(path, sizeof path, "%s/%s", dir, sc->files[i].name); + FILE *f = fopen(path, "wb"); + if (!f) { fprintf(stderr, "949[%s]: write %s\n", sc->label, + sc->files[i].name); rc = -1; goto done; } + fputs(sc->files[i].src, f); + fclose(f); + } + + /* cstage driver build (+run for K_RUN). */ + { + char csdrv[2200]; + snprintf(csdrv, sizeof csdrv, "%s/ww", bin); + if (build_run(dir, csdrv, sc) != 0) { rc = -1; goto done; } + } + + /* wwstage driver build (+run for K_RUN) — race-free in the private + * mktemp dir. Direct proof the wwstage emits a runnable binary. */ + snprintf(wwdrv, sizeof wwdrv, "%s/ww_ww", bin); + if (build_run(dir, wwdrv, sc) != 0) { rc = -1; goto done; } + + if (sc->kind == K_BUILDERR) goto done; + + /* Byte-id net (#263 — the wwstage-correctness proof on a gate-blind + * align-BOTH fix): raw w6c vs w6c_ww on the cstage-driver combined. */ + { + char cs_s[1024], ws_s[1024], comb[1024]; + /* regen the combined via the cstage driver (deterministic). */ + snprintf(cmd, sizeof cmd, + "cd %s && %s/ww build -I %s %s/main.ww >/dev/null 2>&1", + dir, bin, dir, dir); + if (runwait(cmd) != 0) { + fprintf(stderr, "949[%s]: combined regen failed\n", + sc->label); + rc = -1; goto done; + } + snprintf(comb, sizeof comb, "%s/main.combined.ww", dir); + snprintf(cs_s, sizeof cs_s, "%s/cs.s", dir); + snprintf(ws_s, sizeof ws_s, "%s/ww.s", dir); + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", + w6c, cs_s, comb); + if (runwait(cmd) != 0) { + fprintf(stderr, "949[%s]: w6c on combined failed\n", + sc->label); + rc = -1; goto done; + } + snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", + w6c_ww, ws_s, comb); + if (runwait(cmd) != 0) { + fprintf(stderr, "949[%s]: w6c_ww on combined failed\n", + sc->label); + rc = -1; goto done; + } + if (slurp_eq(cs_s, ws_s) != 0) { + fprintf(stderr, "949[%s]: cs.s/ww.s DIFFER (rule-10 " + "byte-id violation)\n", sc->label); + rc = -1; + } + } + +done: + snprintf(cmd, sizeof cmd, "rm -rf %s", dir); + (void)runwait(cmd); + return rc; +} + +int +main(void) +{ + const char *bin = getenv("BIN"); + if (!bin) bin = "out/bin"; + char absbin[2048]; + 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 w6c[2100], w6c_ww[2100]; + snprintf(w6c, sizeof w6c, "%s/w6c", bin); + snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin); + if (access(w6c_ww, X_OK) != 0) { + fprintf(stderr, "949: w6c_ww missing — cannot run the cs==ww " + "byte-id gate (the whole point of this test)\n"); + return 1; + } + + int n = (int)(sizeof scenarios / sizeof scenarios[0]); + int fail = 0; + for (int i = 0; i < n; i++) { + if (run_scenario(bin, w6c, w6c_ww, &scenarios[i]) != 0) + fail++; + } + + if (fail) { + fprintf(stderr, "949 xmod_fnptr_const: %d/%d scenarios failed\n", + fail, n); + return 1; + } + printf("xmod_fnptr_const: %d/%d ok (cs+ww run + cs==ww byte-id)\n", + n, n); + return 0; +}