Skip to content

Commit

Permalink
[dev.go2go] go/go2go: don't import renamed package twice
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
ianlancetaylor committed Jul 23, 2020
1 parent 0030e13 commit 64148d3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/go/go2go/rewrite.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
18 changes: 18 additions & 0 deletions test/gen/g042.go2
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit 64148d3

Please sign in to comment.