[dev.go2go] test: drop type keyword, always provide constraint

This is a rewrite to drop the type keyword for parameterized functions
and types, and to instead always provide a constraint.

We can revert if we don't like this approach.

Change-Id: If4669fb4e2015067e9f955ff8567ed421be639ea
Reviewed-on: https://go-review.googlesource.com/c/go/+/248880
Reviewed-by: Ian Lance Taylor <iant@golang.org>
This commit is contained in:
Ian Lance Taylor 2020-08-17 10:43:45 -07:00
parent af48c2e84b
commit bc00f5ba9f
47 changed files with 90 additions and 90 deletions

View File

@ -6,17 +6,17 @@
package p
func F1[type T comparable]() {}
func F1[T comparable]() {}
func F2() { F1[[]int]() } // ERROR "\[\]int does not satisfy comparable$"
type C interface {
M()
}
func F3[type T C]() {}
func F3[T C]() {}
func F4() { F3[int]() } // ERROR "int does not satisfy C.*method M"
func F5[type T]() { F3[T]() } // ERROR "T does not satisfy C.*method M"
func F5[T any]() { F3[T]() } // ERROR "T does not satisfy C.*method M"
type signed interface {
type int, int8, int16, int32, int64
@ -27,9 +27,9 @@ type integer interface {
uint, uint8, uint16, uint32, uint64, uintptr
}
func F6[type T signed](a T) bool { return a < 0 }
func F7[type T](a T) bool { return F6(a) } // ERROR "T does not satisfy signed.*T has no type constraints"
func F8[type T integer](a T) bool { return F6(a) } // ERROR "T does not satisfy signed.*T type constraint uint not found in"
func F6[T signed](a T) bool { return a < 0 }
func F7[T any](a T) bool { return F6(a) } // ERROR "T does not satisfy signed.*T has no type constraints"
func F8[T integer](a T) bool { return F6(a) } // ERROR "T does not satisfy signed.*T type constraint uint not found in"
func F9(a uint) bool { return F6(a) } // ERROR "uint does not satisfy signed.*uint not found in"
type MyInt int
@ -43,7 +43,7 @@ type C2 interface {
signed
}
func F20[type T C2](a T) bool {
func F20[T C2](a T) bool {
a.M()
return a < 0
}
@ -57,8 +57,8 @@ func (S) M() {}
func F21() bool { return F20(MyInt(0)) }
func F22() bool { return F20(0) } // ERROR "int does not satisfy C2.*(missing method M)"
func F23[type T](a T) bool { return F20(a) } // ERROR "T does not satisfy C2.*(missing method M)"
func F24[type T integer](a T) bool { return F20(a) } // ERROR "T does not satisfy C2.*(missing method M)"
func F23[T any](a T) bool { return F20(a) } // ERROR "T does not satisfy C2.*(missing method M)"
func F24[T integer](a T) bool { return F20(a) } // ERROR "T does not satisfy C2.*(missing method M)"
func F25(a uint) bool { return F20(a) } // ERROR "uint does not satisfy C2.*(missing method M)"
func F26(a MyUint) bool { return F20(a) } // ERROR "MyUint does not satisfy C2.*uint not found in"
func F27(a S) bool { return F20(a) } // ERROR "S does not satisfy C2.*struct{} not found in"

View File

@ -6,5 +6,5 @@ package b
import "./a"
func F1[type T a.C]() {}
func F1[T a.C]() {}
func F2() { F1(int)() } // ERROR "int does not satisfy a.C.*method M"

View File

@ -10,7 +10,7 @@ type Setter interface {
Set(string)
}
func FromStrings[type T Setter](s []string) []T {
func FromStrings[T Setter](s []string) []T {
result := make([]T, len(s))
for i, v := range s {
result[i].Set(v)

View File

@ -7,5 +7,5 @@
package p
func F() {
type t[type T] T // ERROR "parameterized type"
type t[T any] T // ERROR "parameterized type"
}

View File

@ -7,7 +7,7 @@
// Issue 39738.
package p
func F1[type T]() {}
func F1[T any]() {}
func F2() {
type s struct{}

View File

@ -7,7 +7,7 @@
// Issue 39743.
package p
type S[type T] struct{}
type S[T any] struct{}
func (s S[_]) M() {} // ERROR "_"

View File

@ -8,7 +8,7 @@ package main
import "fmt"
func Print[type T](s []T) {
func Print[T any](s []T) {
for _, v := range s {
fmt.Println(v)
}

View File

@ -8,13 +8,13 @@ package main
import "fmt"
func Print1[type T](s []T) {
func Print1[T any](s []T) {
for _, v := range s {
fmt.Println(v)
}
}
func Print2[type T](s []T) {
func Print2[T any](s []T) {
Print1(T)(s)
}

View File

@ -11,7 +11,7 @@ import (
"unsafe"
)
type Pair[type F1, F2] struct {
type Pair[F1, F2 any] struct {
f1 F1
f2 F2
}

View File

@ -8,7 +8,7 @@ package main
import "fmt"
type Value[type T] struct {
type Value[T any] struct {
val T
}

View File

@ -4,7 +4,7 @@
package a
type Stack[type E] []E
type Stack[E any] []E
func (s *Stack[E]) Push(e E) {
*s = append(*s, e)

View File

@ -20,7 +20,7 @@ type Ordered interface {
string
}
type orderedSlice[type Elem Ordered] []Elem
type orderedSlice[Elem Ordered] []Elem
func (s orderedSlice[Elem]) Len() int { return len(s) }
func (s orderedSlice[Elem]) Less(i, j int) bool {
@ -35,7 +35,7 @@ func (s orderedSlice[Elem]) Less(i, j int) bool {
}
func (s orderedSlice[Elem]) Swap(i, j int) { s[i], s[j] = s[j], s[i] }
func OrderedSlice[type Elem Ordered](s []Elem) {
func OrderedSlice[Elem Ordered](s []Elem) {
sort.Sort(orderedSlice[Elem](s))
}
@ -55,7 +55,7 @@ func TestSortOrderedStrings() bool {
return testOrdered("strings", strings, sort.Strings)
}
func testOrdered[type Elem Ordered](name string, s []Elem, sorter func([]Elem)) bool {
func testOrdered[Elem Ordered](name string, s []Elem, sorter func([]Elem)) bool {
s1 := make([]Elem, len(s))
copy(s1, s)
s2 := make([]Elem, len(s))
@ -76,7 +76,7 @@ func testOrdered[type Elem Ordered](name string, s []Elem, sorter func([]Elem))
return ok
}
func sliceEq[type Elem Ordered](s1, s2 []Elem) bool {
func sliceEq[Elem Ordered](s1, s2 []Elem) bool {
for i, v1 := range s1 {
v2 := s2[i]
if v1 != v2 {

View File

@ -9,11 +9,11 @@
package p
type S[type T] struct {
type S[T any] struct {
f T
}
func SliceS[type T]() S[[]T] {
func SliceS[T any]() S[[]T] {
return S[[]T]{nil}
}

View File

@ -10,7 +10,7 @@ type Setter interface {
Set(string)
}
func FromStrings[type T Setter](s []string) []T {
func FromStrings[T Setter](s []string) []T {
result := make([]T, len(s))
for i, v := range s {
result[i].Set(v)

View File

@ -11,12 +11,12 @@ import (
"strconv"
)
type Setter2[type B] interface {
type Setter2[B any] interface {
Set(string)
type *B
}
func FromStrings2[type T interface{}, PT Setter2[T]](s []string) []T {
func FromStrings2[T any, PT Setter2[T]](s []string) []T {
result := make([]T, len(s))
for i, v := range s {
p := PT(&result[i])

View File

@ -11,7 +11,7 @@ import (
"strconv"
)
func FromStrings3[type T](s []string, set func(*T, string)) []T {
func FromStrings3[T any](s []string, set func(*T, string)) []T {
results := make([]T, len(s))
for i, v := range s {
set(&results[i], v)

View File

@ -15,7 +15,7 @@ type Setter interface {
Set(string)
}
func FromStrings[type *T Setter](s []string) []T {
func FromStrings[*T Setter](s []string) []T {
result := make([]T, len(s))
for i, v := range s {
// result[i] is an addressable value of type T,

View File

@ -16,14 +16,14 @@ type Numeric interface {
}
// NumericAbs matches numeric types with an Abs method.
type NumericAbs[type T] interface {
type NumericAbs[T any] interface {
Numeric
Abs() T
}
// AbsDifference computes the absolute value of the difference of
// a and b, where the absolute value is determined by the Abs method.
func AbsDifference[type T NumericAbs](a, b T) T {
func AbsDifference[T NumericAbs](a, b T) T {
d := a - b
return d.Abs()
}
@ -42,7 +42,7 @@ type Complex interface {
// OrderedAbs is a helper type that defines an Abs method for
// ordered numeric types.
type OrderedAbs[type T OrderedNumeric] T
type OrderedAbs[T OrderedNumeric] T
func (a OrderedAbs[T]) Abs() OrderedAbs[T] {
if a < 0 {
@ -53,7 +53,7 @@ func (a OrderedAbs[T]) Abs() OrderedAbs[T] {
// ComplexAbs is a helper type that defines an Abs method for
// complex types.
type ComplexAbs[type T Complex] T
type ComplexAbs[T Complex] T
func (a ComplexAbs[T]) Abs() ComplexAbs[T] {
r := float64(real(a))
@ -64,13 +64,13 @@ func (a ComplexAbs[T]) Abs() ComplexAbs[T] {
// OrderedAbsDifference returns the absolute value of the difference
// between a and b, where a and b are of an ordered type.
func OrderedAbsDifference[type T OrderedNumeric](a, b T) T {
func OrderedAbsDifference[T OrderedNumeric](a, b T) T {
return T(AbsDifference(OrderedAbs(T)(a), OrderedAbs(T)(b)))
}
// ComplexAbsDifference returns the absolute value of the difference
// between a and b, where a and b are of a complex type.
func ComplexAbsDifference[type T Complex](a, b T) T {
func ComplexAbsDifference[T Complex](a, b T) T {
return T(AbsDifference(ComplexAbs(T)(a), ComplexAbs(T)(b)))
}

View File

@ -6,9 +6,9 @@
package main
type Gen[type A] func() (A, bool)
type Gen[A any] func() (A, bool)
func combine[type T1, T2, T](g1 Gen[T1], g2 Gen[T2], join func(T1, T2) T) Gen[T] {
func combine[T1, T2, T any](g1 Gen[T1], g2 Gen[T2], join func(T1, T2) T) Gen[T] {
return func() (T, bool) {
var t T
t1, ok := g1()
@ -23,14 +23,14 @@ func combine[type T1, T2, T](g1 Gen[T1], g2 Gen[T2], join func(T1, T2) T) Gen[T]
}
}
type Pair[type A, B] struct {
type Pair[A, B any] struct {
A A
B B
}
func NewPair[type A, B](a A, b B) Pair[A, B] { return Pair[A, B]{a, b} }
func NewPair[A, B any](a A, b B) Pair[A, B] { return Pair[A, B]{a, b} }
func Combine2[type A, B](ga Gen[A], gb Gen[B]) Gen[Pair[A, B]] {
func Combine2[A, B any](ga Gen[A], gb Gen[B]) Gen[Pair[A, B]] {
return combine(ga, gb, NewPair(A, B))
}

View File

@ -6,7 +6,7 @@
package main
type T[type A] struct {
type T[A any] struct {
V A
}

View File

@ -6,7 +6,7 @@
package p
func F1[type T](x T) {}
func F1[T any](x T) {}
func F2(c <-chan int) {
F1(c)

View File

@ -10,7 +10,7 @@ import "fmt"
type Any interface{}
type Function[type a Any, b Any] interface {
type Function[a, b any] interface {
Apply(x a) b
}
@ -26,7 +26,7 @@ func (this pos) Apply(x int) bool {
return x > 0
}
type compose[type a Any, b Any, c Any] struct {
type compose[a, b, c any] struct {
f Function[a, b]
g Function[b, c]
}
@ -35,7 +35,7 @@ func (this compose[a, b, c]) Apply(x a) c {
return this.g.Apply(this.f.Apply(x))
}
type Eq[type a] interface {
type Eq[a any] interface {
Equal(a) bool
}
@ -45,17 +45,17 @@ func (this Int) Equal(that int) bool {
return int(this) == that
}
type List[type a Any] interface {
type List[a any] interface {
Match(casenil Function[Nil[a], Any], casecons Function[Cons[a], Any]) Any
}
type Nil[type a Any] struct{}
type Nil[a any] struct{}
func (xs Nil[a]) Match(casenil Function[Nil[a], Any], casecons Function[Cons[a], Any]) Any {
return casenil.Apply(xs)
}
type Cons[type a Any] struct {
type Cons[a any] struct {
Head a
Tail List[a]
}
@ -64,13 +64,13 @@ func (xs Cons[a]) Match(casenil Function[Nil[a], Any], casecons Function[Cons[a]
return casecons.Apply(xs)
}
type mapNil[type a Any, b Any] struct{}
type mapNil[a, b any] struct{}
func (m mapNil[a, b]) Apply(_ Nil[a]) Any {
return Nil[b]{}
}
type mapCons[type a Any, b Any] struct {
type mapCons[a, b any] struct {
f Function[a, b]
}
@ -78,7 +78,7 @@ func (m mapCons[a, b]) Apply(xs Cons[a]) Any {
return Cons[b]{m.f.Apply(xs.Head), Map[a, b](m.f, xs.Tail)}
}
func Map[type a, b Any](f Function[a, b], xs List[a]) List[b] {
func Map[a, b any](f Function[a, b], xs List[a]) List[b] {
return xs.Match(mapNil[a, b]{}, mapCons[a, b]{f}).(List[b])
}

View File

@ -8,11 +8,11 @@ package main
type Recv <-chan int
type sliceOf[type E] interface {
type sliceOf[E any] interface {
type []E
}
func Append[type S sliceOf[T], T interface{}](s S, t ...T) S {
func Append[S sliceOf[T], T any](s S, t ...T) S {
return append(s, t...)
}

View File

@ -13,7 +13,7 @@ func (*S1) M() {}
type S2 struct{}
func (S2) M() {}
func F1[type T interface{ M() }](t T) {
func F1[T interface{ M() }](t T) {
_ = T.M
}

View File

@ -9,12 +9,12 @@ package p
type E struct{}
type N[type T] struct {
type N[T any] struct {
i int
n T
}
func F1[type T](i int, n T) N[T] {
func F1[T any](i int, n T) N[T] {
return N[T]{i: i, n: n}
}

View File

@ -10,7 +10,7 @@ import "sync"
// A Lockable is a value that may be safely simultaneously accessed
// from multiple goroutines via the Get and Set methods.
type Lockable[type T] struct {
type Lockable[T any] struct {
T
mu sync.Mutex
}

View File

@ -6,6 +6,6 @@
package p
type F[type T] func(T) error
type F[T any] func(T) error
type instF = F[int]

View File

@ -8,7 +8,7 @@ package p
import "net/url"
type S[type T] struct {
type S[T any] struct {
f T
}

View File

@ -7,7 +7,7 @@
// Issue 39672
package p
type S[type T] T
type S[T any] T
func (r S[T]) M()
@ -16,7 +16,7 @@ func F() {
_ = x
}
func G[type T](v T)
func G[T any](v T)
func F2() {
G(0)

View File

@ -7,17 +7,17 @@
// Issue 39692
package p
type S1[type T1, T2] struct {
type S1[T1, T2 any] struct {
f1 T1
f2 T2
}
type S2[type T1, T2] struct {
type S2[T1, T2 any] struct {
f1 T1
f2 T2
}
func F1[type T](v T) T {
func F1[T any](v T) T {
return v
}

View File

@ -9,7 +9,7 @@ package main
import "io/ioutil"
func F[type T1, T2](f func(T1) (T2, error)) {}
func F[T1, T2 any](f func(T1) (T2, error)) {}
func main() {
F(ioutil.ReadAll)

View File

@ -7,7 +7,7 @@
// Issue 39678, 39881.
package p
type B[type T] struct {
type B[T any] struct {
T
}
@ -21,7 +21,7 @@ func F(s BC) {
s.F = 7
}
type Pair[type T, U] struct {
type Pair[T, U any] struct {
T
U
}

View File

@ -7,7 +7,7 @@
// Issue 39688.
package p
type S[type T] T
type S[T any] T
func (*S[T]) M() {}

View File

@ -7,7 +7,7 @@
// Issue 39741.
package p
func F[type T]() {
func F[T any]() {
type S struct{}
}

View File

@ -4,7 +4,7 @@
package main
func Len[type T](s []T) int {
func Len[T any](s []T) int {
return len(s)
}

View File

@ -7,7 +7,7 @@
// Issue 39737.
package p
func F1[type T]() {}
func F1[T any]() {}
func F2() {
F1(struct { F string `json:"f"` })()

View File

@ -9,7 +9,7 @@ package p
import "net/http"
func F1[type T](f func() T) { f() }
func F1[T any](f func() T) { f() }
func F2() {
F1(func() *http.Request { return nil })

View File

@ -4,6 +4,6 @@
package a
type S[type T] struct {
type S[T any] struct {
F T
}

View File

@ -8,7 +8,7 @@ package p
type I interface{}
type S[type T] struct {
type S[T any] struct {
*T
}

View File

@ -4,6 +4,6 @@
package a
type S[type T] struct {
type S[T any] struct {
f T
}

View File

@ -8,7 +8,7 @@ import "./a"
type S = a.S
func F1[type T](T) {}
func F1[T any](T) {}
func F2() { F1(S[string]{}) }

View File

@ -4,10 +4,10 @@
package a
type S[type T] struct {
type S[T any] struct {
F T
}
func NewS[type T](v T) S[T] {
func NewS[T any](v T) S[T] {
return S[T]{F: v}
}

View File

@ -6,11 +6,11 @@ package main
import "./a"
type S[type T] struct {
type S[T any] struct {
F T
}
func NewS[type T](v T) S[T] {
func NewS[T any](v T) S[T] {
return S[T]{F: v}
}

View File

@ -6,6 +6,6 @@ package a
func F() {}
func G[type T]() {
func G[T any]() {
F()
}

View File

@ -6,7 +6,7 @@
package main
func F[type T comparable](a T) bool {
func F[T comparable](a T) bool {
return a == a
}

View File

@ -6,15 +6,15 @@
package p
type top[type T, T2] struct {
type top[T, T2 any] struct {
p *parent[T, T2]
}
type parent[type T, T2] struct {
type parent[T, T2 any] struct {
child[T, T2]
}
type child[type T, T2] struct {
type child[T, T2 any] struct {
T2
}

View File

@ -6,11 +6,11 @@
package p
type S1[type T] struct{}
type S1[T any] struct{}
type S2[type K comparable] struct{}
type S2[K comparable] struct{}
type S3[type K comparable] struct {
type S3[K comparable] struct {
f map[K]*S1[*S2[K]]
}