]> Cypherpunks repositories - gostls13.git/commitdiff
constraints: remove Slice/Map/Chan
authorIan Lance Taylor <iant@golang.org>
Wed, 27 Oct 2021 21:11:33 +0000 (14:11 -0700)
committerIan Lance Taylor <iant@golang.org>
Wed, 27 Oct 2021 22:17:35 +0000 (22:17 +0000)
Now that we permit arbitrary types as constraints, we no longer need them.

For #48424

Change-Id: I15fef26a563988074650cb0801895b002c44148a
Reviewed-on: https://go-review.googlesource.com/c/go/+/359258
Trust: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
src/constraints/constraints.go
src/constraints/constraints_test.go
test/typeparam/issue48609.go

index 2a5f673a7ef61e22952de2acb8ee0605cea00905..2c033dff47e904fc5cd8643c2e6b5c3d1f42c5fe 100644 (file)
@@ -48,18 +48,3 @@ type Complex interface {
 type Ordered interface {
        Integer | Float | ~string
 }
-
-// Slice is a constraint that matches slices of any element type.
-type Slice[Elem any] interface {
-       ~[]Elem
-}
-
-// Map is a constraint that matches maps of any element and value type.
-type Map[Key comparable, Val any] interface {
-       ~map[Key]Val
-}
-
-// Chan is a constraint that matches channels of any element type.
-type Chan[Elem any] interface {
-       ~chan Elem
-}
index db5a95731320afcea1e64da3dbd0199f842a7d0b..538dc843cca9bec137465e9da8a33b8d054e6aff 100644 (file)
@@ -15,15 +15,12 @@ import (
 )
 
 type (
-       testSigned[T Signed]                      struct{ f T }
-       testUnsigned[T Unsigned]                  struct{ f T }
-       testInteger[T Integer]                    struct{ f T }
-       testFloat[T Float]                        struct{ f T }
-       testComplex[T Complex]                    struct{ f T }
-       testOrdered[T Ordered]                    struct{ f T }
-       testSlice[T Slice[E], E any]              struct{ f T }
-       testMap[T Map[K, V], K comparable, V any] struct{ f T }
-       testChan[T Chan[E], E any]                struct{ f T }
+       testSigned[T Signed]     struct{ f T }
+       testUnsigned[T Unsigned] struct{ f T }
+       testInteger[T Integer]   struct{ f T }
+       testFloat[T Float]       struct{ f T }
+       testComplex[T Complex]   struct{ f T }
+       testOrdered[T Ordered]   struct{ f T }
 )
 
 // TestTypes passes if it compiles.
@@ -40,35 +37,6 @@ type TestTypes struct {
        _ testOrdered[int]
        _ testOrdered[float64]
        _ testOrdered[string]
-       _ testSlice[[]int, int]
-       _ testMap[map[int]bool, int, bool]
-       _ testChan[chan int, int]
-}
-
-func infer1[S Slice[E], E any](s S, v E) S                     { return s }
-func infer2[M Map[K, V], K comparable, V any](m M, k K, v V) M { return m }
-func infer3[C Chan[E], E any](c C, v E) C                      { return c }
-
-func TestInference(t *testing.T) {
-       var empty interface{}
-
-       type S []int
-       empty = infer1(S{}, 0)
-       if _, ok := empty.(S); !ok {
-               t.Errorf("infer1(S) returned %T, expected S", empty)
-       }
-
-       type M map[int]bool
-       empty = infer2(M{}, 0, false)
-       if _, ok := empty.(M); !ok {
-               t.Errorf("infer2(M) returned %T, expected M", empty)
-       }
-
-       type C chan bool
-       empty = infer3(make(C), true)
-       if _, ok := empty.(C); !ok {
-               t.Errorf("infer3(C) returned %T, expected C", empty)
-       }
 }
 
 var prolog = []byte(`
@@ -77,15 +45,12 @@ package constrainttest
 import "constraints"
 
 type (
-       testSigned[T constraints.Signed]                      struct{ f T }
-       testUnsigned[T constraints.Unsigned]                  struct{ f T }
-       testInteger[T constraints.Integer]                    struct{ f T }
-       testFloat[T constraints.Float]                        struct{ f T }
-       testComplex[T constraints.Complex]                    struct{ f T }
-       testOrdered[T constraints.Ordered]                    struct{ f T }
-       testSlice[T constraints.Slice[E], E any]              struct{ f T }
-       testMap[T constraints.Map[K, V], K comparable, V any] struct{ f T }
-       testChan[T constraints.Chan[E], E any]                struct{ f T }
+       testSigned[T constraints.Signed]     struct{ f T }
+       testUnsigned[T constraints.Unsigned] struct{ f T }
+       testInteger[T constraints.Integer]   struct{ f T }
+       testFloat[T constraints.Float]       struct{ f T }
+       testComplex[T constraints.Complex]   struct{ f T }
+       testOrdered[T constraints.Ordered]   struct{ f T }
 )
 `)
 
@@ -115,9 +80,6 @@ func TestFailure(t *testing.T) {
                {"testFloat", "int8"},
                {"testComplex", "float64"},
                {"testOrdered", "bool"},
-               {"testSlice", "int, int"},
-               {"testMap", "string, string, string"},
-               {"testChan", "[]int, int"},
        } {
                i := i
                test := test
index 3ca9d6e7d68376994cad0895dd5926a54cd342ae..6cf6908291256a2a18162c8bc6f6f8aab465cebb 100644 (file)
@@ -6,9 +6,7 @@
 
 package p
 
-import "constraints"
-
-func f[T constraints.Chan[E], E any](e E) T {
+func f[T ~chan E, E any](e E) T {
        ch := make(T)
        go func() {
                defer close(ch)