diff --git a/cmd/guru/definition.go b/cmd/guru/definition.go index 575ed92a00..46d48060b1 100644 --- a/cmd/guru/definition.go +++ b/cmd/guru/definition.go @@ -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() { diff --git a/cmd/guru/testdata/src/definition-json/main.go b/cmd/guru/testdata/src/definition-json/main.go index ba13b6ba57..1674519291 100644 --- a/cmd/guru/testdata/src/definition-json/main.go +++ b/cmd/guru/testdata/src/definition-json/main.go @@ -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" +} diff --git a/cmd/guru/testdata/src/definition-json/main.golden b/cmd/guru/testdata/src/definition-json/main.golden index c2e1349ce2..e93903dbf8 100644 --- a/cmd/guru/testdata/src/definition-json/main.golden +++ b/cmd/guru/testdata/src/definition-json/main.golden @@ -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" +} diff --git a/cmd/guru/testdata/src/definition-json/type.go b/cmd/guru/testdata/src/definition-json/type.go new file mode 100644 index 0000000000..a574bf37fc --- /dev/null +++ b/cmd/guru/testdata/src/definition-json/type.go @@ -0,0 +1,3 @@ +package definition + +type W int diff --git a/cmd/guru/testdata/src/referrers-json/main.golden b/cmd/guru/testdata/src/referrers-json/main.golden index a779a190ae..bc44ab2ca6 100644 --- a/cmd/guru/testdata/src/referrers-json/main.golden +++ b/cmd/guru/testdata/src/referrers-json/main.golden @@ -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\"" } ] } diff --git a/cmd/guru/testdata/src/referrers/main.golden b/cmd/guru/testdata/src/referrers/main.golden index 060d98341d..230e95e732 100644 --- a/cmd/guru/testdata/src/referrers/main.golden +++ b/cmd/guru/testdata/src/referrers/main.golden @@ -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"