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>
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 {
{"testdata/decls2a.src", "testdata/decls2b.src"},
{"testdata/decls3.src"},
{"testdata/decls4.src"},
+ {"testdata/decls5.src"},
{"testdata/const0.src"},
{"testdata/const1.src"},
{"testdata/constdecl.src"},
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)))
--- /dev/null
+// 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
--- /dev/null
+// 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"