mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
[dev.typeparams] cmd/compile/internal/types2: report error for invalid main function signature
Updates #43308. Change-Id: I2caff83f304c7e104edda76ac3623cce9fc94a8d Reviewed-on: https://go-review.googlesource.com/c/go/+/279552 Trust: Robert Griesemer <gri@golang.org> Run-TryBot: Robert Griesemer <gri@golang.org> Reviewed-by: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
parent
53c4c17b09
commit
41e7901ca4
4 changed files with 22 additions and 6 deletions
|
|
@ -397,15 +397,16 @@ func (check *Checker) collectObjects() {
|
||||||
obj := NewFunc(d.Name.Pos(), pkg, name, nil)
|
obj := NewFunc(d.Name.Pos(), pkg, name, nil)
|
||||||
if d.Recv == nil {
|
if d.Recv == nil {
|
||||||
// regular function
|
// regular function
|
||||||
if name == "init" {
|
if name == "init" || name == "main" && pkg.name == "main" {
|
||||||
if d.TParamList != nil {
|
if d.TParamList != nil {
|
||||||
//check.softErrorf(d.TParamList.Pos(), "func init must have no type parameters")
|
check.softErrorf(d, "func %s must have no type parameters", name)
|
||||||
check.softErrorf(d.Name, "func init must have no type parameters")
|
|
||||||
}
|
}
|
||||||
if t := d.Type; len(t.ParamList) != 0 || len(t.ResultList) != 0 {
|
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
|
obj.parent = pkg.scope
|
||||||
check.recordDef(d.Name, obj)
|
check.recordDef(d.Name, obj)
|
||||||
// init functions must have a body
|
// init functions must have a body
|
||||||
|
|
|
||||||
7
src/cmd/compile/internal/types2/testdata/main.go2
vendored
Normal file
7
src/cmd/compile/internal/types2/testdata/main.go2
vendored
Normal file
|
|
@ -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]() {}
|
||||||
9
src/cmd/compile/internal/types2/testdata/main.src
vendored
Normal file
9
src/cmd/compile/internal/types2/testdata/main.src
vendored
Normal file
|
|
@ -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
|
||||||
|
|
@ -1936,7 +1936,6 @@ var excluded = map[string]bool{
|
||||||
"import6.go": true, // issue #43109
|
"import6.go": true, // issue #43109
|
||||||
"initializerr.go": true, // types2 reports extra errors
|
"initializerr.go": true, // types2 reports extra errors
|
||||||
"linkname2.go": true, // error reported by noder (not running for types2 errorcheck test)
|
"linkname2.go": true, // error reported by noder (not running for types2 errorcheck test)
|
||||||
"mainsig.go": true, // issue #43308
|
|
||||||
"shift1.go": true, // issue #42989
|
"shift1.go": true, // issue #42989
|
||||||
"switch4.go": true, // error reported by noder (not running for types2 errorcheck test)
|
"switch4.go": true, // error reported by noder (not running for types2 errorcheck test)
|
||||||
"typecheck.go": true, // invalid function is not causing errors when called
|
"typecheck.go": true, // invalid function is not causing errors when called
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue