github.com/powerman/golang-tools@v0.1.11-0.20220410185822-5ad214d8d803/go/types/typeutil/map_test.go (about) 1 // Copyright 2014 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 typeutil_test 6 7 // TODO(adonovan): 8 // - test use of explicit hasher across two maps. 9 // - test hashcodes are consistent with equals for a range of types 10 // (e.g. all types generated by type-checking some body of real code). 11 12 import ( 13 "go/ast" 14 "go/parser" 15 "go/token" 16 "go/types" 17 "testing" 18 19 "github.com/powerman/golang-tools/go/types/typeutil" 20 "github.com/powerman/golang-tools/internal/typeparams" 21 ) 22 23 var ( 24 tStr = types.Typ[types.String] // string 25 tPStr1 = types.NewPointer(tStr) // *string 26 tPStr2 = types.NewPointer(tStr) // *string, again 27 tInt = types.Typ[types.Int] // int 28 tChanInt1 = types.NewChan(types.RecvOnly, tInt) // <-chan int 29 tChanInt2 = types.NewChan(types.RecvOnly, tInt) // <-chan int, again 30 ) 31 32 func checkEqualButNotIdentical(t *testing.T, x, y types.Type, comment string) { 33 if !types.Identical(x, y) { 34 t.Errorf("%s: not equal: %s, %s", comment, x, y) 35 } 36 if x == y { 37 t.Errorf("%s: identical: %v, %v", comment, x, y) 38 } 39 } 40 41 func TestAxioms(t *testing.T) { 42 checkEqualButNotIdentical(t, tPStr1, tPStr2, "tPstr{1,2}") 43 checkEqualButNotIdentical(t, tChanInt1, tChanInt2, "tChanInt{1,2}") 44 } 45 46 func TestMap(t *testing.T) { 47 var tmap *typeutil.Map 48 49 // All methods but Set are safe on on (*T)(nil). 50 tmap.Len() 51 tmap.At(tPStr1) 52 tmap.Delete(tPStr1) 53 tmap.KeysString() 54 _ = tmap.String() 55 56 tmap = new(typeutil.Map) 57 58 // Length of empty map. 59 if l := tmap.Len(); l != 0 { 60 t.Errorf("Len() on empty Map: got %d, want 0", l) 61 } 62 // At of missing key. 63 if v := tmap.At(tPStr1); v != nil { 64 t.Errorf("At() on empty Map: got %v, want nil", v) 65 } 66 // Deletion of missing key. 67 if tmap.Delete(tPStr1) { 68 t.Errorf("Delete() on empty Map: got true, want false") 69 } 70 // Set of new key. 71 if prev := tmap.Set(tPStr1, "*string"); prev != nil { 72 t.Errorf("Set() on empty Map returned non-nil previous value %s", prev) 73 } 74 75 // Now: {*string: "*string"} 76 77 // Length of non-empty map. 78 if l := tmap.Len(); l != 1 { 79 t.Errorf("Len(): got %d, want 1", l) 80 } 81 // At via insertion key. 82 if v := tmap.At(tPStr1); v != "*string" { 83 t.Errorf("At(): got %q, want \"*string\"", v) 84 } 85 // At via equal key. 86 if v := tmap.At(tPStr2); v != "*string" { 87 t.Errorf("At(): got %q, want \"*string\"", v) 88 } 89 // Iteration over sole entry. 90 tmap.Iterate(func(key types.Type, value interface{}) { 91 if key != tPStr1 { 92 t.Errorf("Iterate: key: got %s, want %s", key, tPStr1) 93 } 94 if want := "*string"; value != want { 95 t.Errorf("Iterate: value: got %s, want %s", value, want) 96 } 97 }) 98 99 // Setion with key equal to present one. 100 if prev := tmap.Set(tPStr2, "*string again"); prev != "*string" { 101 t.Errorf("Set() previous value: got %s, want \"*string\"", prev) 102 } 103 104 // Setion of another association. 105 if prev := tmap.Set(tChanInt1, "<-chan int"); prev != nil { 106 t.Errorf("Set() previous value: got %s, want nil", prev) 107 } 108 109 // Now: {*string: "*string again", <-chan int: "<-chan int"} 110 111 want1 := "{*string: \"*string again\", <-chan int: \"<-chan int\"}" 112 want2 := "{<-chan int: \"<-chan int\", *string: \"*string again\"}" 113 if s := tmap.String(); s != want1 && s != want2 { 114 t.Errorf("String(): got %s, want %s", s, want1) 115 } 116 117 want1 = "{*string, <-chan int}" 118 want2 = "{<-chan int, *string}" 119 if s := tmap.KeysString(); s != want1 && s != want2 { 120 t.Errorf("KeysString(): got %s, want %s", s, want1) 121 } 122 123 // Keys(). 124 I := types.Identical 125 switch k := tmap.Keys(); { 126 case I(k[0], tChanInt1) && I(k[1], tPStr1): // ok 127 case I(k[1], tChanInt1) && I(k[0], tPStr1): // ok 128 default: 129 t.Errorf("Keys(): got %v, want %s", k, want2) 130 } 131 132 if l := tmap.Len(); l != 2 { 133 t.Errorf("Len(): got %d, want 1", l) 134 } 135 // At via original key. 136 if v := tmap.At(tPStr1); v != "*string again" { 137 t.Errorf("At(): got %q, want \"*string again\"", v) 138 } 139 hamming := 1 140 tmap.Iterate(func(key types.Type, value interface{}) { 141 switch { 142 case I(key, tChanInt1): 143 hamming *= 2 // ok 144 case I(key, tPStr1): 145 hamming *= 3 // ok 146 } 147 }) 148 if hamming != 6 { 149 t.Errorf("Iterate: hamming: got %d, want %d", hamming, 6) 150 } 151 152 if v := tmap.At(tChanInt2); v != "<-chan int" { 153 t.Errorf("At(): got %q, want \"<-chan int\"", v) 154 } 155 // Deletion with key equal to present one. 156 if !tmap.Delete(tChanInt2) { 157 t.Errorf("Delete() of existing key: got false, want true") 158 } 159 160 // Now: {*string: "*string again"} 161 162 if l := tmap.Len(); l != 1 { 163 t.Errorf("Len(): got %d, want 1", l) 164 } 165 // Deletion again. 166 if !tmap.Delete(tPStr2) { 167 t.Errorf("Delete() of existing key: got false, want true") 168 } 169 170 // Now: {} 171 172 if l := tmap.Len(); l != 0 { 173 t.Errorf("Len(): got %d, want %d", l, 0) 174 } 175 if s := tmap.String(); s != "{}" { 176 t.Errorf("Len(): got %q, want %q", s, "") 177 } 178 } 179 180 func TestMapGenerics(t *testing.T) { 181 if !typeparams.Enabled { 182 t.Skip("type params are not enabled at this Go version") 183 } 184 185 const src = ` 186 package p 187 188 // Basic defined types. 189 type T1 int 190 type T2 int 191 192 // Identical methods. 193 func (T1) M(int) {} 194 func (T2) M(int) {} 195 196 // A constraint interface. 197 type C interface { 198 ~int | string 199 } 200 201 type I interface { 202 } 203 204 // A generic type. 205 type G[P C] int 206 207 // Generic functions with identical signature. 208 func Fa1[P C](p P) {} 209 func Fa2[Q C](q Q) {} 210 211 // Fb1 and Fb2 are identical and should be mapped to the same entry, even if we 212 // map their arguments first. 213 func Fb1[P any](x *P) { 214 var y *P // Map this first. 215 _ = y 216 } 217 func Fb2[Q any](x *Q) { 218 } 219 220 // G1 and G2 are mutally recursive, and have identical methods. 221 type G1[P any] struct{ 222 Field *G2[P] 223 } 224 func (G1[P]) M(G1[P], G2[P]) {} 225 type G2[Q any] struct{ 226 Field *G1[Q] 227 } 228 func (G2[P]) M(G1[P], G2[P]) {} 229 230 // Method type expressions on different generic types are different. 231 var ME1 = G1[int].M 232 var ME2 = G2[int].M 233 234 // ME1Type should have identical type as ME1. 235 var ME1Type func(G1[int], G1[int], G2[int]) 236 237 // Examples from issue #51314 238 type Constraint[T any] interface{} 239 func Foo[T Constraint[T]]() {} 240 func Fn[T1 ~*T2, T2 ~*T1](t1 T1, t2 T2) {} 241 242 // Bar and Baz are identical to Foo. 243 func Bar[P Constraint[P]]() {} 244 func Baz[Q any]() {} // The underlying type of Constraint[P] is any. 245 // But Quux is not. 246 func Quux[Q interface{ quux() }]() {} 247 ` 248 249 fset := token.NewFileSet() 250 file, err := parser.ParseFile(fset, "p.go", src, 0) 251 if err != nil { 252 t.Fatal(err) 253 } 254 255 var conf types.Config 256 pkg, err := conf.Check("", fset, []*ast.File{file}, nil) 257 if err != nil { 258 t.Fatal(err) 259 } 260 261 // Collect types. 262 scope := pkg.Scope() 263 var ( 264 T1 = scope.Lookup("T1").Type().(*types.Named) 265 T2 = scope.Lookup("T2").Type().(*types.Named) 266 T1M = T1.Method(0).Type() 267 T2M = T2.Method(0).Type() 268 G = scope.Lookup("G").Type() 269 GInt1 = instantiate(t, G, types.Typ[types.Int]) 270 GInt2 = instantiate(t, G, types.Typ[types.Int]) 271 GStr = instantiate(t, G, types.Typ[types.String]) 272 C = scope.Lookup("C").Type() 273 CI = C.Underlying().(*types.Interface) 274 I = scope.Lookup("I").Type() 275 II = I.Underlying().(*types.Interface) 276 U = CI.EmbeddedType(0).(*typeparams.Union) 277 Fa1 = scope.Lookup("Fa1").Type().(*types.Signature) 278 Fa2 = scope.Lookup("Fa2").Type().(*types.Signature) 279 Fa1P = typeparams.ForSignature(Fa1).At(0) 280 Fa2Q = typeparams.ForSignature(Fa2).At(0) 281 Fb1 = scope.Lookup("Fb1").Type().(*types.Signature) 282 Fb1x = Fb1.Params().At(0).Type() 283 Fb1y = scope.Lookup("Fb1").(*types.Func).Scope().Lookup("y").Type() 284 Fb2 = scope.Lookup("Fb2").Type().(*types.Signature) 285 Fb2x = Fb2.Params().At(0).Type() 286 G1 = scope.Lookup("G1").Type().(*types.Named) 287 G1M = G1.Method(0).Type() 288 G1IntM1 = instantiate(t, G1, types.Typ[types.Int]).(*types.Named).Method(0).Type() 289 G1IntM2 = instantiate(t, G1, types.Typ[types.Int]).(*types.Named).Method(0).Type() 290 G1StrM = instantiate(t, G1, types.Typ[types.String]).(*types.Named).Method(0).Type() 291 G2 = scope.Lookup("G2").Type() 292 // See below. 293 // G2M = G2.Method(0).Type() 294 G2IntM = instantiate(t, G2, types.Typ[types.Int]).(*types.Named).Method(0).Type() 295 ME1 = scope.Lookup("ME1").Type() 296 ME1Type = scope.Lookup("ME1Type").Type() 297 ME2 = scope.Lookup("ME2").Type() 298 299 Constraint = scope.Lookup("Constraint").Type() 300 Foo = scope.Lookup("Foo").Type() 301 Fn = scope.Lookup("Fn").Type() 302 Bar = scope.Lookup("Foo").Type() 303 Baz = scope.Lookup("Foo").Type() 304 Quux = scope.Lookup("Quux").Type() 305 ) 306 307 tmap := new(typeutil.Map) 308 309 steps := []struct { 310 typ types.Type 311 name string 312 newEntry bool 313 }{ 314 {T1, "T1", true}, 315 {T2, "T2", true}, 316 {G, "G", true}, 317 {C, "C", true}, 318 {CI, "CI", true}, 319 {U, "U", true}, 320 {I, "I", true}, 321 {II, "II", true}, // should not be identical to CI 322 323 // Methods can be identical, even with distinct receivers. 324 {T1M, "T1M", true}, 325 {T2M, "T2M", false}, 326 327 // Identical instances should map to the same entry. 328 {GInt1, "GInt1", true}, 329 {GInt2, "GInt2", false}, 330 // ..but instantiating with different arguments should yield a new entry. 331 {GStr, "GStr", true}, 332 333 // F1 and F2 should have identical signatures. 334 {Fa1, "F1", true}, 335 {Fa2, "F2", false}, 336 337 // The identity of P and Q should not have been affected by type parameter 338 // masking during signature hashing. 339 {Fa1P, "F1P", true}, 340 {Fa2Q, "F2Q", true}, 341 342 {Fb1y, "Fb1y", true}, 343 {Fb1x, "Fb1x", false}, 344 {Fb2x, "Fb2x", true}, 345 {Fb1, "Fb1", true}, 346 347 // Mapping elements of the function scope should not affect the identity of 348 // Fb2 or Fb1. 349 {Fb2, "Fb1", false}, 350 351 {G1, "G1", true}, 352 {G1M, "G1M", true}, 353 {G2, "G2", true}, 354 355 // See golang/go#49912: receiver type parameter names should be ignored 356 // when comparing method identity. 357 // {G2M, "G2M", false}, 358 {G1IntM1, "G1IntM1", true}, 359 {G1IntM2, "G1IntM2", false}, 360 {G1StrM, "G1StrM", true}, 361 {G2IntM, "G2IntM", false}, // identical to G1IntM1 362 363 {ME1, "ME1", true}, 364 {ME1Type, "ME1Type", false}, 365 {ME2, "ME2", true}, 366 367 // See golang/go#51314: avoid infinite recursion on cyclic type constraints. 368 {Constraint, "Constraint", true}, 369 {Foo, "Foo", true}, 370 {Fn, "Fn", true}, 371 {Bar, "Bar", false}, 372 {Baz, "Baz", false}, 373 {Quux, "Quux", true}, 374 } 375 376 for _, step := range steps { 377 existing := tmap.At(step.typ) 378 if (existing == nil) != step.newEntry { 379 t.Errorf("At(%s) = %v, want new entry: %t", step.name, existing, step.newEntry) 380 } 381 tmap.Set(step.typ, step.name) 382 } 383 } 384 385 func instantiate(t *testing.T, origin types.Type, targs ...types.Type) types.Type { 386 inst, err := typeparams.Instantiate(nil, origin, targs, true) 387 if err != nil { 388 t.Fatal(err) 389 } 390 return inst 391 }