1
0
mirror of https://github.com/golang/go synced 2024-11-26 21:21:34 -07:00

cmd/compile, go/types: error if main.main is not a function

Fixes #21256.

Change-Id: I3af4c76e734c09d07f15525b793a544a7279b906
Reviewed-on: https://go-review.googlesource.com/79435
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
This commit is contained in:
Matthew Dempsky 2017-11-22 09:43:52 -08:00
parent 5e423ed855
commit 3c375f1b7e
5 changed files with 33 additions and 4 deletions

View File

@ -85,12 +85,14 @@ func declare(n *Node, ctxt Class) {
yyerror("cannot declare name %v", s)
}
if ctxt == PEXTERN && s.Name == "init" {
yyerror("cannot declare init - must be func")
}
gen := 0
if ctxt == PEXTERN {
if s.Name == "init" {
yyerror("cannot declare init - must be func")
}
if s.Name == "main" && localpkg.Name == "main" {
yyerror("cannot declare main - must be func")
}
externdcl = append(externdcl, n)
} else {
if Curfn == nil && ctxt == PAUTO {

View File

@ -69,6 +69,7 @@ var tests = [][]string{
{"testdata/decls2a.src", "testdata/decls2b.src"},
{"testdata/decls3.src"},
{"testdata/decls4.src"},
{"testdata/decls5.src"},
{"testdata/const0.src"},
{"testdata/const1.src"},
{"testdata/constdecl.src"},

View File

@ -111,6 +111,13 @@ func (check *Checker) declarePkgObj(ident *ast.Ident, obj Object, d *declInfo) {
return
}
// spec: "The main package must have package name main and declare
// a function main that takes no arguments and returns no value."
if ident.Name == "main" && check.pkg.name == "main" {
check.errorf(ident.Pos(), "cannot declare main - must be func")
return
}
check.declare(check.pkg.scope, ident, obj, token.NoPos)
check.objMap[obj] = d
obj.setOrder(uint32(len(check.objMap)))

10
src/go/types/testdata/decls5.src vendored Normal file
View File

@ -0,0 +1,10 @@
// Copyright 2017 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
// declarations of main
const _, main /* ERROR "cannot declare main" */ , _ = 0, 1, 2
type main /* ERROR "cannot declare main" */ struct{}
var _, main /* ERROR "cannot declare main" */ int

View File

@ -0,0 +1,9 @@
// errorcheck
// Copyright 2017 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
var main = func() {} // ERROR "must be func"