github.com/jhump/golang-x-tools@v0.0.0-20220218190644-4958d6d39439/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/jhump/golang-x-tools/go/types/typeutil" 20 "github.com/jhump/golang-x-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 238 fset := token.NewFileSet() 239 file, err := parser.ParseFile(fset, "p.go", src, 0) 240 if err != nil { 241 t.Fatal(err) 242 } 243 244 var conf types.Config 245 pkg, err := conf.Check("", fset, []*ast.File{file}, nil) 246 if err != nil { 247 t.Fatal(err) 248 } 249 250 // Collect types. 251 scope := pkg.Scope() 252 var ( 253 T1 = scope.Lookup("T1").Type().(*types.Named) 254 T2 = scope.Lookup("T2").Type().(*types.Named) 255 T1M = T1.Method(0).Type() 256 T2M = T2.Method(0).Type() 257 G = scope.Lookup("G").Type() 258 GInt1 = instantiate(t, G, types.Typ[types.Int]) 259 GInt2 = instantiate(t, G, types.Typ[types.Int]) 260 GStr = instantiate(t, G, types.Typ[types.String]) 261 C = scope.Lookup("C").Type() 262 CI = C.Underlying().(*types.Interface) 263 I = scope.Lookup("I").Type() 264 II = I.Underlying().(*types.Interface) 265 U = CI.EmbeddedType(0).(*typeparams.Union) 266 Fa1 = scope.Lookup("Fa1").Type().(*types.Signature) 267 Fa2 = scope.Lookup("Fa2").Type().(*types.Signature) 268 Fa1P = typeparams.ForSignature(Fa1).At(0) 269 Fa2Q = typeparams.ForSignature(Fa2).At(0) 270 Fb1 = scope.Lookup("Fb1").Type().(*types.Signature) 271 Fb1x = Fb1.Params().At(0).Type() 272 Fb1y = scope.Lookup("Fb1").(*types.Func).Scope().Lookup("y").Type() 273 Fb2 = scope.Lookup("Fb2").Type().(*types.Signature) 274 Fb2x = Fb2.Params().At(0).Type() 275 G1 = scope.Lookup("G1").Type().(*types.Named) 276 G1M = G1.Method(0).Type() 277 G1IntM1 = instantiate(t, G1, types.Typ[types.Int]).(*types.Named).Method(0).Type() 278 G1IntM2 = instantiate(t, G1, types.Typ[types.Int]).(*types.Named).Method(0).Type() 279 G1StrM = instantiate(t, G1, types.Typ[types.String]).(*types.Named).Method(0).Type() 280 G2 = scope.Lookup("G2").Type() 281 // See below. 282 // G2M = G2.Method(0).Type() 283 G2IntM = instantiate(t, G2, types.Typ[types.Int]).(*types.Named).Method(0).Type() 284 ME1 = scope.Lookup("ME1").Type() 285 ME1Type = scope.Lookup("ME1Type").Type() 286 ME2 = scope.Lookup("ME2").Type() 287 ) 288 289 tmap := new(typeutil.Map) 290 291 steps := []struct { 292 typ types.Type 293 name string 294 newEntry bool 295 }{ 296 {T1, "T1", true}, 297 {T2, "T2", true}, 298 {G, "G", true}, 299 {C, "C", true}, 300 {CI, "CI", true}, 301 {U, "U", true}, 302 {I, "I", true}, 303 {II, "II", true}, // should not be identical to CI 304 305 // Methods can be identical, even with distinct receivers. 306 {T1M, "T1M", true}, 307 {T2M, "T2M", false}, 308 309 // Identical instances should map to the same entry. 310 {GInt1, "GInt1", true}, 311 {GInt2, "GInt2", false}, 312 // ..but instantiating with different arguments should yield a new entry. 313 {GStr, "GStr", true}, 314 315 // F1 and F2 should have identical signatures. 316 {Fa1, "F1", true}, 317 {Fa2, "F2", false}, 318 319 // The identity of P and Q should not have been affected by type parameter 320 // masking during signature hashing. 321 {Fa1P, "F1P", true}, 322 {Fa2Q, "F2Q", true}, 323 324 {Fb1y, "Fb1y", true}, 325 {Fb1x, "Fb1x", false}, 326 {Fb2x, "Fb2x", true}, 327 {Fb1, "Fb1", true}, 328 329 // Mapping elements of the function scope should not affect the identity of 330 // Fb2 or Fb1. 331 {Fb2, "Fb1", false}, 332 333 {G1, "G1", true}, 334 {G1M, "G1M", true}, 335 {G2, "G2", true}, 336 337 // See golang/go#49912: receiver type parameter names should be ignored 338 // when comparing method identity. 339 // {G2M, "G2M", false}, 340 {G1IntM1, "G1IntM1", true}, 341 {G1IntM2, "G1IntM2", false}, 342 {G1StrM, "G1StrM", true}, 343 {G2IntM, "G2IntM", false}, // identical to G1IntM1 344 345 {ME1, "ME1", true}, 346 {ME1Type, "ME1Type", false}, 347 {ME2, "ME2", true}, 348 } 349 350 for _, step := range steps { 351 existing := tmap.At(step.typ) 352 if (existing == nil) != step.newEntry { 353 t.Errorf("At(%s) = %v, want new entry: %t", step.name, existing, step.newEntry) 354 } 355 tmap.Set(step.typ, step.name) 356 } 357 } 358 359 func instantiate(t *testing.T, origin types.Type, targs ...types.Type) types.Type { 360 inst, err := typeparams.Instantiate(nil, origin, targs, true) 361 if err != nil { 362 t.Fatal(err) 363 } 364 return inst 365 }