Skip to content

Commit 1561060

Browse files
committed
gopls/internal/lsp/source: fix incorrect 'origin' logic for named types
Type names of named types are always canonical. The existing logic to canonicalize them was ineffective, and had the side effect of breaking aliases. Also add more tests of generic name renaming. Fixes golang/go#61625 Change-Id: I318a758176aea6712da16dde29394ffb08bdf715 Reviewed-on: https://go-review.googlesource.com/c/tools/+/513917 Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]>
1 parent fe58b07 commit 1561060

File tree

3 files changed

+149
-12
lines changed

3 files changed

+149
-12
lines changed

gopls/internal/lsp/source/rename.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -343,17 +343,15 @@ func renameOrdinary(ctx context.Context, snapshot Snapshot, f FileHandle, pp pro
343343
// Find objectpath, if object is exported ("" otherwise).
344344
var declObjPath objectpath.Path
345345
if obj.Exported() {
346-
// objectpath.For requires the origin of a generic
347-
// function or type, not an instantiation (a bug?).
348-
// Unfortunately we can't call {Func,TypeName}.Origin
349-
// as these are not available in go/[email protected].
350-
// So we take a scenic route.
346+
// objectpath.For requires the origin of a generic function or type, not an
347+
// instantiation (a bug?). Unfortunately we can't call Func.Origin as this
348+
// is not available in go/[email protected]. So we take a scenic route.
349+
//
350+
// Note that unlike Funcs, TypeNames are always canonical (they are "left"
351+
// of the type parameters, unlike methods).
351352
switch obj.(type) { // avoid "obj :=" since cases reassign the var
352353
case *types.TypeName:
353-
switch t := obj.Type().(type) {
354-
case *types.Named:
355-
obj = t.Obj()
356-
case *typeparams.TypeParam:
354+
if _, ok := obj.Type().(*typeparams.TypeParam); ok {
357355
// As with capitalized function parameters below, type parameters are
358356
// local.
359357
goto skipObjectPath

gopls/internal/regtest/marker/testdata/rename/basic.txt

+19-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,28 @@ This test performs basic coverage of 'rename' within a single package.
33
-- basic.go --
44
package p
55

6-
func f(x int) { println(x) } //@rename("x", y, param_x)
6+
func f(x int) { println(x) } //@rename("x", y, xToy)
77

8-
-- @param_x/basic.go --
8+
-- @xToy/basic.go --
99
package p
1010

11-
func f(y int) { println(y) } //@rename("x", y, param_x)
11+
func f(y int) { println(y) } //@rename("x", y, xToy)
12+
13+
-- alias.go --
14+
package p
15+
16+
// from golang/go#61625
17+
type LongNameHere struct{}
18+
type A = LongNameHere //@rename("A", B, AToB)
19+
func Foo() A
20+
21+
-- @AToB/alias.go --
22+
package p
23+
24+
// from golang/go#61625
25+
type LongNameHere struct{}
26+
type B = LongNameHere //@rename("A", B, AToB)
27+
func Foo() B
1228

1329
-- errors.go --
1430
package p

gopls/internal/regtest/marker/testdata/rename/generics.txt

+123
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,126 @@ type ElemData[F ~string] struct {
115115

116116
type BuilderImpl[S ~[]F, F ~string] struct{ builder[S, F] }
117117

118+
-- instances/type.go --
119+
package instances
120+
121+
type R[P any] struct { //@rename("R", u, Rtou)
122+
Next *R[P] //@rename("R", s, RTos)
123+
}
124+
125+
func (rv R[P]) Do(R[P]) R[P] { //@rename("Do", Do1, DoToDo1)
126+
var x R[P]
127+
return rv.Do(x) //@rename("Do", Do2, DoToDo2)
128+
}
129+
130+
func _() {
131+
var x R[int] //@rename("R", r, RTor)
132+
x = x.Do(x)
133+
}
134+
135+
-- @RTos/instances/type.go --
136+
package instances
137+
138+
type s[P any] struct { //@rename("R", u, Rtou)
139+
Next *s[P] //@rename("R", s, RTos)
140+
}
141+
142+
func (rv s[P]) Do(s[P]) s[P] { //@rename("Do", Do1, DoToDo1)
143+
var x s[P]
144+
return rv.Do(x) //@rename("Do", Do2, DoToDo2)
145+
}
146+
147+
func _() {
148+
var x s[int] //@rename("R", r, RTor)
149+
x = x.Do(x)
150+
}
151+
152+
-- @Rtou/instances/type.go --
153+
package instances
154+
155+
type u[P any] struct { //@rename("R", u, Rtou)
156+
Next *u[P] //@rename("R", s, RTos)
157+
}
158+
159+
func (rv u[P]) Do(u[P]) u[P] { //@rename("Do", Do1, DoToDo1)
160+
var x u[P]
161+
return rv.Do(x) //@rename("Do", Do2, DoToDo2)
162+
}
163+
164+
func _() {
165+
var x u[int] //@rename("R", r, RTor)
166+
x = x.Do(x)
167+
}
168+
169+
-- @DoToDo1/instances/type.go --
170+
package instances
171+
172+
type R[P any] struct { //@rename("R", u, Rtou)
173+
Next *R[P] //@rename("R", s, RTos)
174+
}
175+
176+
func (rv R[P]) Do1(R[P]) R[P] { //@rename("Do", Do1, DoToDo1)
177+
var x R[P]
178+
return rv.Do1(x) //@rename("Do", Do2, DoToDo2)
179+
}
180+
181+
func _() {
182+
var x R[int] //@rename("R", r, RTor)
183+
x = x.Do1(x)
184+
}
185+
186+
-- @DoToDo2/instances/type.go --
187+
package instances
188+
189+
type R[P any] struct { //@rename("R", u, Rtou)
190+
Next *R[P] //@rename("R", s, RTos)
191+
}
192+
193+
func (rv R[P]) Do2(R[P]) R[P] { //@rename("Do", Do1, DoToDo1)
194+
var x R[P]
195+
return rv.Do2(x) //@rename("Do", Do2, DoToDo2)
196+
}
197+
198+
func _() {
199+
var x R[int] //@rename("R", r, RTor)
200+
x = x.Do2(x)
201+
}
202+
203+
-- instances/func.go --
204+
package instances
205+
206+
func Foo[P any](p P) { //@rename("Foo", Bar, FooToBar)
207+
Foo(p) //@rename("Foo", Baz, FooToBaz)
208+
}
209+
210+
-- @FooToBar/instances/func.go --
211+
package instances
212+
213+
func Bar[P any](p P) { //@rename("Foo", Bar, FooToBar)
214+
Bar(p) //@rename("Foo", Baz, FooToBaz)
215+
}
216+
217+
-- @FooToBaz/instances/func.go --
218+
package instances
219+
220+
func Baz[P any](p P) { //@rename("Foo", Bar, FooToBar)
221+
Baz(p) //@rename("Foo", Baz, FooToBaz)
222+
}
223+
224+
-- @RTor/instances/type.go --
225+
package instances
226+
227+
type r[P any] struct { //@rename("R", u, Rtou)
228+
Next *r[P] //@rename("R", s, RTos)
229+
}
230+
231+
func (rv r[P]) Do(r[P]) r[P] { //@rename("Do", Do1, DoToDo1)
232+
var x r[P]
233+
return rv.Do(x) //@rename("Do", Do2, DoToDo2)
234+
}
235+
236+
func _() {
237+
var x r[int] //@rename("R", r, RTor)
238+
x = x.Do(x)
239+
}
240+

0 commit comments

Comments
 (0)