Skip to content

Commit

Permalink
gopls/internal/lsp/source: fix renaming of type parameters
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
findleyr committed Jul 28, 2023
1 parent 3a3c169 commit fe58b07
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 37 deletions.
9 changes: 7 additions & 2 deletions gopls/internal/lsp/source/rename.go
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,13 @@ func renameOrdinary(ctx context.Context, snapshot Snapshot, f FileHandle, pp pro
// So we take a scenic route.
switch obj.(type) { // avoid "obj :=" since cases reassign the var
case *types.TypeName:
if named, ok := obj.Type().(*types.Named); ok {
obj = named.Obj()
switch t := obj.Type().(type) {
case *types.Named:
obj = t.Obj()
case *typeparams.TypeParam:
// As with capitalized function parameters below, type parameters are
// local.
goto skipObjectPath
}
case *types.Func:
obj = funcOrigin(obj.(*types.Func))
Expand Down
117 changes: 117 additions & 0 deletions gopls/internal/regtest/marker/testdata/rename/generics.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
This test exercises various renaming features on generic code.

Fixed bugs:

- golang/go#61614: renaming a method of a type in a package that uses type
parameter composite lits used to panic, because previous iterations of the
satisfy analysis did not account for this language feature.

- golang/go#61635: renaming type parameters did not work when they were
capitalized and the package was imported by another package.

-- flags --
-min_go=go1.18

-- go.mod --
module example.com
go 1.20

-- a.go --
package a

type I int

func (I) m() {} //@rename("m", M, mToM)

func _[P ~[]int]() {
_ = P{}
}

-- @mToM/a.go --
package a

type I int

func (I) M() {} //@rename("m", M, mToM)

func _[P ~[]int]() {
_ = P{}
}

-- g.go --
package a

type S[P any] struct { //@rename("P", Q, PToQ)
P P
F func(P) P
}

func F[R any](r R) {
var _ R //@rename("R", S, RToS)
}

-- @PToQ/g.go --
package a

type S[Q any] struct { //@rename("P", Q, PToQ)
P Q
F func(Q) Q
}

func F[R any](r R) {
var _ R //@rename("R", S, RToS)
}

-- @RToS/g.go --
package a

type S[P any] struct { //@rename("P", Q, PToQ)
P P
F func(P) P
}

func F[S any](r S) {
var _ S //@rename("R", S, RToS)
}

-- issue61635/p.go --
package issue61635

type builder[S ~[]F, F ~string] struct { //@rename("S", T, SToT)
name string
elements S
elemData map[F][]ElemData[F]
// other fields...
}

type ElemData[F ~string] struct {
Name F
// other fields...
}

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

-- importer/i.go --
package importer

import "example.com/issue61635" // importing is necessary to repro golang/go#61635

var _ issue61635.ElemData[string]

-- @SToT/issue61635/p.go --
package issue61635

type builder[T ~[]F, F ~string] struct { //@rename("S", T, SToT)
name string
elements T
elemData map[F][]ElemData[F]
// other fields...
}

type ElemData[F ~string] struct {
Name F
// other fields...
}

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

35 changes: 0 additions & 35 deletions gopls/internal/regtest/marker/testdata/rename/issue61614.txt

This file was deleted.

0 comments on commit fe58b07

Please sign in to comment.