diff --git a/src/cmd/compile/internal/types2/resolver.go b/src/cmd/compile/internal/types2/resolver.go index 2c98ca20e30..7ea9bde5fad 100644 --- a/src/cmd/compile/internal/types2/resolver.go +++ b/src/cmd/compile/internal/types2/resolver.go @@ -397,15 +397,16 @@ func (check *Checker) collectObjects() { obj := NewFunc(d.Name.Pos(), pkg, name, nil) if d.Recv == nil { // regular function - if name == "init" { + if name == "init" || name == "main" && pkg.name == "main" { if d.TParamList != nil { - //check.softErrorf(d.TParamList.Pos(), "func init must have no type parameters") - check.softErrorf(d.Name, "func init must have no type parameters") + check.softErrorf(d, "func %s must have no type parameters", name) } if t := d.Type; len(t.ParamList) != 0 || len(t.ResultList) != 0 { - check.softErrorf(d, "func init must have no arguments and no return values") + check.softErrorf(d, "func %s must have no arguments and no return values", name) } - // don't declare init functions in the package scope - they are invisible + } + // don't declare init functions in the package scope - they are invisible + if name == "init" { obj.parent = pkg.scope check.recordDef(d.Name, obj) // init functions must have a body diff --git a/src/cmd/compile/internal/types2/testdata/main.go2 b/src/cmd/compile/internal/types2/testdata/main.go2 new file mode 100644 index 00000000000..b7ddeaa1a88 --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/main.go2 @@ -0,0 +1,7 @@ +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +func /* ERROR "func main must have no type parameters" */ main[T any]() {} diff --git a/src/cmd/compile/internal/types2/testdata/main.src b/src/cmd/compile/internal/types2/testdata/main.src new file mode 100644 index 00000000000..f892938d4ac --- /dev/null +++ b/src/cmd/compile/internal/types2/testdata/main.src @@ -0,0 +1,9 @@ +// Copyright 2020 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package main + +func main() +func /* ERROR "no arguments and no return values" */ main /* ERROR redeclared */ (int) +func /* ERROR "no arguments and no return values" */ main /* ERROR redeclared */ () int diff --git a/test/run.go b/test/run.go index 5ec33f16f20..fcf8a4fcc96 100644 --- a/test/run.go +++ b/test/run.go @@ -1936,7 +1936,6 @@ var excluded = map[string]bool{ "import6.go": true, // issue #43109 "initializerr.go": true, // types2 reports extra errors "linkname2.go": true, // error reported by noder (not running for types2 errorcheck test) - "mainsig.go": true, // issue #43308 "shift1.go": true, // issue #42989 "switch4.go": true, // error reported by noder (not running for types2 errorcheck test) "typecheck.go": true, // invalid function is not causing errors when called