|
| 1 | +// Copyright 2021 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 | +//go:build go1.18 |
| 6 | +// +build go1.18 |
| 7 | + |
| 8 | +package typeparams |
| 9 | + |
| 10 | +import "fmt" |
| 11 | + |
| 12 | +func TestBasicTypeParams[T interface{ ~int }, E error, F fmt.Formatter, S fmt.Stringer, A any](t T, e E, f F, s S, a A) { |
| 13 | + fmt.Printf("%d", t) |
| 14 | + fmt.Printf("%s", t) // want "wrong type.*contains ~int" |
| 15 | + fmt.Printf("%v", t) |
| 16 | + fmt.Printf("%d", e) |
| 17 | + fmt.Printf("%s", e) |
| 18 | + fmt.Errorf("%w", e) |
| 19 | + fmt.Printf("%a", f) |
| 20 | + fmt.Printf("%d", f) |
| 21 | + fmt.Printf("%T", f.Format) |
| 22 | + fmt.Printf("%p", f.Format) |
| 23 | + fmt.Printf("%s", s) |
| 24 | + fmt.Errorf("%w", s) // want "wrong type" |
| 25 | + fmt.Printf("%d", a) // want "wrong type" |
| 26 | + fmt.Printf("%s", a) // want "wrong type" |
| 27 | + fmt.Printf("%v", a) |
| 28 | + fmt.Printf("%T", a) |
| 29 | +} |
| 30 | + |
| 31 | +func TestNestedTypeParams[T interface{ ~int }, S interface{ ~string }]() { |
| 32 | + var x struct { |
| 33 | + f int |
| 34 | + t T |
| 35 | + } |
| 36 | + fmt.Printf("%d", x) |
| 37 | + fmt.Printf("%s", x) // want "wrong type" |
| 38 | + var y struct { |
| 39 | + f string |
| 40 | + t S |
| 41 | + } |
| 42 | + fmt.Printf("%d", y) // want "wrong type" |
| 43 | + fmt.Printf("%s", y) |
| 44 | + var m1 map[T]T |
| 45 | + fmt.Printf("%d", m1) |
| 46 | + fmt.Printf("%s", m1) // want "wrong type" |
| 47 | + var m2 map[S]S |
| 48 | + fmt.Printf("%d", m2) // want "wrong type" |
| 49 | + fmt.Printf("%s", m2) |
| 50 | +} |
| 51 | + |
| 52 | +type R struct { |
| 53 | + F []R |
| 54 | +} |
| 55 | + |
| 56 | +func TestRecursiveTypeDefinition() { |
| 57 | + var r []R |
| 58 | + fmt.Printf("%d", r) // No error: avoids infinite recursion. |
| 59 | +} |
| 60 | + |
| 61 | +func TestRecursiveTypeParams[T1 ~[]T2, T2 ~[]T1 | string, T3 ~struct{ F T3 }](t1 T1, t2 T2, t3 T3) { |
| 62 | + // No error is reported on the following lines to avoid infinite recursion. |
| 63 | + fmt.Printf("%s", t1) |
| 64 | + fmt.Printf("%s", t2) |
| 65 | + fmt.Printf("%s", t3) |
| 66 | +} |
| 67 | + |
| 68 | +func TestRecusivePointers[T1 ~*T2, T2 ~*T1](t1 T1, t2 T2) { |
| 69 | + // No error: we can't determine if pointer rules apply. |
| 70 | + fmt.Printf("%s", t1) |
| 71 | + fmt.Printf("%s", t2) |
| 72 | +} |
| 73 | + |
| 74 | +func TestEmptyTypeSet[T interface{ int | string; float64 }](t T) { |
| 75 | + fmt.Printf("%s", t) // No error: empty type set. |
| 76 | +} |
| 77 | + |
| 78 | +func TestPointerRules[T ~*[]int|*[2]int](t T) { |
| 79 | + var slicePtr *[]int |
| 80 | + var arrayPtr *[2]int |
| 81 | + fmt.Printf("%d", slicePtr) |
| 82 | + fmt.Printf("%d", arrayPtr) |
| 83 | + fmt.Printf("%d", t) |
| 84 | +} |
| 85 | + |
| 86 | +func TestInterfacePromotion[E interface { |
| 87 | + ~int |
| 88 | + Error() string |
| 89 | +}, S interface { |
| 90 | + float64 |
| 91 | + String() string |
| 92 | +}](e E, s S) { |
| 93 | + fmt.Printf("%d", e) |
| 94 | + fmt.Printf("%s", e) |
| 95 | + fmt.Errorf("%w", e) |
| 96 | + fmt.Printf("%d", s) // want "wrong type.*contains float64" |
| 97 | + fmt.Printf("%s", s) |
| 98 | + fmt.Errorf("%w", s) // want "wrong type" |
| 99 | +} |
| 100 | + |
| 101 | +type myInt int |
| 102 | + |
| 103 | +func TestTermReduction[T1 interface{ ~int | string }, T2 interface { |
| 104 | + ~int | string |
| 105 | + myInt |
| 106 | +}](t1 T1, t2 T2) { |
| 107 | + fmt.Printf("%d", t1) // want "wrong type.*contains string" |
| 108 | + fmt.Printf("%s", t1) // want "wrong type.*contains ~int" |
| 109 | + fmt.Printf("%d", t2) |
| 110 | + fmt.Printf("%s", t2) // want "wrong type.*contains typeparams.myInt" |
| 111 | +} |
0 commit comments