]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile, go/types: error if main.main is not a function
authorMatthew Dempsky <mdempsky@google.com>
Wed, 22 Nov 2017 17:43:52 +0000 (09:43 -0800)
committerMatthew Dempsky <mdempsky@google.com>
Wed, 22 Nov 2017 19:14:31 +0000 (19:14 +0000)
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>
src/cmd/compile/internal/gc/dcl.go
src/go/types/check_test.go
src/go/types/resolver.go
src/go/types/testdata/decls5.src [new file with mode: 0644]
test/fixedbugs/issue21256.go [new file with mode: 0644]

index b39bdb5aa035a6c7585483423258928ed896ba3c..7d963864570cb742a9524d8750955c7542e6dde5 100644 (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 {
index 24b3365717b631af8fe0ae5733a516d32c2aa78e..97e224f870fb4c102587a514b7714a80623d1a87 100644 (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"},
index ba75a0dc2398c67454407af4bb49a936ac33adb0..7bcfaabcde078d5f5631c43923c1cdad4942822b 100644 (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)))
diff --git a/src/go/types/testdata/decls5.src b/src/go/types/testdata/decls5.src
new file mode 100644 (file)
index 0000000..88d3194
--- /dev/null
@@ -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
diff --git a/test/fixedbugs/issue21256.go b/test/fixedbugs/issue21256.go
new file mode 100644 (file)
index 0000000..3d36124
--- /dev/null
@@ -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"