cmd/wcc/check.c silently accepted `let a; let a;` in the same block and similar redecls. Pre-#27 the localoff dedup masked it; post-#27 last-write-wins via head-first localfind. Surfaced by worker-27 during the #27 review. Cstage: 5 guard sites (check_scope_define-NULL → err) covering N_LET block-bind, N_MLET tuple binders (incl. same-tuple `let (a,a)`), N_FORRANGE tuple binders, top-level let, fn param. Voice: "<kind> '<name>' redeclared in same scope" for inner; "duplicate let %s" for top-let, matching the existing "duplicate <kind>" idiom at 1812/1851/1872. Wwstage: TODO(#11) comments at the 4 mirror sites (installdecl, N_FORRANGE, N_LET, installparams). Full enforcement waits on the checkfile pass per rob. **Unmasked by #32 (worth flagging):** selfhost/cmd/wcc/cgenexpr.ww cgcall had `let callee: *node = n.lhs;` twice at fn-body scope (copy-paste, identical value). Pre-fix silent-redecl absorbed it; post-fix the new guard rejects. Removed the second decl — outer `callee` stays visible across the intermediate block. Test 712 (redecl): 10 rows (6 neg + 4 pos), cstage-only per rob. Negative rows cover all 5 guard sites + same-tuple-dup. Positive rows pin the legal counter-shapes (cross-block, name-only bucket, forrange body, mcase-per-arm). Test 300 row 34 ("shadowing in inner scope; same scope flagged") was incorrectly asserting the bug; flipped to expect "redeclared" and added a sibling row pinning cross-block shadow stays ok. Test 709's `same_block_redecl_pin` canary (explicitly documented as flipping under #32) removed; pointer to 712 left in its place.
282 lines
8.5 KiB
C
282 lines
8.5 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 <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;
|
|
}
|
|
|
|
/*
|
|
* 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], cmd[2048];
|
|
snprintf(src, sizeof src, "/tmp/wcredecl_%d_%d.ww", getpid(), i);
|
|
snprintf(tmpdir, sizeof tmpdir, "/tmp/wcredecl_%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 >/dev/null 2>&1",
|
|
tmpdir, driver, src);
|
|
int rc = runwait(cmd);
|
|
|
|
const char *base = strrchr(src, '/');
|
|
base = base ? base + 1 : src;
|
|
char outbin[256];
|
|
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
|
|
char *dot = strrchr(outbin, '.');
|
|
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
|
|
|
|
if (r->kind == 0) {
|
|
/* Negative — build must fail. */
|
|
if (rc == 0) {
|
|
fprintf(stderr,
|
|
"redecl[%s]: build unexpectedly succeeded\n",
|
|
r->label);
|
|
unlink(outbin);
|
|
}
|
|
unlink(src);
|
|
/* combined.ww left next to src by the driver */
|
|
char combined[256];
|
|
snprintf(combined, sizeof combined, "%s.combined.ww", src);
|
|
dot = strrchr(combined, '.');
|
|
(void)dot;
|
|
snprintf(combined, sizeof combined, "/tmp/wcredecl_%d_%d.combined.ww",
|
|
getpid(), i);
|
|
unlink(combined);
|
|
rmdir(tmpdir);
|
|
return rc == 0 ? -1 : 0;
|
|
}
|
|
|
|
/* Positive — build then run. */
|
|
if (rc != 0) {
|
|
fprintf(stderr, "redecl[%s]: build failed\n", r->label);
|
|
unlink(src);
|
|
rmdir(tmpdir);
|
|
return -1;
|
|
}
|
|
int got = runwait(outbin);
|
|
unlink(src);
|
|
unlink(outbin);
|
|
char combined[256];
|
|
snprintf(combined, sizeof combined, "/tmp/wcredecl_%d_%d.combined.ww",
|
|
getpid(), i);
|
|
unlink(combined);
|
|
rmdir(tmpdir);
|
|
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;
|
|
}
|