Skip to content

Commit

Permalink
refactor/satisfy/find: composite lits may have type parameter type
Browse files Browse the repository at this point in the history
Fix an oversight in the satisfaction check: composite lits may indeed
have type parameter type, and therefore we must consider their core
type.

Fixes golang/go#61614

Change-Id: I2119ba308816d02742d8e790f8cd00c4d862e789
Reviewed-on: https://go-review.googlesource.com/c/tools/+/513775
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Run-TryBot: Robert Findley <rfindley@google.com>
  • Loading branch information
findleyr committed Jul 27, 2023
1 parent bacac14 commit 03562de
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 2 deletions.
35 changes: 35 additions & 0 deletions gopls/internal/regtest/marker/testdata/rename/issue61614.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
This test renames a method of a type in a package that uses type parameter
composite lits. Previous iterations of the satisfy analysis did not account for
this language feature.

See issue #60789.

-- 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{}
}

3 changes: 1 addition & 2 deletions refactor/satisfy/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,8 +355,7 @@ func (f *Finder) expr(e ast.Expr) types.Type {
f.sig = saved

case *ast.CompositeLit:
// No need for coreType here: go1.18 disallows P{...} for type param P.
switch T := deref(tv.Type).Underlying().(type) {
switch T := coreType(tv.Type).(type) {
case *types.Struct:
for i, elem := range e.Elts {
if kv, ok := elem.(*ast.KeyValueExpr); ok {
Expand Down
9 changes: 9 additions & 0 deletions refactor/satisfy/find_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ type S struct{impl}
type T struct{impl}
type U struct{impl}
type V struct{impl}
type W struct{impl}
type X struct{impl}
type Generic[T any] struct{impl}
func (Generic[T]) g(T) {}
Expand Down Expand Up @@ -164,6 +166,11 @@ func _() {
// golang/go#56227: the finder should visit calls in the unsafe package.
_ = unsafe.Slice(&x[0], func() int { var _ I = x[0]; return 3 }()) // I <- V
}
func _[P ~struct{F I}]() {
_ = P{W{}}
_ = P{F: X{}}
}
`
got := constraints(t, src)
want := []string{
Expand Down Expand Up @@ -194,6 +201,8 @@ func _() {
"p.I <- p.T",
"p.I <- p.U",
"p.I <- p.V",
"p.I <- p.W",
"p.I <- p.X",
}
if !reflect.DeepEqual(got, want) {
t.Fatalf("found unexpected constraints: got %s, want %s", got, want)
Expand Down

0 comments on commit 03562de

Please sign in to comment.