w6c+selfhost: same-module preference for bare-leaf lookup

This commit is contained in:
2026-05-15 11:24:33 +09:00
parent 35b32c1304
commit e6045f3ada
13 changed files with 382 additions and 12 deletions

View File

@@ -0,0 +1,93 @@
/*
* 697_samemod_prefer — same-module preference for bare-leaf lookup.
*
* Two modules each export `read` with intentionally distinct
* signatures (mod1: i32 → i32, mod2: str → i32). Each module's
* `caller()` invokes bare `read(...)` with its own arg type. The
* bare-leaf lookup must resolve to the SAME-MODULE `read`; otherwise
* the call is a check-time signature mismatch.
*
* Pre-fix (flat scope, first-wins by hash order): one of the bare
* lookups picked the wrong module's `read` and the build failed with
* "argument type not assignable". Post-fix (scope_lookup_prefer): each
* bare-leaf inside its own module binds to the same-module entry, both
* calls type-check, and the build succeeds.
*
* Runtime exit codes aren't asserted here because cgen still emits
* unmangled `TEXT read` labels for both modules and the linker
* collapses them. Mangling fn labels by module is a separate codegen
* sweep; this test pins the resolver fix alone.
*
* Fixtures live in test/wcc/data/samemodprefer/. Same shape as
* 696_modtype_leaf_collision.c.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
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
run_pos(const char *driver, const char *fixdir, const char *tag)
{
char cmd[2048];
/* cd into the fixture dir so the driver's source-dir-first
* import search resolves `use mod1; use mod2;`. */
snprintf(cmd, sizeof cmd,
"cd %s && %s build pos.ww >/dev/null 2>&1", fixdir, driver);
if (runwait(cmd) != 0) {
fprintf(stderr,
"samemod_prefer[%s]: pos.ww build failed — bare-leaf "
"`read` lookup likely cross-bound\n", tag);
return 1;
}
char bin[2048];
snprintf(bin, sizeof bin, "%s/pos", fixdir);
unlink(bin);
return 0;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[1024];
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 cdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
char fixdir[1024];
if (getcwd(fixdir, sizeof fixdir) == NULL) return 1;
size_t cwd_n = strlen(fixdir);
const char *rel = "/test/wcc/data/samemodprefer";
if (cwd_n + strlen(rel) + 1 >= sizeof fixdir) return 1;
memcpy(fixdir + cwd_n, rel, strlen(rel) + 1);
int fail = 0;
fail += run_pos(cdrv, fixdir, "cstage");
if (fail) {
fprintf(stderr,
"samemod_prefer: %d case(s) failed\n", fail);
return 1;
}
printf("samemod_prefer: 1/1 ok\n");
return 0;
}

View File

@@ -0,0 +1,18 @@
// samemodprefer/mod1 — exports a `read` that takes an i32 plus a
// `caller` that invokes bare `read(1i32)`. The bare-leaf lookup must
// resolve to mod1.read (i32 → i32) rather than mod2.read (str → i32),
// which coexists in the flat scope. Pre-fix the lookup picked
// whichever entry hashed earliest into the bucket; a wrong bind would
// surface as a check-time signature mismatch. Paired with mod2/mod2.ww
// and test/wcc/697_samemod_prefer.c.
export fn read(x: i32) i32 = {
return x + 100i32;
};
export fn caller() i32 = {
// Bare-leaf call inside mod1: must bind to mod1.read (i32 arg).
// If preference doesn't kick in this fails check: mod2.read
// expects a str, so `read(1i32)` would be a signature mismatch.
return read(1i32);
};

View File

@@ -0,0 +1,18 @@
// samemodprefer/mod2 — exports a `read` that takes a str plus a
// `caller` that invokes bare `read("ok")`. The bare-leaf lookup must
// resolve to mod2.read (str → i32) rather than mod1.read (i32 → i32),
// which coexists in the flat scope. Pre-fix the lookup picked
// whichever entry hashed earliest into the bucket; a wrong bind would
// surface as a check-time signature mismatch. Paired with mod1/mod1.ww
// and test/wcc/697_samemod_prefer.c.
export fn read(x: str) i32 = {
return x.len + 200i32;
};
export fn caller() i32 = {
// Bare-leaf call inside mod2: must bind to mod2.read (str arg).
// If preference doesn't kick in this fails check: mod1.read
// expects an i32, so `read("ok")` would be a signature mismatch.
return read("ok");
};

View File

@@ -0,0 +1,20 @@
// Positive case: two modules each export `read` with DIFFERENT
// signatures (mod1: i32 → i32, mod2: str → i32). Each module also
// exports a `caller` that invokes bare `read(...)` with its own arg
// type. The bare-leaf inside mod1 must bind to mod1.read; inside mod2
// to mod2.read. If preference doesn't kick in and the bare lookup
// picks the other module's `read`, the build fails with a signature
// mismatch at check-time. The driver test asserts that this fixture
// builds successfully.
//
// We don't assert runtime exit codes here because cgen still emits
// unmangled `TEXT read` labels for both modules and the linker
// collapses them — that's a separate codegen sweep, orthogonal to the
// resolver fix this test pins.
use mod1;
use mod2;
fn main() i32 = {
return 0i32;
};