Files
ww/test/wcc/300_check.c
Hojun-Cho b8b32ab80c cstage+selfhost+test: refuse same-block let / param redecl (#32)
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.
2026-05-16 11:17:20 +09:00

203 lines
7.8 KiB
C

/*
* 300_check — type-checker tests.
*
* Each row is (src, expect) where expect is "ok" (no errors) or a
* substring that must appear in the captured stderr.
*/
#include "ww.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
static int
runrow(const char *src, const char *expect)
{
Arena *a = newarena();
Lex l;
Parser p;
Checker c;
lexinit(&l, a, "<test>", src, strlen(src));
parserinit(&p, a, &l);
Node *file = parsefile(&p);
char *errbuf = NULL;
size_t errlen = 0;
FILE *prev = errout;
errout = open_memstream(&errbuf, &errlen);
check_init(&c, a);
check_file(&c, file);
fclose(errout);
errout = prev;
int ok = 0;
if (strcmp(expect, "ok") == 0) {
ok = (c.errs == 0 && p.errs == 0 && l.errs == 0);
if (!ok)
fprintf(stderr, "expected ok but got %d errors:\n%s"
" src: %s\n", c.errs + p.errs + l.errs, errbuf, src);
} else {
ok = (errbuf && strstr(errbuf, expect) != NULL);
if (!ok)
fprintf(stderr, "expected substring '%s' in errs:\n%s"
" src: %s\n", expect, errbuf, src);
}
free(errbuf);
freearena(a);
return ok;
}
struct row { const char *src, *expect; };
static const struct row rows[] = {
/* OK cases */
{ "fn main() void = {};", "ok" },
{ "fn id(x: i32) i32 = { return x; };", "ok" },
{ "fn add(a: i32, b: i32) i32 = { return a + b; };", "ok" },
{ "def MAX: i32 = 4096;", "ok" },
{ "type point = struct { x: i32, y: i32 };", "ok" },
{ "type point = struct { x: i32, y: i32 };\n"
"fn move(p: *point, dx: i32) void = { p.x += dx; };", "ok" },
{ "fn nums() void = { let x: i32 = 1; let y: i64 = 2; };", "ok" },
{ "fn cond(x: i32) i32 = { if (x > 0) { return 1; }; return 0; };", "ok" },
{ "fn lo() void = { for (let i: i32 = 0; i < 10; i += 1) { }; };", "ok" },
{ "fn ptr(p: *i32) i32 = { return *p; };", "ok" },
{ "fn addr(x: i32) *i32 = { return &x; };", "ok" },
{ "fn cmp(a: i32, b: i32) bool = { return a == b; };", "ok" },
{ "fn b() bool = { return true && false || !true; };", "ok" },
{ "fn cast() void = { let x = (5 + 1): i64; };", "ok" },
{ "fn slc(s: []u8) []u8 = { return s; };", "ok" },
{ "fn arr() void = { let a: [16]u8; };", "ok" },
{ "fn lit() void = { let p = point { x = 1, y = 2 }; };\n"
"type point = struct { x: i32, y: i32 };", "ok" },
{ "fn idx(a: []i32) i32 = { return a[0]; };", "ok" },
{ "fn nilptr(p: *i32) bool = { return p == nil; };", "ok" },
{ "fn untyped() void = { let x: i64 = 42; };", "ok" },
{ "fn fld(p: *point) i32 = { return p.x; };\n"
"type point = struct { x: i32 };", "ok" },
{ "fn loops() void = { for () { break; }; for () { continue; }; };", "ok" },
/* multi-return tuples */
{ "fn divmod(a: i64, b: i64) (i64, i64) = { return a / b, a % b; };", "ok" },
{ "fn dm(a: i64, b: i64) (i64, i64) = { return a, b; };\n"
"fn caller() i64 = { let q, r = dm(10, 3); return q + r; };", "ok" },
{ "fn dm() (i64, i64) = { return 1, 2; };\n"
"fn ass() void = { let q: i64 = 0; let r: i64 = 0; q, r = dm(); };", "ok" },
/* error cases */
{ "fn f() void = { return 1; };", "return value in void" },
{ "fn f() i32 = { return; };", "not assignable" },
{ "fn f() void = { x = 1; };", "undefined" },
{ "fn f() void = { let x: nope = 1; };", "unknown type" },
{ "fn f() void = { let x: bool = 1; };", "not assignable" },
{ "fn f() i32 = { return \"hi\"; };", "not assignable" },
{ "fn f() void = { 1 + true; };", "non-numeric" },
{ "fn f() void = { -true; };", "non-numeric" },
{ "fn f() void = { *5; };", "deref non-pointer" },
{ "fn f() void = { let x: i32 = 1; let x: i32 = 2; };",
"redeclared" }, /* same-scope let-redecl rejected (#32). */
{ "fn f() void = { let x: i32 = 1; { let x: i32 = 2; }; };",
"ok" }, /* nested-block shadow stays legal. */
{ "fn f() void = { break; };", "break outside loop" },
{ "fn f() void = { continue; };", "continue outside loop" },
{ "fn f(x: i32) void = { x[0]; };", "indexing non-indexable" },
{ "fn f() i32 = { return 1; }; fn g() i32 = { return f(1); };",
"too many arguments" },
{ "fn f(a: i32) i32 = { return a; }; fn g() i32 = { return f(); };",
"not enough arguments" },
{ "fn f() void = { if (1) { }; };", "if condition" },
/* tagged-union normalization & exhaustiveness */
{ "fn f() (i32 | never) = { return 7; }; "
"fn g() i32 = { return f(); };", "ok" }, /* (T|never) → T */
{ "fn f() (i32 | i32) = { return 5; }; "
"fn g() i32 = { return f(); };", "ok" }, /* dedup → i32 */
{ "fn f() (i32 | void) = { return; };", "ok" }, /* bare return → void variant */
{ "fn f(b: bool) (i32 | void) = { if (b) { return 1; }; return; };",
"ok" },
{ "fn f() (i32 | str | bool) = { return 1; }; "
"fn g() void = { let v: (i32 | str | bool) = f(); "
"match (v) { case let x: i32 => { }; case let x: str => { }; }; };",
"variant bool not handled" },
{ "fn f() (i32 | str | bool) = { return 1; }; "
"fn g() void = { let v: (i32 | str | bool) = f(); "
"match (v) { case let x: i32 => { }; case => { }; }; };",
"ok" }, /* default arm absorbs missing variants */
{ "fn die() never = { for (true) { let _: i32 = 1; }; }; "
"fn f() i32 = { die(); };", "ok" }, /* never assignable to anything */
/* ? error-subset propagation typecheck */
{ "fn inner() (i32 | str) = { return 1; }; "
"fn outer() i32 = { let v: i32 = inner()?; return v; };",
"enclosing function must return a tagged union" },
{ "fn inner() (i32 | str | bool) = { return 1; }; "
"fn outer() (i64 | str) = { let v: i32 = inner()?; return v: i64; };",
"error variant bool not in enclosing return" },
{ "fn inner() (i32 | str | bool) = { return 1; }; "
"fn outer() (i64 | bool | str) = { let v: i32 = inner()?; return v: i64; };",
"ok" }, /* reversed-order error subset is OK */
/* `!`-flagged error variants (Hare's explicit error marker) */
{ "type invalid = !i32; "
"fn parse() (i64 | invalid) = { return 5: invalid; }; "
"fn caller() (i64 | invalid) = { let v: i64 = parse()?; return v + 1; };",
"ok" },
{ "type invalid = !i32; type overflow = !void; "
"fn parse() (invalid | i64 | overflow) = { return 0i64; }; "
"fn caller() i32 = { let v: i64 = parse()?; return v: i32; };",
"enclosing function must return a tagged union" },
/* nullable pointer folding accepts `(*T | void)` */
{ "fn lookup(p: *i32) (*i32 | void) = { return p; };", "ok" },
{ "fn lookup() (*i32 | void) = { return; };", "ok" }, /* bare return → null */
/* `case T` must name a variant of the scrutinee */
{ "fn pick() (i32 | str) = { return 1; }; "
"fn caller() void = { let v: (i32 | str) = pick(); "
"match (v) { case let n: i32 => { }; case let s: str => { }; "
"case let f: f64 => { }; }; };",
"case: f64 is not a variant" },
{ "fn pick() (i32 | str | bool) = { return 1; }; "
"fn caller() i32 = { let v: (i32 | str | bool) = pick(); "
"match (v) { case let n: i32 => return n; "
"case str | f64 => return 9; case let b: bool => return 1; }; "
"return 0; };",
"case: f64 is not a variant" },
/* enum types */
{ "type color = enum { RED, GREEN, BLUE };\n"
"fn f() i32 = { return color.GREEN as i32; };", "ok" },
{ "type mode = enum u8 { R = 1, W = 2, RW = R | W };\n"
"fn f() i32 = { let m: mode = mode.RW; return m as i32; };", "ok" },
{ "type mode = enum u8 { R = 1, W = 2 };\n"
"fn f() i32 = { return (mode.R | mode.W) as i32; };", "ok" },
{ "type c = enum { A, B };\n"
"fn f() i32 = { return c.NOPE as i32; };",
"no enum member 'NOPE'" },
{ "type c = enum { A, A };\n"
"fn f() i32 = { return c.A as i32; };",
"duplicate enum member" },
{ "type c = enum bool { A };\n"
"fn f() i32 = { return c.A as i32; };",
"enum storage type must be integer" },
};
int
main(void)
{
int n = sizeof rows / sizeof rows[0];
int fail = 0;
for (int i = 0; i < n; i++)
if (!runrow(rows[i].src, rows[i].expect)) {
fprintf(stderr, "row %d failed\n", i);
fail++;
}
if (fail) {
fprintf(stderr, "%d/%d check tests failed\n", fail, n);
return 1;
}
printf("check: %d/%d ok\n", n, n);
return 0;
}