Files
ww/test/wcc/759_check_enum_fold.c
Hojun-Cho 7b9488706b parse: enforce strict-package — reject package-less files (#24a)
Flip the soft-default to a hard "missing package clause" error symmetrically in
both stages (cmd/wcc/parse.c + lib/ww/syntax/parse.ww): the first real decl of a
primary section with empty pathmod/resetmod and no seen clause is now rejected.
Closes the documented soft-default divergence (the 63-wrapper carve-out).

The gate flip can't be split from the migration it breaks, so this is one atomic
commit: ~80 test/wcc wrappers gain `package main;` via a shared wwtestpkg.h
helper, 6 data fixtures plus 17 asm-grep assertions update for the bare->main.<leaf>
root-helper mangle shift, and rt/ declares `package rt;` with @symbol pinning the
bare rt_ensure/rt_malloc linker names.

Root mangling narrows: the executable entry `main` stays bare (existing
carve-out), but root helper symbols become main.X. The #84 cluster is rewritten
to assert main.run distinct from aa.run/test.run; its cgen fix and bare machinery
are retained — still load-bearing for package-less module-reset deps. New
table-driven test 782_strict_package.c (6 rows, both stages).

Retiring //ww:module-reset is deferred to #24b: it is load-bearing (clears the
.wwi pathmod so the body's package clause asserts), not a vestige; fusing its
removal here would be a silent mismatch.

All byte-id gates green; full make test reports "all 335 tests passed".
2026-06-29 03:55:26 +09:00

299 lines
8.5 KiB
C

/*
* 759_check_enum_fold — table-driven sentinel for #22 / A.6.2.1a.
*
* check-side enum fold (selfhost/cmd/wcc/check.ww enumvalfold) now
* mirrors cstage cmd/wcc/check.c:210-284 eval_enum_value: literals
* (INTLIT/RUNELIT), unary +/-/~, binary arithmetic (+ - * / %),
* bitwise (& | ^), shifts (<< >>), and sibling backref via
* N_IDENT. The N_DOT fold mutates the AST to N_INTLIT in place, so
* a wrong fold value leaks into cgen as the wrong literal and the
* exit code reflects it. Each row is a self-contained ww program
* whose `return EnumT.MEMBER: i32` carries the expected fold.
*
* Pre-#22 the check-side fold bailed on every shape but bare
* N_INTLIT and auto-increment; cgen's enumevalmember
* (selfhost/cmd/wcc/cgen.ww:158-227) absorbed the slack at codegen
* time. Post-#22 check.ww does the fold itself so A.6.2.1e's
* post-checker e.type_ assertion no longer fires on `EnumT.MEMBER`
* references whose body is a backref or arithmetic expression.
*
* Each row runs through the cstage `ww` driver and through `ww_ww`
* when present; the asm-byte-id diff between w6c and w6c_ww pins
* the symmetric-emit contract (CLAUDE.md rule 10) so a future
* drift between the two enumvalfold copies fails loud.
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include "wwtestpkg.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[] = {
/* Baseline N_INTLIT — regression: the literal-only path was the
* pre-#22 fold contract and must still resolve. */
{ "intlit",
"type e = enum i32 { A = 7 };\n"
"fn main() i32 = { return e.A: i32; };\n",
7 },
/* N_RUNELIT — rune literals carry their codepoint in n.uval
* exactly like N_INTLIT (cmd/wcc/check.c:191). */
{ "runelit",
"type e = enum i32 { A = 'A' };\n"
"fn main() i32 = { return e.A: i32; };\n",
65 },
/* Bare sibling backref (no op): B = A. Tests the N_IDENT arm
* without a wrapping N_BIN/N_UN. */
{ "sibling_backref",
"type e = enum i32 { A = 7, B = A };\n"
"fn main() i32 = { return e.B: i32; };\n",
7 },
/* N_UN TK_MINUS over literal, fed through a sibling backref. The
* NEG member exercises N_UN; the R member exercises the IDENT
* lookup that must resolve NEG's already-folded value. */
{ "unary_minus",
"type e = enum i32 { NEG = -7, R = NEG + 14 };\n"
"fn main() i32 = { return e.R: i32; };\n",
7 },
/* Nested unary: ~(-8) folds to 7. Tests N_UN recursion (TK_TILDE
* over TK_MINUS over N_INTLIT). */
{ "unary_tilde_nested",
"type e = enum i32 { A = ~(-8) };\n"
"fn main() i32 = { return e.A: i32; };\n",
7 },
/* N_UN TK_PLUS — noop wrapper; completes the unary whitelist. */
{ "unary_plus",
"type e = enum i32 { A = +7 };\n"
"fn main() i32 = { return e.A: i32; };\n",
7 },
/* N_BIN TK_PLUS with sibling backref — the headline RW=R|W
* shape's relative. */
{ "bin_plus",
"type e = enum i32 { A = 3, B = A + 4 };\n"
"fn main() i32 = { return e.B: i32; };\n",
7 },
{ "bin_minus",
"type e = enum i32 { A = 10, B = A - 3 };\n"
"fn main() i32 = { return e.B: i32; };\n",
7 },
{ "bin_star",
"type e = enum i32 { A = 2, B = A * 4 };\n"
"fn main() i32 = { return e.B: i32; };\n",
8 },
{ "bin_slash",
"type e = enum i32 { A = 14, B = A / 2 };\n"
"fn main() i32 = { return e.B: i32; };\n",
7 },
{ "bin_percent",
"type e = enum i32 { A = 17, B = A % 10 };\n"
"fn main() i32 = { return e.B: i32; };\n",
7 },
{ "bin_amp",
"type e = enum i32 { A = 15, B = A & 7 };\n"
"fn main() i32 = { return e.B: i32; };\n",
7 },
/* N_BIN TK_PIPE — overlaps with 700_e2e's `RW = R | W` row, kept
* here for symmetry with the rest of the binop set. */
{ "bin_pipe",
"type e = enum i32 { A = 4, B = A | 3 };\n"
"fn main() i32 = { return e.B: i32; };\n",
7 },
{ "bin_caret",
"type e = enum i32 { A = 5, B = A ^ 2 };\n"
"fn main() i32 = { return e.B: i32; };\n",
7 },
{ "bin_lshift",
"type e = enum i32 { A = 1, B = A << 3 };\n"
"fn main() i32 = { return e.B: i32; };\n",
8 },
{ "bin_rshift",
"type e = enum i32 { A = 28, B = A >> 2 };\n"
"fn main() i32 = { return e.B: i32; };\n",
7 },
/* Chained sibling backref — pins the O(N²) walker contract.
* Each member's lhs ident lookup re-walks the prefix; if the
* `until` bound were >= instead of > (off-by-one) the lookup
* of D would walk past C and the fold would diverge or loop. */
{ "chained_backref",
"type e = enum i32 { A = 1, B = A + 1, C = B + 1, D = C + 4 };\n"
"fn main() i32 = { return e.D: i32; };\n",
7 },
};
static int
run_driver(const char *driver, const struct row *r, int i)
{
char tmpdir[64], src[128], outbin[128], rmcmd[160], cmd[1024];
snprintf(tmpdir, sizeof tmpdir, "/tmp/cef_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/cef_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/cef_%d_%d", tmpdir, getpid(), i);
snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", tmpdir);
FILE *f = fopen(src, "wb");
if (!f) { runwait(rmcmd); return -1; }
wwtest_fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s build -o %s %s 2>/dev/null",
driver, outbin, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: build via %s failed\n",
r->label, driver);
runwait(rmcmd);
return -1;
}
int got = runwait(outbin);
runwait(rmcmd);
return got;
}
/* asm_byte_identical — diff w6c against w6c_ww for the same source.
* Pins the symmetric-emit contract: a future drift between cstage's
* eval_enum_value and wwstage's enumvalfold would show up here as a
* byte diff even if both stages produce semantically-correct
* constants. */
static int
asm_byte_identical(const char *bin, const struct row *r, int i)
{
char src[64], cs[64], ws[64], cmd[1024];
snprintf(src, sizeof src, "/tmp/cef_asm_%d_%d.ww", getpid(), i);
snprintf(cs, sizeof cs, "/tmp/cef_asm_%d_%d_c.s", getpid(), i);
snprintf(ws, sizeof ws, "/tmp/cef_asm_%d_%d_w.s", getpid(), i);
FILE *f = fopen(src, "wb");
if (!f) return -1;
wwtest_fputs(r->src, f);
fclose(f);
snprintf(cmd, sizeof cmd, "%s/w6c -o %s %s 2>/dev/null", bin, cs, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c errored\n", r->label);
unlink(src);
return -1;
}
snprintf(cmd, sizeof cmd, "%s/w6c_ww -o %s %s 2>/dev/null",
bin, ws, src);
if (runwait(cmd) != 0) {
fprintf(stderr, "row[%s]: w6c_ww errored\n", r->label);
unlink(src); unlink(cs);
return -1;
}
FILE *fc = fopen(cs, "rb");
FILE *fw = fopen(ws, "rb");
int rc = 0;
if (!fc || !fw) {
rc = -1;
} else {
for (;;) {
int a = fgetc(fc);
int b = fgetc(fw);
if (a != b) { rc = -1; break; }
if (a == EOF) break;
}
}
if (fc) fclose(fc);
if (fw) fclose(fw);
if (rc != 0)
fprintf(stderr, "row[%s]: cstage vs wwstage asm differs\n",
r->label);
unlink(src); unlink(cs); unlink(ws);
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 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 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, "check_enum_fold: 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,
"check_enum_fold[%s][%s]: exit=%d want=%d\n",
drivers[d].name, rows[i].label,
got, rows[i].want);
fail++;
}
}
}
/* Asm byte-identity diff, only when wwstage is built. */
if (access(wdrv, X_OK) == 0) {
for (int i = 0; i < n; i++) {
total++;
if (asm_byte_identical(bin, &rows[i], i) != 0)
fail++;
}
}
if (fail) {
fprintf(stderr,
"check_enum_fold: %d/%d fixtures failed\n", fail, total);
return 1;
}
printf("check_enum_fold: %d/%d ok\n", total, total);
return 0;
}