Files
ww/test/wcc/989_m1union_run.c
Hojun-Cho eb6083e54a test: retarget Pattern-B gates to sep .sepwork layout; 915 off private global (M4 E3, #93)
The driver flip moves build artifacts from next-to-source <stem>.s to a
.sepwork/ scratch dir. 29 Pattern-B gates now build `ww build --sep -o <stem>`
and read <stem>.sepwork/__root.s (multi-package gates concat all
<stem>.sepwork/*.s, since cross-package labels live in per-package .s).
All intermediates redirect to /tmp (WW_PKGCACHE + -o), so the corpus runs
parallel-safe with no source-tree pollution. Tests pass now (--sep is live)
and survive the flip.

915 additionally retargeted off strconv's PRIVATE left_shift_table (a let,
not export) — which separate compilation correctly hides — onto a test-local
package that exports its own probe table (#96). The combined path only linked
it via a single-unit private leak; encapsulation is now honored under sep.

Test-only; all 5 *_ww binaries HOLD. 989_m1mangle_run deferred (blocked by
#99, imported-package fn main mangling under sep).
2026-06-18 11:16:56 +09:00

241 lines
7.5 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.
* #93 sep layout: `--sep -o <stem>` emits per-unit asm under
* <stem>.sepwork/ (root in __root.s, encoding.utf8 in encoding.utf8.s); the
* binary is still <stem>. WW_PKGCACHE is pinned beside <stem>. */
static int
build_run(const char *driver, const char *src, const char *stem)
{
char cmd[1024];
snprintf(cmd, sizeof cmd,
"WW_PKGCACHE='%s.pkgc' timeout 180 %s build --sep -o '%s' '%s' "
"2>/dev/null", stem, 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];
/* #93: drop the sep scratch + pinned pkgcache too. */
snprintf(cmd, sizeof cmd, "rm -rf '%s' '%s' "
"'%s.sepwork' '%s.pkgc'", src, 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], cmd[512];
snprintf(csrc, sizeof csrc, "/tmp/m1union_bid_%d.ww", pid);
snprintf(cstem, sizeof cstem, "/tmp/m1union_bid_c_%d", pid);
char wstem[80];
snprintf(wstem, sizeof wstem, "/tmp/m1union_bid_w_%d", pid);
if (write_src(csrc, r) == 0) {
/* #93 sep layout: each stage emits per-unit asm under
* <stem>.sepwork/ (__root.s + encoding.utf8.s); the
* rule-10 byte-id gate becomes a recursive diff of the
* two sepwork trees. */
snprintf(cmd, sizeof cmd,
"WW_PKGCACHE='%s.pkgc' %s build --sep -o '%s' '%s' "
"2>/dev/null", cstem, cdrv, cstem, csrc);
int cb = runwait(cmd);
snprintf(cmd, sizeof cmd,
"WW_PKGCACHE='%s.pkgc' %s build --sep -o '%s' '%s' "
"2>/dev/null", wstem, 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 {
/* Concat each tree's per-unit *.s (sorted glob
* order is deterministic) and cmp — sidesteps a
* stray <pkg>.md5tmp the wwstage drops in the
* sep dir, which a full `diff -r` would flag. */
snprintf(cmd, sizeof cmd,
"cat '%s.sepwork/'*.s > '%s.scat' && "
"cat '%s.sepwork/'*.s > '%s.scat' && "
"cmp -s '%s.scat' '%s.scat'",
cstem, cstem, wstem, wstem, cstem, wstem);
if (runwait(cmd) != 0) {
fprintf(stderr, "m1union_run: cstage.s "
"!= wwstage.s (byte-id break)\n");
fail++;
}
}
}
snprintf(cmd, sizeof cmd, "rm -rf '%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;
}