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 to compute the operational type.
177 type MyInt int
178 func add1[T interface{MyInt}](x T) T {
179 return x + 1
180 }
181
182 type MyString string
183 func double[T interface{MyInt|MyString}](x T) T {
184 return x + x
185 }
186
187 // Embedding of interfaces with term lists leads to interfaces
188 // with term lists that are the intersection of the embedded
189 // term lists.
190
191 type E0 interface {
192 ~int | ~bool | ~string
193 }
194
195 type E1 interface {
196 ~int | ~float64 | ~string
197 }
198
199 type E2 interface {
200 ~float64
201 }
202
203 type I0 interface {
204 E0
205 }
206
207 func f0[T I0]() {}
208 var _ = f0[int]
209 var _ = f0[bool]
210 var _ = f0[string]
211 var _ = f0[float64 /* ERROR does not implement I0 */ ]
212
213 type I01 interface {
214 E0
215 E1
216 }
217
218 func f01[T I01]() {}
219 var _ = f01[int]
220 var _ = f01[bool /* ERROR does not implement I0 */ ]
221 var _ = f01[string]
222 var _ = f01[float64 /* ERROR does not implement I0 */ ]
223
224 type I012 interface {
225 E0
226 E1
227 E2
228 }
229
230 func f012[T I012]() {}
231 var _ = f012[int /* ERROR cannot implement I012.*empty type set */ ]
232 var _ = f012[bool /* ERROR cannot implement I012.*empty type set */ ]
233 var _ = f012[string /* ERROR cannot implement I012.*empty type set */ ]
234 var _ = f012[float64 /* ERROR cannot implement I012.*empty type set */ ]
235
236 type I12 interface {
237 E1
238 E2
239 }
240
241 func f12[T I12]() {}
242 var _ = f12[int /* ERROR does not implement I12 */ ]
243 var _ = f12[bool /* ERROR does not implement I12 */ ]
244 var _ = f12[string /* ERROR does not implement I12 */ ]
245 var _ = f12[float64]
246
247 type I0_ interface {
248 E0
249 ~int
250 }
251
252 func f0_[T I0_]() {}
253 var _ = f0_[int]
254 var _ = f0_[bool /* ERROR does not implement I0_ */ ]
255 var _ = f0_[string /* ERROR does not implement I0_ */ ]
256 var _ = f0_[float64 /* ERROR does not implement I0_ */ ]
257
258 // Using a function instance as a type is an error.
259 var _ f0 // ERROR not a type
260 var _ f0 /* ERROR not a type */ [int]
261
262 // Empty type sets can only be satisfied by empty type sets.
263 type none interface {
264 // force an empty type set
265 int
266 string
267 }
268
269 func ff[T none]() {}
270 func gg[T any]() {}
271 func hh[T ~int]() {}
272
273 func _[T none]() {
274 _ = ff[int /* ERROR cannot implement none \(empty type set\) */ ]
275 _ = ff[T] // pathological but ok because T's type set is empty, too
276 _ = gg[int]
277 _ = gg[T]
278 _ = hh[int]
279 _ = hh[T]
280 }
281
View as plain text