Skip to content

Commit

Permalink
fix(go_indexer): fix refs to anonymous members across packages (#5195)
Browse files Browse the repository at this point in the history
  • Loading branch information
schroederc committed Jan 25, 2022
1 parent 3806d62 commit e40b0a0
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 5 deletions.
6 changes: 6 additions & 0 deletions kythe/go/indexer/BUILD
Expand Up @@ -231,6 +231,12 @@ go_indexer_test(
srcs = ["testdata/basic/anonymous.go"],
)

go_indexer_test(
name = "anonref_test",
srcs = ["testdata/basic/anonref.go"],
deps = [":anonymous_test"],
)

go_indexer_test(
name = "structinit_test",
srcs = ["testdata/structinit.go"],
Expand Down
16 changes: 13 additions & 3 deletions kythe/go/indexer/indexer.go
Expand Up @@ -588,7 +588,7 @@ func (pi *PackageInfo) newSignature(obj types.Object) (tag, base string) {
_, base := pi.newSignature(owner)
return tagField, base + "." + t.Name()
}
return tagField, fmt.Sprintf("[%p].%s", t, t.Name())
return tagField, pi.anonSignature(t)
} else if owner, ok := pi.owner[t]; ok {
_, base := pi.newSignature(owner)
return tagParam, base + ":" + t.Name()
Expand Down Expand Up @@ -620,7 +620,7 @@ func (pi *PackageInfo) newSignature(obj types.Object) (tag, base string) {
}

case *types.Label:
return tagLabel, fmt.Sprintf("[%p].%s", t, t.Name())
return tagLabel, pi.anonSignature(t)

default:
log.Panicf("Unexpected object kind: %T", obj)
Expand All @@ -638,7 +638,17 @@ func (pi *PackageInfo) newSignature(obj types.Object) (tag, base string) {
}

// Objects in interior (local) scopes, i.e., everything else.
return topLevelTag, fmt.Sprintf("[%p].%s", obj, obj.Name())
return topLevelTag, pi.anonSignature(obj)
}

func (pi *PackageInfo) anonSignature(obj types.Object) string {
// Use the object's line number and file basename to differentiate the
// node while allowing for cross-package references (other parts of the
// Position may differ). This may collide if a source file isn't gofmt'd
// and defines multiple anonymous fields with the same name on the same
// line, but that's unlikely to happen in practice.
pos := pi.FileSet.Position(obj.Pos())
return fmt.Sprintf("[%s#%d].%s", filepath.Base(pos.Filename), pos.Line, obj.Name())
}

// addOwners updates pi.owner from the types in pkg, adding mapping from fields
Expand Down
9 changes: 9 additions & 0 deletions kythe/go/indexer/testdata/basic/anonref.go
@@ -0,0 +1,9 @@
package anonref

import (
anonymous "kythe/go/indexer/anonymous_test"
)

//- @Struct ref Struct
//- @V ref V
var _ = anonymous.Struct.V
6 changes: 4 additions & 2 deletions kythe/go/indexer/testdata/basic/anonymous.go
Expand Up @@ -28,7 +28,9 @@ func f(planx struct {
return planx.T
}

var v = struct {
//- @Struct defines/binding Struct
//- Struct.node/kind variable
var Struct = struct {
//- @V defines/binding V
//- V.node/kind variable
//- V.subkind field
Expand All @@ -39,7 +41,7 @@ var v = struct {
}

//- @V ref V
var _ = v.V
var _ = Struct.V

var w struct {
//- @X defines/binding X
Expand Down

0 comments on commit e40b0a0

Please sign in to comment.