]> Cypherpunks.ru repositories - gostls13.git/commitdiff
[dev.typeparams] cmd/compile/internal/types2: must not import a package called "init"
authorRobert Griesemer <gri@golang.org>
Thu, 28 Jan 2021 02:04:46 +0000 (18:04 -0800)
committerRobert Griesemer <gri@golang.org>
Thu, 28 Jan 2021 16:29:00 +0000 (16:29 +0000)
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>
src/cmd/compile/internal/types2/resolver.go
src/cmd/compile/internal/types2/testdata/importdecl0/importdecl0a.src
test/fixedbugs/issue43962.dir/a.go [new file with mode: 0644]
test/fixedbugs/issue43962.dir/b.go [new file with mode: 0644]
test/fixedbugs/issue43962.go [new file with mode: 0644]

index 2a84015cfce43aa6c2a5ac8c46bd60a8db662ebf..44fa51a8e52e722911f4dd88ca4fdedb79a5541b 100644 (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)
index e96fca3cdd56fa86cb284276e78412b3c5f296a8..5ceb96e1fada684387f20c56894810a772875de2 100644 (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"
diff --git a/test/fixedbugs/issue43962.dir/a.go b/test/fixedbugs/issue43962.dir/a.go
new file mode 100644 (file)
index 0000000..168b206
--- /dev/null
@@ -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
diff --git a/test/fixedbugs/issue43962.dir/b.go b/test/fixedbugs/issue43962.dir/b.go
new file mode 100644 (file)
index 0000000..f55fea1
--- /dev/null
@@ -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"
diff --git a/test/fixedbugs/issue43962.go b/test/fixedbugs/issue43962.go
new file mode 100644 (file)
index 0000000..dca4d07
--- /dev/null
@@ -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