The wwstage checker silently accepted enums with a non-integer storage type, duplicate members, or a non-constant member value; cstage already rejects all three (cmd/wcc/check.c:1000-1042). Add validateenummembers, a pure read-only diagnostic dispatched once per enum decl from resolvewalk's N_TENUM arm (check.ww:791, beside stampenumvals -- not the per-query recompute arms), mirroring the catB-7/14 validatestructfields pattern. Storage gate uses typeisint on the resolved tinfo (the exact type_isint mirror: chases TY_NAMED.under and TY_ENUM.sub, so an int-alias storage is accepted; raw-AST isinttypeast would not). Duplicate members: O(n^2) name walk. Unfoldable values reuse enumvalfold with until=member (forward-only). Emits via cerr + c.errs, no mutation, so valid-program codegen is unchanged and cstage==wwstage byte-id holds. wwstage's value-fold message is intentionally generic where cstage's eval_enum_value gives per-reason text (enumvalfold returns a bool, not a reason); both reject. Documented at the site, filed as follow-up task #10. Test: new table-driven both-stage reject test 850_enum_reject (non-int storage / duplicate member / forward-ref rows + a distinct-member control whose backward-ref value builds and runs). Full make test: 452 green incl. 990-997 byte-id.
190 lines
5.6 KiB
C
190 lines
5.6 KiB
C
/*
|
|
* 850_enum_reject (catB-2) — cstage and wwstage LOUD-REJECT the three
|
|
* invalid enum-declaration shapes: a non-integer storage type, a
|
|
* duplicate member name, and an unfoldable (non-constant) member value.
|
|
*
|
|
* cstage already rejected at cmd/wcc/check.c:1000-1042 (the N_TENUM arm
|
|
* of resolve_type — storage type_isint gate, the per-member dup walk,
|
|
* and eval_enum_value's constant fold). wwstage silently ACCEPTED — it
|
|
* stamped member types but never validated storage/dup/foldability. The
|
|
* fix adds validateenummembers(), a pure read-only diagnostic fired once
|
|
* per enum decl from resolvewalk's eager type-decl dispatch (selfhost/
|
|
* cmd/wcc/check.ww), the rule-10 twin of cstage's once-per-resolve_type
|
|
* site, sibling to validatestructfields (catB-7/14).
|
|
*
|
|
* Byte-id: pure diagnostic, no n.type_ / member-value / checker-state
|
|
* mutation; the valid corpus has integer storage, distinct members, and
|
|
* foldable values, so codegen of every valid program is unchanged →
|
|
* cstage == wwstage byte-id holds.
|
|
*
|
|
* row | shape | expect
|
|
* -------------+--------------------------------------+--------
|
|
* nonint_stor | enum f64 { A } | REJECT
|
|
* dup_member | enum { A, B, A } | REJECT
|
|
* fwd_ref | enum { A = B, B } (non-constant) | REJECT
|
|
* distinct | enum { A, B=5, C=B+1 }, use A+C | BUILD, run 6
|
|
*/
|
|
#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;
|
|
}
|
|
|
|
static void
|
|
outbin(char *dst, size_t n, const char *tmpdir, const char *src)
|
|
{
|
|
const char *base = strrchr(src, '/');
|
|
base = base ? base + 1 : src;
|
|
snprintf(dst, n, "%s/%s", tmpdir, base);
|
|
char *dot = strrchr(dst, '.');
|
|
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
|
}
|
|
|
|
static int
|
|
build_should_fail(const char *driver, const char *label, const char *src,
|
|
int i)
|
|
{
|
|
char s[64], tmpdir[64], cmd[1024], bin[128];
|
|
snprintf(s, sizeof s, "/tmp/enr_%d_%d.ww", getpid(), i);
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/enr_%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);
|
|
outbin(bin, sizeof bin, tmpdir, s);
|
|
unlink(s);
|
|
unlink(bin);
|
|
rmdir(tmpdir);
|
|
if (rc == 0)
|
|
fprintf(stderr, "enr[%s][%s]: built ok, expected a reject\n",
|
|
driver, label);
|
|
return rc == 0 ? -1 : 0;
|
|
}
|
|
|
|
static int
|
|
run_build(const char *driver, const char *label, const char *src, int i)
|
|
{
|
|
char s[64], tmpdir[64], cmd[1024], bin[128];
|
|
snprintf(s, sizeof s, "/tmp/enr_ok_%d_%d.ww", getpid(), i);
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/enr_ok_%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);
|
|
if (runwait(cmd) != 0) {
|
|
fprintf(stderr, "enr[%s][%s]: build failed (valid enum must "
|
|
"compile)\n", driver, label);
|
|
unlink(s); rmdir(tmpdir);
|
|
return -1;
|
|
}
|
|
outbin(bin, sizeof bin, tmpdir, s);
|
|
int got = runwait(bin);
|
|
unlink(s); unlink(bin); rmdir(tmpdir);
|
|
return got;
|
|
}
|
|
|
|
struct rejrow { const char *label; const char *src; };
|
|
|
|
static const struct rejrow reject_rows[] = {
|
|
{ "nonint_stor",
|
|
"package main;\n"
|
|
"type e = enum f64 { A };\n"
|
|
"fn main() i32 = { return e.A as i32; };\n" },
|
|
{ "dup_member",
|
|
"package main;\n"
|
|
"type e = enum { A, B, A };\n"
|
|
"fn main() i32 = { return e.A as i32; };\n" },
|
|
{ "fwd_ref",
|
|
"package main;\n"
|
|
"type e = enum { A = B, B };\n"
|
|
"fn main() i32 = { return e.A as i32; };\n" },
|
|
};
|
|
|
|
struct okrow { const char *label; const char *src; int want; };
|
|
|
|
static const struct okrow ok_rows[] = {
|
|
{ "distinct",
|
|
"package main;\n"
|
|
"type e = enum { A, B = 5, C = B + 1 };\n"
|
|
"fn main() i32 = { return e.A as i32 + e.C as i32; };\n",
|
|
6 },
|
|
};
|
|
|
|
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);
|
|
|
|
struct { const char *name; const char *path; int gated; } drivers[] = {
|
|
{ "cstage", cdrv, 0 },
|
|
{ "wwstage", wdrv, 1 },
|
|
{ NULL, NULL, 0 },
|
|
};
|
|
|
|
int nrej = (int)(sizeof reject_rows / sizeof reject_rows[0]);
|
|
int nok = (int)(sizeof ok_rows / sizeof ok_rows[0]);
|
|
int total = 0, fail = 0;
|
|
|
|
for (int d = 0; drivers[d].name; d++) {
|
|
if (drivers[d].gated && access(drivers[d].path, X_OK) != 0) {
|
|
fprintf(stderr, "enr: skip %s (no %s)\n",
|
|
drivers[d].name, drivers[d].path);
|
|
continue;
|
|
}
|
|
for (int i = 0; i < nrej; i++) {
|
|
total++;
|
|
if (build_should_fail(drivers[d].path,
|
|
reject_rows[i].label, reject_rows[i].src,
|
|
d * 100 + i) != 0)
|
|
fail++;
|
|
}
|
|
for (int i = 0; i < nok; i++) {
|
|
total++;
|
|
int got = run_build(drivers[d].path, ok_rows[i].label,
|
|
ok_rows[i].src, d * 100 + i);
|
|
if (got != ok_rows[i].want) {
|
|
fprintf(stderr, "enr[%s][%s]: exit=%d want=%d\n",
|
|
drivers[d].name, ok_rows[i].label,
|
|
got, ok_rows[i].want);
|
|
fail++;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (fail) {
|
|
fprintf(stderr, "enr: %d/%d fixtures failed\n", fail, total);
|
|
return 1;
|
|
}
|
|
printf("enr: %d/%d ok\n", total, total);
|
|
return 0;
|
|
}
|