Files
ww/test/wcc/712_redecl.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

259 lines
8.0 KiB
C

/*
* 712_redecl — check: refuse same-scope let/mlet/param/top-let
* redeclarations (task #32).
*
* Pre-fix: cmd/wcc/check.c silently dropped the duplicate insert when
* scope_define returned NULL. `let a: i32 = 1; let a: i32 = 2;` in
* one block compiled cleanly and the second store last-write-wins;
* `fn f(a: i32, a: i32)` accepted both params, body resolved to the
* second. Surfaced post-#27 (which dropped the localoff name-dedup
* shim that had been masking the issue at codegen) — see commit
* 1292f98's follow-up note and worker-27's filed task.
*
* Post-fix: cstage check.c errors at every site where scope_define /
* scope_define_in_module's NULL return was previously ignored:
*
* site | message
* ---------------------------+--------------------------------------
* block-body N_LET | let '%s' redeclared in same scope
* N_MLET (`let (a,b)=…`) | let '%s' redeclared in same scope
* N_FORRANGE tuple-binding | binding '%s' redeclared in same scope
* N_FNDECL param | param '%s' redeclared
* top-level N_LET | duplicate let %s
*
* N_MCASE (case-binding) and N_FORRANGE single-binding already lived
* in fresh per-arm / per-loop scopes with at most one bind, so they
* weren't part of the bug class. Top-level N_TYPEDECL / N_DEF /
* N_FNDECL already errored on NULL ("duplicate <kind> %s").
*
* Cstage-only. Wwstage's check.ww is a single-pass resolve walk with
* no per-block scoping (line 1015 comment), exercised only by
* wwdump_ww as a diagnostic; adding the guard there today would
* false-positive on legal cross-block shadow. Tracked under #11
* (wwstage checkfile pass with per-block scoping). Matches the
* test/wcc/708 + test/wcc/696 cstage-only neg-case precedent.
*
* row | gate | what it pins
* --------------------------+----------------+--------------------
* neg_let_same_block | build fails | site 1443
* neg_mlet_same_block | build fails | site 1562
* neg_mlet_tuple_dup | build fails | site 1562 (dup-in)
* neg_forrange_tuple_dup | build fails | site 1505
* neg_param_dup | build fails | site 1914
* neg_toplet_dup | build fails | site 1880
* pos_nested_block_shadow | exit=1 | scope boundary
* pos_nested_shadow_typed | exit=2 | (name,type) — bucket
* | | keys by name only
* pos_forrange_body_shadow | exit=99 | for body N_BLOCK
* | | opens a fresh scope
* pos_mcase_per_arm | exit=7 | match arm scope
*/
#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;
}
/*
* kind == 0: negative — build must fail (any nonzero exit).
* kind == 1: positive — build must succeed AND binary exits with `want`.
*/
struct row { const char *label; int kind; const char *src; int want; };
static const struct row rows[] = {
/* neg: site 1443 — block-body let dup. */
{ "neg_let_same_block", 0,
"fn main() i32 = {\n"
" let a: i32 = 1;\n"
" let a: i32 = 2;\n"
" return a;\n"
"};\n",
0 },
/* neg: site 1562 — mlet shadows earlier same-block let. */
{ "neg_mlet_same_block", 0,
"fn pair() (i32, i32) = { return (10, 20); };\n"
"fn main() i32 = {\n"
" let a: i32 = 1;\n"
" let (a, b) = pair();\n"
" return a + b;\n"
"};\n",
0 },
/* neg: site 1562 — mlet pattern lists the same name twice. */
{ "neg_mlet_tuple_dup", 0,
"fn pair() (i32, i32) = { return (10, 20); };\n"
"fn main() i32 = {\n"
" let (a, a) = pair();\n"
" return a;\n"
"};\n",
0 },
/* neg: site 1505 — forrange tuple-pattern lists same name twice. */
{ "neg_forrange_tuple_dup", 0,
"fn main() i32 = {\n"
" let xs: [1](i32, i32) = [(10i32, 20i32)];\n"
" for (let (a, a) .. xs) {\n"
" return a;\n"
" };\n"
" return -1;\n"
"};\n",
0 },
/* neg: site 1914 — two params with the same name. */
{ "neg_param_dup", 0,
"fn f(a: i32, a: i32) i32 = { return a; };\n"
"fn main() i32 = { return f(1, 2); };\n",
0 },
/* neg: site 1880 — top-level let dup. */
{ "neg_toplet_dup", 0,
"let x: i32 = 1;\n"
"let x: i32 = 2;\n"
"fn main() i32 = { return x; };\n",
0 },
/* pos: nested-block shadow stays legal. Inner block opens a
* fresh scope, scope_define's per-scope hash bucket isolates the
* inner `a` from the outer one. Outer `a` untouched after the
* inner block exits. */
{ "pos_nested_block_shadow", 1,
"fn main() i32 = {\n"
" let a: i32 = 1;\n"
" {\n"
" let a: i32 = 99;\n"
" };\n"
" return a;\n"
"};\n",
1 },
/* pos: nested-block shadow with a different type. Confirms the
* scope-bucket key is the name alone, not (name, type) — the
* inner `s: str` does NOT collide with the outer `s: i32`. */
{ "pos_nested_shadow_typed", 1,
"fn main() i32 = {\n"
" let s: i32 = 2;\n"
" {\n"
" let s: str = \"hi\";\n"
" let _ = s;\n"
" };\n"
" return s;\n"
"};\n",
2 },
/* pos: for-loop body N_BLOCK opens a fresh scope, so a `let x`
* inside the body doesn't collide with the for-range's binding
* `x` (which lives in the per-loop scope, one level above). */
{ "pos_forrange_body_shadow", 1,
"fn main() i32 = {\n"
" let arr: [1]i32 = [42i32];\n"
" let r: i32 = 0;\n"
" for (let x .. arr) {\n"
" let x: i32 = 99;\n"
" r = x;\n"
" };\n"
" return r;\n"
"};\n",
99 },
/* pos: each match arm is its own scope, so two arms each
* binding `v` don't collide. Pre-#32 this was already legal
* (newscope wrapper at check.c:1186); pinned here so a future
* scope-tightening can't quietly remove that wrapper. */
{ "pos_mcase_per_arm", 1,
"type T = (i32 | f64);\n"
"fn main() i32 = {\n"
" let t: T = 7i32;\n"
" match (t) {\n"
" case let v: i32 => { return v; };\n"
" case let v: f64 => { return -1; };\n"
" };\n"
"};\n",
7 },
};
static int
run_row(const char *driver, const struct row *r, int i)
{
char src[128], tmpdir[128], outbin[128], rmcmd[160], cmd[2048];
snprintf(tmpdir, sizeof tmpdir, "/tmp/wcredecl_%d_d_%d", getpid(), i);
mkdir(tmpdir, 0755);
snprintf(src, sizeof src, "%s/wcredecl_%d_%d.ww", tmpdir, getpid(), i);
snprintf(outbin, sizeof outbin, "%s/wcredecl_%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 >/dev/null 2>&1",
driver, outbin, src);
int rc = runwait(cmd);
if (r->kind == 0) {
/* Negative — build must fail. */
if (rc == 0)
fprintf(stderr,
"redecl[%s]: build unexpectedly succeeded\n",
r->label);
runwait(rmcmd);
return rc == 0 ? -1 : 0;
}
/* Positive — build then run. */
if (rc != 0) {
fprintf(stderr, "redecl[%s]: build failed\n", r->label);
runwait(rmcmd);
return -1;
}
int got = runwait(outbin);
runwait(rmcmd);
if (got != r->want) {
fprintf(stderr, "redecl[%s]: exit=%d want=%d\n",
r->label, got, r->want);
return -1;
}
return 0;
}
int
main(void)
{
const char *bin = getenv("BIN");
if (!bin) bin = "out/bin";
char absbin[512];
if (bin[0] != '/') {
char cwd[256];
if (getcwd(cwd, sizeof cwd) == NULL) return 1;
snprintf(absbin, sizeof absbin, "%s/%s", cwd, bin);
bin = absbin;
}
char cdrv[640];
snprintf(cdrv, sizeof cdrv, "%s/ww", bin);
int n = (int)(sizeof rows / sizeof rows[0]);
int fail = 0;
for (int i = 0; i < n; i++) {
if (run_row(cdrv, &rows[i], i) != 0) fail++;
}
if (fail) {
fprintf(stderr, "redecl: %d/%d row(s) failed\n", fail, n);
return 1;
}
printf("redecl: %d/%d ok\n", n, n);
return 0;
}