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
name := imp.name
if d.spec.Name != nil {
check.errorf(d.spec.Name, _ImportCRenamed, `cannot rename import "C"`)
return
}
- if name == "init" {
- check.errorf(d.spec.Name, _InvalidInitDecl, "cannot declare init - must be func")
- return
- }
+ }
+
+ if name == "init" {
+ check.errorf(d.spec.Name, _InvalidInitDecl, "cannot import package as init - init must be a func")
+ 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)
// 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"
--- /dev/null
+// 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
--- /dev/null
+// 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"
--- /dev/null
+// 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