Files
ww/test/wcc/989_trycallcollide_run.c
Hojun-Cho ce3a25a0b4 test: contain sepwork scratch per-driver tmpdir, fix /tmp+in-repo leak (#8)
The wcc test drivers ran `ww build <bare-/tmp src>` with no -o, so the
compiler's <stem>.sepwork scratch landed beside the source and was never
cleaned: unbounded /tmp growth (2195 stale dirs observed) that fills tmpfs
and fabricates phantom test failures + silent harness aborts, and for
in-repo fixture builds leaked .sepwork into the tracked tree.

Each leaking build now writes its source + output inside a per-invocation
tmpdir, passes -o <tmpdir>/<stem> so the .sepwork lands inside it, and
rm -rf's the tmpdir on every exit path -- including fopen-fail and the
expected-fail reject builds (scratch is mkdir'd before the build can fail).
`ww run` and explicit-`-o`/byte-id helpers are left as-is; the 990/993
byte-id comparison logic is byte-for-byte unchanged.

Two items filed separately (this commit holds the no-Makefile / no-main.c
rail):
- #13: a stale <src>.s byte-id readback (749) silently no-ops since
  separate-compile emits .s to <ostem>.sepwork/__root.s; documented inline.
- #14: build-system Makefile recipes build selfhost/cmd/*/main.ww with no
  -o and leak main.sepwork in-tree (bounded, gitignored; own commit).

One concern -- sepwork leak hygiene -- across 228 drivers; uniform
transform applied per-file and two-round reviewed. make test: all 402
passed, zero net-new /tmp scratch, zero test-driven in-repo .sepwork.
2026-06-22 23:29:39 +09:00

175 lines
5.3 KiB
C

/*
* 989_trycallcollide_run (#11a, c3) — exprtypeoftry must prefer the current
* module's symbol when typing a `?`/`!` operand whose bare leaf collides with
* a same-leaf decl in a later module.
*
* THE BUG (review item #11, 2026-06-11): exprtypeoftry (check.ww ~5850 N_IDENT,
* ~5874 N_CALL bare-leaf callee) typed the try operand with bare scopelookup —
* no (name,module) preference. scopedefineinmodule PREPENDS, so a later
* module's same-leaf decl heads the chain and the operand is typed against ITS
* signature. For `op()?` the wrong op's error subset is then checked against
* the enclosing fn's return: a spurious "?: error variant not in enclosing
* return" rejects valid code (or, with a narrower foreign subset, silently
* skips the F8 multi-success reject). cstage types the operand via cexpr with
* cur_mod. THE FIX: both arms use scopelookupprefer(c.cur, c.curmod, ...),
* mirroring exprtype's own N_IDENT/N_CALL arms (:2897/:3336).
*
* GATING NOTE: this swap broke 995_self_rebuild once in a CONTAMINATED earlier
* attempt (bundled with a u64-guard-buggy scopelookuptype). Re-probed clean
* atop c1 (i32-correct scopelookuptype): repro closes AND 990-997/995 green.
*
* row | shape | run (cs==ww)
* -----------+---------------------------------------------+-------------
* callcollide| beta.user() does `op()?`; gamma also has | 42 (was ww
* | `op` with a wider error subset | build-fail)
* noclash | same, gamma.op renamed (no collision) | 42 (control)
*
* The N_DOT-callee module-keyed resolution stays task #51 (out of scope).
* Single-file multi-package source is the sanctioned shape (cmd/ww/main.c).
*/
#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;
}
struct row { const char *label; const char *src; int want; };
static const struct row rows[] = {
{ "callcollide",
"package beta;\n"
"export type berr = !void;\n"
"export fn op() (i32 | berr) = { return 5; };\n"
"export fn user() (i32 | berr) = {\n"
" let v: i32 = op()?;\n"
" return v + 37;\n"
"};\n"
"package gamma;\n"
"export type gerr1 = !void;\n"
"export type gerr2 = !u8;\n"
"export fn op() (i32 | gerr1 | gerr2) = { return 0; };\n"
"package main;\n"
"import beta;\n"
"import gamma;\n"
"fn main() int = {\n"
" match (beta.user()) {\n"
" case let n: i32 => return n: int;\n"
" case berr => return 99;\n"
" };\n"
"};\n",
42 },
{ "noclash",
"package beta;\n"
"export type berr = !void;\n"
"export fn op() (i32 | berr) = { return 5; };\n"
"export fn user() (i32 | berr) = {\n"
" let v: i32 = op()?;\n"
" return v + 37;\n"
"};\n"
"package gamma;\n"
"export type gerr1 = !void;\n"
"export fn other() (i32 | gerr1) = { return 0; };\n"
"package main;\n"
"import beta;\n"
"import gamma;\n"
"fn main() int = {\n"
" match (beta.user()) {\n"
" case let n: i32 => return n: int;\n"
" case berr => return 99;\n"
" };\n"
"};\n",
42 },
};
static int
run_build(const char *driver, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], cmd[1024], rmcmd[160];
snprintf(tmpdir, sizeof tmpdir, "/tmp/tcc_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/tcc_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/tcc_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -2; }
fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
driver, outbin, src);
int brc = runwait(cmd);
int got = -1;
if (brc == 0) got = runwait(outbin);
runwait(rmcmd);
return brc == 0 ? got : -1; /* -1 == build failed */
}
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 have_ww = (access(wdrv, X_OK) == 0);
int n = (int)(sizeof rows / sizeof rows[0]);
int total = 0, fail = 0;
for (int i = 0; i < n; i++) {
total++;
int gc = run_build(cdrv, &rows[i], i);
if (gc != rows[i].want) {
fprintf(stderr, "trycallcollide[cstage][%s]: got=%d want=%d\n",
rows[i].label, gc, rows[i].want);
fail++;
}
if (!have_ww) {
fprintf(stderr, "trycallcollide: skip wwstage (no %s)\n", wdrv);
continue;
}
int gw = run_build(wdrv, &rows[i], i);
if (gw != gc) {
fprintf(stderr, "trycallcollide[%s]: cs=%d != ww=%d "
"(exprtypeoftry cross-module try-operand misbind — #11a c3)\n",
rows[i].label, gc, gw);
fail++;
}
if (gw != rows[i].want) {
fprintf(stderr, "trycallcollide[wwstage][%s]: got=%d want=%d\n",
rows[i].label, gw, rows[i].want);
fail++;
}
}
if (fail) {
fprintf(stderr, "trycallcollide_run: %d/%d checks failed\n",
fail, total);
return 1;
}
printf("trycallcollide_run: %d/%d ok\n", total, total);
return 0;
}