mirror of
https://github.com/golang/go.git
synced 2025-05-05 23:53:05 +00:00
[dev.regabi] go/types: must not import a package called "init"
This is a port of CL 287494 to go/types. The additional checks in test/fixedbugs are included, though they won't be executed by go/types. Support for errorcheckdir checks will be added to go/types in a later CL. Change-Id: I37e202ea5daf7d7b8fc6ae93a4c4dbd11762480f Reviewed-on: https://go-review.googlesource.com/c/go/+/290570 Reviewed-by: Robert Griesemer <gri@golang.org> Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org>
This commit is contained in:
parent
c48d1503ba
commit
493363ccff
@ -252,14 +252,6 @@ func (check *Checker) collectObjects() {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 d.spec.Name != nil {
|
if d.spec.Name != nil {
|
||||||
@ -269,10 +261,19 @@ func (check *Checker) collectObjects() {
|
|||||||
check.errorf(d.spec.Name, _ImportCRenamed, `cannot rename import "C"`)
|
check.errorf(d.spec.Name, _ImportCRenamed, `cannot rename import "C"`)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if name == "init" {
|
if name == "init" {
|
||||||
check.errorf(d.spec.Name, _InvalidInitDecl, "cannot declare init - must be func")
|
check.errorf(d.spec.Name, _InvalidInitDecl, "cannot import package as init - init must be a func")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 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(d.spec.Pos(), pkg, name, imp)
|
pkgName := NewPkgName(d.spec.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…
x
Reference in New Issue
Block a user