Align bare-int array-init assignability to Hare's literal-fits rule (ref/harec/src/types.c promote_flexible): accept untyped-int array elements that FIT the element type, reject out-of-range loud. cstage (rejected all bare-int arrays, over-strict) and wwstage (accepted + silently truncated out-of-range, over-loose) converge to the same accept-if-fits rule. Per-element: foldable int literal range-checked against element type [min,max] via def_cast_fits (rule-13 type-table widths); non-foldable element falls back to type_assignable. cstage: new arrlit_init_fits, N_LET decl-check fallback after whole- array type_assignable fails. wwstage: checkletassign array branch + route top-level lets through checkletassign (were unchecked — only function-body lets ran assignability; closes #146 wwstage str->u8 over-accept). Scalar-init range-check (let X:u8=300 truncates, both stages, pre-existing) deferred to #148 — language-wide, needs bootstrap audit + explicit-cast conversion. def-array accept-if-fits deferred to #151 (def constfold machinery, different risk). Both bootstrap-NEUTRAL. Test 920 (14 rows — accept: in-range u8/u32/u64/i32 + u8/i8 boundary + typed regression + non-foldable-body; reject: over-range + over-256 + i8-over + neg-for-unsigned + str->u8 + non-foldable-wider). Non- foldable else-branch cs==ww verified (matching-type accept + byte-id; wider-runtime-int reject both stages). Make test: 183/183 incl 990-997 byte-id + combined_ww_fresh.
286 lines
9.3 KiB
C
286 lines
9.3 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 <string.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 src[64];
|
|
snprintf(src, sizeof src, "/tmp/wwafit_%d_%d.ww", getpid(), i);
|
|
FILE *f = fopen(src, "wb");
|
|
if (f == NULL) { fail++; continue; }
|
|
fputs(accept_rows[i].src, f);
|
|
fclose(f);
|
|
|
|
char tmpdir[64];
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/wwafit_%d_d_%d",
|
|
getpid(), i);
|
|
mkdir(tmpdir, 0755);
|
|
|
|
char cmd[2048];
|
|
snprintf(cmd, sizeof cmd, "cd %s && %s/ww build %s",
|
|
tmpdir, bin, src);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "accept[%s]: cstage build failed\n",
|
|
accept_rows[i].label);
|
|
fail++; unlink(src); rmdir(tmpdir); continue;
|
|
}
|
|
char outbin[128];
|
|
const char *base = strrchr(src, '/');
|
|
base = base ? base + 1 : src;
|
|
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
|
char *dot = strrchr(outbin, '.');
|
|
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
|
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++;
|
|
}
|
|
unlink(outbin); rmdir(tmpdir);
|
|
|
|
char cs_s[64], ws_s[64];
|
|
snprintf(cs_s, sizeof cs_s, "/tmp/wwafit_%d_%d_cs.s",
|
|
getpid(), i);
|
|
snprintf(ws_s, sizeof ws_s, "/tmp/wwafit_%d_%d_ww.s",
|
|
getpid(), i);
|
|
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++; unlink(src); 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++; unlink(src); unlink(cs_s); 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++;
|
|
}
|
|
unlink(src); unlink(cs_s); unlink(ws_s);
|
|
}
|
|
|
|
/* 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;
|
|
}
|