wcc/ww: mangle imported symbols on dotted import path (#22 M1, #32)

Switch symbol mangling from the import leaf clause to the full dotted import path for directory packages; single-file imports keep package-clause mangling (isdir-gate: imported<=>directory-import). The root build unit's fn main stays bare, every other top-level decl mangles, closing #31's duplicate-main hazard by construction (#32). Both stages, byte-identical.

Single commit, not split: the bare rename (f244af3) is red on its own because it unmasks cross-module resolution gaps that do not reproduce pre-M1, so the fixes are intrinsic to making the rename correct. Included: wwstage fnret/fnparamslookupmod map import alias->path (#199b cross-module union-variant scrutinee resolved the wrong fn's union); cstage use_path prefers the referencing module's import for an ambiguous leaf alias (sha256 crypto.math vs strconv math). Tests table-driven: 989_m1mangle_run/_sym, 989_m1union_run (gate-visible per-arm exit codes + cs==ww byte-id).
This commit is contained in:
2026-06-15 17:37:18 +09:00
parent 64d6c15e41
commit f308818b4b
30 changed files with 1851 additions and 241 deletions

View File

@@ -144,7 +144,11 @@ static const struct row rows[] = {
/* Cross-mod: cstage emits the LEAQ via N_DOT TK_AMP (already
* working pre-#180). Wwstage bails asserttyped on the same
* shape — filed as #184; this row stays cstage-only until that
* lifts. */
* lifts. M1 #22: `wcamffn764mod` is a directory package, so its
* exported `somefn` path-mangles to `wcamffn764mod.somefn` (the
* pre-M1 bare `somefn` is gone, mirroring 989_m1mangle_sym); the
* &-of hint routes through use_hint and the LEAQ matches the
* mangled definition. */
{ "cross_module",
"import wcamffn764mod;\n"
"export fn main() i32 = {\n"
@@ -153,7 +157,7 @@ static const struct row rows[] = {
"};\n",
"wcamffn764mod",
"export fn somefn(x: i32) i32 = { return x + 1; };\n",
"LEAQ\tsomefn(SB)",
"LEAQ\twcamffn764mod.somefn(SB)",
STAGE_CS },
};

212
test/wcc/989_m1mangle_run.c Normal file
View File

@@ -0,0 +1,212 @@
/*
* 989_m1mangle_run — M1 (#22 + #32): path-qualified symbol mangling and
* root-unit entry detection. Table-driven, both stages (rule-10: cstage
* `ww` and wwstage `ww_ww` must AGREE and hit want_exit).
*
* Each row lays out a tiny package tree next to a root `main.ww` and
* builds+runs the root. Because a directory import path-mangles its
* symbols (`aa.bb.val` etc.), a broken hint or skip rule shows up as a
* link failure (-1) or wrong runtime value — observable end-to-end.
*
* row | proves
* -----------------+--------------------------------------------------
* nested_call | DIRECTORY import path-mangles + the qualified-ref
* | hint resolves to the PATH (aa.bb.val), not the
* | leaf alias — cross-boundary call returns 42
* imported_main | #32/#31: an imported pkg's `fn main` mangles
* | (aa.bb.main) instead of colliding with the bare
* | root entry; both link, root main runs → 7
* priv_global | a module-PRIVATE value global mangles on the path
* | (aa.bb.pv) and the owning module resolves it → 9
* single_level | control: a single-level package's symbols are
* | UNCHANGED (cc.v stays cc.v) — still resolves → 5
* deep_nest | 2-level nest aa.bb.cc → aa.bb.cc.val path-mangle → 3
*/
#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;
}
struct file { const char *path; const char *content; };
struct row {
const char *label;
struct file files[4]; /* {NULL,NULL}-terminated package files */
const char *root; /* root main.ww content */
int want_exit;
};
static const struct row rows[] = {
{ "nested_call",
{ { "aa/bb/bb.ww",
"package bb;\n"
"export fn val() int = { return 42; };\n" },
{ NULL, NULL } },
"package main;\n"
"import aa.bb;\n"
"export fn main() int = { return bb.val(); };\n",
42 },
{ "imported_main",
{ { "aa/bb/bb.ww",
"package bb;\n"
"fn main() int = { return 7; };\n"
"export fn run() int = { return main(); };\n" },
{ NULL, NULL } },
"package main;\n"
"import aa.bb;\n"
"export fn main() int = { return bb.run(); };\n",
7 },
{ "priv_global",
{ { "aa/bb/bb.ww",
"package bb;\n"
"let pv: int = 9;\n"
"export fn getpv() int = { return pv; };\n" },
{ NULL, NULL } },
"package main;\n"
"import aa.bb;\n"
"export fn main() int = { return bb.getpv(); };\n",
9 },
{ "single_level",
{ { "cc/cc.ww",
"package cc;\n"
"export fn v() int = { return 5; };\n" },
{ NULL, NULL } },
"package main;\n"
"import cc;\n"
"export fn main() int = { return cc.v(); };\n",
5 },
{ "deep_nest",
{ { "aa/bb/cc/cc.ww",
"package cc;\n"
"export fn val() int = { return 3; };\n" },
{ NULL, NULL } },
"package main;\n"
"import aa.bb.cc;\n"
"export fn main() int = { return cc.val(); };\n",
3 },
};
/* write_file — create `dir/rel` (mkdir -p its parents) with `content`. */
static int
write_file(const char *dir, const char *rel, const char *content)
{
char path[512], cmd[1024];
snprintf(path, sizeof path, "%s/%s", dir, rel);
/* mkdir -p the parent of `path` */
char parent[512];
snprintf(parent, sizeof parent, "%s", path);
char *slash = strrchr(parent, '/');
if (slash) {
*slash = '\0';
snprintf(cmd, sizeof cmd, "mkdir -p '%s'", parent);
if (runwait(cmd) != 0) return -1;
}
FILE *f = fopen(path, "wb");
if (!f) return -1;
fputs(content, f);
fclose(f);
return 0;
}
/* run_build — lay out the row's package tree + root under a temp dir,
* build+run the root via `driver`; return the binary's exit code, or -1
* on a build failure. */
static int
run_build(const char *driver, const struct row *r, int i)
{
char dir[64], cmd[2048];
snprintf(dir, sizeof dir, "/tmp/m1mangle_%d_%d", getpid(), i);
snprintf(cmd, sizeof cmd, "rm -rf '%s' && mkdir -p '%s'", dir, dir);
if (runwait(cmd) != 0) return -2;
for (int k = 0; r->files[k].path; k++)
if (write_file(dir, r->files[k].path, r->files[k].content) != 0)
return -2;
if (write_file(dir, "main.ww", r->root) != 0) return -2;
/* cd into the build dir so the source-dir-first search path finds the
* sibling package tree (mirrors Hare's CWD == module-dir). */
snprintf(cmd, sizeof cmd, "cd '%s' && %s build main.ww 2>/dev/null",
dir, driver);
int brc = runwait(cmd);
int got = -1;
if (brc == 0) {
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/main", dir);
got = runwait(outbin);
}
snprintf(cmd, sizeof cmd, "rm -rf '%s'", dir);
runwait(cmd);
return brc == 0 ? got : -1;
}
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], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *drv; int gated; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) {
fprintf(stderr, "m1mangle_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
for (int i = 0; i < n; i++) {
total++;
int got = run_build(drivers[d].drv, &rows[i], i);
if (got != rows[i].want_exit) {
fprintf(stderr, "m1mangle_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, rows[i].want_exit);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "m1mangle_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("m1mangle_run: %d/%d ok\n", total, total);
return 0;
}

149
test/wcc/989_m1mangle_sym.c Normal file
View File

@@ -0,0 +1,149 @@
/*
* 989_m1mangle_sym — M1 (#22) symbol-name proof. Builds a fixture that
* imports the real nested DIRECTORY package `encoding.utf8` to `.s` on
* BOTH stages and asserts the emitted symbol table:
*
* needle | want | proves
* --------------------------+------+-----------------------------------
* "encoding.utf8.runesz" | yes | nested pkg path-mangles (the M1
* | | DELTA: leaf `utf8` → path
* | | `encoding.utf8`)
* "TEXT main," | yes | root entry stays BARE (#32)
* "TEXT utf8.runesz," | no | the pre-M1 leaf-only mangle is gone
*
* Plus cstage.s == wwstage.s byte-for-byte on the re-baselined names
* (rule-10 — the cs==ww gate over the new symbols).
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.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
file_has(const char *path, const char *needle)
{
FILE *f = fopen(path, "rb");
if (!f) return -1;
char line[4096];
int found = 0;
while (fgets(line, sizeof line, f)) {
if (strstr(line, needle)) { found = 1; break; }
}
fclose(f);
return found;
}
struct check { const char *needle; int want; };
static const struct check checks[] = {
{ "encoding.utf8.runesz", 1 },
{ "TEXT main,", 1 },
{ "TEXT utf8.runesz,", 0 },
};
/* build_s — build `src` to `<stem>.s` via `driver`; returns 0 on success. */
static int
build_s(const char *driver, const char *src, const char *stem)
{
char cmd[1024];
snprintf(cmd, sizeof cmd, "%s build -o '%s' '%s' 2>/dev/null",
driver, stem, src);
return runwait(cmd);
}
static int
checkall(const char *name, const char *sp)
{
int fail = 0;
for (int i = 0; i < (int)(sizeof checks / sizeof checks[0]); i++) {
int got = file_has(sp, checks[i].needle);
if (got != checks[i].want) {
fprintf(stderr, "m1mangle_sym[%s]: '%s' present=%d want=%d\n",
name, checks[i].needle, got, checks[i].want);
fail++;
}
}
return fail;
}
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], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
char src[64];
snprintf(src, sizeof src, "/tmp/m1sym_%d.ww", getpid());
FILE *f = fopen(src, "wb");
if (!f) return 1;
fputs("package main;\n"
"import encoding.utf8;\n"
"export fn main() int = { return utf8.runesz('A'): int; };\n", f);
fclose(f);
int fail = 0;
char cstem[64], cs[80];
snprintf(cstem, sizeof cstem, "/tmp/m1sym_c_%d", getpid());
snprintf(cs, sizeof cs, "%s.s", cstem);
if (build_s(cdrv, src, cstem) != 0) {
fprintf(stderr, "m1mangle_sym: cstage build failed\n");
fail++;
} else {
fail += checkall("cstage", cs);
}
int have_ww = (access(wdrv, X_OK) == 0);
char wstem[64], ws[80];
snprintf(wstem, sizeof wstem, "/tmp/m1sym_w_%d", getpid());
snprintf(ws, sizeof ws, "%s.s", wstem);
if (have_ww) {
if (build_s(wdrv, src, wstem) != 0) {
fprintf(stderr, "m1mangle_sym: wwstage build failed\n");
fail++;
} else {
fail += checkall("wwstage", ws);
/* rule-10: cs.s == ww.s on the new mangled names. */
char cmp[256];
snprintf(cmp, sizeof cmp, "cmp -s '%s' '%s'", cs, ws);
if (runwait(cmp) != 0) {
fprintf(stderr, "m1mangle_sym: cstage.s != "
"wwstage.s (byte-id break)\n");
fail++;
}
}
} else {
fprintf(stderr, "m1mangle_sym: skip wwstage (no ww_ww)\n");
}
char cmd[256];
snprintf(cmd, sizeof cmd, "rm -f '%s' '%s'* '%s'*", src, cstem, wstem);
runwait(cmd);
if (fail) {
fprintf(stderr, "m1mangle_sym: %d checks failed\n", fail);
return 1;
}
printf("m1mangle_sym: ok\n");
return 0;
}

225
test/wcc/989_m1union_run.c Normal file
View File

@@ -0,0 +1,225 @@
/*
* 989_m1union_run — M1 (#199b): a `match` on a tagged union returned by a
* CROSS-MODULE fn must keep each nominal variant on a DISTINCT tag. The
* canonical case is `utf8.next` (encoding.utf8), whose return
* `(rune | done | more | invalid)` has three structurally-identical
* void-alias variants (done/more = void, invalid = !void). Pre-#199b the
* wwstage collapsed done/more/invalid onto one tag (a byte-id-only
* miscompile: the doc's repro hit the rune arm and returned the same value
* either way). This test is GATE-VISIBLE: each arm maps to a distinct
* return code and the inputs deterministically hit each arm, so a collapse
* mis-routes an arm and changes the runtime exit — caught on BOTH stages.
*
* row | input | arm | want_exit
* -------------+------------------+-----------+----------
* rune | [65] | rune | 1
* done | [65] next twice | done | 2
* more | [195] (lead-only)| more | 3
* invalid | [255] | invalid | 4
* all_arms | all four, mixed | (1,2,4,3) | 219 (1243 % 256)
*
* Plus a byte-id gate: cstage.s == wwstage.s on the all_arms program
* (rule-10 — the cs==ww gate over the cross-module match the doc flagged).
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.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;
}
/* classify() is shared boilerplate: a cross-module match whose four arms
* each return a distinct code. Each row supplies only the main body. */
#define PRELUDE \
"package main;\n" \
"import encoding.utf8;\n" \
"fn classify(d: *utf8.decoder) int = {\n" \
" match (utf8.next(d)) {\n" \
" case let r: rune => return 1;\n" \
" case let dn: utf8.done => return 2;\n" \
" case let m: utf8.more => return 3;\n" \
" case let e: utf8.invalid => return 4;\n" \
" };\n" \
" return 0;\n" \
"};\n"
struct row {
const char *label;
const char *body; /* main() body, appended to PRELUDE */
int want_exit;
};
static const struct row rows[] = {
{ "rune",
"export fn main() int = {\n"
" let s: []u8 = [65u8];\n"
" let d = utf8.decode(s);\n"
" return classify(&d);\n"
"};\n", 1 },
{ "done",
"export fn main() int = {\n"
" let s: []u8 = [65u8];\n"
" let d = utf8.decode(s);\n"
" classify(&d);\n"
" return classify(&d);\n"
"};\n", 2 },
{ "more",
"export fn main() int = {\n"
" let s: []u8 = [195u8];\n"
" let d = utf8.decode(s);\n"
" return classify(&d);\n"
"};\n", 3 },
{ "invalid",
"export fn main() int = {\n"
" let s: []u8 = [255u8];\n"
" let d = utf8.decode(s);\n"
" return classify(&d);\n"
"};\n", 4 },
{ "all_arms",
"export fn main() int = {\n"
" let g: []u8 = [65u8];\n"
" let dg = utf8.decode(g);\n"
" let a = classify(&dg);\n"
" let b = classify(&dg);\n"
" let bad: []u8 = [255u8];\n"
" let db = utf8.decode(bad);\n"
" let c = classify(&db);\n"
" let tr: []u8 = [195u8];\n"
" let dt = utf8.decode(tr);\n"
" let e = classify(&dt);\n"
" return a * 1000 + b * 100 + c * 10 + e;\n"
"};\n", 219 },
};
/* write_src — PRELUDE + row body into <path>. */
static int
write_src(const char *path, const struct row *r)
{
FILE *f = fopen(path, "wb");
if (!f) return -1;
fputs(PRELUDE, f);
fputs(r->body, f);
fclose(f);
return 0;
}
/* build_run — build `src` to `<stem>` binary via `driver`, run it; return
* the binary's exit code, or -1 on a build failure. */
static int
build_run(const char *driver, const char *src, const char *stem)
{
char cmd[1024];
snprintf(cmd, sizeof cmd, "%s build -o '%s' '%s' 2>/dev/null",
driver, stem, src);
if (runwait(cmd) != 0) return -1;
return runwait(stem);
}
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], wdrv[1024];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
struct { const char *name; const char *drv; int gated; }
drivers[] = {
{ "cstage", cdrv, 0 },
{ "wwstage", wdrv, 1 },
{ NULL, NULL, 0 },
};
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
int pid = (int)getpid();
char src[80], stem[80];
for (int d = 0; drivers[d].name; d++) {
if (drivers[d].gated && access(drivers[d].drv, X_OK) != 0) {
fprintf(stderr, "m1union_run: skip %s (no %s)\n",
drivers[d].name, drivers[d].drv);
continue;
}
for (int i = 0; i < n; i++) {
total++;
snprintf(src, sizeof src, "/tmp/m1union_%d_%d_%d.ww",
pid, d, i);
snprintf(stem, sizeof stem, "/tmp/m1union_%d_%d_%d",
pid, d, i);
if (write_src(src, &rows[i]) != 0) { fail++; continue; }
int got = build_run(drivers[d].drv, src, stem);
if (got != rows[i].want_exit) {
fprintf(stderr, "m1union_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, rows[i].want_exit);
fail++;
}
char cmd[256];
snprintf(cmd, sizeof cmd, "rm -f '%s' '%s' '%s.s' "
"'%s.o' '%s.combined.ww'", src, stem, stem, stem,
stem);
runwait(cmd);
}
}
/* rule-10 byte-id gate on the all_arms cross-module match. */
if (access(wdrv, X_OK) == 0) {
total++;
const struct row *r = &rows[n - 1]; /* all_arms */
char csrc[80], cstem[80], cs[96], ws[96], cmd[512];
snprintf(csrc, sizeof csrc, "/tmp/m1union_bid_%d.ww", pid);
snprintf(cstem, sizeof cstem, "/tmp/m1union_bid_c_%d", pid);
snprintf(cs, sizeof cs, "%s.s", cstem);
char wstem[80];
snprintf(wstem, sizeof wstem, "/tmp/m1union_bid_w_%d", pid);
snprintf(ws, sizeof ws, "%s.s", wstem);
if (write_src(csrc, r) == 0) {
snprintf(cmd, sizeof cmd, "%s build -o '%s' '%s' "
"2>/dev/null", cdrv, cstem, csrc);
int cb = runwait(cmd);
snprintf(cmd, sizeof cmd, "%s build -o '%s' '%s' "
"2>/dev/null", wdrv, wstem, csrc);
int wb = runwait(cmd);
if (cb != 0 || wb != 0) {
fprintf(stderr, "m1union_run: byte-id build "
"failed (cstage=%d wwstage=%d)\n", cb, wb);
fail++;
} else {
snprintf(cmd, sizeof cmd, "cmp -s '%s' '%s'",
cs, ws);
if (runwait(cmd) != 0) {
fprintf(stderr, "m1union_run: cstage.s "
"!= wwstage.s (byte-id break)\n");
fail++;
}
}
}
snprintf(cmd, sizeof cmd, "rm -f '%s' '%s'* '%s'*", csrc,
cstem, wstem);
runwait(cmd);
}
if (fail) {
fprintf(stderr, "m1union_run: %d/%d failed\n", fail, total);
return 1;
}
printf("m1union_run: %d/%d ok\n", total, total);
return 0;
}