Skip to content

Commit

Permalink
[dev.regabi] go/types: report error for invalid main function signature
Browse files Browse the repository at this point in the history
This is a port of CL 279424, which didn't make it into master in time
for go1.16. Move it to dev.regabi so that it may be merged.

Notably, this port no longer removes the _InvalidInitSig error code,
instead opting to deprecate it. Now that error codes are 'locked in' for
go1.16, even if their API may not yet be exposed, we should follow the
practice of not changing their values. In the future, code generation
can make it easier to keep error code values constant.

For #43308

Change-Id: I5260b93fd063393d38d6458e45a67e7f9b7426ed
Reviewed-on: https://go-review.googlesource.com/c/go/+/289714
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
  • Loading branch information
findleyr committed Feb 4, 2021
1 parent 52d5cb2 commit 120b819
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
8 changes: 6 additions & 2 deletions src/go/types/decl.go
Original file line number Diff line number Diff line change
Expand Up @@ -751,8 +751,12 @@ func (check *Checker) funcDecl(obj *Func, decl *declInfo) {
obj.typ = sig // guard against cycles
fdecl := decl.fdecl
check.funcType(sig, fdecl.Recv, fdecl.Type)
if sig.recv == nil && obj.name == "init" && (sig.params.Len() > 0 || sig.results.Len() > 0) {
check.errorf(fdecl, _InvalidInitSig, "func init must have no arguments and no return values")
if sig.recv == nil {
if obj.name == "init" && (sig.params.Len() > 0 || sig.results.Len() > 0) {
check.errorf(fdecl, _InvalidInitDecl, "func init must have no arguments and no return values")
} else if obj.name == "main" && check.pkg.name == "main" && (sig.params.Len() > 0 || sig.results.Len() > 0) {
check.errorf(fdecl, _InvalidMainDecl, "func main must have no arguments and no return values")
}
// ok to continue
}

Expand Down
7 changes: 5 additions & 2 deletions src/go/types/errorcodes.go
Original file line number Diff line number Diff line change
Expand Up @@ -386,15 +386,18 @@ const (
// _InvalidInitSig occurs when an init function declares parameters or
// results.
//
// Example:
// func init() int { return 1 }
// Deprecated: no longer emitted by the type checker. _InvalidInitDecl is
// used instead.
_InvalidInitSig

// _InvalidInitDecl occurs when init is declared as anything other than a
// function.
//
// Example:
// var init = 1
//
// Example:
// func init() int { return 1 }
_InvalidInitDecl

// _InvalidMainDecl occurs when main is declared as anything other than a
Expand Down
9 changes: 9 additions & 0 deletions src/go/types/testdata/main.src
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 120b819

Please sign in to comment.