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:
parent
b2560d12f6
commit
6e2a0ce36e
|
|
@ -86,12 +86,19 @@ func definition(q *Query) error {
|
||||||
return fmt.Errorf("no identifier here")
|
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 {
|
if obj == nil {
|
||||||
// Happens for y in "switch y := x.(type)",
|
obj = qpos.info.Defs[id]
|
||||||
// and the package declaration,
|
if obj == nil {
|
||||||
// but I think that's all.
|
// Happens for y in "switch y := x.(type)",
|
||||||
return fmt.Errorf("no object for identifier")
|
// and the package declaration,
|
||||||
|
// but I think that's all.
|
||||||
|
return fmt.Errorf("no object for identifier")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if !obj.Pos().IsValid() {
|
if !obj.Pos().IsValid() {
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,8 @@
|
||||||
package definition
|
package definition
|
||||||
|
|
||||||
// Tests of 'definition' query, -json output.
|
// Tests of 'definition' query, -json output.
|
||||||
// See go.tools/guru/guru_test.go for explanation.
|
// See golang.org/x/tools/cmd/guru/guru_test.go for explanation.
|
||||||
// See definition.golden for expected query results.
|
// See main.golden for expected query results.
|
||||||
|
|
||||||
// TODO(adonovan): test: selection of member of same package defined in another file.
|
// 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()
|
func (T) method()
|
||||||
|
|
||||||
type U struct{ T }
|
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"
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -65,3 +65,29 @@ Error: no object for identifier
|
||||||
"objpos": "testdata/src/definition-json/main.go:42:10",
|
"objpos": "testdata/src/definition-json/main.go:42:10",
|
||||||
"desc": "func (T).method()"
|
"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"
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,3 @@
|
||||||
|
package definition
|
||||||
|
|
||||||
|
type W int
|
||||||
|
|
@ -32,6 +32,10 @@
|
||||||
{
|
{
|
||||||
"pos": "testdata/src/definition-json/main.go:30:8",
|
"pos": "testdata/src/definition-json/main.go:30:8",
|
||||||
"text": "\tvar _ lib.Nonesuch // @definition qualified-nomember \"Nonesuch\""
|
"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\""
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ references to package lib
|
||||||
_ = (lib.Type).Method // ref from internal test package
|
_ = (lib.Type).Method // ref from internal test package
|
||||||
const c = lib.Const // @describe ref-const "Const"
|
const c = lib.Const // @describe ref-const "Const"
|
||||||
lib.Func() // @describe ref-func "Func"
|
lib.Func() // @describe ref-func "Func"
|
||||||
|
lib.Type // @definition embedded-other-pkg "Type"
|
||||||
lib.Var++ // @describe ref-var "Var"
|
lib.Var++ // @describe ref-var "Var"
|
||||||
var _ lib.Const // @definition qualified-const "Const"
|
var _ lib.Const // @definition qualified-const "Const"
|
||||||
var _ lib.Func // @definition qualified-func "Func"
|
var _ lib.Func // @definition qualified-func "Func"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue