Files
ww/test/wcc/989_m1usehint_run.c
Hojun-Cho e9c11cb5ae wcc/ww: module-scope the cgen mangle-hint (#40)
use_hint/usehint were unit-global first-leaf-match: two directory-
packages exporting the same fn leaf, each imported by a different module
aliasing the same bareword, mis-routed every qualified call to whichever
use was collected first. Identically wrong on both stages (byte-id-green
#263-class). Key the hint on (owner-module, alias) and prefer cur_mod,
mirroring the checker's use_path curmod-preference (55f54fb).

989_m1usehint_run: two same-leaf pick() across a.math/b.math, each
module's call routes to its own import (111/222) + cs.s==ww.s.
2026-06-15 19:12:15 +09:00

256 lines
7.6 KiB
C

/*
* 989_m1usehint_run — M1 (#40): the cgen mangle-hint must be MODULE-scoped,
* not unit-global. When two directory-packages in the same compilation unit
* export the same leaf fn (a.math and b.math both `export fn pick`), and two
* referencing modules each `import <X>.math` aliasing `math`, every
* `math.pick()` call must mangle to the package its OWN module imported —
* not to whichever `use` the file-global hint collected first.
*
* Pre-#40 the hint (use_hint / usehint) was file-global first-match: both
* modules' `math.pick()` resolved to the same package, so one call landed
* the wrong body and returned the wrong value — IDENTICALLY on both stages
* (a byte-id-green #263-class miscompile). Gate-visible: each module's call
* has a distinct expected value, so a mis-route changes the runtime exit.
*
* row | proves
* -----------------+--------------------------------------------------
* leaf_collision | two same-leaf exported fns; each module's qualified
* | call mangles to its OWN import (111 / 222) -> 0
* collision_diff | same, with mismatched signatures — the checker's
* | curmod-preferenced resolve agrees with the hint
* | (pick(int) vs pick()) -> 0
*
* Plus a byte-id gate: cstage.s == wwstage.s on the leaf_collision unit
* (rule-10).
*/
#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;
}
struct file { const char *path; const char *content; };
struct row {
const char *label;
struct file files[6]; /* {NULL,NULL}-terminated package files */
const char *root; /* root main.ww content */
int want_exit;
};
static const struct row rows[] = {
{ "leaf_collision",
{ { "a/math/math.ww",
"package math;\n"
"export fn pick() int = { return 111; };\n" },
{ "b/math/math.ww",
"package math;\n"
"export fn pick() int = { return 222; };\n" },
{ "one/one.ww",
"package one;\n"
"import a.math;\n"
"export fn getone() int = { return math.pick(); };\n" },
{ "two/two.ww",
"package two;\n"
"import b.math;\n"
"export fn gettwo() int = { return math.pick(); };\n" },
{ NULL, NULL } },
"package main;\n"
"import one;\n"
"import two;\n"
"export fn main() int = {\n"
" if (one.getone() != 111) { return 1; };\n"
" if (two.gettwo() != 222) { return 2; };\n"
" return 0;\n"
"};\n",
0 },
{ "collision_diff",
{ { "a/math/math.ww",
"package math;\n"
"export fn pick(x: int) int = { return x + 100; };\n" },
{ "b/math/math.ww",
"package math;\n"
"export fn pick() int = { return 222; };\n" },
{ "one/one.ww",
"package one;\n"
"import a.math;\n"
"export fn getone() int = { return math.pick(11); };\n" },
{ "two/two.ww",
"package two;\n"
"import b.math;\n"
"export fn gettwo() int = { return math.pick(); };\n" },
{ NULL, NULL } },
"package main;\n"
"import one;\n"
"import two;\n"
"export fn main() int = {\n"
" if (one.getone() != 111) { return 1; };\n"
" if (two.gettwo() != 222) { return 2; };\n"
" return 0;\n"
"};\n",
0 },
};
/* 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);
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;
}
/* layout — lay out the row's package tree + root under `dir`. */
static int
layout(const char *dir, const struct row *r)
{
char cmd[1024];
snprintf(cmd, sizeof cmd, "rm -rf '%s' && mkdir -p '%s'", dir, dir);
if (runwait(cmd) != 0) return -1;
for (int k = 0; r->files[k].path; k++)
if (write_file(dir, r->files[k].path, r->files[k].content) != 0)
return -1;
return write_file(dir, "main.ww", r->root);
}
/* run_build — build+run the row's 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/m1usehint_%d_%d", getpid(), i);
if (layout(dir, r) != 0) return -2;
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;
}
/* byteid — build the leaf_collision unit with both drivers and cmp the
* emitted combined .s (rule-10). Returns 0 on byte-identical, nonzero else. */
static int
byteid(const char *cdrv, const char *wdrv, const struct row *r)
{
char dir[64], cmd[2048];
char cs[96], ws[96];
snprintf(dir, sizeof dir, "/tmp/m1usehint_bid_%d", getpid());
if (layout(dir, r) != 0) return -1;
snprintf(cs, sizeof cs, "/tmp/m1usehint_c_%d.s", getpid());
snprintf(ws, sizeof ws, "/tmp/m1usehint_w_%d.s", getpid());
int rc = -1;
snprintf(cmd, sizeof cmd, "cd '%s' && %s build -o '/tmp/m1usehint_c_%d' "
"main.ww 2>/dev/null", dir, cdrv, getpid());
int cb = runwait(cmd);
snprintf(cmd, sizeof cmd, "cd '%s' && %s build -o '/tmp/m1usehint_w_%d' "
"main.ww 2>/dev/null", dir, wdrv, getpid());
int wb = runwait(cmd);
if (cb == 0 && wb == 0) {
snprintf(cmd, sizeof cmd, "cmp -s '%s' '%s'", cs, ws);
rc = runwait(cmd);
} else {
fprintf(stderr, "m1usehint_run: byte-id build failed "
"(cstage=%d wwstage=%d)\n", cb, wb);
}
snprintf(cmd, sizeof cmd, "rm -rf '%s' '/tmp/m1usehint_c_%d'* "
"'/tmp/m1usehint_w_%d'*", dir, getpid(), getpid());
runwait(cmd);
return rc;
}
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, "m1usehint_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, "m1usehint_run[%s][%s]: exit=%d "
"want=%d\n", drivers[d].name, rows[i].label,
got, rows[i].want_exit);
fail++;
}
}
}
/* rule-10 byte-id gate on the leaf_collision unit. */
if (access(wdrv, X_OK) == 0) {
total++;
if (byteid(cdrv, wdrv, &rows[0]) != 0) {
fprintf(stderr, "m1usehint_run: cstage.s != wwstage.s "
"(byte-id break)\n");
fail++;
}
}
if (fail) {
fprintf(stderr, "m1usehint_run: %d/%d failed\n", fail, total);
return 1;
}
printf("m1usehint_run: %d/%d ok\n", total, total);
return 0;
}