wcc/ww: reject a duplicate top-level main (F-D)

A second top-level decl named `main` (fn/let/def/type) collides with
the entry main on the single bare `main` symbol: today both lower to a
bare `main`, w6l silently accepts the duplicate, and the program links
rc=0 then segfaults (or runs wrong), in both stages. The existing
duplicate-decl rejects key on (name, module), so a cross-module
`foo.main` vs the bare entry `main` read as distinct and slip through.

Add a program-global, name-only, cross-module uniqueness check on
`main` in the checker (both stages), colocated with the duplicate-decl
rejects and counting user decls before the -T synthesized test main.
Corpus-safe: a lone `fn main` in any package stays legal (ww has no
package-main convention -- cmatrix/lisp/mandelbrot are non-main-package
entries and keep building). This converts the silent segfault to a
loud compile error and subsumes the w6l silent-dup-main case (#31);
correct package-aware mangling of a non-entry main is deferred to the
root-unit entry-detection work (#22/#32).

Regenerates the w6c and wwdump combined.ww. Table-driven 842 test:
reject rows for let/fn/def/type main (genuine cross-module import form)
plus a negative single-main corpus-safe row that must still build+run.
This commit is contained in:
2026-06-14 17:05:46 +09:00
parent c86c6a3bbf
commit 34c1051a63
6 changed files with 373 additions and 0 deletions

View File

@@ -246,6 +246,7 @@ TESTS = $(BIN)/test_smoke $(BIN)/test_lex $(BIN)/test_parse $(BIN)/test_check \
$(BIN)/test_arr_enum_elem \
$(BIN)/test_arr_strslice_elem \
$(BIN)/test_arr_tagged_elem \
$(BIN)/test_dup_main_reject \
$(BIN)/test_arr_infer_len \
$(BIN)/test_arr_cap_reject \
$(BIN)/test_tagged_subset_reject \
@@ -1431,6 +1432,12 @@ $(BIN)/test_arr_tagged_elem: test/wcc/685_arr_tagged_elem.c $(BIN)/ww \
$(LIB)/libwwrt.a | $(BIN)
$(CC) $(CFLAGS) -o $@ $<
$(BIN)/test_dup_main_reject: test/wcc/842_dup_main_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_slice_str_global_zero: test/wcc/686_slice_str_global_zero.c $(BIN)/ww \
$(BIN)/w6c $(BIN)/w6a $(BIN)/w6l \
$(BIN)/ww_ww $(BIN)/w6c_ww $(BIN)/w6a_ww $(BIN)/w6l_ww \

View File

@@ -2962,6 +2962,33 @@ check_file(Checker *c, Node *file)
}
c->cur_mod = NULL;
/* Program-global, name-only, cross-module uniqueness on `main`.
* `main` lowers to ONE bare entry symbol, so a second top-level
* decl named `main` (any kind, any package) collides with the
* entry at link time — today a silent segfault / link-fail in
* both stages. The (name, module) duplicate rejects above read a
* cross-package `foo.main` and the bare entry as distinct, so they
* miss this. Correct multi-main mangling (entry stays bare, the
* rest qualify) is deferred (task #32); reject loudly meanwhile
* (rule 7). Walks USER decls only — runs before the -T synth main
* is appended below — so a hosted-test build never false-counts. */
{
Node *firstmain = NULL;
for (Node *d = file->list; d; d = d->next) {
if (d->str == NULL || strcmp(d->str, "main") != 0)
continue;
if (d->kind != N_FNDECL && d->kind != N_LET
&& d->kind != N_DEF && d->kind != N_TYPEDECL)
continue;
if (firstmain == NULL) {
firstmain = d;
continue;
}
err(c, d->pos, "duplicate top-level main: only the "
"entry main may exist (task #32)");
}
}
/*
* #15 @test harness — under `w6c -T`, synthesize the entry the
* driver would otherwise hand-wire. We sit at the seam between

View File

@@ -16697,6 +16697,35 @@ export fn checkfile(c: *checker, file: *node) void = {
d = d.next;
};
// Program-global, name-only, cross-module uniqueness on `main`.
// `main` lowers to ONE bare entry symbol, so a second top-level
// decl named `main` (any kind, any package) collides with the
// entry at link time — today a silent segfault / link-fail in
// both stages. The (name, module) duplicate rejects in installtop
// read a cross-package `foo.main` and the bare entry as distinct,
// so they miss this. Correct multi-main mangling (entry stays bare,
// the rest qualify) is deferred (task #32); reject loudly meanwhile
// (rule 7). Walks USER decls only — runs before the -T synth main
// is appended below — so a hosted-test build never false-counts.
// Twin of cmd/wcc/check.c.
let firstmain: *node = nil;
let mm: *node = file.list;
for (mm != nil) {
let ismain: bool = (mm.kind == nkind.N_FNDECL
|| mm.kind == nkind.N_LET || mm.kind == nkind.N_DEF
|| mm.kind == nkind.N_TYPEDECL) && streq(mm.str, "main");
if (ismain) {
if (firstmain == nil) {
firstmain = mm;
} else {
cerr(mm.file);
cerr(": error: duplicate top-level main: only the entry main may exist (task #32)\n");
c.errs += 1;
};
};
mm = mm.next;
};
// #15 @test harness — under `w6c_ww -T`, synthesize the entry the
// driver would otherwise hand-wire. We sit at the seam between
// fn-install (Pass 1, all names now in scope so the synth callees

View File

@@ -6227,6 +6227,35 @@ export fn checkfile(c: *checker, file: *node) void = {
d = d.next;
};
// Program-global, name-only, cross-module uniqueness on `main`.
// `main` lowers to ONE bare entry symbol, so a second top-level
// decl named `main` (any kind, any package) collides with the
// entry at link time — today a silent segfault / link-fail in
// both stages. The (name, module) duplicate rejects in installtop
// read a cross-package `foo.main` and the bare entry as distinct,
// so they miss this. Correct multi-main mangling (entry stays bare,
// the rest qualify) is deferred (task #32); reject loudly meanwhile
// (rule 7). Walks USER decls only — runs before the -T synth main
// is appended below — so a hosted-test build never false-counts.
// Twin of cmd/wcc/check.c.
let firstmain: *node = nil;
let mm: *node = file.list;
for (mm != nil) {
let ismain: bool = (mm.kind == nkind.N_FNDECL
|| mm.kind == nkind.N_LET || mm.kind == nkind.N_DEF
|| mm.kind == nkind.N_TYPEDECL) && streq(mm.str, "main");
if (ismain) {
if (firstmain == nil) {
firstmain = mm;
} else {
cerr(mm.file);
cerr(": error: duplicate top-level main: only the entry main may exist (task #32)\n");
c.errs += 1;
};
};
mm = mm.next;
};
// #15 @test harness — under `w6c_ww -T`, synthesize the entry the
// driver would otherwise hand-wire. We sit at the seam between
// fn-install (Pass 1, all names now in scope so the synth callees

View File

@@ -16697,6 +16697,35 @@ export fn checkfile(c: *checker, file: *node) void = {
d = d.next;
};
// Program-global, name-only, cross-module uniqueness on `main`.
// `main` lowers to ONE bare entry symbol, so a second top-level
// decl named `main` (any kind, any package) collides with the
// entry at link time — today a silent segfault / link-fail in
// both stages. The (name, module) duplicate rejects in installtop
// read a cross-package `foo.main` and the bare entry as distinct,
// so they miss this. Correct multi-main mangling (entry stays bare,
// the rest qualify) is deferred (task #32); reject loudly meanwhile
// (rule 7). Walks USER decls only — runs before the -T synth main
// is appended below — so a hosted-test build never false-counts.
// Twin of cmd/wcc/check.c.
let firstmain: *node = nil;
let mm: *node = file.list;
for (mm != nil) {
let ismain: bool = (mm.kind == nkind.N_FNDECL
|| mm.kind == nkind.N_LET || mm.kind == nkind.N_DEF
|| mm.kind == nkind.N_TYPEDECL) && streq(mm.str, "main");
if (ismain) {
if (firstmain == nil) {
firstmain = mm;
} else {
cerr(mm.file);
cerr(": error: duplicate top-level main: only the entry main may exist (task #32)\n");
c.errs += 1;
};
};
mm = mm.next;
};
// #15 @test harness — under `w6c_ww -T`, synthesize the entry the
// driver would otherwise hand-wire. We sit at the seam between
// fn-install (Pass 1, all names now in scope so the synth callees

View File

@@ -0,0 +1,252 @@
/*
* 841_dup_main_reject — cstage and wwstage LOUD-REJECT a program that
* declares more than one top-level `main` (F-D; rob ruling 2026-06-14).
*
* `main` lowers to ONE bare entry symbol. A second top-level decl named
* `main` (any kind, any package) collides with the entry at link time:
* the `let main` DATA symbol and the entry `fn main` TEXT symbol clash.
* Pre-fix BOTH stages built the program rc=0 and SEGFAULTED at runtime
* (the w6l silent-dup-symbol latent, task #31); align both UP to a loud
* compile-time reject. Correct multi-main mangling (entry stays bare,
* every other `main` module-qualifies) is the deferred root-unit entry
* detection (task #32) — until then the collision is rejected, never
* silently miscompiled (rule 7).
*
* The reject is program-GLOBAL, name-only, CROSS-MODULE: it counts
* top-level decls named "main" across the fully-bundled program. The
* existing duplicate-decl rejects key on (name, MODULE), so they read a
* cross-package `foo.main` and the bare entry `main` as DISTINCT and
* miss this clash. The trigger is COUNT("main") > 1, NOT package
* identity — ww has no package-main convention (examples/cmatrix, lisp,
* mandelbrot are working entries with `fn main` in a non-main package).
*
* Each reject row is a GENUINE cross-module case: a real `import foo;`
* binds package foo, so decl_mod stamps foo's `main` with module "foo"
* and the entry `main` with module NULL — DISTINCT keys the existing
* (name, module) reject reads apart and lets through. Without the
* import both `main`s collapse to module NULL and the OLD `duplicate fn
* main` reject already fires, so the import is what exercises THIS
* check (verified: pre-fix base 7a6b67f builds every row rc=0).
*
* row | shape | expect
* ----------------+-----------------------------------------+--------
* let_main | import foo; foo `let main` + entry main | REJECT
* fn_main | import foo; foo `fn main` + entry main | REJECT
* def_main | import foo; foo `def main` + entry main | REJECT
* type_main | import foo; foo `type main`+ entry main | REJECT
* single_nonmain | one `fn main` in a NON-main package | BUILD,
* | (cmatrix-style) — the corpus-safety | run 7
* | guard: a lone main must STILL compile. |
*
* DISCRIMINATION (verified against a clean 7a6b67f base): pre-fix the
* let_main/def_main rows built rc=0 then exited 139 (SIGSEGV) and
* fn_main/type_main built rc=0 and ran the WRONG entry (two bare `TEXT
* main` symbols, the w6l silent-dup latent task #31); post-fix every
* row loud-rejects at compile (build rc!=0, no binary), both stages.
* The single_nonmain row proves the reject did not eat the working
* single-`main` corpus.
*/
#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;
}
/* build_should_fail — a dup-`main` program must error on `driver`
* (loud reject); returns 0 when the build correctly FAILS, non-zero
* when it wrongly succeeded. Intermediates land in tmpdir, never the
* source tree (rule 14). */
static int
build_should_fail(const char *driver, const char *label, const char *src,
int i)
{
char s[64], tmpdir[64], cmd[1024];
snprintf(s, sizeof s, "/tmp/dupmain_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/dupmain_%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);
const char *base = strrchr(s, '/');
base = base ? base + 1 : s;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
unlink(s);
unlink(outbin);
rmdir(tmpdir);
if (rc == 0)
fprintf(stderr,
"dup_main[%s][%s]: built ok, expected a loud reject\n",
driver, label);
return rc == 0 ? -1 : 0; /* build must NOT succeed */
}
/* run_build — build a program that MUST compile, run it, return its
* exit code (the corpus-safety guard). */
static int
run_build(const char *driver, const char *label, const char *src, int i)
{
char s[64], tmpdir[64], cmd[1024];
snprintf(s, sizeof s, "/tmp/dupmain_ok_%d_%d.ww", getpid(), i);
snprintf(tmpdir, sizeof tmpdir, "/tmp/dupmain_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, "dup_main[%s][%s]: build failed (corpus "
"regression — a lone main must compile)\n", driver, label);
unlink(s); rmdir(tmpdir);
return -1;
}
const char *base = strrchr(s, '/');
base = base ? base + 1 : s;
char outbin[128];
snprintf(outbin, sizeof outbin, "%s/%s", tmpdir, base);
char *dot = strrchr(outbin, '.');
if (dot && strcmp(dot, ".ww") == 0) *dot = '\0';
int got = runwait(outbin);
unlink(s); unlink(outbin); rmdir(tmpdir);
return got;
}
struct rejrow { const char *label; const char *src; };
/* Each program declares a second top-level `main` (in package foo)
* alongside the entry `fn main` (in package main), bound by a REAL
* `import foo;` — so decl_mod stamps the two `main`s with DISTINCT
* modules ("foo" vs NULL) and the existing (name, module) reject reads
* them apart and misses the clash; only THIS program-global name-only
* reject fires. The driver bundles both packages into one unit; foo
* also exports `anchor` so the import is used and foo resolves. */
static const struct rejrow reject_rows[] = {
{ "let_main",
"package foo;\n"
"export let main: i32 = 99;\n"
"export fn anchor() i32 = { return main; };\n"
"package main;\n"
"import foo;\n"
"fn main() i32 = { return foo.anchor(); };\n" },
{ "fn_main",
"package foo;\n"
"export fn main() i32 = { return 1; };\n"
"export fn anchor() i32 = { return 2; };\n"
"package main;\n"
"import foo;\n"
"fn main() i32 = { return foo.anchor(); };\n" },
{ "def_main",
"package foo;\n"
"export def main: i32 = 5;\n"
"export fn anchor() i32 = { return main; };\n"
"package main;\n"
"import foo;\n"
"fn main() i32 = { return foo.anchor(); };\n" },
{ "type_main",
"package foo;\n"
"export type main = i32;\n"
"export fn anchor() i32 = { return 3; };\n"
"package main;\n"
"import foo;\n"
"fn main() i32 = { return foo.anchor(); };\n" },
};
struct okrow { const char *label; const char *src; int want; };
/* The corpus-safety guard: a SINGLE `fn main` in a NON-main package
* (cmatrix/lisp/mandelbrot style) MUST still compile and run. */
static const struct okrow ok_rows[] = {
{ "single_nonmain",
"package cmatrix;\n"
"fn main() i32 = { return 7; };\n",
7 },
};
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, "dup_main: 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,
"dup_main[%s][%s]: exit=%d want=%d\n",
drivers[d].name, ok_rows[i].label,
got, ok_rows[i].want);
fail++;
}
}
}
if (fail) {
fprintf(stderr, "dup_main: %d/%d fixtures failed\n",
fail, total);
return 1;
}
printf("dup_main: %d/%d ok\n", total, total);
return 0;
}