1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
7 // import "io" // for type assertion tests
9 var _ any // ok to use any anywhere
10 func _[_ any, _ interface{any}](any) {
14 func identity[T any](x T) T { return x }
16 func _[_ any](x int) int { panic(0) }
17 func _[T any](T /* ERROR "redeclared" */ T)() {}
18 func _[T, T /* ERROR "redeclared" */ any]() {}
20 // Constraints (incl. any) may be parenthesized.
22 func _[_ (interface{})]() {}
24 func reverse[T any](list []T) []T {
25 rlist := make([]T, len(list))
27 for _, x := range list {
34 var _ = reverse /* ERROR "cannot use generic function reverse" */
35 var _ = reverse[int, float32 /* ERROR "got 2 type arguments" */ ] ([]int{1, 2, 3})
36 var _ = reverse[int]([ /* ERROR "cannot use" */ ]float32{1, 2, 3})
37 var f = reverse[chan int]
38 var _ = f(0 /* ERRORx `cannot use 0 .* as \[\]chan int` */ )
40 func swap[A, B any](a A, b B) (B, A) { return b, a }
42 var _ = swap /* ERROR "multiple-value" */ [int, float32](1, 2)
43 var f32, i = swap[int, float32](swap[float32, int](1, 2))
47 func swapswap[A, B any](a A, b B) (A, B) {
48 return swap[B, A](b, a)
51 type F[A, B any] func(A, B) (B, A)
53 func min[T interface{ ~int }](x, y T) T {
60 func _[T interface{~int | ~float32}](x, y T) bool { return x < y }
61 func _[T any](x, y T) bool { return x /* ERROR "type parameter T is not comparable" */ < y }
62 func _[T interface{~int | ~float32 | ~bool}](x, y T) bool { return x /* ERROR "type parameter T is not comparable" */ < y }
64 func _[T C1[T]](x, y T) bool { return x /* ERROR "type parameter T is not comparable" */ < y }
65 func _[T C2[T]](x, y T) bool { return x < y }
67 type C1[T any] interface{}
68 type C2[T any] interface{ ~int | ~float32 }
70 func new[T any]() *T {
75 var _ = new /* ERROR "cannot use generic function new" */
76 var _ *int = new[int]()
78 func _[T any](map[T /* ERROR "invalid map key type T (missing comparable constraint)" */]int) {} // w/o constraint we don't know if T is comparable
80 func f1[T1 any](struct{T1 /* ERRORx `cannot be a .* type parameter` */ }) int { panic(0) }
81 var _ = f1[int](struct{T1}{})
84 func f2[t1 any](struct{t1 /* ERRORx `cannot be a .* type parameter` */ ; x float32}) int { panic(0) }
85 var _ = f2[t1](struct{t1; x float32}{})
89 func f3[A, B, C any](A, struct{x B}, func(A, struct{x B}, *C)) int { panic(0) }
91 var _ = f3[int, rune, bool](1, struct{x rune}{}, nil)
95 func _[T any] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
96 func _[T interface{ ~int }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
97 func _[T interface{ ~string }] (x T, i int) { _ = x[i] }
98 func _[T interface{ ~[]int }] (x T, i int) { _ = x[i] }
99 func _[T interface{ ~[10]int | ~*[20]int | ~map[int]int }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] } // map and non-map types
100 func _[T interface{ ~string | ~[]byte }] (x T, i int) { _ = x[i] }
101 func _[T interface{ ~[]int | ~[1]rune }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
102 func _[T interface{ ~string | ~[]rune }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
104 // indexing with various combinations of map types in type sets (see issue #42616)
105 func _[T interface{ ~[]E | ~map[int]E }, E any](x T, i int) { _ = x /* ERROR "cannot index" */ [i] } // map and non-map types
106 func _[T interface{ ~[]E }, E any](x T, i int) { _ = &x[i] }
107 func _[T interface{ ~map[int]E }, E any](x T, i int) { _, _ = x[i] } // comma-ok permitted
108 func _[T interface{ ~map[int]E }, E any](x T, i int) { _ = &x /* ERROR "cannot take address" */ [i] }
109 func _[T interface{ ~map[int]E | ~map[uint]E }, E any](x T, i int) { _ = x /* ERROR "cannot index" */ [i] } // different map element types
110 func _[T interface{ ~[]E | ~map[string]E }, E any](x T, i int) { _ = x /* ERROR "cannot index" */ [i] } // map and non-map types
112 // indexing with various combinations of array and other types in type sets
113 func _[T interface{ [10]int }](x T, i int) { _ = x[i]; _ = x[9]; _ = x[10 /* ERROR "out of bounds" */ ] }
114 func _[T interface{ [10]byte | string }](x T, i int) { _ = x[i]; _ = x[9]; _ = x[10 /* ERROR "out of bounds" */ ] }
115 func _[T interface{ [10]int | *[20]int | []int }](x T, i int) { _ = x[i]; _ = x[9]; _ = x[10 /* ERROR "out of bounds" */ ] }
117 // indexing with strings and non-variable arrays (assignment not permitted)
118 func _[T string](x T) { _ = x[0]; x /* ERROR "cannot assign" */ [0] = 0 }
119 func _[T []byte | string](x T) { x /* ERROR "cannot assign" */ [0] = 0 }
120 func _[T [10]byte]() { f := func() (x T) { return }; f /* ERROR "cannot assign" */ ()[0] = 0 }
121 func _[T [10]byte]() { f := func() (x *T) { return }; f /* ERROR "cannot index" */ ()[0] = 0 }
122 func _[T [10]byte]() { f := func() (x *T) { return }; (*f())[0] = 0 }
123 func _[T *[10]byte]() { f := func() (x T) { return }; f()[0] = 0 }
127 func _[T interface{ ~[10]E }, E any] (x T, i, j, k int) { var _ []E = x[i:j] }
128 func _[T interface{ ~[10]E }, E any] (x T, i, j, k int) { var _ []E = x[i:j:k] }
129 func _[T interface{ ~[]byte }] (x T, i, j, k int) { var _ T = x[i:j] }
130 func _[T interface{ ~[]byte }] (x T, i, j, k int) { var _ T = x[i:j:k] }
131 func _[T interface{ ~string }] (x T, i, j, k int) { var _ T = x[i:j] }
132 func _[T interface{ ~string }] (x T, i, j, k int) { var _ T = x[i:j:k /* ERROR "3-index slice of string" */ ] }
136 func _[T interface{ []byte | myByte1 | myByte2 }] (x T, i, j, k int) { var _ T = x[i:j:k] }
137 func _[T interface{ []byte | myByte1 | []int }] (x T, i, j, k int) { var _ T = x /* ERROR "no core type" */ [i:j:k] }
139 func _[T interface{ []byte | myByte1 | myByte2 | string }] (x T, i, j, k int) { var _ T = x[i:j] }
140 func _[T interface{ []byte | myByte1 | myByte2 | string }] (x T, i, j, k int) { var _ T = x[i:j:k /* ERROR "3-index slice of string" */ ] }
141 func _[T interface{ []byte | myByte1 | []int | string }] (x T, i, j, k int) { var _ T = x /* ERROR "no core type" */ [i:j] }
145 func _[T any](x T) { _ = len(x /* ERROR "invalid argument" */ ) }
146 func _[T interface{ ~int }](x T) { _ = len(x /* ERROR "invalid argument" */ ) }
147 func _[T interface{ ~string | ~[]byte | ~int }](x T) { _ = len(x /* ERROR "invalid argument" */ ) }
148 func _[T interface{ ~string }](x T) { _ = len(x) }
149 func _[T interface{ ~[10]int }](x T) { _ = len(x) }
150 func _[T interface{ ~[]byte }](x T) { _ = len(x) }
151 func _[T interface{ ~map[int]int }](x T) { _ = len(x) }
152 func _[T interface{ ~chan int }](x T) { _ = len(x) }
153 func _[T interface{ ~string | ~[]byte | ~chan int }](x T) { _ = len(x) }
155 func _[T any](x T) { _ = cap(x /* ERROR "invalid argument" */ ) }
156 func _[T interface{ ~int }](x T) { _ = cap(x /* ERROR "invalid argument" */ ) }
157 func _[T interface{ ~string | ~[]byte | ~int }](x T) { _ = cap(x /* ERROR "invalid argument" */ ) }
158 func _[T interface{ ~string }](x T) { _ = cap(x /* ERROR "invalid argument" */ ) }
159 func _[T interface{ ~[10]int }](x T) { _ = cap(x) }
160 func _[T interface{ ~[]byte }](x T) { _ = cap(x) }
161 func _[T interface{ ~map[int]int }](x T) { _ = cap(x /* ERROR "invalid argument" */ ) }
162 func _[T interface{ ~chan int }](x T) { _ = cap(x) }
163 func _[T interface{ ~[]byte | ~chan int }](x T) { _ = cap(x) }
167 func _[T interface{}](x T) {
168 for range x /* ERROR "cannot range" */ {}
174 B1 interface{ string },
175 B2 interface{ string | myString },
177 C1 interface{ chan int },
178 C2 interface{ chan int | <-chan int },
179 C3 interface{ chan<- int },
181 S1 interface{ []int },
182 S2 interface{ []int | [10]int },
184 A1 interface{ [10]int },
185 A2 interface{ [10]int | []int },
187 P1 interface{ *[10]int },
188 P2 interface{ *[10]int | *[]int },
190 M1 interface{ map[string]int },
191 M2 interface{ map[string]int | map[string]string },
196 for _, _ = range b0 {}
201 for _, _ = range b1 {}
209 for _, _ /* ERROR "permits only one iteration variable" */ = range c0 {}
214 for _, _ /* ERROR "permits only one iteration variable" */ = range c1 {}
220 for range c3 /* ERROR "receive from send-only channel" */ {}
225 for _, _ = range s0 {}
230 for _, _ = range s1 {}
233 for range s2 /* ERRORx `cannot range over s2.*no core type` */ {}
238 for _, _ = range a0 {}
243 for _, _ = range a1 {}
246 for range a2 /* ERRORx `cannot range over a2.*no core type` */ {}
251 for _, _ = range p0 {}
256 for _, _ = range p1 {}
259 for range p2 /* ERRORx `cannot range over p2.*no core type` */ {}
261 var m0 map[string]int
264 for _, _ = range m0 {}
269 for _, _ = range m1 {}
272 for range m2 /* ERRORx `cannot range over m2.*no core type` */ {}
275 // type inference checks
277 var _ = new /* ERROR "cannot infer T" */ ()
279 func f4[A, B, C any](A, B) C { panic(0) }
281 var _ = f4 /* ERROR "cannot infer C" */ (1, 2)
282 var _ = f4[int, float32, complex128](1, 2)
284 func f5[A, B, C any](A, []*B, struct{f []C}) int { panic(0) }
286 var _ = f5[int, float32, complex128](0, nil, struct{f []complex128}{})
287 var _ = f5 /* ERROR "cannot infer" */ (0, nil, struct{f []complex128}{})
288 var _ = f5(0, []*float32{new[float32]()}, struct{f []complex128}{})
290 func f6[A any](A, []A) int { panic(0) }
294 func f6nil[A any](A) int { panic(0) }
296 var _ = f6nil /* ERROR "cannot infer" */ (nil)
298 // type inference with variadic functions
300 func f7[T any](...T) T { panic(0) }
302 var _ int = f7 /* ERROR "cannot infer T" */ ()
305 var _ int = f7([]int{}...)
306 var _ int = f7 /* ERROR "cannot use" */ ([]float64{}...)
307 var _ float64 = f7([]float64{}...)
308 var _ = f7[float64](1, 2.3)
309 var _ = f7(float64(1), 2.3)
310 var _ = f7(1, 2.3 /* ERROR "does not match" */ )
311 var _ = f7(1.2, 3 /* ERROR "does not match" */ )
313 func f8[A, B any](A, B, ...B) int { panic(0) }
315 var _ = f8(1) /* ERROR "not enough arguments" */
317 var _ = f8(1, 2.3, 3.4, 4.5)
318 var _ = f8(1, 2.3, 3.4, 4 /* ERROR "does not match" */ )
319 var _ = f8[int, float64](1, 2.3, 3.4, 4)
321 var _ = f8[int, float64](0, 0, nil...) // test case for #18268
323 // init functions cannot have type parameters
326 func init[_ /* ERROR "func init must have no type parameters" */ any]() {}
327 func init[P /* ERROR "func init must have no type parameters" */ any]() {}
332 func (T) m2[ /* ERROR "method must have no type parameters" */ _ any]() {}
333 func (T) m3[ /* ERROR "method must have no type parameters" */ P any]() {}
335 // type inference across parameterized types
337 type S1[P any] struct { f P }
339 func f9[P any](x S1[P]) {}
346 type S2[A, B, C any] struct{}
348 func f10[X, Y, Z any](a S2[X, int, Z], b S2[X, Y, bool]) {}
351 f10[int, float32, string](S2[int, int, string]{}, S2[int, float32, bool]{})
352 f10(S2[int, int, string]{}, S2[int, float32, bool]{})
353 f10(S2[P, int, P]{}, S2[P, float32, bool]{})
356 // corner case for type inference
357 // (was bug: after instantiating f11, the type-checker didn't mark f11 as non-generic)
365 // the previous example was extracted from
367 // For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639).
368 // func f12[T interface{m() T}]() {}
372 // func (a A[T]) m() A[T]
378 // method expressions
384 m(struct { f int }{42})
387 func _[T any] (x T) {
392 type I1[A any] interface {
396 var _ I1[int] = r1[int]{}
398 type r1[T any] struct{}
402 type I2[A, B any] interface {
407 var _ I2[int, float32] = R2[int, float32]{}
409 type R2[P, Q any] struct{}
411 func (_ R2[X, Y]) m1(X)
412 func (_ R2[X, Y]) m2(X) Y
414 // type assertions and type switches over generic types
415 // NOTE: These are currently disabled because it's unclear what the correct
416 // approach is, and one can always work around by assigning the variable to
417 // an interface first.
419 // // ReadByte1 corresponds to the ReadByte example in the draft design.
420 // func ReadByte1[T io.Reader](r T) (byte, error) {
421 // if br, ok := r.(io.ByteReader); ok {
422 // return br.ReadByte()
425 // _, err := r.Read(b[:])
429 // // ReadBytes2 is like ReadByte1 but uses a type switch instead.
430 // func ReadByte2[T io.Reader](r T) (byte, error) {
431 // switch br := r.(type) {
432 // case io.ByteReader:
433 // return br.ReadByte()
436 // _, err := r.Read(b[:])
440 // // type assertions and type switches over generic types are strict
441 // type I3 interface {
445 // type I4 interface {
446 // m() int // different signature from I3.m
449 // func _[T I3](x I3, p T) {
450 // // type assertions and type switches over interfaces are not strict
456 // // type assertions and type switches over generic types are strict
457 // _ = p /* ERROR "cannot have dynamic type I4" */.(I4)
459 // case I4 /* ERROR "cannot have dynamic type I4" */ :
463 // type assertions and type switches over generic types lead to errors for now
466 _ = x /* ERROR "cannot use type assertion" */ .(int)
467 switch x /* ERROR "cannot use type switch" */ .(type) {
471 var t interface{} = x
477 func _[T interface{~int}](x T) {
478 _ = x /* ERROR "cannot use type assertion" */ .(int)
479 switch x /* ERROR "cannot use type switch" */ .(type) {
483 var t interface{} = x
489 // error messages related to type bounds mention those bounds
490 type C[P any] interface{}
492 func _[P C[P]] (x P) {
493 x.m /* ERROR "x.m undefined" */ ()
499 x.m /* ERROR "type P has no field or method m" */ ()
502 func _[P interface{}] (x P) {
503 x.m /* ERROR "type P has no field or method m" */ ()
506 func _[P any] (x P) {
507 x.m /* ERROR "type P has no field or method m" */ ()