Files
ww/test/wcc/789_named_ptr_alias_variant_widen.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

295 lines
9.5 KiB
C

/*
* 789_named_ptr_alias_variant_widen — project #15 close. Pins that
* widening a BARE pointer into a tagged-union variant that is a NAMED
* ALIAS of that pointer type computes the correct variant tag, on BOTH
* stages byte-identically (rule-10).
*
* THE BUG (cgen, BOTH stages): cg_variant_match (cmd/w6c/cgen.c) /
* cgvariantmatch (selfhost/cmd/wcc/cgenutil.ww), in the "exactly one
* side NAMED" branch, only matched TAGGED-vs-TAGGED and otherwise
* returned no-match. So widening a bare `*vtable` into the `stream`
* (= *vtable) variant of `handle = (file | stream)` matched NO variant
* → the tag-selection (cg_tag_for_variant / flatvariantidxt) defaulted
* to tag 0 (= file). The eFinal `io.write(&cgoutstream.vt, buf)` in the
* compiler's own emit path (cgen.ww:745, `&cgoutstream.vt` a bare
* *io.vtable widening into io.handle) then dispatched the fd arm,
* wrote asm to a garbage fd, and the w6c_ww it built miscompiled every
* program (#15 — a "scale" red herring; it is per-call deterministic).
*
* THE FIX (#15 + drew proviso): the tag-selection does exact-match
* first (unchanged); only when NO variant exact-matches does it
* structurally match a BARE source against a NAMED-alias variant. A
* NAMED source still binds its own NAMED variant (a bare `i64` into
* `(i64 | oserror)` picks the exact `i64`, not the oserror alias). The
* structural fallback is guarded the way #218 guards its nested-widen
* site: a bare source matching >=2 NAMED variants is ambiguous without
* nominal layout → LOUD hard-error, never a silent first-pick.
*
* row | shape | exit | byte-id
* -------------------+-----------------------------------------+------+--------
* handle_widen | bare *vtable widened into the `stream` | 42 | cs==ww
* | (= *vtable) variant of (file|stream), | |
* | summed 10x via the stream match arm | |
* | (pre-fix took the file arm → 86) | |
* degenerate_ambig | (sa | sb), both = *vtable; a bare | both | (reject
* | *vtable widen matches BOTH NAMED |reject| net)
* | variants → guard must LOUD hard-error | |
* | on BOTH stages (drew proviso lock) | |
*
* GATE POLARITY: must stay GREEN. Red on handle_widen means the
* NAMED-ptr-alias-variant widen regressed (wrong tag); red on
* degenerate_ambig means the >=2 ambiguity guard stopped firing.
*/
#include <stdio.h>
#include <stdlib.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;
}
#define STAGE_CS 1
#define STAGE_WW 2
struct row {
const char *label;
const char *src;
int want_exit;
int stage_mask;
int byte_id;
int expect_reject; /* 1 = BOTH stages must hard-error */
};
static const struct row rows[] = {
/* POSITIVE byte-id + runtime: bare *vtable widened into the NAMED
* `stream` variant — the #15 fix. */
{ "handle_widen",
"package main;\n"
"type vtable = struct { x: i32 };\n"
"type stream = *vtable;\n"
"type file = i32;\n"
"type handle = (file | stream);\n"
"fn hwrite(h: handle, n: u64) u64 = {\n"
" match (h) {\n"
" case file => return 111u64;\n"
" case let s: stream => return n;\n"
" };\n"
"};\n"
"fn emit(v: *vtable, n: u64) u64 = { return hwrite(v, n); };\n"
"export fn main() i32 = {\n"
" let vt: vtable; vt.x = 0;\n"
" let sum: u64 = 0u64; let i: i32 = 0;\n"
" for (i < 10) { sum = sum + emit(&vt, 4u64); i += 1; };\n"
" if (sum == 40u64) { return 42; };\n"
" return (sum: i32);\n"
"};\n",
42, STAGE_CS | STAGE_WW, 1, 0 },
/* GUARD lock (drew proviso): a bare *vtable widened into a union of
* TWO NAMED *vtable aliases is ambiguous → BOTH stages hard-error. */
{ "degenerate_ambig",
"package main;\n"
"type vtable = struct { x: i32 };\n"
"type sa = *vtable;\n"
"type sb = *vtable;\n"
"type h = (sa | sb);\n"
"fn w(x: h) i32 = { match (x) { case sa => return 1; case sb => return 2; }; };\n"
"fn emit(v: *vtable) i32 = { return w(v); };\n"
"export fn main() i32 = { let vt: vtable; vt.x = 0; return emit(&vt); };\n",
0, STAGE_CS | STAGE_WW, 0, 1 },
};
static void
cleanup_tmp(const char *tmpdir, const char *base)
{
char p[1024];
snprintf(p, sizeof p, "%s/%s", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.ww", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "rm -rf %s/%s.sepwork %s/pkgc", tmpdir, base, tmpdir);
if (system(p)) {} /* best-effort */
snprintf(p, sizeof p, "%s/%s.o", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.combined.ww", tmpdir, base); unlink(p);
rmdir(tmpdir);
}
static int
write_source(const char *path, const char *src)
{
FILE *f = fopen(path, "wb");
if (!f) return -1;
fputs(src, f);
fclose(f);
return 0;
}
static int
build_via_driver(const char *driver, const char *tmpdir, const char *src)
{
char cmd[2048];
/* #93 sep layout: emit asm to <src-stem>.sepwork/__root.s; pin
* WW_PKGCACHE under tmpdir so out/.pkgcache is untouched. */
snprintf(cmd, sizeof cmd,
"cd %s && b='%s'; WW_PKGCACHE=%s/pkgc timeout 180 %s build --sep "
"-o \"${b%%.ww}\" \"$b\" 2>/dev/null",
tmpdir, src, tmpdir, driver);
return runwait(cmd);
}
static int
run_row(const char *driver, const struct row *r, int seq)
{
char tmpdir[256], src[512], base[64], outbin[768];
snprintf(tmpdir, sizeof tmpdir, "/tmp/npav_%d_d_%d", getpid(), seq);
snprintf(base, sizeof base, "main789");
snprintf(src, sizeof src, "%s/%s.ww", tmpdir, base);
mkdir(tmpdir, 0755);
if (write_source(src, r->src) != 0) { cleanup_tmp(tmpdir, base); return -1; }
int rc;
if (build_via_driver(driver, tmpdir, src) == 0) {
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
rc = runwait(outbin);
} else {
rc = -1;
}
cleanup_tmp(tmpdir, base);
return rc;
}
/* build_fails — 1 iff the driver build FAILS (used by expect_reject). */
static int
build_fails(const char *driver, const struct row *r, int seq)
{
char tmpdir[256], src[512], base[64];
snprintf(tmpdir, sizeof tmpdir, "/tmp/npav_%d_x_%d", getpid(), seq);
snprintf(base, sizeof base, "main789");
snprintf(src, sizeof src, "%s/%s.ww", tmpdir, base);
mkdir(tmpdir, 0755);
if (write_source(src, r->src) != 0) { cleanup_tmp(tmpdir, base); return 0; }
int br = build_via_driver(driver, tmpdir, src);
cleanup_tmp(tmpdir, base);
return br != 0;
}
static int
asm_byte_identical(const char *cdrv, const char *wdrv, const struct row *r,
int seq)
{
char src[512], tdc[256], tdw[256], base[64], cs[512], ws[512];
snprintf(tdc, sizeof tdc, "/tmp/npav_%d_c_%d", getpid(), seq);
snprintf(tdw, sizeof tdw, "/tmp/npav_%d_w_%d", getpid(), seq);
snprintf(base, sizeof base, "main789");
mkdir(tdc, 0755);
mkdir(tdw, 0755);
snprintf(src, sizeof src, "%s/%s.ww", tdc, base);
if (write_source(src, r->src) != 0) { cleanup_tmp(tdc, base); cleanup_tmp(tdw, base); return -1; }
int rc = -1;
if (build_via_driver(cdrv, tdc, src) != 0) goto out;
snprintf(cs, sizeof cs, "%s/%s.sepwork/__root.s", tdc, base);
snprintf(src, sizeof src, "%s/%s.ww", tdw, base);
if (write_source(src, r->src) != 0) goto out;
if (build_via_driver(wdrv, tdw, src) != 0) goto out;
snprintf(ws, sizeof ws, "%s/%s.sepwork/__root.s", tdw, base);
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
if (fc && fw) {
rc = 0;
for (;;) {
int a = fgetc(fc);
int b = fgetc(fw);
if (a != b) { rc = -1; break; }
if (a == EOF) break;
}
}
if (fc) fclose(fc);
if (fw) fclose(fw);
out:
cleanup_tmp(tdc, base);
cleanup_tmp(tdw, base);
return rc;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
char absbin[512];
if (bin[0] != '/') {
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[640], wdrv[640];
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, seq = 0;
int wwpresent = (access(wdrv, X_OK) == 0);
for (int i = 0; i < n; i++) {
const struct row *r = &rows[i];
if (r->stage_mask & STAGE_CS) {
total++;
if (r->expect_reject) {
if (!build_fails(cdrv, r, seq++)) {
fprintf(stderr, "npav[cs][%s]: built but expected reject\n",
r->label);
fail++;
}
} else {
int got = run_row(cdrv, r, seq++);
if (got != r->want_exit) {
fprintf(stderr, "npav[cs][%s]: exit=%d want=%d\n",
r->label, got, r->want_exit);
fail++;
}
}
}
if (wwpresent && (r->stage_mask & STAGE_WW)) {
total++;
if (r->expect_reject) {
if (!build_fails(wdrv, r, seq++)) {
fprintf(stderr, "npav[ww][%s]: built but expected reject\n",
r->label);
fail++;
}
} else {
int got = run_row(wdrv, r, seq++);
if (got != r->want_exit) {
fprintf(stderr, "npav[ww][%s]: exit=%d want=%d\n",
r->label, got, r->want_exit);
fail++;
}
if (r->byte_id) {
total++;
if (asm_byte_identical(cdrv, wdrv, r, seq++) != 0) {
fprintf(stderr, "npav[byte-id][%s]: cstage vs wwstage asm differs\n",
r->label);
fail++;
}
}
}
}
}
if (fail) {
fprintf(stderr, "named_ptr_alias_variant_widen: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("named_ptr_alias_variant_widen: %d/%d ok\n", total, total);
return 0;
}