aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Griesemer <gri@golang.org>2022-03-30 15:09:13 -0700
committerRobert Griesemer <gri@golang.org>2022-03-31 00:27:01 +0000
commit0a6ddcc4f03b5a89bf2eadee1fe284faf0e20be2 (patch)
tree62e259464374196c3a6b2ad974482625ce90a2dd
parent077573018027c30e5f422d50f0b96e1733ac6001 (diff)
downloadgo-0a6ddcc4f03b5a89bf2eadee1fe284faf0e20be2.tar.xz
go/types, types2: no "imported but not used" error for unimported packages
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>
-rw-r--r--src/cmd/compile/internal/types2/resolver.go13
-rw-r--r--src/cmd/compile/internal/types2/testdata/fixedbugs/issue43109.go10
-rw-r--r--src/go/types/resolver.go13
-rw-r--r--src/go/types/testdata/fixedbugs/issue43109.go10
4 files changed, 34 insertions, 12 deletions
diff --git a/src/cmd/compile/internal/types2/resolver.go b/src/cmd/compile/internal/types2/resolver.go
index 61963cb043..5c64ecdfc8 100644
--- a/src/cmd/compile/internal/types2/resolver.go
+++ b/src/cmd/compile/internal/types2/resolver.go
@@ -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
index 0000000000..a4533c9bf7
--- /dev/null
+++ b/src/cmd/compile/internal/types2/testdata/fixedbugs/issue43109.go
@@ -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
diff --git a/src/go/types/resolver.go b/src/go/types/resolver.go
index 9edf41bf3c..ae21c6d927 100644
--- a/src/go/types/resolver.go
+++ b/src/go/types/resolver.go
@@ -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
index 0000000000..a4533c9bf7
--- /dev/null
+++ b/src/go/types/testdata/fixedbugs/issue43109.go
@@ -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