Files
ww/test/wcc/989_m1union_run.c
Hojun-Cho f308818b4b 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).
2026-06-15 17:37:18 +09:00

226 lines
6.6 KiB
C

/*
* 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;
}