refactor/rename: fix crash when renaming type embedded in another package.

Fixes golang/go#12038.

Change-Id: I9026edef7f8769b451f2b1502c107d6b2bb45096
Reviewed-on: https://go-review.googlesource.com/13451
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
David R. Jenni 2015-08-09 19:07:02 +02:00 committed by Alan Donovan
parent a8498a77d5
commit e7d14dfe5b
2 changed files with 60 additions and 0 deletions

View File

@ -720,6 +720,57 @@ type _ struct{ *foo.U }
},
},
// Renaming of embedded field that is a qualified reference with the '-from' flag.
// (Regression test for bug 12038.)
{
ctxt: fakeContext(map[string][]string{
"foo": {`package foo; type T int`},
"main": {`package main
import "foo"
type V struct{ *foo.T }
`},
}),
from: "(main.V).T", to: "U", // the "T" in *foo.T
want: map[string]string{
"/go/src/foo/0.go": `package foo
type U int
`,
"/go/src/main/0.go": `package main
import "foo"
type V struct{ *foo.U }
`,
},
},
{
ctxt: fakeContext(map[string][]string{
"foo": {`package foo; type T int`},
"main": {`package main
import "foo"
type V struct{ foo.T }
`},
}),
from: "(main.V).T", to: "U", // the "T" in *foo.T
want: map[string]string{
"/go/src/foo/0.go": `package foo
type U int
`,
"/go/src/main/0.go": `package main
import "foo"
type V struct{ foo.U }
`,
},
},
// Interface method renaming.
{
ctxt: fakeContext(map[string][]string{

View File

@ -452,6 +452,15 @@ func findObjects(info *loader.PackageInfo, spec *spec) ([]types.Object, error) {
}
if spec.searchFor == "" {
// If it is an embedded field, return the type of the field.
if v, ok := obj.(*types.Var); ok && v.Anonymous() {
switch t := v.Type().(type) {
case *types.Pointer:
return []types.Object{t.Elem().(*types.Named).Obj()}, nil
case *types.Named:
return []types.Object{t.Obj()}, nil
}
}
return []types.Object{obj}, nil
}