Files
ww/test/xmod/collide_test.ww
Hojun-Cho c7d9dc92de selfhost: migrate tool sources to directory packages
Build w6a and w6l from package-main directories and expose the wcc backend through a narrow package API so w6c and wwdump no longer import implementation files. Retarget the remaining load-bearing fixtures and example sources to directory packages; retain the one intentional flat compiler collision as an explicitly composed raw unit.
2026-08-12 17:12:03 +09:00

283 lines
10 KiB
Plaintext

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 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);
};