package collide_test; // Cross-module leaf-name collision observers on both driver stages. // Ports of the retired native carriers test/wcc/989_fnptrcollide_run.c // and 989_barefn_collide_run.c; every assertion preserved. // // fnptrcollide (#14 F7-c7) — a data global `slot: i64` whose LEAF // collides with the imported fn bar.slot must FAIL to build on BOTH // stages: type-keyed nodefnptr refuses to fold `&slot` into the fn's // TEXT reloc. Pre-fix wwstage was name-keyed and silently BUILT it // (a DATAR to the fn symbol — cat-A: cs loud-fails, ww builds). The // collision REQUIRES the import path (imported fn leaf vs local data // global), so a single-file fixture cannot express it. // // barefn (#84/#24a) — a `package main;` root `fn run` coexists with // imported aa.run: build+run exit 9 on BOTH stages (the user's // main.run wins, never aa.run=5); the concatenated // __root.s+aa.s (fixed #93 sep order) carries EXACTLY ONE // `TEXT main.run,` and EXACTLY ONE `TEXT aa.run,` (distinct symbols, // no #31-class w6l-tolerated duplicate, no silently-dead bare fn); // cs==ww byte-for-byte (rule 10). // // Dropped C machinery, not assertions: the ww_ww-absent skip gate // (the Make target declares both drivers), the shell `timeout 240` // (runcommand's deadline carries it), and the unlink/rmdir accounting // (testenv.clean asserts the removal). import os; import os.exec; import strings; import testenv; import time; fn fail(label: str, why: str) void = { let m: str = strings.concat("collide FAIL: ", label, " -- ", why, "\n"); os.write(2, m.ptr, m.len: u64); assert(false); }; fn tmo() time.duration = { return (240i64 * (time.second: i64)): time.duration; }; // -1 encodes an abnormal (non-EXIT) termination, never a valid code. fn runcode(dir: str, name: str, argv: []str) i32 = { let co: testenv.commandout; testenv.runcommand(dir, dir, name, argv, tmo(), &co); if (co.termination != exec.termination.EXIT) { return -1; }; return co.code; }; @test fn fnptrcollide() void = { let drvs: []str = ["ww", "ww_ww"]; let i: i32 = 0; for (i < 2) { let td: str = testenv.fresh(); assert(os.mkdir(strings.concat(td, "/bar"), 493) == 0); testenv.writefile(strings.concat(td, "/bar/bar.ww"), strings.concat( "package bar;\n", "export fn slot() i64 = { return 99; };\n")); testenv.writefile(strings.concat(td, "/main.ww"), strings.concat( "package main;\n", "import bar;\n", "let slot: i64 = 7;\n", "let fp: *i64 = &slot;\n", "export fn main() int = { return (*fp): int; };\n")); let av: []str = [testenv.driver(drvs[i]), "build", "-I", "bar", "main.ww"]; if (runcode(td, strings.concat("build_", drvs[i]), av) == 0) { fail("fnptrcollide", strings.concat(drvs[i], " built ok, ", "expected the leaf-name collision to be rejected (#14 -- ", "&slot mis-folded to the fn TEXT reloc)")); }; testenv.clean(td); i += 1; }; }; // __root.s then aa.s: the carrier's fixed #93 sep concat order. fn catfixed(stem: str) str = { return strings.concat( testenv.readfile(strings.concat(stem, ".sepwork/__root.s")), testenv.readfile(strings.concat(stem, ".sepwork/aa.s"))); }; // column-0 anchored label lines; the '\n' prepend counts a file-leading // label too. fn textcount(s: str, sym: str) i32 = { return testenv.occurrences(strings.concat("\n", s), strings.concat("\nTEXT ", sym, ",")); }; @test fn barefn() void = { let td: str = testenv.fresh(); assert(os.mkdir(strings.concat(td, "/aa"), 493) == 0); testenv.writefile(strings.concat(td, "/aa/aa.ww"), strings.concat( "package aa;\n", "export fn run() i32 = { return 5; };\n")); testenv.writefile(strings.concat(td, "/root.ww"), strings.concat( "package main;\n", "import aa;\n", "fn run() i32 = { return 9; };\n", "export fn main() i32 = { return run(); };\n")); let drvs: []str = ["ww", "ww_ww"]; let tags: []str = ["cs", "ww"]; let asms: []str = ["", ""]; let s: i32 = 0; for (s < 2) { let stem: str = strings.concat(td, "/prog.", tags[s]); let av: []str = [testenv.driver(drvs[s]), "build", "-o", stem, "-I", td, strings.concat(td, "/root.ww")]; if (runcode(td, strings.concat("build_", tags[s]), av) != 0) { fail("barefn", strings.concat(drvs[s], " build failed")); }; let rav: []str = [stem]; if (runcode(td, strings.concat("run_", tags[s]), rav) != 9) { fail("barefn", strings.concat(drvs[s], " exit != 9 ", "(user main.run must win, not aa.run=5)")); }; asms[s] = catfixed(stem); if (textcount(asms[s], "main.run") != 1 || textcount(asms[s], "aa.run") != 1) { fail("barefn", strings.concat(drvs[s], " TEXT label counts ", "!= 1/1 (user main.run distinct from import, no dup)")); }; s += 1; }; if (!testenv.same(asms[0], asms[1])) { fail("barefn", "cs .s != ww .s (rule 10)"); }; testenv.clean(td); }; // barevalue (#55 cgen-side sibling) — the migrated 794 carrier // (test/wcc/794_xmod_ident_prefer.c, retired with this row; its header // documents both #55 halves). A bare VALUE ident read inside an // imported module must be classified by the checker-stamped type, not // a unit-wide leaf table: aa exports `v: i32 = 7` and getv() reads the // bare `v`; the root declares a same-leaf `fn v() i64`. FLAT layout // (file-keyed import) is REQUIRED — both files fold into one unit so // the foreign fn lands in the leaf table; a dir-keyed aa/ compiles aa // as its own sep unit where main.v is invisible and the collision // cannot express. Pre-fix wwstage cgen took fnretlookup's leaf // fallback and emitted `LEAQ aa.v(SB)` (fn address, no load) — the // binary exited 0; cstage loads 7 (LEAQ+MOVSXD). Both drivers must // build, run 7, and the flat __root.s must be cs==ww byte-identical // (rule 10) — the stronger assertion set the carrier deferred while // the cgen side was open. @test fn barevalue() void = { let td: str = testenv.fresh(); testenv.writefile(strings.concat(td, "/aa.ww"), strings.concat( "package aa;\n", "export let v: i32 = 7;\n", "export fn getv() i32 = {\n", " return v;\n", "};\n")); testenv.writefile(strings.concat(td, "/main.ww"), strings.concat( "package main;\n", "import aa;\n", "fn v() i64 = { return 100; };\n", "export fn main() i32 = {\n", " return aa.getv();\n", "};\n")); let drvs: []str = ["ww", "ww_ww"]; let tags: []str = ["cs", "ww"]; let asms: []str = ["", ""]; let s: i32 = 0; for (s < 2) { let stem: str = strings.concat(td, "/prog.", tags[s]); let av: []str = [testenv.driver(drvs[s]), "build", "-o", stem, "-I", td, strings.concat(td, "/main.ww")]; if (runcode(td, strings.concat("build_", tags[s]), av) != 0) { fail("barevalue", strings.concat(drvs[s], " build failed")); }; let rav: []str = [stem]; if (runcode(td, strings.concat("run_", tags[s]), rav) != 7) { fail("barevalue", strings.concat(drvs[s], " exit != 7 ", "(bare v in aa.getv must LOAD aa.v, not take the ", "foreign fn's address)")); }; asms[s] = testenv.readfile(strings.concat(stem, ".sepwork/__root.s")); s += 1; }; if (!testenv.same(asms[0], asms[1])) { fail("barevalue", "cs .s != ww .s (rule 10)"); }; testenv.clean(td); }; // defshadow — corner (c) of the same class: a foreign scalar `def` // leaf colliding with a curmod fn. The def arm ran BEFORE // fn-classification in wwstage cgident (cstage checks TY_FN first), // so the bare `MSG` fn value read `MOVQ main.MSG(SB)` data where // cstage takes the fn address; the qualified `p5aa.MSG` data read hit // the mirror corner (d) in cgdot. Runtime pre-fix: 141, want 42. @test fn defshadow() void = { let td: str = testenv.fresh(); testenv.writefile(strings.concat(td, "/p5aa.ww"), strings.concat( "package p5aa;\n", "export def MSG: i32 = 3;\n")); testenv.writefile(strings.concat(td, "/main.ww"), strings.concat( "package main;\n", "import p5aa;\n", "fn MSG(x: i32) i32 = { return x + 40; };\n", "export fn main() i32 = {\n", " let f = MSG;\n", " return f(2) + p5aa.MSG - 3;\n", "};\n")); let drvs: []str = ["ww", "ww_ww"]; let tags: []str = ["cs", "ww"]; let asms: []str = ["", ""]; let s: i32 = 0; for (s < 2) { let stem: str = strings.concat(td, "/prog.", tags[s]); let av: []str = [testenv.driver(drvs[s]), "build", "-o", stem, "-I", td, strings.concat(td, "/main.ww")]; if (runcode(td, strings.concat("build_", tags[s]), av) != 0) { fail("defshadow", strings.concat(drvs[s], " build failed")); }; let rav: []str = [stem]; if (runcode(td, strings.concat("run_", tags[s]), rav) != 42) { fail("defshadow", strings.concat(drvs[s], " exit != 42 ", "(bare MSG is the curmod fn; p5aa.MSG is the ", "foreign data def)")); }; asms[s] = testenv.readfile(strings.concat(stem, ".sepwork/__root.s")); s += 1; }; if (!testenv.same(asms[0], asms[1])) { fail("defshadow", "cs .s != ww .s (rule 10)"); }; testenv.clean(td); }; // modqualfnval — corner (d) positive twin: a module-QUALIFIED fn used // as a value must still take the address arm under the stamped-type // gate (proves the checker stamps the N_DOT fn rvalue as TY_FN). @test fn modqualfnval() void = { let td: str = testenv.fresh(); testenv.writefile(strings.concat(td, "/p6aa.ww"), strings.concat( "package p6aa;\n", "export fn hit(x: i32) i32 = { return x + 40; };\n")); testenv.writefile(strings.concat(td, "/main.ww"), strings.concat( "package main;\n", "import p6aa;\n", "export fn main() i32 = {\n", " let f = p6aa.hit;\n", " return f(2);\n", "};\n")); let drvs: []str = ["ww", "ww_ww"]; let tags: []str = ["cs", "ww"]; let asms: []str = ["", ""]; let s: i32 = 0; for (s < 2) { let stem: str = strings.concat(td, "/prog.", tags[s]); let av: []str = [testenv.driver(drvs[s]), "build", "-o", stem, "-I", td, strings.concat(td, "/main.ww")]; if (runcode(td, strings.concat("build_", tags[s]), av) != 0) { fail("modqualfnval", strings.concat(drvs[s], " build failed")); }; let rav: []str = [stem]; if (runcode(td, strings.concat("run_", tags[s]), rav) != 42) { fail("modqualfnval", strings.concat(drvs[s], " exit != 42")); }; asms[s] = testenv.readfile(strings.concat(stem, ".sepwork/__root.s")); s += 1; }; if (!testenv.same(asms[0], asms[1])) { fail("modqualfnval", "cs .s != ww .s (rule 10)"); }; testenv.clean(td); };