1
0
mirror of https://github.com/golang/go synced 2024-11-22 20:50:05 -07:00

[dev.typeparams] cmd/compile/internal/types2: must not import a package called "init"

Updates #43962.

Change-Id: I070153c55baec62d13ca9284f02781b8c1276844
Reviewed-on: https://go-review.googlesource.com/c/go/+/287494
Trust: Robert Griesemer <gri@golang.org>
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Robert Griesemer 2021-01-27 18:04:46 -08:00
parent 217a461f56
commit f7d1c5990b
5 changed files with 35 additions and 13 deletions

View File

@ -250,14 +250,6 @@ func (check *Checker) collectObjects() {
continue
}
// add package to list of explicit imports
// (this functionality is provided as a convenience
// for clients; it is not needed for type-checking)
if !pkgImports[imp] {
pkgImports[imp] = true
pkg.imports = append(pkg.imports, imp)
}
// local name overrides imported package name
name := imp.name
if s.LocalPkgName != nil {
@ -267,10 +259,19 @@ func (check *Checker) collectObjects() {
check.errorf(s.LocalPkgName, `cannot rename import "C"`)
continue
}
if name == "init" {
check.errorf(s.LocalPkgName, "cannot declare init - must be func")
continue
}
}
if name == "init" {
check.errorf(s.LocalPkgName, "cannot import package as init - init must be a func")
continue
}
// add package to list of explicit imports
// (this functionality is provided as a convenience
// for clients; it is not needed for type-checking)
if !pkgImports[imp] {
pkgImports[imp] = true
pkg.imports = append(pkg.imports, imp)
}
pkgName := NewPkgName(s.Pos(), pkg, name, imp)

View File

@ -10,7 +10,7 @@ import (
// we can have multiple blank imports (was bug)
_ "math"
_ "net/rpc"
init /* ERROR "cannot declare init" */ "fmt"
init /* ERROR "cannot import package as init" */ "fmt"
// reflect defines a type "flag" which shows up in the gc export data
"reflect"
. /* ERROR "imported but not used" */ "reflect"

View File

@ -0,0 +1,5 @@
// Copyright 2021 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 init

View File

@ -0,0 +1,7 @@
// Copyright 2021 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 b
import "./a" // ERROR "cannot import package as init"

View File

@ -0,0 +1,9 @@
// errorcheckdir
// Copyright 2021 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.
// Issue 43962: Importing a package called "init" is an error.
package ignored