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