mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00: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:
parent
217a461f56
commit
f7d1c5990b
5 changed files with 35 additions and 13 deletions
|
|
@ -250,14 +250,6 @@ func (check *Checker) collectObjects() {
|
||||||
continue
|
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
|
// local name overrides imported package name
|
||||||
name := imp.name
|
name := imp.name
|
||||||
if s.LocalPkgName != nil {
|
if s.LocalPkgName != nil {
|
||||||
|
|
@ -267,10 +259,19 @@ func (check *Checker) collectObjects() {
|
||||||
check.errorf(s.LocalPkgName, `cannot rename import "C"`)
|
check.errorf(s.LocalPkgName, `cannot rename import "C"`)
|
||||||
continue
|
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)
|
pkgName := NewPkgName(s.Pos(), pkg, name, imp)
|
||||||
|
|
|
||||||
|
|
@ -10,7 +10,7 @@ import (
|
||||||
// we can have multiple blank imports (was bug)
|
// we can have multiple blank imports (was bug)
|
||||||
_ "math"
|
_ "math"
|
||||||
_ "net/rpc"
|
_ "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 defines a type "flag" which shows up in the gc export data
|
||||||
"reflect"
|
"reflect"
|
||||||
. /* ERROR "imported but not used" */ "reflect"
|
. /* ERROR "imported but not used" */ "reflect"
|
||||||
|
|
|
||||||
5
test/fixedbugs/issue43962.dir/a.go
Normal file
5
test/fixedbugs/issue43962.dir/a.go
Normal 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
|
||||||
7
test/fixedbugs/issue43962.dir/b.go
Normal file
7
test/fixedbugs/issue43962.dir/b.go
Normal 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"
|
||||||
9
test/fixedbugs/issue43962.go
Normal file
9
test/fixedbugs/issue43962.go
Normal 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
|
||||||
Loading…
Add table
Add a link
Reference in a new issue