diff --git a/Makefile b/Makefile index e89b182e..c69f8f5c 100644 --- a/Makefile +++ b/Makefile @@ -249,6 +249,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \ $(BIN)/test_dup_main_reject \ $(BIN)/test_tuple_trailing_comma_reject \ $(BIN)/test_voidless_return_reject \ + $(BIN)/test_const_reassign_reject \ $(BIN)/test_size_untyped_int \ $(BIN)/test_tagged_staticinit \ $(BIN)/test_arr_infer_len \ @@ -1463,6 +1464,12 @@ $(BIN)/test_voidless_return_reject: test/wcc/846_voidless_return_reject.c $(BIN) $(LIB)/libwwrt.a | $(BIN) $(CC) $(CFLAGS) -o $@ $< +$(BIN)/test_const_reassign_reject: test/wcc/847_const_reassign_reject.c $(BIN)/ww \ + $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ + $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ + $(LIB)/libwwrt.a | $(BIN) + $(CC) $(CFLAGS) -o $@ $< + $(BIN)/test_tagged_staticinit: test/wcc/843_tagged_staticinit.c $(BIN)/ww \ $(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \ $(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \ diff --git a/selfhost/cmd/w6c/main.combined.ww b/selfhost/cmd/w6c/main.combined.ww index 58ede432..3c8e21bb 100644 --- a/selfhost/cmd/w6c/main.combined.ww +++ b/selfhost/cmd/w6c/main.combined.ww @@ -11247,7 +11247,14 @@ fn resolvewalk(c: *checker, n: *node) void = { let nm: str = n.str; if (nm.len > 0) { checkmoduleshadow(c, nm, "let"); - scopedefine(c.cur, nm, skind.SK_VAR, nil, n); + let s: *sym = scopedefine(c.cur, nm, skind.SK_VAR, nil, n); + // catB-22: flag a `const` binding so checkassign can + // reject a later reassignment. The parser stamps + // n.op = TK_CONST (parse/stmt.ww). Mirror cstage + // cmd/wcc/check.c:2408. + if (s != nil) { + if (n.op == tkind.TK_CONST) { s.is_const = 1i32; }; + }; }; }; @@ -15947,6 +15954,21 @@ fn checkassign(c: *checker, n: *node) void = { if (n == nil) { return; }; if (n.lhs == nil) { return; }; if (n.rhs == nil) { return; }; + // catB-22: reject reassigning a `const`-flagged binding (the + // is_const bit set at the let-install above). A bare `_` discard + // lvalue carries an empty str and is skipped. Mirror cstage + // cmd/wcc/check.c:1889-1896. + if (n.lhs.kind == nkind.N_IDENT) { + if (n.lhs.str.len > 0) { + let s: *sym = scopelookupprefer(c.cur, c.curmod, n.lhs.str); + if (s != nil) { + if (s.is_const != 0i32) { + cerr("error: cannot assign to const binding\n"); + c.errs += 1; + }; + }; + }; + }; let ltn: *node = exprtype(c, n.lhs, nil); let rtn: *node = exprtype(c, n.rhs, nil); // #29: a rune literal narrowing into an integer assign/index-store diff --git a/selfhost/cmd/wcc/check.ww b/selfhost/cmd/wcc/check.ww index 51634b61..a0075869 100644 --- a/selfhost/cmd/wcc/check.ww +++ b/selfhost/cmd/wcc/check.ww @@ -773,7 +773,14 @@ fn resolvewalk(c: *checker, n: *node) void = { let nm: str = n.str; if (nm.len > 0) { checkmoduleshadow(c, nm, "let"); - scopedefine(c.cur, nm, skind.SK_VAR, nil, n); + let s: *sym = scopedefine(c.cur, nm, skind.SK_VAR, nil, n); + // catB-22: flag a `const` binding so checkassign can + // reject a later reassignment. The parser stamps + // n.op = TK_CONST (parse/stmt.ww). Mirror cstage + // cmd/wcc/check.c:2408. + if (s != nil) { + if (n.op == tkind.TK_CONST) { s.is_const = 1i32; }; + }; }; }; @@ -5473,6 +5480,21 @@ fn checkassign(c: *checker, n: *node) void = { if (n == nil) { return; }; if (n.lhs == nil) { return; }; if (n.rhs == nil) { return; }; + // catB-22: reject reassigning a `const`-flagged binding (the + // is_const bit set at the let-install above). A bare `_` discard + // lvalue carries an empty str and is skipped. Mirror cstage + // cmd/wcc/check.c:1889-1896. + if (n.lhs.kind == nkind.N_IDENT) { + if (n.lhs.str.len > 0) { + let s: *sym = scopelookupprefer(c.cur, c.curmod, n.lhs.str); + if (s != nil) { + if (s.is_const != 0i32) { + cerr("error: cannot assign to const binding\n"); + c.errs += 1; + }; + }; + }; + }; let ltn: *node = exprtype(c, n.lhs, nil); let rtn: *node = exprtype(c, n.rhs, nil); // #29: a rune literal narrowing into an integer assign/index-store diff --git a/selfhost/cmd/wwdump/main.combined.ww b/selfhost/cmd/wwdump/main.combined.ww index 7bf81d1c..6b5bad14 100644 --- a/selfhost/cmd/wwdump/main.combined.ww +++ b/selfhost/cmd/wwdump/main.combined.ww @@ -11247,7 +11247,14 @@ fn resolvewalk(c: *checker, n: *node) void = { let nm: str = n.str; if (nm.len > 0) { checkmoduleshadow(c, nm, "let"); - scopedefine(c.cur, nm, skind.SK_VAR, nil, n); + let s: *sym = scopedefine(c.cur, nm, skind.SK_VAR, nil, n); + // catB-22: flag a `const` binding so checkassign can + // reject a later reassignment. The parser stamps + // n.op = TK_CONST (parse/stmt.ww). Mirror cstage + // cmd/wcc/check.c:2408. + if (s != nil) { + if (n.op == tkind.TK_CONST) { s.is_const = 1i32; }; + }; }; }; @@ -15947,6 +15954,21 @@ fn checkassign(c: *checker, n: *node) void = { if (n == nil) { return; }; if (n.lhs == nil) { return; }; if (n.rhs == nil) { return; }; + // catB-22: reject reassigning a `const`-flagged binding (the + // is_const bit set at the let-install above). A bare `_` discard + // lvalue carries an empty str and is skipped. Mirror cstage + // cmd/wcc/check.c:1889-1896. + if (n.lhs.kind == nkind.N_IDENT) { + if (n.lhs.str.len > 0) { + let s: *sym = scopelookupprefer(c.cur, c.curmod, n.lhs.str); + if (s != nil) { + if (s.is_const != 0i32) { + cerr("error: cannot assign to const binding\n"); + c.errs += 1; + }; + }; + }; + }; let ltn: *node = exprtype(c, n.lhs, nil); let rtn: *node = exprtype(c, n.rhs, nil); // #29: a rune literal narrowing into an integer assign/index-store diff --git a/test/wcc/847_const_reassign_reject.c b/test/wcc/847_const_reassign_reject.c new file mode 100644 index 00000000..bba612e8 --- /dev/null +++ b/test/wcc/847_const_reassign_reject.c @@ -0,0 +1,173 @@ +/* + * 847_const_reassign_reject (catB-22) — cstage and wwstage LOUD-REJECT + * a reassignment of a `const`-declared binding. + * + * `const x = 5; x = 6;` mutates an immutable binding — a programmer + * error. The sym carries an is_const flag (lib/ww/sym.ww), but wwstage + * never SET it at the let-install nor CONSUMED it at assignment, so the + * reassignment slipped through silently. The fix mirrors cstage's two + * sites: set is_const when n.op == TK_CONST at the local let-install + * (cmd/wcc/check.c:2408) and reject an N_ASSIGN whose lhs ident resolves + * to an is_const sym (cmd/wcc/check.c:1889-1896). cstage already + * rejected; this aligns wwstage UP. + * + * Both rows annotate `: i32` so the inferred-int default (#34 int/i32 + * cs/ww divergence) does not contaminate the const signal. + * + * row | shape | expect + * -----------+--------------------------------+-------- + * const_set | const x: i32 = 5; x = 6; | REJECT + * let_set | let y: i32 = 5; y = 6; | BUILD, run 6 + */ +#include +#include +#include +#include +#include +#include + +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/creas_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/creas_%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, "creas[%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/creas_ok_%d_%d.ww", getpid(), i); + snprintf(tmpdir, sizeof tmpdir, "/tmp/creas_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, "creas[%s][%s]: build failed (a mutable let " + "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[] = { + { "const_set", + "package main;\n" + "fn main() i32 = { const x: i32 = 5; x = 6; return x; };\n" }, +}; + +struct okrow { const char *label; const char *src; int want; }; + +static const struct okrow ok_rows[] = { + { "let_set", + "package main;\n" + "fn main() i32 = { let y: i32 = 5; y = 6; return y; };\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, "creas: 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, "creas[%s][%s]: exit=%d want=%d\n", + drivers[d].name, ok_rows[i].label, + got, ok_rows[i].want); + fail++; + } + } + } + + if (fail) { + fprintf(stderr, "creas: %d/%d fixtures failed\n", fail, total); + return 1; + } + printf("creas: %d/%d ok\n", total, total); + return 0; +}