1
0
mirror of https://github.com/golang/go synced 2024-11-18 16:54:43 -07:00

imports: fix renamed sibling imports more

This is the fix I should have made in CL 153440. What I get for being in
a rush.

Fixes golang/go#29180

Change-Id: I7ee3e26173b86c70574b9710f84094e46db27a37
Reviewed-on: https://go-review.googlesource.com/c/153859
Run-TryBot: Heschi Kreinick <heschi@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Heschi Kreinick 2018-12-12 13:11:39 -05:00
parent 837e80568c
commit 5f4a60f04f
2 changed files with 34 additions and 1 deletions

View File

@ -200,7 +200,12 @@ func (p *pass) findMissingImport(pkg string, syms map[string]bool) *importInfo {
if !ok {
continue
}
if candidate.name != pkg && pkgInfo.name != pkg {
// If the candidate import has a name, it must match pkg.
if candidate.name != "" && candidate.name != pkg {
continue
}
// Otherwise, the real name of the package must match.
if candidate.name == "" && pkgInfo.name != pkg {
continue
}

View File

@ -1966,6 +1966,34 @@ func LogSomethingElse() {
}.processTest(t, "foo.com", "p/needs_import.go", nil, nil, want)
}
// Tests #29180: a sibling import of the right package with the wrong name is used.
func TestSiblingImport_Misnamed(t *testing.T) {
const sibling = `package main
import renamed "fmt"
var _ = renamed.Printf
`
const input = `package pkg
var _ = fmt.Printf
`
const want = `package pkg
import "fmt"
var _ = fmt.Printf
`
testConfig{
module: packagestest.Module{
Name: "foo.com",
Files: fm{
"pkg/main.go": sibling,
"pkg/uses.go": input,
},
},
}.processTest(t, "foo.com", "pkg/uses.go", nil, nil, want)
}
func TestPkgIsCandidate(t *testing.T) {
tests := []struct {
name string