github.com/AndrienkoAleksandr/go@v0.0.19/src/go/types/context_test.go (about) 1 // Code generated by "go test -run=Generate -write=all"; DO NOT EDIT. 2 3 // Copyright 2021 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 package types 8 9 import ( 10 "testing" 11 ) 12 13 func TestContextHashCollisions(t *testing.T) { 14 if debug { 15 t.Skip("hash collisions are expected, and would fail debug assertions") 16 } 17 // Unit test the de-duplication fall-back logic in Context. 18 // 19 // We can't test this via Instantiate because this is only a fall-back in 20 // case our hash is imperfect. 21 // 22 // These lookups and updates use reasonable looking types in an attempt to 23 // make them robust to internal type assertions, but could equally well use 24 // arbitrary types. 25 26 // Create some distinct origin types. nullaryP and nullaryQ have no 27 // parameters and are identical (but have different type parameter names). 28 // unaryP has a parameter. 29 var nullaryP, nullaryQ, unaryP Type 30 { 31 // type nullaryP = func[P any]() 32 tparam := NewTypeParam(NewTypeName(nopos, nil, "P", nil), &emptyInterface) 33 nullaryP = NewSignatureType(nil, nil, []*TypeParam{tparam}, nil, nil, false) 34 } 35 { 36 // type nullaryQ = func[Q any]() 37 tparam := NewTypeParam(NewTypeName(nopos, nil, "Q", nil), &emptyInterface) 38 nullaryQ = NewSignatureType(nil, nil, []*TypeParam{tparam}, nil, nil, false) 39 } 40 { 41 // type unaryP = func[P any](_ P) 42 tparam := NewTypeParam(NewTypeName(nopos, nil, "P", nil), &emptyInterface) 43 params := NewTuple(NewVar(nopos, nil, "_", tparam)) 44 unaryP = NewSignatureType(nil, nil, []*TypeParam{tparam}, params, nil, false) 45 } 46 47 ctxt := NewContext() 48 49 // Update the context with an instantiation of nullaryP. 50 inst := NewSignatureType(nil, nil, nil, nil, nil, false) 51 if got := ctxt.update("", nullaryP, []Type{Typ[Int]}, inst); got != inst { 52 t.Error("bad") 53 } 54 55 // unaryP is not identical to nullaryP, so we should not get inst when 56 // instantiated with identical type arguments. 57 if got := ctxt.lookup("", unaryP, []Type{Typ[Int]}); got != nil { 58 t.Error("bad") 59 } 60 61 // nullaryQ is identical to nullaryP, so we *should* get inst when 62 // instantiated with identical type arguments. 63 if got := ctxt.lookup("", nullaryQ, []Type{Typ[Int]}); got != inst { 64 t.Error("bad") 65 } 66 67 // ...but verify we don't get inst with different type arguments. 68 if got := ctxt.lookup("", nullaryQ, []Type{Typ[String]}); got != nil { 69 t.Error("bad") 70 } 71 }