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.
282 lines
9.1 KiB
C
282 lines
9.1 KiB
C
/*
|
|
* 920_array_init_acceptiffits_run — cs==ww net for #130: module-level
|
|
* `let A: [N]T = [...]` array-init "accept-if-fits".
|
|
*
|
|
* Pre-#130 divergence (Drew-adjudicated B/E synthesis):
|
|
* - cstage REJECTED all bare-int array elements ("init not assignable")
|
|
* — its array assignability path didn't wire the untyped-int→narrow-
|
|
* element coercion its OWN scalar path has.
|
|
* - wwstage ACCEPTED everything (no per-element validation): bare-int
|
|
* in-range (correct), bare-int out-of-range (silent truncate —
|
|
* miscompile), str→u8 (silent garbage). Top-level lets weren't even
|
|
* run through checkletassign.
|
|
*
|
|
* #130 fix = accept-if-fits, BOTH stages converge:
|
|
* - foldable int literal element → range-check against T via
|
|
* def_cast_fits/defcastfits (type table, rule 13). In-range accepts;
|
|
* out-of-range REJECTS loud (rule-7 / Drew: Hare range-checks at the
|
|
* literal-value level, ref/harec/src/types.c:923 promote_flexible).
|
|
* - non-foldable element → type_assignable / isassignable to T
|
|
* (untyped-int→u8 ok; str→u8 rejected).
|
|
* - cstage: check.c arrlit_init_fits fallback after whole-array
|
|
* type_assignable fails.
|
|
* - wwstage: check.ww checkletassign array branch + top-level lets now
|
|
* routed through checkletassign (were missed). Merges #146 (the
|
|
* wwstage str→u8 over-accept).
|
|
*
|
|
* Scope: ARRAY-init only. Scalar `let X: u8 = 300` truncation is a
|
|
* pre-existing language-wide gap (#148), deferred.
|
|
*
|
|
* This test asserts the ACCEPT rows compile + run + cs==ww byte-id, and
|
|
* (via the cstage/wwstage exit-code probe) that the REJECT rows fail to
|
|
* build on BOTH stages. Reject rows use a separate build-must-fail check.
|
|
*
|
|
* Accept rows (size strata 1B/4B/8B, sign, typed-vs-bare, both stages):
|
|
* - bare_inrange `[4]u8 = [1,2,3,4]` → A[0]==1
|
|
* - bare_u32 `[4]u32 = [10,20,30,40]` → A[0]==10
|
|
* - bare_u64 `[2]u64 = [5,6]` → A[0]==5
|
|
* - signed_inrange `[4]i32 = [-1,-2,-3,-4]` → A[0]==-1 (255)
|
|
* - typed_regress `[4]u8 = [1u8,2u8,3u8,4u8]` → A[0]==1
|
|
* - boundary_max `[2]u8 = [255, 0]` → A[0]==255
|
|
*
|
|
* Reject rows (must FAIL build on both stages):
|
|
* - over_range `[2]u8 = [300, 1]`
|
|
* - neg_for_unsigned `[2]u8 = [-1, 0]`
|
|
* - str_to_u8 `[2]u8 = ["x", "y"]`
|
|
*/
|
|
#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 arow { const char *label; const char *src; int want_exit; };
|
|
struct rrow { const char *label; const char *src; };
|
|
|
|
static const struct arow accept_rows[] = {
|
|
{ "bare_inrange",
|
|
"package main;\n"
|
|
"let A: [4]u8 = [1, 2, 3, 4];\n"
|
|
"export fn main() i32 = { return A[0]: i32; };\n", 1 },
|
|
{ "bare_u32",
|
|
"package main;\n"
|
|
"let A: [4]u32 = [10, 20, 30, 40];\n"
|
|
"export fn main() i32 = { return A[0]: i32; };\n", 10 },
|
|
{ "bare_u64",
|
|
"package main;\n"
|
|
"let A: [2]u64 = [5, 6];\n"
|
|
"export fn main() i32 = { return A[0]: i32; };\n", 5 },
|
|
{ "signed_inrange",
|
|
"package main;\n"
|
|
"let A: [4]i32 = [-1, -2, -3, -4];\n"
|
|
"export fn main() i32 = { return A[0]; };\n", 255 /* -1 */ },
|
|
{ "typed_regress",
|
|
"package main;\n"
|
|
"let A: [4]u8 = [1u8, 2u8, 3u8, 4u8];\n"
|
|
"export fn main() i32 = { return A[0]: i32; };\n", 1 },
|
|
{ "boundary_max",
|
|
"package main;\n"
|
|
"let A: [2]u8 = [255, 0];\n"
|
|
"export fn main() i32 = { return A[0]: i32; };\n", 255 },
|
|
/* i8 signed boundary: -128..127 both in-range. */
|
|
{ "i8_boundary",
|
|
"package main;\n"
|
|
"let A: [2]i8 = [127, -128];\n"
|
|
"export fn main() i32 = { return A[0]: i32; };\n", 127 },
|
|
/* Non-foldable element of matching type — exercises the else
|
|
* branch (type_assignable / isassignable, not the fold path).
|
|
* Must be a FUNCTION-BODY array: a module-level array with a
|
|
* non-foldable (runtime-valued) element isn't statically
|
|
* emittable (no const fold → no DATA row). The checker else
|
|
* branch fires identically for body lets, where the stack slot
|
|
* takes the runtime value. cs==ww verified. */
|
|
{ "nonfold_match",
|
|
"package main;\n"
|
|
"export fn main() i32 = {\n"
|
|
" let u: u8 = 5u8;\n"
|
|
" let A: [2]u8 = [u, 0u8];\n"
|
|
" return A[0]: i32;\n"
|
|
"};\n", 5 },
|
|
{ NULL, NULL, 0 }
|
|
};
|
|
|
|
static const struct rrow reject_rows[] = {
|
|
{ "over_range",
|
|
"package main;\n"
|
|
"let A: [2]u8 = [300, 1];\n"
|
|
"export fn main() i32 = { return A[0]: i32; };\n" },
|
|
{ "neg_for_unsigned",
|
|
"package main;\n"
|
|
"let A: [2]u8 = [-1, 0];\n"
|
|
"export fn main() i32 = { return 0; };\n" },
|
|
{ "str_to_u8",
|
|
"package main;\n"
|
|
"let A: [2]u8 = [\"x\", \"y\"];\n"
|
|
"export fn main() i32 = { return 0; };\n" },
|
|
/* Just-over-256 — pins the u8 upper bound exactly. */
|
|
{ "over_256",
|
|
"package main;\n"
|
|
"let A: [2]u8 = [256, 0];\n"
|
|
"export fn main() i32 = { return 0; };\n" },
|
|
/* i8 over-range (128 > 127). */
|
|
{ "i8_over",
|
|
"package main;\n"
|
|
"let A: [2]i8 = [128, 0];\n"
|
|
"export fn main() i32 = { return 0; };\n" },
|
|
/* Non-foldable wider-runtime int → narrow element needs an
|
|
* explicit cast in Hare; reject both stages (the else-branch
|
|
* reject path, cs==ww verified). */
|
|
{ "nonfold_wider",
|
|
"package main;\n"
|
|
"let i: int = 5;\n"
|
|
"let A: [2]u8 = [i, 0u8];\n"
|
|
"export fn main() i32 = { return 0; };\n" },
|
|
{ NULL, NULL }
|
|
};
|
|
|
|
static int
|
|
slurp_eq(const char *a, const char *b)
|
|
{
|
|
FILE *fa = fopen(a, "rb");
|
|
FILE *fb = fopen(b, "rb");
|
|
if (!fa || !fb) { if (fa) fclose(fa); if (fb) fclose(fb); return -1; }
|
|
int rc = 0;
|
|
for (;;) {
|
|
int ca = fgetc(fa);
|
|
int cb = fgetc(fb);
|
|
if (ca != cb) { rc = -1; break; }
|
|
if (ca == EOF) break;
|
|
}
|
|
fclose(fa); fclose(fb);
|
|
return rc;
|
|
}
|
|
|
|
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 w6c[1100], w6c_ww[1100];
|
|
snprintf(w6c, sizeof w6c, "%s/w6c", bin);
|
|
snprintf(w6c_ww, sizeof w6c_ww, "%s/w6c_ww", bin);
|
|
if (access(w6c_ww, X_OK) != 0) {
|
|
fprintf(stderr, "arrfit: w6c_ww missing — cannot run the "
|
|
"cs==ww gate (the whole point of this test)\n");
|
|
return 1;
|
|
}
|
|
|
|
int n = 0, fail = 0;
|
|
|
|
/* Accept rows: cstage build + run + cs==ww byte-id. */
|
|
for (int i = 0; accept_rows[i].src; i++, n++) {
|
|
char tmpdir[64];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwafit_%d_d_%d",
|
|
getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
|
|
char src[128], outbin[128], cs_s[128], ws_s[128], rmcmd[160];
|
|
snprintf(src, sizeof src, "%s/wwafit_%d_%d.ww",
|
|
tmpdir, getpid(), i);
|
|
snprintf(outbin, sizeof outbin, "%s/wwafit_%d_%d",
|
|
tmpdir, getpid(), i);
|
|
snprintf(cs_s, sizeof cs_s, "%s/wwafit_%d_%d_cs.s",
|
|
tmpdir, getpid(), i);
|
|
snprintf(ws_s, sizeof ws_s, "%s/wwafit_%d_%d_ww.s",
|
|
tmpdir, getpid(), i);
|
|
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
|
|
|
|
FILE *f = fopen(src, "wb");
|
|
if (f == NULL) { fail++; runwait(rmcmd); continue; }
|
|
fputs(accept_rows[i].src, f);
|
|
fclose(f);
|
|
|
|
char cmd[2048];
|
|
snprintf(cmd, sizeof cmd, "%s/ww build -o %s %s",
|
|
bin, outbin, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "accept[%s]: cstage build failed\n",
|
|
accept_rows[i].label);
|
|
fail++; runwait(rmcmd); continue;
|
|
}
|
|
int got = runwait(outbin);
|
|
if (got != accept_rows[i].want_exit) {
|
|
fprintf(stderr, "accept[%s]: exit %d, want %d\n",
|
|
accept_rows[i].label, got, accept_rows[i].want_exit);
|
|
fail++;
|
|
}
|
|
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, cs_s, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "accept[%s]: w6c failed\n", accept_rows[i].label);
|
|
fail++; runwait(rmcmd); continue;
|
|
}
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c_ww, ws_s, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "accept[%s]: w6c_ww failed\n", accept_rows[i].label);
|
|
fail++; runwait(rmcmd); continue;
|
|
}
|
|
if (slurp_eq(cs_s, ws_s) != 0) {
|
|
fprintf(stderr, "accept[%s]: cs/ww .s DIFFER (rule-10)\n",
|
|
accept_rows[i].label);
|
|
fail++;
|
|
}
|
|
runwait(rmcmd);
|
|
}
|
|
|
|
/* Reject rows: BOTH stages must fail to build (loud reject). */
|
|
for (int i = 0; reject_rows[i].src; i++, n++) {
|
|
char src[64];
|
|
snprintf(src, sizeof src, "/tmp/wwrfit_%d_%d.ww", getpid(), i);
|
|
FILE *f = fopen(src, "wb");
|
|
if (f == NULL) { fail++; continue; }
|
|
fputs(reject_rows[i].src, f);
|
|
fclose(f);
|
|
|
|
char cmd[2048], dst[80];
|
|
snprintf(dst, sizeof dst, "/tmp/wwrfit_%d_%d.s", getpid(), i);
|
|
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c, dst, src);
|
|
int cs_rc = runwait(cmd);
|
|
snprintf(cmd, sizeof cmd, "%s -o %s %s 2>/dev/null", w6c_ww, dst, src);
|
|
int ww_rc = runwait(cmd);
|
|
|
|
if (cs_rc == 0) {
|
|
fprintf(stderr, "reject[%s]: cstage ACCEPTED (want reject)\n",
|
|
reject_rows[i].label);
|
|
fail++;
|
|
}
|
|
if (ww_rc == 0) {
|
|
fprintf(stderr, "reject[%s]: wwstage ACCEPTED (want reject)\n",
|
|
reject_rows[i].label);
|
|
fail++;
|
|
}
|
|
unlink(src); unlink(dst);
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr, "%d/%d array-init-accept-if-fits tests failed\n",
|
|
fail, n);
|
|
return 1;
|
|
}
|
|
printf("arrfit: %d/%d ok (accept: run + cs==ww; reject: both-stage fail)\n",
|
|
n, n);
|
|
return 0;
|
|
}
|