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; their collision teeth remain explicit under // the package-initialization contract. // // fnptrcollide (#14 F7-c7) — a data global `slot: i64` whose leaf // collides with imported fn bar.slot. `let fp: *i64 = &slot` is valid // runtime package initialization: both stages must address main.slot, // initialize fp before main, and run 7. The old loud-reject expectation // predated runtime package lets. The collision still requires an import, // and name-keyed lowering to bar.slot is rejected by exact asm/runtime pins. // // barefn (#84/#24a) — a `package main;` root `fn run` coexists with // imported aa.run: build+run exit 9 on BOTH stages while an explicit // `aa.run()` use keeps the source import real; the user's unqualified // `run()` resolves to main.run, 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 td: str = testenv.fresh(); let bar: str = strings.concat(td, "/bar"); let main: str = strings.concat(td, "/main"); assert(os.mkdir(bar, 493) == 0); assert(os.mkdir(main, 493) == 0); testenv.writefile(strings.concat(bar, "/bar.ww"), strings.concat( "package bar;\n", "export fn slot() i64 = { return 99; };\n")); testenv.writefile(strings.concat(main, "/main.ww"), strings.concat( "package main;\n", "import bar;\n", "let slot: i64 = 7;\n", "let fp: *i64 = &slot;\n", "export fn main() int = { let _ = bar.slot(); ", "return (*fp): int; };\n")); let drvs: []str = ["ww", "ww_ww"]; let tags: []str = ["cs", "ww"]; let works: []str = [strings.concat(td, "/work-c"), strings.concat(td, "/work-ww")]; let progs: []str = [strings.concat(td, "/prog.cs"), strings.concat(td, "/prog.ww")]; let i: i32 = 0; for (i < 2) { assert(os.mkdir(works[i], 493) == 0); let av: []str = [testenv.driver(drvs[i]), "build", "-w", works[i], "-I", td, "-o", progs[i], "main"]; if (runcode(td, strings.concat("build_", tags[i]), av) != 0) { fail("fnptrcollide", strings.concat(drvs[i], " build failed")); }; let rav: []str = [progs[i]]; if (runcode(td, strings.concat("run_", tags[i]), rav) != 7) { fail("fnptrcollide", strings.concat(drvs[i], " exit != 7")); }; let sourceasm: str = testenv.readfile(strings.concat(works[i], "/main.s")); let initasm: str = testenv.readfile(strings.concat(works[i], "/main.init.s")); let barasm: str = testenv.readfile(strings.concat(works[i], "/bar.s")); if (testenv.occurrences(sourceasm, "DATAW main.slot(SB)") != 1 || testenv.occurrences(sourceasm, "DATAW main.fp(SB)") != 1 || testenv.occurrences(sourceasm, "CALL\t__ww..dispatch(SB)") != 1 || testenv.occurrences(sourceasm, "LEAQ\tmain.slot(SB), AX") != 1 || testenv.occurrences(sourceasm, "MOVQ\tAX, main.fp(SB)") != 1 || testenv.has(sourceasm, "LEAQ\tbar.slot(SB)") || testenv.has(sourceasm, "DATAR main.fp") || testenv.occurrences(initasm, "CALL\t__ww..pkg.p.bar.v0.r0.init(SB)") != 1 || testenv.occurrences(initasm, "CALL\t__ww..pkg.p.main.v0.r0.init(SB)") != 1 || testenv.occurrences(barasm, "TEXT bar.slot,") != 1) { fail("fnptrcollide", "runtime initializer selected the colliding fn"); }; i += 1; }; let wantdispatch: str = strings.concat( "//ww:init-root __ww..pkg.p.main.v0.r0.init\n", "//ww:init-call __ww..pkg.p.bar.v0.r0.init\n", "//ww:init-call __ww..pkg.p.main.v0.r0.init\n"); if (!testenv.same(wantdispatch, testenv.readfile(strings.concat(works[0], "/main.init.unit.ww")))) { fail("fnptrcollide", "dispatcher did not initialize dependency first"); }; let names: []str = ["bar.unit.ww", "bar.wwi", "bar.s", "bar.o", "bar.a", "main.unit.ww", "main.wwi", "main.s", "main.o", "main.a", "main.init.unit.ww", "main.init.s", "main.init.o"]; i = 0; for (i < names.len) { if (!testenv.same(testenv.readfile(strings.concat(works[0], "/", names[i])), testenv.readfile(strings.concat(works[1], "/", names[i])))) { fail("fnptrcollide", strings.concat(names[i], " differs by stage")); }; i += 1; }; if (!testenv.same(testenv.readfile(progs[0]), testenv.readfile(progs[1]))) { fail("fnptrcollide", "Cstage/WWstage binaries differ"); }; testenv.clean(td); }; // __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 = {\n", "\tlet foreign: i32 = aa.run();\n", "\treturn run() + foreign - foreign;\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, "/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 compiler 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`. This compiler // collision requires one explicitly composed raw unit; directory packages // correctly isolate the leaf tables and the driver no longer folds source // imports. 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 compilers must // emit byte-identical assembly and the raw fixture must run 7. @test fn barevalue() void = { let td: str = testenv.fresh(); let unit: str = strings.concat(td, "/barevalue.unit.ww"); testenv.writefile(unit, strings.concat( "//ww:module aa\n", "package aa;\n", "export let v: i32 = 7;\n", "export fn getv() i32 = {\n", " return v;\n", "};\n", "//ww:module-reset\n", "package main;\n", "import aa;\n", "fn v() i64 = { return 100; };\n", "export fn main() i32 = {\n", " return aa.getv();\n", "};\n")); let comps: []str = ["w6c", "w6c_ww"]; let tags: []str = ["cs", "ww"]; let asms: []str = ["", ""]; let s: i32 = 0; for (s < 2) { asms[s] = strings.concat(td, "/", tags[s], ".s"); let av: []str = [testenv.driver(comps[s]), "-o", asms[s], unit]; if (runcode(td, strings.concat("build_", tags[s]), av) != 0) { fail("barevalue", strings.concat(comps[s], " compile failed")); }; s += 1; }; if (!testenv.same(testenv.readfile(asms[0]), testenv.readfile(asms[1]))) { fail("barevalue", "cs .s != ww .s (rule 10)"); }; let obj: str = strings.concat(td, "/barevalue.o"); let aav: []str = [testenv.driver("w6a"), "-o", obj, asms[0]]; if (runcode(td, "assemble", aav) != 0) { fail("barevalue", "w6a failed"); }; let prog: str = strings.concat(td, "/barevalue"); let lav: []str = [testenv.driver("w6l"), "-o", prog, obj, strings.concat(testenv.repo(), "/out/lib/libwwrt.a")]; if (runcode(td, "link", lav) != 0) { fail("barevalue", "w6l failed"); }; let rav: []str = [prog]; if (runcode(td, "run", rav) != 7) { fail("barevalue", "bare v loaded the foreign fn address"); }; 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(); assert(os.mkdir(strings.concat(td, "/p5aa"), 493) == 0); testenv.writefile(strings.concat(td, "/p5aa/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(); assert(os.mkdir(strings.concat(td, "/p6aa"), 493) == 0); testenv.writefile(strings.concat(td, "/p6aa/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); };