Files
ww/test/wcc/779_log_vstream_run.c
Hojun-Cho 3d44f742a0 wwstage: expand spread variants in match check + size them off flattened members (#209)
The wwstage checker walked a match's raw AST variant list and never expanded a ...inner spread variant, so it rejected fmt's match over field = (...formattable | *mods) ('not a variant of scrutinee'). cstage's resolve_type flattens the spread at type-build. Mirror that in the AST exhaustiveness walk (casevariantin + a recursive checkvariantcovered): when a variant resolves to N_TTAGGED via a spread, recurse into its members. Additive + spread-gated -- typeeqast / casevariantpairmatch (#13) / casecovers untouched, so non-spread matches and 990-997 byte-id are unaffected. Also size a spread N_TTAGGED off each flattened member (mirror cstage check.c), dropping the inner union tag word (field 40B to 32B). Closes the #209 CHECKER reject; full fmt-byte-id still awaits cgen cluster #226 (io.read nominal-remap) + #227 (spread-widen ABI), so fmt tests stay cstage-only with retargeted comments. Adds test 792; regenerates w6c/wwdump combined.ww.
2026-05-31 20:31:40 +09:00

361 lines
12 KiB
C

/*
* 779_log_vstream_run — project #94 fold-eFinal sentinel. Pins the
* single-surface lib/log: module-static stderrsink_ctx_g with
* `vt: io.vtable` as the first field for the intrusive cast, the 10
* exported variants of log.ww's surface (new / lprintln / println /
* lprintfln / printfln / lfatal / fatal / lfatalf / fatalf /
* setlogger), a logger vtable + stdlogger over an io.stream sink, and
* lazy ensureinit wiring the stderr default + silent / default /
* global *logger globals. The fold-eFinal FLIP collapsed the dual
* surface into this one Hare-shaped surface (bare names, no suffix).
*
* Each row imports os + log + memio + io. log itself imports fmt,
* so the formattable / field types flow through transitively.
*
* Cstage-only per row (no STAGE_WW, no byte_id). #209 (the wwstage
* checker bail on match-arm-over-formattable when fmt is imported
* transitively — which lib/log does for the logger vtable signatures +
* the fmt.fprint / fprintf dispatch in stdprintln / stdprintfln) is now
* CLOSED; wwstage compiles fmt/log. The residual blocker is the
* pre-existing cgen cluster #226 (io.read error-remap nominal-identity)
* + #227 (spread-union widen/return-ABI), surfaced once fmt codegens
* under wwstage. Existing 971 logtest runs the @test fixture under
* cstage `ww run` only. 995 self-rebuild dodges it because no selfhost
* cmd imports log transitively. Byte-id graduates when #226 (+#227) land.
*
* row | what it pins
* --------------------------+--------------------------------------
* println_v_default_stderr | log.println through the [[default]]
* | global, routed to fd 2 via the
* | module-static stderrsink_ctx_g. Asserts
* | ensureinit wires + dispatches without
* | crashing; stderr capture is left to the
* | shell runner. Pins the full chain:
* | println → lprintln(global) →
* | (*logger).println → stdprintln →
* | fmt.fprint + io.write("\n").
* printfln_v_default_stderr | Same chain as row 1 but through the
* | format-string slot (printfln →
* | lprintfln(global) → stdprintfln
* | → fmt.fprintf + io.write("\n")).
* | Pins the {n}-placeholder dispatch
* | through vformatfield reaches the
* | default sink.
* lprintln_v_custom_sink | memio.fixed-backed stdlogger;
* | log.new wires the vtable + sink,
* | log.lprintln dispatches "hi" + 7i64
* | through stdprintln. Asserts the
* | exact bytes ("hi 7\n") land in the
* | caller's buffer (fixed borrows
* | the slice, so the bytes are visible
* | back through buf[]).
* branched_lprintln | branched callee per #105: two distinct
* | stdlogger sinks runtime-selected via
* | *logger pointer. lprintln dispatches
* | through the picked sink only; the
* | other stays empty. Catches a constant-
* | fold mistake in the fn-ptr dispatch
* | (mirror of 778's branched_bufio_wrap).
*
* DEFERRALS / RELATED (NOT addressed by this row):
*
* - #206 (bare &fn → (*alias|void)): 2 cast sites at ensureinit
* in lib/log/log.ww; the explicit casts are KEPT (ken/#214 —
* cast-drop is a separate deferred payoff gated on #214).
*
* - #173 (TRY-on-tagged-return both-stages broken): stderrwrite
* constructs nomem and widens to io.error explicitly rather than
* using `os.trywrite(...)?`; same shape memio.ww + fmt.ww +
* bufio.ww adopt.
*
* - #226 (io.read error-remap nominal-identity) + #227 (spread-union
* widen/return-ABI): every row is STAGE_CS-only; byte-id deferred
* until they land. #209 (the checker bail) is already closed.
*
* BOOTSTRAP-EMBED CHECK: log is NOT embedded in any selfhost
* combined.ww (grep `package log\|import log` returns empty), so the
* fold-eFinal log collapse does not require a Makefile regen (#110);
* only lib/log/ + lib/fmt/ test paths see the surface. 990-997
* byte-id gates stay green by virtue of log being test-only.
*
* GATE POLARITY: must stay GREEN. A red here means lib/log
* regressed, the module-static stderrsink_ctx_g zero-init or chained
* vt-field assigns regressed, the intrusive io.stream→*stderrsink_ctx
* cast miscomputed offsets, ensureinit's one-shot guard broke, or
* the fn-ptr dispatch through logger.println / printfln stopped
* resolving.
*/
#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;
};
static const struct row rows[] = {
{ "println_v_default_stderr",
"package main;\n"
"import os;\n"
"import log;\n"
"export fn main() i32 = {\n"
" log.println(\"hello\");\n"
" return 42;\n"
"};\n",
42,
STAGE_CS, 0 },
{ "printfln_v_default_stderr",
"package main;\n"
"import os;\n"
"import log;\n"
"export fn main() i32 = {\n"
" log.printfln(\"v={}\", 7i64);\n"
" return 43;\n"
"};\n",
43,
STAGE_CS, 0 },
{ "lprintln_v_custom_sink",
"package main;\n"
"import os;\n"
"import log;\n"
"import memio;\n"
"import io;\n"
"export fn main() i32 = {\n"
" let buf: [16]u8;\n"
" let st: memio.stream = memio.fixed(buf[0:16]);\n"
" let vs: io.stream = &st.vt;\n"
" let sl: log.stdlogger = log.new(vs);\n"
" log.lprintln(&sl.logger, \"hi\", 7i64);\n"
" if (buf[0] != 104u8) { return 91; };\n"
" if (buf[1] != 105u8) { return 92; };\n"
" if (buf[2] != 32u8) { return 93; };\n"
" if (buf[3] != 55u8) { return 94; };\n"
" if (buf[4] != 10u8) { return 95; };\n"
" return 44;\n"
"};\n",
44,
STAGE_CS, 0 },
{ "branched_lprintln",
"package main;\n"
"import os;\n"
"import log;\n"
"import memio;\n"
"import io;\n"
"export fn main() i32 = {\n"
" let bufA: [16]u8;\n"
" let bufB: [16]u8;\n"
" let stA: memio.stream = memio.fixed(bufA[0:16]);\n"
" let vsA: io.stream = &stA.vt;\n"
" let stB: memio.stream = memio.fixed(bufB[0:16]);\n"
" let vsB: io.stream = &stB.vt;\n"
" let slA: log.stdlogger = log.new(vsA);\n"
" let slB: log.stdlogger = log.new(vsB);\n"
" let sel: i32 = 1;\n"
" let chosen: *log.logger = &slA.logger;\n"
" if (sel == 0) { chosen = &slB.logger; };\n"
" log.lprintln(chosen, \"pick\");\n"
" if (bufA[0] != 112u8) { return 91; };\n"
" if (bufA[1] != 105u8) { return 92; };\n"
" if (bufA[2] != 99u8) { return 93; };\n"
" if (bufA[3] != 107u8) { return 94; };\n"
" if (bufA[4] != 10u8) { return 95; };\n"
" if (bufB[0] != 0u8) { return 96; };\n"
" return 45;\n"
"};\n",
45,
STAGE_CS, 0 },
};
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;
}
/* Per-row tmpdir cleanup. ww_ww writes intermediates next to the
* source (filed task #15), so each row's build leaves
* <src>.{combined.ww,s,o} + bare exe alongside. Sweep all then
* rmdir. Mirror of 778's cleanup_tmp. */
static void
cleanup_tmp(const char *tmpdir, const char *base)
{
char p[640];
snprintf(p, sizeof p, "%s/%s.ww", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.s", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.o", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s.combined.ww", tmpdir, base); unlink(p);
snprintf(p, sizeof p, "%s/%s", tmpdir, base); unlink(p);
rmdir(tmpdir);
}
static int
build_via_driver(const char *driver, const char *tmpdir, const char *cwd,
const char *src)
{
char cmd[2048];
snprintf(cmd, sizeof cmd,
"cd %s && timeout 180 %s build -I %s/lib %s 2>/dev/null",
tmpdir, driver, cwd, src);
return runwait(cmd);
}
static int
run_row(const char *driver, const char *cwd, const struct row *r, int seq)
{
char tmpdir[256], src[512], base[64], outbin[768];
snprintf(tmpdir, sizeof tmpdir, "/tmp/lvs_%d_d_%d", getpid(), seq);
snprintf(base, sizeof base, "main779");
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;
int br = build_via_driver(driver, tmpdir, cwd, src);
if (br == 0) {
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
/* Suppress stderr for rows 1/2 — the default-sink probes
* write through fd 2; don't pollute the test runner's
* stderr. */
char runcmd[1024];
snprintf(runcmd, sizeof runcmd, "%s 2>/dev/null", outbin);
rc = runwait(runcmd);
} else {
rc = -1;
}
cleanup_tmp(tmpdir, base);
return rc;
}
/* asm_byte_identical — diff cstage vs wwstage .s. Parallel trees so
* ww_ww writing intermediates next to the source doesn't clobber the
* cstage .s (CLAUDE.md rule 14 phase split). Mirror of 778's. Unused
* for fold-e5 — every row is STAGE_CS-only per #226/#227 (#209, the
* checker bail, is closed) — but kept scaffolded for when those cgen
* folds land and byte-id graduates. */
static int
asm_byte_identical(const char *cdrv, const char *wdrv, const char *cwd,
const struct row *r, int seq)
{
char src[512], tdc[256], tdw[256], base[64], cs[512], ws[512];
snprintf(tdc, sizeof tdc, "/tmp/lvs_%d_c_%d", getpid(), seq);
snprintf(tdw, sizeof tdw, "/tmp/lvs_%d_w_%d", getpid(), seq);
snprintf(base, sizeof base, "main779");
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, cwd, src) != 0) goto out;
snprintf(cs, sizeof cs, "%s/%s.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, cwd, src) != 0) goto out;
snprintf(ws, sizeof ws, "%s/%s.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;
int wwpresent = (access(wdrv, X_OK) == 0);
int seq = 0;
for (int i = 0; i < n; i++) {
if (rows[i].stage_mask & STAGE_CS) {
total++;
int got = run_row(cdrv, cwd, &rows[i], seq++);
if (got != rows[i].want_exit) {
fprintf(stderr,
"log_vstream_run[cs][%s]: exit=%d want=%d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
}
if (wwpresent && (rows[i].stage_mask & STAGE_WW)) {
total++;
int got = run_row(wdrv, cwd, &rows[i], seq++);
if (got != rows[i].want_exit) {
fprintf(stderr,
"log_vstream_run[ww][%s]: exit=%d want=%d\n",
rows[i].label, got, rows[i].want_exit);
fail++;
}
if (rows[i].byte_id) {
total++;
if (asm_byte_identical(cdrv, wdrv, cwd, &rows[i], seq++) != 0) {
fprintf(stderr,
"log_vstream_run[byte-id][%s]: cstage vs wwstage asm differs\n",
rows[i].label);
fail++;
}
}
}
}
if (!wwpresent)
fprintf(stderr, "log_vstream_run: skip wwstage (no %s)\n", wdrv);
if (fail) {
fprintf(stderr, "log_vstream_run: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("log_vstream_run: %d/%d ok\n", total, total);
return 0;
}