Source file src/go/types/context_test.go

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

View as plain text