| 1 | // Copyright 2022 The Go Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
| 5 | package satisfy_test |
| 6 | |
| 7 | import ( |
| 8 | "fmt" |
| 9 | "go/ast" |
| 10 | "go/importer" |
| 11 | "go/parser" |
| 12 | "go/token" |
| 13 | "go/types" |
| 14 | "reflect" |
| 15 | "sort" |
| 16 | "testing" |
| 17 | |
| 18 | "golang.org/x/tools/internal/testenv" |
| 19 | "golang.org/x/tools/refactor/satisfy" |
| 20 | ) |
| 21 | |
| 22 | // This test exercises various operations on core types of type parameters. |
| 23 | // (It also provides pretty decent coverage of the non-generic operations.) |
| 24 | func TestGenericCoreOperations(t *testing.T) { |
| 25 | const src = `package foo |
| 26 | |
| 27 | import "unsafe" |
| 28 | |
| 29 | type I interface { f() } |
| 30 | |
| 31 | type impl struct{} |
| 32 | func (impl) f() {} |
| 33 | |
| 34 | // A big pile of single-serving types that implement I. |
| 35 | type A struct{impl} |
| 36 | type B struct{impl} |
| 37 | type C struct{impl} |
| 38 | type D struct{impl} |
| 39 | type E struct{impl} |
| 40 | type F struct{impl} |
| 41 | type G struct{impl} |
| 42 | type H struct{impl} |
| 43 | type J struct{impl} |
| 44 | type K struct{impl} |
| 45 | type L struct{impl} |
| 46 | type M struct{impl} |
| 47 | type N struct{impl} |
| 48 | type O struct{impl} |
| 49 | type P struct{impl} |
| 50 | type Q struct{impl} |
| 51 | type R struct{impl} |
| 52 | type S struct{impl} |
| 53 | type T struct{impl} |
| 54 | type U struct{impl} |
| 55 | type V struct{impl} |
| 56 | type W struct{impl} |
| 57 | type X struct{impl} |
| 58 | |
| 59 | type Generic[T any] struct{impl} |
| 60 | func (Generic[T]) g(T) {} |
| 61 | |
| 62 | type GI[T any] interface{ |
| 63 | g(T) |
| 64 | } |
| 65 | |
| 66 | func _[Slice interface{ []I }](s Slice) Slice { |
| 67 | s[0] = L{} // I <- L |
| 68 | return append(s, A{}) // I <- A |
| 69 | } |
| 70 | |
| 71 | func _[Func interface{ func(I) B }](fn Func) { |
| 72 | b := fn(C{}) // I <- C |
| 73 | var _ I = b // I <- B |
| 74 | } |
| 75 | |
| 76 | func _[Chan interface{ chan D }](ch Chan) { |
| 77 | var i I |
| 78 | for i = range ch {} // I <- D |
| 79 | _ = i |
| 80 | } |
| 81 | |
| 82 | func _[Chan interface{ chan E }](ch Chan) { |
| 83 | var _ I = <-ch // I <- E |
| 84 | } |
| 85 | |
| 86 | func _[Chan interface{ chan I }](ch Chan) { |
| 87 | ch <- F{} // I <- F |
| 88 | } |
| 89 | |
| 90 | func _[Map interface{ map[G]H }](m Map) { |
| 91 | var k, v I |
| 92 | for k, v = range m {} // I <- G, I <- H |
| 93 | _, _ = k, v |
| 94 | } |
| 95 | |
| 96 | func _[Map interface{ map[I]K }](m Map) { |
| 97 | var _ I = m[J{}] // I <- J, I <- K |
| 98 | delete(m, R{}) // I <- R |
| 99 | _, _ = m[J{}] |
| 100 | } |
| 101 | |
| 102 | func _[Array interface{ [1]I }](a Array) { |
| 103 | a[0] = M{} // I <- M |
| 104 | } |
| 105 | |
| 106 | func _[Array interface{ [1]N }](a Array) { |
| 107 | var _ I = a[0] // I <- N |
| 108 | } |
| 109 | |
| 110 | func _[Array interface{ [1]O }](a Array) { |
| 111 | var v I |
| 112 | for _, v = range a {} // I <- O |
| 113 | _ = v |
| 114 | } |
| 115 | |
| 116 | func _[ArrayPtr interface{ *[1]P }](a ArrayPtr) { |
| 117 | var v I |
| 118 | for _, v = range a {} // I <- P |
| 119 | _ = v |
| 120 | } |
| 121 | |
| 122 | func _[Slice interface{ []Q }](s Slice) { |
| 123 | var v I |
| 124 | for _, v = range s {} // I <- Q |
| 125 | _ = v |
| 126 | } |
| 127 | |
| 128 | func _[Func interface{ func() (S, bool) }](fn Func) { |
| 129 | var i I |
| 130 | i, _ = fn() // I <- S |
| 131 | _ = i |
| 132 | } |
| 133 | |
| 134 | func _() I { |
| 135 | var _ I = T{} // I <- T |
| 136 | var _ I = Generic[T]{} // I <- Generic[T] |
| 137 | var _ I = Generic[string]{} // I <- Generic[string] |
| 138 | return U{} // I <- U |
| 139 | } |
| 140 | |
| 141 | var _ GI[string] = Generic[string]{} // GI[string] <- Generic[string] |
| 142 | |
| 143 | // universally quantified constraints: |
| 144 | // the type parameter may appear on the left, the right, or both sides. |
| 145 | |
| 146 | func _[T any](g Generic[T]) GI[T] { |
| 147 | return g // GI[T] <- Generic[T] |
| 148 | } |
| 149 | |
| 150 | func _[T any]() { |
| 151 | type GI2[T any] interface{ g(string) } |
| 152 | var _ GI2[T] = Generic[string]{} // GI2[T] <- Generic[string] |
| 153 | } |
| 154 | |
| 155 | type Gen2[T any] struct{} |
| 156 | func (f Gen2[T]) g(string) { global = f } // GI[string] <- Gen2[T] |
| 157 | |
| 158 | var global GI[string] |
| 159 | |
| 160 | func _() { |
| 161 | var x [3]V |
| 162 | // golang/go#56227: the finder should visit calls in the unsafe package. |
| 163 | _ = unsafe.Slice(&x[0], func() int { var _ I = x[0]; return 3 }()) // I <- V |
| 164 | } |
| 165 | |
| 166 | func _[P ~struct{F I}]() { |
| 167 | _ = P{W{}} |
| 168 | _ = P{F: X{}} |
| 169 | } |
| 170 | |
| 171 | // golang/go#77625: don't panic on a method expression of an anonymous interface. |
| 172 | var _ = interface{ m() }.m |
| 173 | ` |
| 174 | got := constraints(t, src) |
| 175 | want := []string{ |
| 176 | "p.GI2[T] <- p.Generic[string]", // implicitly "forall T" quantified |
| 177 | "p.GI[T] <- p.Generic[T]", // implicitly "forall T" quantified |
| 178 | "p.GI[string] <- p.Gen2[T]", // implicitly "forall T" quantified |
| 179 | "p.GI[string] <- p.Generic[string]", |
| 180 | "p.I <- p.A", |
| 181 | "p.I <- p.B", |
| 182 | "p.I <- p.C", |
| 183 | "p.I <- p.D", |
| 184 | "p.I <- p.E", |
| 185 | "p.I <- p.F", |
| 186 | "p.I <- p.G", |
| 187 | "p.I <- p.Generic[p.T]", |
| 188 | "p.I <- p.Generic[string]", |
| 189 | "p.I <- p.H", |
| 190 | "p.I <- p.J", |
| 191 | "p.I <- p.K", |
| 192 | "p.I <- p.L", |
| 193 | "p.I <- p.M", |
| 194 | "p.I <- p.N", |
| 195 | "p.I <- p.O", |
| 196 | "p.I <- p.P", |
| 197 | "p.I <- p.Q", |
| 198 | "p.I <- p.R", |
| 199 | "p.I <- p.S", |
| 200 | "p.I <- p.T", |
| 201 | "p.I <- p.U", |
| 202 | "p.I <- p.V", |
| 203 | "p.I <- p.W", |
| 204 | "p.I <- p.X", |
| 205 | } |
| 206 | if !reflect.DeepEqual(got, want) { |
| 207 | t.Fatalf("found unexpected constraints: got %s, want %s", got, want) |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | func TestNewExpr(t *testing.T) { |
| 212 | testenv.NeedsGo1Point(t, 26) |
| 213 | const src = `package p |
| 214 | |
| 215 | type I interface{ f() } |
| 216 | type C int |
| 217 | func (C) f() {} |
| 218 | |
| 219 | var _ I = new(C(123)) |
| 220 | ` |
| 221 | got := constraints(t, src) |
| 222 | want := []string{ |
| 223 | "p.I <- *p.C", |
| 224 | } |
| 225 | if !reflect.DeepEqual(got, want) { |
| 226 | t.Fatalf("found unexpected constraints: got %s, want %s", got, want) |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | func constraints(t *testing.T, src string) []string { |
| 231 | // parse |
| 232 | fset := token.NewFileSet() |
| 233 | f, err := parser.ParseFile(fset, "p.go", src, 0) |
| 234 | if err != nil { |
| 235 | t.Fatal(err) // parse error |
| 236 | } |
| 237 | files := []*ast.File{f} |
| 238 | |
| 239 | // type-check |
| 240 | info := &types.Info{ |
| 241 | Types: make(map[ast.Expr]types.TypeAndValue), |
| 242 | Defs: make(map[*ast.Ident]types.Object), |
| 243 | Uses: make(map[*ast.Ident]types.Object), |
| 244 | Implicits: make(map[ast.Node]types.Object), |
| 245 | Instances: make(map[*ast.Ident]types.Instance), |
| 246 | Scopes: make(map[ast.Node]*types.Scope), |
| 247 | Selections: make(map[*ast.SelectorExpr]*types.Selection), |
| 248 | FileVersions: make(map[*ast.File]string), |
| 249 | } |
| 250 | conf := types.Config{ |
| 251 | Importer: importer.Default(), |
| 252 | } |
| 253 | if _, err := conf.Check("p", fset, files, info); err != nil { |
| 254 | t.Fatal(err) // type error |
| 255 | } |
| 256 | |
| 257 | // gather constraints |
| 258 | var finder satisfy.Finder |
| 259 | finder.Find(info, files) |
| 260 | var constraints []string |
| 261 | for c := range finder.Result { |
| 262 | constraints = append(constraints, fmt.Sprintf("%v <- %v", c.LHS, c.RHS)) |
| 263 | } |
| 264 | sort.Strings(constraints) |
| 265 | return constraints |
| 266 | } |