wcc/check: #24-sib reject array-payload tagged-union construction, both stages
Constructing an array-typed payload into a tagged-union variant silently dropped it (cstage MOVQ $0 -> returns 0; wwstage match-loud only). Reject the construct when the selected variant chases to TY_ARRAY (target TY_TAGGED, non-tagged source). tagged-struct/slice/scalar/str variants and the array TYPE-decl stay legal. Faithful array-into-box block-store deferred (#6). test/wcc/834 (new) + Makefile.
This commit is contained in:
293
test/wcc/834_tagged_arr_variant.c
Normal file
293
test/wcc/834_tagged_arr_variant.c
Normal file
@@ -0,0 +1,293 @@
|
||||
/*
|
||||
* 834_tagged_arr_variant — constructing an ARRAY-typed payload into a
|
||||
* tagged-union variant is unwired in cgen and must be LOUDLY REJECTED at
|
||||
* check time on BOTH stages (#5 / #60, rob spec .ai/rob-24sib-spec.md).
|
||||
*
|
||||
* cstage zero-filled the array payload at box materialization (a silent
|
||||
* MOVQ $0 drop — `let v:V=[7,8,9]` then `match` read 0); wwstage only
|
||||
* loud at the dead match arm (errbadcase), SILENTLY accepting the
|
||||
* construct. The fix is a construct-site checker reject keyed on: target
|
||||
* chases TY_TAGGED AND the variant the source selects chases TY_ARRAY.
|
||||
* cstage tagged_array_variant (check.c) + wwstage taggedarrayvariantctor
|
||||
* (check.ww), at the let-init and return assignability paths.
|
||||
*
|
||||
* The tagged TYPE-decl WITH an array variant stays legal — only the
|
||||
* array-variant CONSTRUCTION is refused. The narrow_in_wide positive
|
||||
* (944's `(void|size|[5]size)`, boxing the scalar `size`) proves the
|
||||
* type-decl + scalar ctor remain accepted. struct / slice / scalar / str
|
||||
* variants all WORK (sib2/sib3) and are positive controls. Both stages
|
||||
* REJECT the neg rows and emit no asm; selfhost never boxes an array, so
|
||||
* 990-997 byte-id is untouched. Diagnostic TEXT is byte-id-blind; do not
|
||||
* chase message parity. Full array-block-store fix deferred to task #6.
|
||||
*
|
||||
* neg row | shape | gate
|
||||
* ----------------+---------------------------------------------+------
|
||||
* arrlit_ctor | V=([3]int|int); let v:V=[7,8,9] | FAIL
|
||||
* arrvar_ctor | let a:[3]int=..; let v:V=a (variable src) | FAIL
|
||||
* arr_return | fn()V { return [7,8,9]; } | FAIL
|
||||
*
|
||||
* pos row | shape | want
|
||||
* ----------------+---------------------------------------------+------
|
||||
* struct_variant | V=(P|int); v=P{x=1,y=2}; match -> p.y | 2
|
||||
* slice_variant | V=([]int|int); v=s; match -> s[2] | 9
|
||||
* scalar_variant | V=(int|bool); v=5; match -> n | 5
|
||||
* str_variant | V=(str|int); v="hi"; match -> s.len | 2
|
||||
* narrow_in_wide | big=(void|size|[5]size); m=7:size; match | 7
|
||||
*/
|
||||
#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 row { const char *label; const char *src; int want; };
|
||||
|
||||
static const struct row rows[] = {
|
||||
/* struct variant box + match (sib2 — works + byte-id). */
|
||||
{ "struct_variant",
|
||||
"package main;\n"
|
||||
"type P = struct { x: int, y: int };\n"
|
||||
"type V = (P | int);\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet v: V = P { x = 1, y = 2 };\n"
|
||||
"\tmatch (v) {\n"
|
||||
"\tcase let p: P => return p.y: i32;\n"
|
||||
"\tcase int => return 0;\n"
|
||||
"\t};\n"
|
||||
"\treturn 9;\n"
|
||||
"};\n",
|
||||
2 },
|
||||
|
||||
/* slice variant box + match (sib3 — works + byte-id). */
|
||||
{ "slice_variant",
|
||||
"package main;\n"
|
||||
"type V = ([]int | int);\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet hb: [3]int = [7, 8, 9];\n"
|
||||
"\tlet a: []int = hb;\n"
|
||||
"\tlet v: V = a;\n"
|
||||
"\tmatch (v) {\n"
|
||||
"\tcase let s: []int => return s[2]: i32;\n"
|
||||
"\tcase int => return 0;\n"
|
||||
"\t};\n"
|
||||
"\treturn 9;\n"
|
||||
"};\n",
|
||||
9 },
|
||||
|
||||
/* scalar variant box + match. */
|
||||
{ "scalar_variant",
|
||||
"package main;\n"
|
||||
"type V = (int | bool);\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet v: V = 5;\n"
|
||||
"\tmatch (v) {\n"
|
||||
"\tcase let n: int => return n: i32;\n"
|
||||
"\tcase bool => return 0;\n"
|
||||
"\t};\n"
|
||||
"\treturn 9;\n"
|
||||
"};\n",
|
||||
5 },
|
||||
|
||||
/* str variant box + match (24B header payload). */
|
||||
{ "str_variant",
|
||||
"package main;\n"
|
||||
"type V = (str | int);\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet v: V = \"hi\";\n"
|
||||
"\tmatch (v) {\n"
|
||||
"\tcase let s: str => return s.len: i32;\n"
|
||||
"\tcase int => return 0;\n"
|
||||
"\t};\n"
|
||||
"\treturn 9;\n"
|
||||
"};\n",
|
||||
2 },
|
||||
|
||||
/* narrow_in_wide — 944's green shape: a tagged union that DECLARES an
|
||||
* array variant ([5]size) but boxes only the narrow scalar `size`.
|
||||
* Proves the type-decl + scalar ctor stay legal under the reject. */
|
||||
{ "narrow_in_wide",
|
||||
"package main;\n"
|
||||
"type big = (void | size | [5]size);\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet m: big = 7: size;\n"
|
||||
"\tmatch (m) {\n"
|
||||
"\tcase let s: size => return s: i32;\n"
|
||||
"\tcase => return 9;\n"
|
||||
"\t};\n"
|
||||
"\treturn 9;\n"
|
||||
"};\n",
|
||||
7 },
|
||||
};
|
||||
|
||||
/* Constructing an array payload into a tagged variant — both stages must
|
||||
* FAIL the build (loud construct-site checker reject). */
|
||||
static const char *neg[] = {
|
||||
/* arrlit_ctor — array LITERAL boxed into ([3]int|int). */
|
||||
"package main;\n"
|
||||
"type V = ([3]int | int);\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet v: V = [7, 8, 9];\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n",
|
||||
/* arrvar_ctor — array VARIABLE boxed (not literal-specific). */
|
||||
"package main;\n"
|
||||
"type V = ([3]int | int);\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\tlet a: [3]int = [7, 8, 9];\n"
|
||||
"\tlet v: V = a;\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n",
|
||||
/* arr_return — array boxed into a tagged return type. */
|
||||
"package main;\n"
|
||||
"type V = ([3]int | int);\n"
|
||||
"fn f() V = {\n"
|
||||
"\treturn [7, 8, 9];\n"
|
||||
"};\n"
|
||||
"export fn main() i32 = {\n"
|
||||
"\treturn 0;\n"
|
||||
"};\n",
|
||||
};
|
||||
|
||||
static int
|
||||
run_driver(const char *driver, const struct row *r, int i)
|
||||
{
|
||||
char src[64], tmpdir[64], cmd[1024];
|
||||
snprintf(src, sizeof src, "/tmp/tav_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/tav_%d_d_%d", getpid(), i);
|
||||
|
||||
FILE *f = fopen(src, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(r->src, f);
|
||||
fclose(f);
|
||||
|
||||
mkdir(tmpdir, 0755);
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
|
||||
tmpdir, driver, src);
|
||||
if (runwait(cmd) != 0) {
|
||||
fprintf(stderr, "row[%s]: build via %s failed\n",
|
||||
r->label, driver);
|
||||
unlink(src); rmdir(tmpdir);
|
||||
return -1;
|
||||
}
|
||||
|
||||
const char *base = strrchr(src, '/');
|
||||
base = base ? base + 1 : src;
|
||||
char outbin[128];
|
||||
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
||||
char *dot = strrchr(outbin, '.');
|
||||
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
||||
int got = runwait(outbin);
|
||||
|
||||
unlink(src); unlink(outbin); rmdir(tmpdir);
|
||||
return got;
|
||||
}
|
||||
|
||||
/* build_should_fail — the array-variant construct must error on `driver`;
|
||||
* returns 0 when the build correctly FAILS, non-zero when it wrongly
|
||||
* succeeded. */
|
||||
static int
|
||||
build_should_fail(const char *driver, const char *src, int i)
|
||||
{
|
||||
char s[64], tmpdir[64], cmd[1024];
|
||||
snprintf(s, sizeof s, "/tmp/tavn_%d_%d.ww", getpid(), i);
|
||||
snprintf(tmpdir, sizeof tmpdir, "/tmp/tavn_%d_d_%d", getpid(), i);
|
||||
|
||||
FILE *f = fopen(s, "wb");
|
||||
if (!f) return -1;
|
||||
fputs(src, f);
|
||||
fclose(f);
|
||||
|
||||
mkdir(tmpdir, 0755);
|
||||
snprintf(cmd, sizeof cmd, "cd %s && %s build %s 2>/dev/null",
|
||||
tmpdir, driver, s);
|
||||
int rc = runwait(cmd);
|
||||
unlink(s);
|
||||
const char *base = strrchr(s, '/');
|
||||
base = base ? base + 1 : s;
|
||||
char outbin[128];
|
||||
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
||||
char *dot = strrchr(outbin, '.');
|
||||
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
||||
unlink(outbin);
|
||||
rmdir(tmpdir);
|
||||
return rc == 0 ? -1 : 0; /* build must NOT succeed */
|
||||
}
|
||||
|
||||
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];
|
||||
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
|
||||
char wdrv[1024];
|
||||
snprintf(wdrv, sizeof wdrv, "%s/ww_ww", bin);
|
||||
|
||||
struct { const char *name; const char *path; int gated_on_existence; }
|
||||
drivers[] = {
|
||||
{ "cstage", cdrv, 0 },
|
||||
{ "wwstage", wdrv, 1 },
|
||||
{ NULL, NULL, 0 },
|
||||
};
|
||||
|
||||
int n = (int)(sizeof rows / sizeof rows[0]);
|
||||
int nn = (int)(sizeof neg / sizeof neg[0]);
|
||||
int total = 0, fail = 0;
|
||||
|
||||
for (int d = 0; drivers[d].name; d++) {
|
||||
if (drivers[d].gated_on_existence
|
||||
&& access(drivers[d].path, X_OK) != 0) {
|
||||
fprintf(stderr, "tagged_arr_variant: skip %s (no %s)\n",
|
||||
drivers[d].name, drivers[d].path);
|
||||
continue;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
int got = run_driver(drivers[d].path, &rows[i], i);
|
||||
total++;
|
||||
if (got != rows[i].want) {
|
||||
fprintf(stderr,
|
||||
"tagged_arr_variant[%s][%s]: exit=%d want=%d\n",
|
||||
drivers[d].name, rows[i].label,
|
||||
got, rows[i].want);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < nn; i++) {
|
||||
total++;
|
||||
if (build_should_fail(drivers[d].path, neg[i],
|
||||
100 + i) != 0) {
|
||||
fprintf(stderr,
|
||||
"tagged_arr_variant[%s][neg%d]: built ok, "
|
||||
"expected a loud reject\n",
|
||||
drivers[d].name, i);
|
||||
fail++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (fail) {
|
||||
fprintf(stderr,
|
||||
"tagged_arr_variant: %d/%d fixtures failed\n", fail, total);
|
||||
return 1;
|
||||
}
|
||||
printf("tagged_arr_variant: %d/%d ok\n", total, total);
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user