Skip to content

Commit 45115c1

Browse files
committed
internal/lsp/source: rename uses of embedded fields
When renaming a type name, also rename indirect uses of the name as an embedded field. This is conservatively isolated to just renames for now; it's not clear to me that users would also want to see uses of embedded fields as references. Fixes golang/go#43616 Change-Id: I41913d037fedb8c27a448cd922eeaf11a02d01f1 Reviewed-on: https://go-review.googlesource.com/c/tools/+/282932 Run-TryBot: Robert Findley <rfindley@google.com> gopls-CI: kokoro <noreply+kokoro@google.com> Reviewed-by: Rebecca Stambler <rstambler@golang.org> Trust: Robert Findley <rfindley@google.com>
1 parent 1e6ecd4 commit 45115c1

File tree

5 files changed

+39
-11
lines changed

5 files changed

+39
-11
lines changed

internal/lsp/source/references.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func References(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Posit
4242
return nil, err
4343
}
4444

45-
refs, err := references(ctx, s, qualifiedObjs, includeDeclaration, true)
45+
refs, err := references(ctx, s, qualifiedObjs, includeDeclaration, true, false)
4646
if err != nil {
4747
return nil, err
4848
}
@@ -62,10 +62,10 @@ func References(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Posit
6262
}
6363

6464
// references is a helper function to avoid recomputing qualifiedObjsAtProtocolPos.
65-
func references(ctx context.Context, snapshot Snapshot, qos []qualifiedObject, includeDeclaration, includeInterfaceRefs bool) ([]*ReferenceInfo, error) {
65+
func references(ctx context.Context, snapshot Snapshot, qos []qualifiedObject, includeDeclaration, includeInterfaceRefs, includeEmbeddedRefs bool) ([]*ReferenceInfo, error) {
6666
var (
6767
references []*ReferenceInfo
68-
seen = make(map[token.Position]bool)
68+
seen = make(map[token.Pos]bool)
6969
)
7070

7171
filename := snapshot.FileSet().Position(qos[0].obj.Pos()).Filename
@@ -105,13 +105,25 @@ func references(ctx context.Context, snapshot Snapshot, qos []qualifiedObject, i
105105
for _, pkg := range searchPkgs {
106106
for ident, obj := range pkg.GetTypesInfo().Uses {
107107
if obj != qo.obj {
108-
continue
108+
// If ident is not a use of qo.obj, skip it, with one exception: uses
109+
// of an embedded field can be considered references of the embedded
110+
// type name.
111+
if !includeEmbeddedRefs {
112+
continue
113+
}
114+
v, ok := obj.(*types.Var)
115+
if !ok || !v.Embedded() {
116+
continue
117+
}
118+
named, ok := v.Type().(*types.Named)
119+
if !ok || named.Obj() != qo.obj {
120+
continue
121+
}
109122
}
110-
pos := snapshot.FileSet().Position(ident.Pos())
111-
if seen[pos] {
123+
if seen[ident.Pos()] {
112124
continue
113125
}
114-
seen[pos] = true
126+
seen[ident.Pos()] = true
115127
rng, err := posToMappedRange(snapshot, pkg, ident.Pos(), ident.End())
116128
if err != nil {
117129
return nil, err
@@ -150,7 +162,7 @@ func references(ctx context.Context, snapshot Snapshot, qos []qualifiedObject, i
150162
return references, nil
151163
}
152164

153-
// interfaceReferences returns the references to the interfaces implemeneted by
165+
// interfaceReferences returns the references to the interfaces implemented by
154166
// the type or method at the given position.
155167
func interfaceReferences(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position) ([]*ReferenceInfo, error) {
156168
implementations, err := implementations(ctx, s, f, pp)
@@ -163,7 +175,7 @@ func interfaceReferences(ctx context.Context, s Snapshot, f FileHandle, pp proto
163175

164176
var refs []*ReferenceInfo
165177
for _, impl := range implementations {
166-
implRefs, err := references(ctx, s, []qualifiedObject{impl}, false, false)
178+
implRefs, err := references(ctx, s, []qualifiedObject{impl}, false, false, false)
167179
if err != nil {
168180
return nil, err
169181
}

internal/lsp/source/rename.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ func Rename(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position,
9191
if pkg == nil || pkg.IsIllTyped() {
9292
return nil, errors.Errorf("package for %s is ill typed", f.URI())
9393
}
94-
refs, err := references(ctx, s, qos, true, false)
94+
refs, err := references(ctx, s, qos, true, false, true)
9595
if err != nil {
9696
return nil, err
9797
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- bar-rename --
2+
package issue43616
3+
4+
type bar int //@rename("foo","bar")
5+
6+
var x struct{ bar }
7+
8+
var _ = x.bar
9+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package issue43616
2+
3+
type foo int //@rename("foo","bar")
4+
5+
var x struct{ foo }
6+
7+
var _ = x.foo

internal/lsp/testdata/summary.txt.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ DefinitionsCount = 64
1919
TypeDefinitionsCount = 2
2020
HighlightsCount = 69
2121
ReferencesCount = 25
22-
RenamesCount = 30
22+
RenamesCount = 31
2323
PrepareRenamesCount = 7
2424
SymbolsCount = 5
2525
WorkspaceSymbolsCount = 20

0 commit comments

Comments
 (0)