Skip to content

Commit 03562de

Browse files
committed
refactor/satisfy/find: composite lits may have type parameter type
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 <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Robert Findley <[email protected]>
1 parent bacac14 commit 03562de

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
This test renames a method of a type in a package that uses type parameter
2+
composite lits. Previous iterations of the satisfy analysis did not account for
3+
this language feature.
4+
5+
See issue #60789.
6+
7+
-- flags --
8+
-min_go=go1.18
9+
10+
-- go.mod --
11+
module example.com
12+
go 1.20
13+
14+
-- a.go --
15+
package a
16+
17+
type I int
18+
19+
func (I) m() {} //@rename("m", M, mToM)
20+
21+
func _[P ~[]int]() {
22+
_ = P{}
23+
}
24+
25+
-- @mToM/a.go --
26+
package a
27+
28+
type I int
29+
30+
func (I) M() {} //@rename("m", M, mToM)
31+
32+
func _[P ~[]int]() {
33+
_ = P{}
34+
}
35+

refactor/satisfy/find.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,8 +355,7 @@ func (f *Finder) expr(e ast.Expr) types.Type {
355355
f.sig = saved
356356

357357
case *ast.CompositeLit:
358-
// No need for coreType here: go1.18 disallows P{...} for type param P.
359-
switch T := deref(tv.Type).Underlying().(type) {
358+
switch T := coreType(tv.Type).(type) {
360359
case *types.Struct:
361360
for i, elem := range e.Elts {
362361
if kv, ok := elem.(*ast.KeyValueExpr); ok {

refactor/satisfy/find_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ type S struct{impl}
5757
type T struct{impl}
5858
type U struct{impl}
5959
type V struct{impl}
60+
type W struct{impl}
61+
type X struct{impl}
6062
6163
type Generic[T any] struct{impl}
6264
func (Generic[T]) g(T) {}
@@ -164,6 +166,11 @@ func _() {
164166
// golang/go#56227: the finder should visit calls in the unsafe package.
165167
_ = unsafe.Slice(&x[0], func() int { var _ I = x[0]; return 3 }()) // I <- V
166168
}
169+
170+
func _[P ~struct{F I}]() {
171+
_ = P{W{}}
172+
_ = P{F: X{}}
173+
}
167174
`
168175
got := constraints(t, src)
169176
want := []string{
@@ -194,6 +201,8 @@ func _() {
194201
"p.I <- p.T",
195202
"p.I <- p.U",
196203
"p.I <- p.V",
204+
"p.I <- p.W",
205+
"p.I <- p.X",
197206
}
198207
if !reflect.DeepEqual(got, want) {
199208
t.Fatalf("found unexpected constraints: got %s, want %s", got, want)

0 commit comments

Comments
 (0)