1
0
mirror of https://github.com/golang/go synced 2024-09-29 11:24:28 -06:00

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>
This commit is contained in:
Robert Griesemer 2022-03-30 15:09:13 -07:00
parent 0775730180
commit 0a6ddcc4f0
4 changed files with 34 additions and 12 deletions

View 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
}

View File

@ -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

View 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
}

View File

@ -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