]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile/internal/types2: add a test for Context deduplication of hash collisions
authorRobert Griesemer <gri@golang.org>
Tue, 16 Nov 2021 19:36:09 +0000 (11:36 -0800)
committerRobert Griesemer <gri@golang.org>
Wed, 17 Nov 2021 04:32:02 +0000 (04:32 +0000)
This CL is a clean port of CL 363517 from go/types to types2,
with the exception that types_test.go was not removed because
it's still needed to set a types2-specific test flag.

Change-Id: I12177866537c0f95f3fa36fa0f4aa02016609ca9
Reviewed-on: https://go-review.googlesource.com/c/go/+/364494
Trust: Robert Griesemer <gri@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
src/cmd/compile/internal/types2/context_test.go [new file with mode: 0644]
src/cmd/compile/internal/types2/types_test.go

diff --git a/src/cmd/compile/internal/types2/context_test.go b/src/cmd/compile/internal/types2/context_test.go
new file mode 100644 (file)
index 0000000..aa649b1
--- /dev/null
@@ -0,0 +1,69 @@
+// Copyright 2021 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package types2
+
+import (
+       "testing"
+)
+
+func TestContextHashCollisions(t *testing.T) {
+       if debug {
+               t.Skip("hash collisions are expected, and would fail debug assertions")
+       }
+       // Unit test the de-duplication fall-back logic in Context.
+       //
+       // We can't test this via Instantiate because this is only a fall-back in
+       // case our hash is imperfect.
+       //
+       // These lookups and updates use reasonable looking types in an attempt to
+       // make them robust to internal type assertions, but could equally well use
+       // arbitrary types.
+
+       // Create some distinct origin types. nullaryP and nullaryQ have no
+       // parameters and are identical (but have different type parameter names).
+       // unaryP has a parameter.
+       var nullaryP, nullaryQ, unaryP Type
+       {
+               // type nullaryP = func[P any]()
+               tparam := NewTypeParam(NewTypeName(nopos, nil, "P", nil), &emptyInterface)
+               nullaryP = NewSignatureType(nil, nil, []*TypeParam{tparam}, nil, nil, false)
+       }
+       {
+               // type nullaryQ = func[Q any]()
+               tparam := NewTypeParam(NewTypeName(nopos, nil, "Q", nil), &emptyInterface)
+               nullaryQ = NewSignatureType(nil, nil, []*TypeParam{tparam}, nil, nil, false)
+       }
+       {
+               // type unaryP = func[P any](_ P)
+               tparam := NewTypeParam(NewTypeName(nopos, nil, "P", nil), &emptyInterface)
+               params := NewTuple(NewVar(nopos, nil, "_", tparam))
+               unaryP = NewSignatureType(nil, nil, []*TypeParam{tparam}, params, nil, false)
+       }
+
+       ctxt := NewContext()
+
+       // Update the context with an instantiation of nullaryP.
+       inst := NewSignatureType(nil, nil, nil, nil, nil, false)
+       if got := ctxt.update("", nullaryP, []Type{Typ[Int]}, inst); got != inst {
+               t.Error("bad")
+       }
+
+       // unaryP is not identical to nullaryP, so we should not get inst when
+       // instantiated with identical type arguments.
+       if got := ctxt.lookup("", unaryP, []Type{Typ[Int]}); got != nil {
+               t.Error("bad")
+       }
+
+       // nullaryQ is identical to nullaryP, so we *should* get inst when
+       // instantiated with identical type arguments.
+       if got := ctxt.lookup("", nullaryQ, []Type{Typ[Int]}); got != inst {
+               t.Error("bad")
+       }
+
+       // ...but verify we don't get inst with different type arguments.
+       if got := ctxt.lookup("", nullaryQ, []Type{Typ[String]}); got != nil {
+               t.Error("bad")
+       }
+}
index 1525844f2dfcc2cbf07784b75afe6cfb9bbdd9ab..11dca0b53de86421a3a34f5ae505d90dca61d3f2 100644 (file)
@@ -7,6 +7,3 @@ package types2
 func init() {
        acceptMethodTypeParams = true
 }
-
-// Debug is set if types2 is built with debug mode enabled.
-const Debug = debug