Files
ww/test/wcc/789_named_ptr_alias_variant_widen.c
Hojun-Cho a95a7a316b test/wcc: carrier ownership repair and driver-contract adaptation
Every surviving carrier now owns its artifacts: checked mkdir/mkdtemp/
fopen acquisition, one all-exit cleanup funnel per carrier, ENOENT-
tolerant checked unlinks, exact-path deletion (rm -rf only for an
owned pid-keyed dir or a .sepwork beneath one), and cleanup failure
fails a passing carrier without overwriting its diagnostic. In the
same pass the carriers adapt to the driver contract this branch lands:
--sep and WW_PKGCACHE are gone, -S and the /tmp/ww_run_<pid> scratch
contract are asserted, and rows whose runtime or reject coverage moved
to test/wcc/data fixtures or test/lang @test owners are trimmed to the
byte/artifact/diagnostic observations only they can make.

Repair and adaptation ride together because most files interleave both
in the same hunks; splitting would manufacture intermediate carrier
states that never existed and cannot run against either driver.
2026-08-07 23:21:04 +09:00

238 lines
7.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) | |
*
* GATE POLARITY: must stay GREEN. Red on handle_widen means the
* NAMED-ptr-alias-variant widen regressed (wrong tag).
*
* The symmetric degenerate_ambig rejection now lives in
* test/wcc/data/r78_named_ptr_alias_degenerate_ambig/case.ww, where the
* shared C/WW diagnostic pins the >=2 ambiguity guard directly. This
* wrapper retains the positive runtime and assembly-byte-identity evidence.
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.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;
};
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 },
};
/* nonzero on any cleanup failure; ENOENT tolerated because legs that
* fail early never create the later artifacts. */
static int
cleanup_tmp(const char *tmpdir, const char *base)
{
char p[1024];
int bad = 0;
snprintf(p, sizeof p, "%s/%s", tmpdir, base);
if (unlink(p) != 0 && errno != ENOENT) { perror(p); bad = 1; }
snprintf(p, sizeof p, "%s/%s.ww", tmpdir, base);
if (unlink(p) != 0 && errno != ENOENT) { perror(p); bad = 1; }
snprintf(p, sizeof p, "rm -rf %s/%s.sepwork", tmpdir, base);
if (runwait(p) != 0) {
fprintf(stderr, "cleanup failed: %s\n", p);
bad = 1;
}
snprintf(p, sizeof p, "%s/%s.o", tmpdir, base);
if (unlink(p) != 0 && errno != ENOENT) { perror(p); bad = 1; }
snprintf(p, sizeof p, "%s/%s.combined.ww", tmpdir, base);
if (unlink(p) != 0 && errno != ENOENT) { perror(p); bad = 1; }
if (rmdir(tmpdir) != 0) { perror(tmpdir); bad = 1; }
return bad;
}
static int
write_source(const char *path, const char *src)
{
FILE *f = fopen(path, "wb");
if (!f) return -1;
if (fputs(src, f) == EOF) {
fclose(f);
return -1;
}
if (fclose(f) != 0) return -1;
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
* all outputs stay under tmpdir. */
snprintf(cmd, sizeof cmd,
"cd %s && b='%s'; timeout 180 %s build -S "
"-o \"${b%%.ww}\" \"$b\" 2>/dev/null",
tmpdir, src, driver);
return runwait(cmd);
}
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");
int rc = -1, tdw_owned = 0, cleanfail = 0;
if (mkdir(tdc, 0755) != 0) {
perror(tdc);
return -1;
}
if (mkdir(tdw, 0755) != 0) {
perror(tdw);
goto out;
}
tdw_owned = 1;
snprintf(src, sizeof src, "%s/%s.ww", tdc, base);
if (write_source(src, r->src) != 0) goto out;
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:
cleanfail = cleanup_tmp(tdc, base);
if (tdw_owned && cleanup_tmp(tdw, base) != 0)
cleanfail = 1;
/* a leaked tree fails an otherwise-green row; a real diff result
* (rc == -1) is never overwritten (110's cleanfail shape). */
if (cleanfail && rc == 0)
rc = -1;
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 (wwpresent && 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;
}