]> Cypherpunks repositories - gostls13.git/commitdiff
go/types, types2: no "imported but not used" error for unimported packages
authorRobert Griesemer <gri@golang.org>
Wed, 30 Mar 2022 22:09:13 +0000 (15:09 -0700)
committerRobert Griesemer <gri@golang.org>
Thu, 31 Mar 2022 00:27:01 +0000 (00:27 +0000)
If a package could not be imported for whatever reason, the type checker
creates fake package with which it continues for more tolerant type
checking.

Do not report an "imported but not used" error in that case.

Clarify a few comments along the way.

Fixes #43109.

Change-Id: Ifeec0daa688fbf666412dc9176ff1522d02a23ef
Reviewed-on: https://go-review.googlesource.com/c/go/+/396875
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
src/cmd/compile/internal/types2/resolver.go
src/cmd/compile/internal/types2/testdata/fixedbugs/issue43109.go [new file with mode: 0644]
src/go/types/resolver.go
src/go/types/testdata/fixedbugs/issue43109.go [new file with mode: 0644]

index 61963cb0437eeda5685a5a3595801ae09a348fe3..5c64ecdfc80819ec819c4f1b68eb2f754f782563 100644 (file)
@@ -179,8 +179,9 @@ func (check *Checker) importPackage(pos syntax.Pos, path, dir string) *Package {
        // package should be complete or marked fake, but be cautious
        if imp.complete || imp.fake {
                check.impMap[key] = imp
-               // Once we've formatted an error message once, keep the pkgPathMap
-               // up-to-date on subsequent imports.
+               // Once we've formatted an error message, keep the pkgPathMap
+               // up-to-date on subsequent imports. It is used for package
+               // qualification in error messages.
                if check.pkgPathMap != nil {
                        check.markImports(imp)
                }
@@ -268,7 +269,7 @@ func (check *Checker) collectObjects() {
                                if s.LocalPkgName != nil {
                                        name = s.LocalPkgName.Value
                                        if path == "C" {
-                                               // match cmd/compile (not prescribed by spec)
+                                               // match 1.17 cmd/compile (not prescribed by spec)
                                                check.error(s.LocalPkgName, `cannot rename import "C"`)
                                                continue
                                        }
@@ -295,8 +296,8 @@ func (check *Checker) collectObjects() {
                                        check.recordImplicit(s, pkgName)
                                }
 
-                               if path == "C" {
-                                       // match cmd/compile (not prescribed by spec)
+                               if imp.fake {
+                                       // match 1.17 cmd/compile (not prescribed by spec)
                                        pkgName.used = true
                                }
 
@@ -700,7 +701,7 @@ func (a inSourceOrder) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
 
 // unusedImports checks for unused imports.
 func (check *Checker) unusedImports() {
-       // if function bodies are not checked, packages' uses are likely missing - don't check
+       // If function bodies are not checked, packages' uses are likely missing - don't check.
        if check.conf.IgnoreFuncBodies {
                return
        }
diff --git a/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43109.go b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43109.go
new file mode 100644 (file)
index 0000000..a4533c9
--- /dev/null
@@ -0,0 +1,10 @@
+// Copyright 2022 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.
+
+// Ensure there is no "imported but not used" error
+// if a package wasn't imported in the first place.
+
+package p
+
+import . "/foo" // ERROR could not import \/foo
index 9edf41bf3c2f5fc63f2c639c0fce9cfc3e4b19dc..ae21c6d927726d506b2120c74b85f5b62e4d8faf 100644 (file)
@@ -192,8 +192,9 @@ func (check *Checker) importPackage(at positioner, path, dir string) *Package {
        // package should be complete or marked fake, but be cautious
        if imp.complete || imp.fake {
                check.impMap[key] = imp
-               // Once we've formatted an error message once, keep the pkgPathMap
-               // up-to-date on subsequent imports.
+               // Once we've formatted an error message, keep the pkgPathMap
+               // up-to-date on subsequent imports. It is used for package
+               // qualification in error messages.
                if check.pkgPathMap != nil {
                        check.markImports(imp)
                }
@@ -269,7 +270,7 @@ func (check *Checker) collectObjects() {
                                if d.spec.Name != nil {
                                        name = d.spec.Name.Name
                                        if path == "C" {
-                                               // match cmd/compile (not prescribed by spec)
+                                               // match 1.17 cmd/compile (not prescribed by spec)
                                                check.errorf(d.spec.Name, _ImportCRenamed, `cannot rename import "C"`)
                                                return
                                        }
@@ -296,8 +297,8 @@ func (check *Checker) collectObjects() {
                                        check.recordImplicit(d.spec, pkgName)
                                }
 
-                               if path == "C" {
-                                       // match cmd/compile (not prescribed by spec)
+                               if imp.fake {
+                                       // match 1.17 cmd/compile (not prescribed by spec)
                                        pkgName.used = true
                                }
 
@@ -673,7 +674,7 @@ func (a inSourceOrder) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }
 
 // unusedImports checks for unused imports.
 func (check *Checker) unusedImports() {
-       // if function bodies are not checked, packages' uses are likely missing - don't check
+       // If function bodies are not checked, packages' uses are likely missing - don't check.
        if check.conf.IgnoreFuncBodies {
                return
        }
diff --git a/src/go/types/testdata/fixedbugs/issue43109.go b/src/go/types/testdata/fixedbugs/issue43109.go
new file mode 100644 (file)
index 0000000..a4533c9
--- /dev/null
@@ -0,0 +1,10 @@
+// Copyright 2022 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.
+
+// Ensure there is no "imported but not used" error
+// if a package wasn't imported in the first place.
+
+package p
+
+import . "/foo" // ERROR could not import \/foo