[dev.go2go] go/go2go: don't import renamed package twice

We keep an import spec if it uses a local alias. Avoid importing it
again, unless it is required by something else.

Fixes #40318

Change-Id: If90de3bf30412645c9144083372e6b07df3c6a0b
Reviewed-on: https://go-review.googlesource.com/c/go/+/244621
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Ian Lance Taylor 2020-07-23 16:14:38 -07:00
parent 0030e13efb
commit 64148d34c7
2 changed files with 26 additions and 3 deletions

View File

@ -284,11 +284,16 @@ func rewriteAST(fset *token.FileSet, importer *Importer, importPath string, tpkg
}
// We picked up Go 2 imports above, but we still
// need to pick up Go 1 imports here.
path := strings.TrimPrefix(strings.TrimSuffix(imp.Path.Value, `"`), `"`)
if imps[path] {
path, err := strconv.Unquote(imp.Path.Value)
if err != nil || imps[path] {
continue
}
imps[path] = true
if imp.Name == nil {
// If Name != nil we are keeping the spec.
// Don't add the import again here,
// only if it is needed elsewhere.
imps[path] = true
}
for _, p := range importer.transitiveImports(path) {
imps[p] = true
}

18
test/gen/g042.go2 Normal file
View File

@ -0,0 +1,18 @@
// compile
// Copyright 2020 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 40318
package p
import (
crand "crypto/rand"
"math/rand"
)
func F() {
_ = crand.Reader
_ = rand.Source(nil)
}