ww package: reject non-function main declarations

This commit is contained in:
2026-08-21 15:13:09 +09:00
parent 2b7fafde0c
commit 2387d3e2d2
6 changed files with 520 additions and 3 deletions

View File

@@ -3885,6 +3885,35 @@ classify_init_decls(Checker *c, Node *file)
}
}
/* Pinned Go 1.26.5 types2 rejects a non-function package-scope `main`
* before declaring it, but only when the declared package name is `main`
* (resolver.go:90-110 declarePkgObj). WW's function entry ABI deliberately
* permits argument/result-bearing functions, so this is the independent
* declaration-kind rule: canonical identity, physical directory, path leaf,
* and root/action status are not inputs. Remove rejected declarations from
* later name installation just as the pinned resolver returns without
* declaring its object. */
static void
reject_nonfunction_main_decls(Checker *c, Node *file)
{
Node *prev = NULL;
for (Node *d = file->list; d; ) {
Node *next = d->next;
int invalid = top_decl_kind(d) && d->kind != N_FNDECL
&& d->str != NULL && strcmp(d->str, "main") == 0
&& d->pkgname != NULL && strcmp(d->pkgname, "main") == 0;
if (invalid) {
err(c, d->pos, "cannot declare main - must be func");
if (prev == NULL)
file->list = next;
else
prev->next = next;
} else
prev = d;
d = next;
}
}
/* Import usage is a property of the file-local qualifier occurrence. Record
* qualified syntax before resolving declaration bodies so import diagnostics
* retain production Go's source order without making a failed bare lookup a
@@ -4049,6 +4078,7 @@ check_file(Checker *c, Node *file)
file->list = usenode;
}
}
reject_nonfunction_main_decls(c, file);
mark_import_uses(c, file);
check_import_redeclarations(c, file);
check_import_usage_and_collisions(c, file);