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

cmd/guru: show correct definition for embedded type.

Fixes golang/go#16263.

Change-Id: I081a12306ac5415d2223e3509a29a1b47700e1ff
Reviewed-on: https://go-review.googlesource.com/27001
Reviewed-by: Alan Donovan <adonovan@google.com>
This commit is contained in:
David R. Jenni 2016-08-14 17:12:16 -06:00 committed by Alan Donovan
parent b2560d12f6
commit 6e2a0ce36e
6 changed files with 72 additions and 7 deletions

View File

@ -86,12 +86,19 @@ func definition(q *Query) error {
return fmt.Errorf("no identifier here")
}
obj := qpos.info.ObjectOf(id)
// Look up the declaration of this identifier.
// If id is an anonymous field declaration,
// it is both a use of a type and a def of a field;
// prefer the use in that case.
obj := qpos.info.Uses[id]
if obj == nil {
// Happens for y in "switch y := x.(type)",
// and the package declaration,
// but I think that's all.
return fmt.Errorf("no object for identifier")
obj = qpos.info.Defs[id]
if obj == nil {
// Happens for y in "switch y := x.(type)",
// and the package declaration,
// but I think that's all.
return fmt.Errorf("no object for identifier")
}
}
if !obj.Pos().IsValid() {

View File

@ -1,8 +1,8 @@
package definition
// Tests of 'definition' query, -json output.
// See go.tools/guru/guru_test.go for explanation.
// See definition.golden for expected query results.
// See golang.org/x/tools/cmd/guru/guru_test.go for explanation.
// See main.golden for expected query results.
// TODO(adonovan): test: selection of member of same package defined in another file.
@ -42,3 +42,27 @@ type T struct{ field int }
func (T) method()
type U struct{ T }
type V1 struct {
W // @definition embedded-other-file "W"
}
type V2 struct {
*W // @definition embedded-other-file-pointer "W"
}
type V3 struct {
int // @definition embedded-basic "int"
}
type V4 struct {
*int // @definition embedded-basic-pointer "int"
}
type V5 struct {
lib.Type // @definition embedded-other-pkg "Type"
}
type V6 struct {
T // @definition embedded-same-file "T"
}

View File

@ -65,3 +65,29 @@ Error: no object for identifier
"objpos": "testdata/src/definition-json/main.go:42:10",
"desc": "func (T).method()"
}
-------- @definition embedded-other-file --------
{
"objpos": "testdata/src/definition-json/type.go:3:6",
"desc": "type W int"
}
-------- @definition embedded-other-file-pointer --------
{
"objpos": "testdata/src/definition-json/type.go:3:6",
"desc": "type W int"
}
-------- @definition embedded-basic --------
Error: int is built in
-------- @definition embedded-basic-pointer --------
Error: int is built in
-------- @definition embedded-other-pkg --------
{
"objpos": "testdata/src/lib/lib.go:3:6",
"desc": "type lib.Type"
}
-------- @definition embedded-same-file --------
{
"objpos": "$GOPATH/src/definition-json/main.go:40:6",
"desc": "type T"
}

View File

@ -0,0 +1,3 @@
package definition
type W int

View File

@ -32,6 +32,10 @@
{
"pos": "testdata/src/definition-json/main.go:30:8",
"text": "\tvar _ lib.Nonesuch // @definition qualified-nomember \"Nonesuch\""
},
{
"pos": "testdata/src/definition-json/main.go:63:2",
"text": "\tlib.Type // @definition embedded-other-pkg \"Type\""
}
]
}

View File

@ -13,6 +13,7 @@ references to package lib
_ = (lib.Type).Method // ref from internal test package
const c = lib.Const // @describe ref-const "Const"
lib.Func() // @describe ref-func "Func"
lib.Type // @definition embedded-other-pkg "Type"
lib.Var++ // @describe ref-var "Var"
var _ lib.Const // @definition qualified-const "Const"
var _ lib.Func // @definition qualified-func "Func"