/* * 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) * * Runtime ownership is now r989_m1union_*; this wrapper retains all_arms * solely for cstage.s == wwstage.s across the separate-package build. */ #include #include #include #include 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[] = { /* Runtime rows moved to r989_m1union_*; retain byte-id source only. */ { "all_arms", "export fn main() int = {\n" "\tlet g: []u8 = [65u8];\n" "\tlet dg = utf8.decode(g);\n" "\tlet a = classify(&dg);\n" "\tlet b = classify(&dg);\n" "\tlet bad: []u8 = [255u8];\n" "\tlet db = utf8.decode(bad);\n" "\tlet c = classify(&db);\n" "\tlet tr: []u8 = [195u8];\n" "\tlet dt = utf8.decode(tr);\n" "\tlet e = classify(&dt);\n" "\treturn a * 1000 + b * 100 + c * 10 + e;\n" "};\n", 219 }, }; /* write_src — PRELUDE + row body into . */ 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; } 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); int n = (int)(sizeof rows / sizeof rows[0]); int total = 0, fail = 0; int pid = (int)getpid(); /* 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) { /* a silent skip here would print "1/1 ok" having * asserted nothing; nothing was created, so no * cleanup is owed either. */ fprintf(stderr, "m1union_run: cannot write %s\n", csrc); fail++; } else { /* #93 sep layout: each stage emits per-unit asm under * .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, "%s build -S -o '%s' '%s' " "2>/dev/null", cdrv, cstem, csrc); int cb = runwait(cmd); snprintf(cmd, sizeof cmd, "%s build -S -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 { /* Concat each tree's per-unit *.s (sorted glob * order is deterministic) and cmp — sidesteps a * stray .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++; } } /* exact owned paths only (`build -S` writes solely * under .sepwork/, plus the .scat concats) — * no prefix glob; a failed rm must fail the carrier, * not leak silently. */ snprintf(cmd, sizeof cmd, "rm -rf '%s' '%s.scat' '%s.sepwork' " "'%s.scat' '%s.sepwork'", csrc, cstem, cstem, wstem, wstem); if (runwait(cmd) != 0) { fprintf(stderr, "m1union_run: cleanup rm failed\n"); fail++; } } } else { fprintf(stderr, "m1union_run: skip byte-id (no ww_ww)\n"); } 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; }