Commit 3c375f1b authored by Matthew Dempsky's avatar Matthew Dempsky

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: 's avatarRobert Griesemer <gri@golang.org>
parent 5e423ed8
......@@ -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 {
......
......@@ -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"},
......
......@@ -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)))
......
// 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
// 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"
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment