| 1 |
// Copyright 2019 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 |
type List[E any] []E |
| 8 |
var _ List[List[List[int]]] |
| 9 |
var _ List[List[List[int]]] = []List[List[int]]{} |
| 10 |
|
| 11 |
type ( |
| 12 |
T1[P1 any] struct { |
| 13 |
f1 T2[P1, float32] |
| 14 |
} |
| 15 |
|
| 16 |
T2[P2, P3 any] struct { |
| 17 |
f2 P2 |
| 18 |
f3 P3 |
| 19 |
} |
| 20 |
) |
| 21 |
|
| 22 |
func _() { |
| 23 |
var x1 T1[int] |
| 24 |
var x2 T2[int, float32] |
| 25 |
|
| 26 |
x1.f1.f2 = 0 |
| 27 |
x1.f1 = x2 |
| 28 |
} |
| 29 |
|
| 30 |
type T3[P any] T1[T2[P, P]] |
| 31 |
|
| 32 |
func _() { |
| 33 |
var x1 T3[int] |
| 34 |
var x2 T2[int, int] |
| 35 |
x1.f1.f2 = x2 |
| 36 |
} |
| 37 |
|
| 38 |
func f[P any] (x P) List[P] { |
| 39 |
return List[P]{x} |
| 40 |
} |
| 41 |
|
| 42 |
var ( |
| 43 |
_ []int = f(0) |
| 44 |
_ []float32 = f[float32](10) |
| 45 |
_ List[complex128] = f(1i) |
| 46 |
_ []List[int] = f(List[int]{}) |
| 47 |
_ List[List[int]] = []List[int]{} |
| 48 |
_ = []List[int]{} |
| 49 |
) |
| 50 |
|
| 51 |
// Parameterized types with methods |
| 52 |
|
| 53 |
func (l List[E]) Head() (_ E, _ bool) { |
| 54 |
if len(l) > 0 { |
| 55 |
return l[0], true |
| 56 |
} |
| 57 |
return |
| 58 |
} |
| 59 |
|
| 60 |
// A test case for instantiating types with other types (extracted from map.go2) |
| 61 |
|
| 62 |
type Pair[K any] struct { |
| 63 |
key K |
| 64 |
} |
| 65 |
|
| 66 |
type Receiver[T any] struct { |
| 67 |
values T |
| 68 |
} |
| 69 |
|
| 70 |
type Iterator[K any] struct { |
| 71 |
r Receiver[Pair[K]] |
| 72 |
} |
| 73 |
|
| 74 |
func Values [T any] (r Receiver[T]) T { |
| 75 |
return r.values |
| 76 |
} |
| 77 |
|
| 78 |
func (it Iterator[K]) Next() K { |
| 79 |
return Values[Pair[K]](it.r).key |
| 80 |
} |
| 81 |
|
| 82 |
// A more complex test case testing type bounds (extracted from linalg.go2 and reduced to essence) |
| 83 |
|
| 84 |
type NumericAbs[T any] interface { |
| 85 |
Abs() T |
| 86 |
} |
| 87 |
|
| 88 |
func AbsDifference[T NumericAbs[T]](x T) { panic(0) } |
| 89 |
|
| 90 |
// For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639). |
| 91 |
// type OrderedAbs[T any] T |
| 92 |
// |
| 93 |
// func (a OrderedAbs[T]) Abs() OrderedAbs[T] |
| 94 |
// |
| 95 |
// func OrderedAbsDifference[T any](x T) { |
| 96 |
// AbsDifference(OrderedAbs[T](x)) |
| 97 |
// } |
| 98 |
|
| 99 |
// same code, reduced to essence |
| 100 |
|
| 101 |
func g[P interface{ m() P }](x P) { panic(0) } |
| 102 |
|
| 103 |
// For now, a lone type parameter is not permitted as RHS in a type declaration (issue #45639). |
| 104 |
// type T4[P any] P |
| 105 |
// |
| 106 |
// func (_ T4[P]) m() T4[P] |
| 107 |
// |
| 108 |
// func _[Q any](x Q) { |
| 109 |
// g(T4[Q](x)) |
| 110 |
// } |
| 111 |
|
| 112 |
// Another test case that caused problems in the past |
| 113 |
|
| 114 |
type T5[_ interface { a() }, _ interface{}] struct{} |
| 115 |
|
| 116 |
type A[P any] struct{ x P } |
| 117 |
|
| 118 |
func (_ A[P]) a() {} |
| 119 |
|
| 120 |
var _ T5[A[int], int] |
| 121 |
|
| 122 |
// Invoking methods with parameterized receiver types uses |
| 123 |
// type inference to determine the actual type arguments matching |
| 124 |
// the receiver type parameters from the actual receiver argument. |
| 125 |
// Go does implicit address-taking and dereferenciation depending |
| 126 |
// on the actual receiver and the method's receiver type. To make |
| 127 |
// type inference work, the type-checker matches "pointer-ness" |
| 128 |
// of the actual receiver and the method's receiver type. |
| 129 |
// The following code tests this mechanism. |
| 130 |
|
| 131 |
type R1[A any] struct{} |
| 132 |
func (_ R1[A]) vm() |
| 133 |
func (_ *R1[A]) pm() |
| 134 |
|
| 135 |
func _[T any](r R1[T], p *R1[T]) { |
| 136 |
r.vm() |
| 137 |
r.pm() |
| 138 |
p.vm() |
| 139 |
p.pm() |
| 140 |
} |
| 141 |
|
| 142 |
type R2[A, B any] struct{} |
| 143 |
func (_ R2[A, B]) vm() |
| 144 |
func (_ *R2[A, B]) pm() |
| 145 |
|
| 146 |
func _[T any](r R2[T, int], p *R2[string, T]) { |
| 147 |
r.vm() |
| 148 |
r.pm() |
| 149 |
p.vm() |
| 150 |
p.pm() |
| 151 |
} |
| 152 |
|
| 153 |
// It is ok to have multiple embedded unions. |
| 154 |
type _ interface { |
| 155 |
m0() |
| 156 |
~int | ~string | ~bool |
| 157 |
~float32 | ~float64 |
| 158 |
m1() |
| 159 |
m2() |
| 160 |
~complex64 | ~complex128 |
| 161 |
~rune |
| 162 |
} |
| 163 |
|
| 164 |
// Type sets may contain each type at most once. |
| 165 |
type _ interface { |
| 166 |
~int|~ /* ERROR "overlapping terms ~int" */ int |
| 167 |
~int|int /* ERROR "overlapping terms int" */ |
| 168 |
int|int /* ERROR "overlapping terms int" */ |
| 169 |
} |
| 170 |
|
| 171 |
type _ interface { |
| 172 |
~struct{f int} | ~struct{g int} | ~ /* ERROR "overlapping terms" */ struct{f int} |
| 173 |
} |
| 174 |
|
| 175 |
// Interface term lists can contain any type, incl. *Named types. |
| 176 |
// Verify that we use the underlying type(s) of the type(s) in the |
| 177 |
// term list when determining if an operation is permitted. |
| 178 |
|
| 179 |
type MyInt int |
| 180 |
func add1[T interface{MyInt}](x T) T { |
| 181 |
return x + 1 |
| 182 |
} |
| 183 |
|
| 184 |
type MyString string |
| 185 |
func double[T interface{MyInt|MyString}](x T) T { |
| 186 |
return x + x |
| 187 |
} |
| 188 |
|
| 189 |
// Embedding of interfaces with term lists leads to interfaces |
| 190 |
// with term lists that are the intersection of the embedded |
| 191 |
// term lists. |
| 192 |
|
| 193 |
type E0 interface { |
| 194 |
~int | ~bool | ~string |
| 195 |
} |
| 196 |
|
| 197 |
type E1 interface { |
| 198 |
~int | ~float64 | ~string |
| 199 |
} |
| 200 |
|
| 201 |
type E2 interface { |
| 202 |
~float64 |
| 203 |
} |
| 204 |
|
| 205 |
type I0 interface { |
| 206 |
E0 |
| 207 |
} |
| 208 |
|
| 209 |
func f0[T I0]() {} |
| 210 |
var _ = f0[int] |
| 211 |
var _ = f0[bool] |
| 212 |
var _ = f0[string] |
| 213 |
var _ = f0[float64 /* ERROR "does not satisfy I0" */ ] |
| 214 |
|
| 215 |
type I01 interface { |
| 216 |
E0 |
| 217 |
E1 |
| 218 |
} |
| 219 |
|
| 220 |
func f01[T I01]() {} |
| 221 |
var _ = f01[int] |
| 222 |
var _ = f01[bool /* ERROR "does not satisfy I0" */ ] |
| 223 |
var _ = f01[string] |
| 224 |
var _ = f01[float64 /* ERROR "does not satisfy I0" */ ] |
| 225 |
|
| 226 |
type I012 interface { |
| 227 |
E0 |
| 228 |
E1 |
| 229 |
E2 |
| 230 |
} |
| 231 |
|
| 232 |
func f012[T I012]() {} |
| 233 |
var _ = f012[int /* ERRORx `cannot satisfy I012.*empty type set` */ ] |
| 234 |
var _ = f012[bool /* ERRORx `cannot satisfy I012.*empty type set` */ ] |
| 235 |
var _ = f012[string /* ERRORx `cannot satisfy I012.*empty type set` */ ] |
| 236 |
var _ = f012[float64 /* ERRORx `cannot satisfy I012.*empty type set` */ ] |
| 237 |
|
| 238 |
type I12 interface { |
| 239 |
E1 |
| 240 |
E2 |
| 241 |
} |
| 242 |
|
| 243 |
func f12[T I12]() {} |
| 244 |
var _ = f12[int /* ERROR "does not satisfy I12" */ ] |
| 245 |
var _ = f12[bool /* ERROR "does not satisfy I12" */ ] |
| 246 |
var _ = f12[string /* ERROR "does not satisfy I12" */ ] |
| 247 |
var _ = f12[float64] |
| 248 |
|
| 249 |
type I0_ interface { |
| 250 |
E0 |
| 251 |
~int |
| 252 |
} |
| 253 |
|
| 254 |
func f0_[T I0_]() {} |
| 255 |
var _ = f0_[int] |
| 256 |
var _ = f0_[bool /* ERROR "does not satisfy I0_" */ ] |
| 257 |
var _ = f0_[string /* ERROR "does not satisfy I0_" */ ] |
| 258 |
var _ = f0_[float64 /* ERROR "does not satisfy I0_" */ ] |
| 259 |
|
| 260 |
// Using a function instance as a type is an error. |
| 261 |
var _ f0 // ERROR "not a type" |
| 262 |
var _ f0 /* ERROR "not a type" */ [int] |
| 263 |
|
| 264 |
// Empty type sets can only be satisfied by empty type sets. |
| 265 |
type none interface { |
| 266 |
// force an empty type set |
| 267 |
int |
| 268 |
string |
| 269 |
} |
| 270 |
|
| 271 |
func ff[T none]() {} |
| 272 |
func gg[T any]() {} |
| 273 |
func hh[T ~int]() {} |
| 274 |
|
| 275 |
func _[T none]() { |
| 276 |
_ = ff[int /* ERROR "cannot satisfy none (empty type set)" */ ] |
| 277 |
_ = ff[T] // pathological but ok because T's type set is empty, too |
| 278 |
_ = gg[int] |
| 279 |
_ = gg[T] |
| 280 |
_ = hh[int] |
| 281 |
_ = hh[T] |
| 282 |
} |