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