Skip to content

Commit fe58b07

Browse files
committed
gopls/internal/lsp/source: fix renaming of type parameters
Type parameters were incorrectly being treated as exported for the purpose of renaming checks, and imported type parameters do not have the correct scope information for rename checks to work. Fixes golang/go#61635 Change-Id: I4261c5e7b3622affbeb81a96ea4f510a5b9e4fe3 Reviewed-on: https://go-review.googlesource.com/c/tools/+/513916 gopls-CI: kokoro <[email protected]> Reviewed-by: Hyang-Ah Hana Kim <[email protected]> Run-TryBot: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]>
1 parent 3a3c169 commit fe58b07

File tree

3 files changed

+124
-37
lines changed

3 files changed

+124
-37
lines changed

gopls/internal/lsp/source/rename.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -350,8 +350,13 @@ func renameOrdinary(ctx context.Context, snapshot Snapshot, f FileHandle, pp pro
350350
// So we take a scenic route.
351351
switch obj.(type) { // avoid "obj :=" since cases reassign the var
352352
case *types.TypeName:
353-
if named, ok := obj.Type().(*types.Named); ok {
354-
obj = named.Obj()
353+
switch t := obj.Type().(type) {
354+
case *types.Named:
355+
obj = t.Obj()
356+
case *typeparams.TypeParam:
357+
// As with capitalized function parameters below, type parameters are
358+
// local.
359+
goto skipObjectPath
355360
}
356361
case *types.Func:
357362
obj = funcOrigin(obj.(*types.Func))
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
This test exercises various renaming features on generic code.
2+
3+
Fixed bugs:
4+
5+
- golang/go#61614: renaming a method of a type in a package that uses type
6+
parameter composite lits used to panic, because previous iterations of the
7+
satisfy analysis did not account for this language feature.
8+
9+
- golang/go#61635: renaming type parameters did not work when they were
10+
capitalized and the package was imported by another package.
11+
12+
-- flags --
13+
-min_go=go1.18
14+
15+
-- go.mod --
16+
module example.com
17+
go 1.20
18+
19+
-- a.go --
20+
package a
21+
22+
type I int
23+
24+
func (I) m() {} //@rename("m", M, mToM)
25+
26+
func _[P ~[]int]() {
27+
_ = P{}
28+
}
29+
30+
-- @mToM/a.go --
31+
package a
32+
33+
type I int
34+
35+
func (I) M() {} //@rename("m", M, mToM)
36+
37+
func _[P ~[]int]() {
38+
_ = P{}
39+
}
40+
41+
-- g.go --
42+
package a
43+
44+
type S[P any] struct { //@rename("P", Q, PToQ)
45+
P P
46+
F func(P) P
47+
}
48+
49+
func F[R any](r R) {
50+
var _ R //@rename("R", S, RToS)
51+
}
52+
53+
-- @PToQ/g.go --
54+
package a
55+
56+
type S[Q any] struct { //@rename("P", Q, PToQ)
57+
P Q
58+
F func(Q) Q
59+
}
60+
61+
func F[R any](r R) {
62+
var _ R //@rename("R", S, RToS)
63+
}
64+
65+
-- @RToS/g.go --
66+
package a
67+
68+
type S[P any] struct { //@rename("P", Q, PToQ)
69+
P P
70+
F func(P) P
71+
}
72+
73+
func F[S any](r S) {
74+
var _ S //@rename("R", S, RToS)
75+
}
76+
77+
-- issue61635/p.go --
78+
package issue61635
79+
80+
type builder[S ~[]F, F ~string] struct { //@rename("S", T, SToT)
81+
name string
82+
elements S
83+
elemData map[F][]ElemData[F]
84+
// other fields...
85+
}
86+
87+
type ElemData[F ~string] struct {
88+
Name F
89+
// other fields...
90+
}
91+
92+
type BuilderImpl[S ~[]F, F ~string] struct{ builder[S, F] }
93+
94+
-- importer/i.go --
95+
package importer
96+
97+
import "example.com/issue61635" // importing is necessary to repro golang/go#61635
98+
99+
var _ issue61635.ElemData[string]
100+
101+
-- @SToT/issue61635/p.go --
102+
package issue61635
103+
104+
type builder[T ~[]F, F ~string] struct { //@rename("S", T, SToT)
105+
name string
106+
elements T
107+
elemData map[F][]ElemData[F]
108+
// other fields...
109+
}
110+
111+
type ElemData[F ~string] struct {
112+
Name F
113+
// other fields...
114+
}
115+
116+
type BuilderImpl[S ~[]F, F ~string] struct{ builder[S, F] }
117+

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

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)