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.
4
5 package p
6
7 // import "io" // for type assertion tests
8
9 // The predeclared identifier "any" is only visible as a constraint
10 // in a type parameter list.
11 var _ any // ERROR undeclared
12 func _[_ any /* ok here */ , _ interface{any /* ERROR undeclared */ }](any /* ERROR undeclared */ ) {
13 var _ any /* ERROR undeclared */
14 }
15
16 func identity[T any](x T) T { return x }
17
18 func _[_ any](x int) int
19 func _[T any](T /* ERROR redeclared */ T)()
20 func _[T, T /* ERROR redeclared */ any]()
21
22 // Constraints (incl. any) may be parenthesized.
23 func _[_ (any)]() {}
24 func _[_ (interface{})]() {}
25
26 func reverse[T any](list []T) []T {
27 rlist := make([]T, len(list))
28 i := len(list)
29 for _, x := range list {
30 i--
31 rlist[i] = x
32 }
33 return rlist
34 }
35
36 var _ = reverse /* ERROR cannot use generic function reverse */
37 var _ = reverse[int, float32 /* ERROR got 2 type arguments */ ] ([]int{1, 2, 3})
38 var _ = reverse[int]([ /* ERROR cannot use */ ]float32{1, 2, 3})
39 var f = reverse[chan int]
40 var _ = f(0 /* ERROR cannot use 0 .* as \[\]chan int */ )
41
42 func swap[A, B any](a A, b B) (B, A) { return b, a }
43
44 var _ = swap /* ERROR single value is expected */ [int, float32](1, 2)
45 var f32, i = swap[int, float32](swap[float32, int](1, 2))
46 var _ float32 = f32
47 var _ int = i
48
49 func swapswap[A, B any](a A, b B) (A, B) {
50 return swap[B, A](b, a)
51 }
52
53 type F[A, B any] func(A, B) (B, A)
54
55 func min[T interface{ type int }](x, y T) T {
56 if x < y {
57 return x
58 }
59 return y
60 }
61
62 func _[T interface{type int, float32}](x, y T) bool { return x < y }
63 func _[T any](x, y T) bool { return x /* ERROR cannot compare */ < y }
64 func _[T interface{type int, float32, bool}](x, y T) bool { return x /* ERROR cannot compare */ < y }
65
66 func _[T C1[T]](x, y T) bool { return x /* ERROR cannot compare */ < y }
67 func _[T C2[T]](x, y T) bool { return x < y }
68
69 type C1[T any] interface{}
70 type C2[T any] interface{ type int, float32 }
71
72 func new[T any]() *T {
73 var x T
74 return &x
75 }
76
77 var _ = new /* ERROR cannot use generic function new */
78 var _ *int = new[int]()
79
80 func _[T any](map[T /* ERROR incomparable map key type T \(missing comparable constraint\) */]int) // w/o constraint we don't know if T is comparable
81
82 func f1[T1 any](struct{T1}) int
83 var _ = f1[int](struct{T1}{})
84 type T1 = int
85
86 func f2[t1 any](struct{t1; x float32}) int
87 var _ = f2[t1](struct{t1; x float32}{})
88 type t1 = int
89
90
91 func f3[A, B, C any](A, struct{x B}, func(A, struct{x B}, *C)) int
92
93 var _ = f3[int, rune, bool](1, struct{x rune}{}, nil)
94
95 // indexing
96
97 func _[T any] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
98 func _[T interface{ type int }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
99 func _[T interface{ type string }] (x T, i int) { _ = x[i] }
100 func _[T interface{ type []int }] (x T, i int) { _ = x[i] }
101 func _[T interface{ type [10]int, *[20]int, map[int]int }] (x T, i int) { _ = x[i] }
102 func _[T interface{ type string, []byte }] (x T, i int) { _ = x[i] }
103 func _[T interface{ type []int, [1]rune }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
104 func _[T interface{ type string, []rune }] (x T, i int) { _ = x /* ERROR "cannot index" */ [i] }
105
106 // indexing with various combinations of map types in type lists (see issue #42616)
107 func _[T interface{ type []E, map[int]E }, E any](x T, i int) { _ = x[i] }
108 func _[T interface{ type []E }, E any](x T, i int) { _ = &x[i] }
109 func _[T interface{ type map[int]E }, E any](x T, i int) { _, _ = x[i] } // comma-ok permitted
110 func _[T interface{ type []E, map[int]E }, E any](x T, i int) { _ = &x /* ERROR cannot take address */ [i] }
111 func _[T interface{ type []E, map[int]E, map[uint]E }, E any](x T, i int) { _ = x /* ERROR cannot index */ [i] } // different map element types
112 func _[T interface{ type []E, map[string]E }, E any](x T, i int) { _ = x[i /* ERROR cannot use i */ ] }
113
114 // slicing
115 // TODO(gri) implement this
116
117 func _[T interface{ type string }] (x T, i, j, k int) { _ = x /* ERROR invalid operation */ [i:j:k] }
118
119 // len/cap built-ins
120
121 func _[T any](x T) { _ = len(x /* ERROR invalid argument */ ) }
122 func _[T interface{ type int }](x T) { _ = len(x /* ERROR invalid argument */ ) }
123 func _[T interface{ type string, []byte, int }](x T) { _ = len(x /* ERROR invalid argument */ ) }
124 func _[T interface{ type string }](x T) { _ = len(x) }
125 func _[T interface{ type [10]int }](x T) { _ = len(x) }
126 func _[T interface{ type []byte }](x T) { _ = len(x) }
127 func _[T interface{ type map[int]int }](x T) { _ = len(x) }
128 func _[T interface{ type chan int }](x T) { _ = len(x) }
129 func _[T interface{ type string, []byte, chan int }](x T) { _ = len(x) }
130
131 func _[T any](x T) { _ = cap(x /* ERROR invalid argument */ ) }
132 func _[T interface{ type int }](x T) { _ = cap(x /* ERROR invalid argument */ ) }
133 func _[T interface{ type string, []byte, int }](x T) { _ = cap(x /* ERROR invalid argument */ ) }
134 func _[T interface{ type string }](x T) { _ = cap(x /* ERROR invalid argument */ ) }
135 func _[T interface{ type [10]int }](x T) { _ = cap(x) }
136 func _[T interface{ type []byte }](x T) { _ = cap(x) }
137 func _[T interface{ type map[int]int }](x T) { _ = cap(x /* ERROR invalid argument */ ) }
138 func _[T interface{ type chan int }](x T) { _ = cap(x) }
139 func _[T interface{ type []byte, chan int }](x T) { _ = cap(x) }
140
141 // range iteration
142
143 func _[T interface{}](x T) {
144 for range x /* ERROR cannot range */ {}
145 }
146
147 func _[T interface{ type string, []string }](x T) {
148 for range x {}
149 for i := range x { _ = i }
150 for i, _ := range x { _ = i }
151 for i, e := range x /* ERROR must have the same element type */ { _ = i }
152 for _, e := range x /* ERROR must have the same element type */ {}
153 var e rune
154 _ = e
155 for _, (e) = range x /* ERROR must have the same element type */ {}
156 }
157
158
159 func _[T interface{ type string, []rune, map[int]rune }](x T) {
160 for _, e := range x { _ = e }
161 for i, e := range x { _ = i; _ = e }
162 }
163
164 func _[T interface{ type string, []rune, map[string]rune }](x T) {
165 for _, e := range x { _ = e }
166 for i, e := range x /* ERROR must have the same key type */ { _ = e }
167 }
168
169 func _[T interface{ type string, chan int }](x T) {
170 for range x {}
171 for i := range x { _ = i }
172 for i, _ := range x { _ = i } // TODO(gri) should get an error here: channels only return one value
173 }
174
175 func _[T interface{ type string, chan<-int }](x T) {
176 for i := range x /* ERROR send-only channel */ { _ = i }
177 }
178
179 // type inference checks
180
181 var _ = new /* ERROR cannot infer T */ ()
182
183 func f4[A, B, C any](A, B) C
184
185 var _ = f4 /* ERROR cannot infer C */ (1, 2)
186 var _ = f4[int, float32, complex128](1, 2)
187
188 func f5[A, B, C any](A, []*B, struct{f []C}) int
189
190 var _ = f5[int, float32, complex128](0, nil, struct{f []complex128}{})
191 var _ = f5 /* ERROR cannot infer */ (0, nil, struct{f []complex128}{})
192 var _ = f5(0, []*float32{new[float32]()}, struct{f []complex128}{})
193
194 func f6[A any](A, []A) int
195
196 var _ = f6(0, nil)
197
198 func f6nil[A any](A) int
199
200 var _ = f6nil /* ERROR cannot infer */ (nil)
201
202 // type inference with variadic functions
203
204 func f7[T any](...T) T
205
206 var _ int = f7 /* ERROR cannot infer T */ ()
207 var _ int = f7(1)
208 var _ int = f7(1, 2)
209 var _ int = f7([]int{}...)
210 var _ int = f7 /* ERROR cannot use */ ([]float64{}...)
211 var _ float64 = f7([]float64{}...)
212 var _ = f7[float64](1, 2.3)
213 var _ = f7(float64(1), 2.3)
214 var _ = f7(1, 2.3 /* ERROR does not match */ )
215 var _ = f7(1.2, 3 /* ERROR does not match */ )
216
217 func f8[A, B any](A, B, ...B) int
218
219 var _ = f8(1) /* ERROR not enough arguments */
220 var _ = f8(1, 2.3)
221 var _ = f8(1, 2.3, 3.4, 4.5)
222 var _ = f8(1, 2.3, 3.4, 4 /* ERROR does not match */ )
223 var _ = f8[int, float64](1, 2.3, 3.4, 4)
224
225 var _ = f8[int, float64](0, 0, nil...) // test case for #18268
226
227 // init functions cannot have type parameters
228
229 func init() {}
230 func init[/* ERROR func init must have no type parameters */ _ any]() {}
231 func init[/* ERROR func init must have no type parameters */ P any]() {}
232
233 type T struct {}
234
235 func (T) m1() {}
236 func (T) m2[ /* ERROR methods cannot have type parameters */ _ any]() {}
237 func (T) m3[ /* ERROR methods cannot have type parameters */ P any]() {}
238
239 // type inference across parameterized types
240
241 type S1[P any] struct { f P }
242
243 func f9[P any](x S1[P])
244
245 func _() {
246 f9[int](S1[int]{42})
247 f9(S1[int]{42})
248 }
249
250 type S2[A, B, C any] struct{}
251
252 func f10[X, Y, Z any](a S2[X, int, Z], b S2[X, Y, bool])
253
254 func _[P any]() {
255 f10[int, float32, string](S2[int, int, string]{}, S2[int, float32, bool]{})
256 f10(S2[int, int, string]{}, S2[int, float32, bool]{})
257 f10(S2[P, int, P]{}, S2[P, float32, bool]{})
258 }
259
260 // corner case for type inference
261 // (was bug: after instanting f11, the type-checker didn't mark f11 as non-generic)
262
263 func f11[T any]()
264
265 func _() {
266 f11[int]()
267 }
268
269 // the previous example was extracted from
270
271 func f12[T interface{m() T}]()
272
273 type A[T any] T
274
275 func (a A[T]) m() A[T]
276
277 func _[T any]() {
278 f12[A[T]]()
279 }
280
281 // method expressions
282
283 func (_ S1[P]) m()
284
285 func _() {
286 m := S1[int].m
287 m(struct { f int }{42})
288 }
289
290 func _[T any] (x T) {
291 m := S1[T].m
292 m(S1[T]{x})
293 }
294
295 // type parameters in methods (generalization)
296
297 type R0 struct{}
298
299 func (R0) _[ /* ERROR methods cannot have type parameters */ T any](x T)
300 func (R0 /* ERROR invalid receiver */ ) _[ /* ERROR methods cannot have type parameters */ R0 any]() // scope of type parameters starts at "func"
301
302 type R1[A, B any] struct{}
303
304 func (_ R1[A, B]) m0(A, B)
305 func (_ R1[A, B]) m1[ /* ERROR methods cannot have type parameters */ T any](A, B, T) T
306 func (_ R1 /* ERROR not a generic type */ [R1, _]) _()
307 func (_ R1[A, B]) _[ /* ERROR methods cannot have type parameters */ A /* ERROR redeclared */ any](B)
308
309 func _() {
310 var r R1[int, string]
311 r.m1[rune](42, "foo", 'a')
312 r.m1[rune](42, "foo", 1.2 /* ERROR cannot use .* as rune .* \(truncated\) */)
313 r.m1(42, "foo", 1.2) // using type inference
314 var _ float64 = r.m1(42, "foo", 1.2)
315 }
316
317 type I1[A any] interface {
318 m1(A)
319 }
320
321 var _ I1[int] = r1[int]{}
322
323 type r1[T any] struct{}
324
325 func (_ r1[T]) m1(T)
326
327 type I2[A, B any] interface {
328 m1(A)
329 m2(A) B
330 }
331
332 var _ I2[int, float32] = R2[int, float32]{}
333
334 type R2[P, Q any] struct{}
335
336 func (_ R2[X, Y]) m1(X)
337 func (_ R2[X, Y]) m2(X) Y
338
339 // type assertions and type switches over generic types
340 // NOTE: These are currently disabled because it's unclear what the correct
341 // approach is, and one can always work around by assigning the variable to
342 // an interface first.
343
344 // // ReadByte1 corresponds to the ReadByte example in the draft design.
345 // func ReadByte1[T io.Reader](r T) (byte, error) {
346 // if br, ok := r.(io.ByteReader); ok {
347 // return br.ReadByte()
348 // }
349 // var b [1]byte
350 // _, err := r.Read(b[:])
351 // return b[0], err
352 // }
353 //
354 // // ReadBytes2 is like ReadByte1 but uses a type switch instead.
355 // func ReadByte2[T io.Reader](r T) (byte, error) {
356 // switch br := r.(type) {
357 // case io.ByteReader:
358 // return br.ReadByte()
359 // }
360 // var b [1]byte
361 // _, err := r.Read(b[:])
362 // return b[0], err
363 // }
364 //
365 // // type assertions and type switches over generic types are strict
366 // type I3 interface {
367 // m(int)
368 // }
369 //
370 // type I4 interface {
371 // m() int // different signature from I3.m
372 // }
373 //
374 // func _[T I3](x I3, p T) {
375 // // type assertions and type switches over interfaces are not strict
376 // _ = x.(I4)
377 // switch x.(type) {
378 // case I4:
379 // }
380 //
381 // // type assertions and type switches over generic types are strict
382 // _ = p /* ERROR cannot have dynamic type I4 */.(I4)
383 // switch p.(type) {
384 // case I4 /* ERROR cannot have dynamic type I4 */ :
385 // }
386 // }
387
388 // type assertions and type switches over generic types lead to errors for now
389
390 func _[T any](x T) {
391 _ = x /* ERROR not an interface */ .(int)
392 switch x /* ERROR not an interface */ .(type) {
393 }
394
395 // work-around
396 var t interface{} = x
397 _ = t.(int)
398 switch t.(type) {
399 }
400 }
401
402 func _[T interface{type int}](x T) {
403 _ = x /* ERROR not an interface */ .(int)
404 switch x /* ERROR not an interface */ .(type) {
405 }
406
407 // work-around
408 var t interface{} = x
409 _ = t.(int)
410 switch t.(type) {
411 }
412 }
413
414 // error messages related to type bounds mention those bounds
415 type C[P any] interface{}
416
417 func _[P C[P]] (x P) {
418 x.m /* ERROR x.m undefined */ ()
419 }
420
421 type I interface {}
422
423 func _[P I] (x P) {
424 x.m /* ERROR interface I has no method m */ ()
425 }
426
427 func _[P interface{}] (x P) {
428 x.m /* ERROR type bound for P has no method m */ ()
429 }
430
431 func _[P any] (x P) {
432 x.m /* ERROR type bound for P has no method m */ ()
433 }
434
View as plain text