aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/compile/internal/types2/testdata/check/typeinst2.go2
blob: d087c26a47e637836a8cc56308dfb53e7314bf1f (plain)
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
// 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) }

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) }

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
}

// Interface type lists may contain each type at most once.
// (If there are multiple lists, we assume the author intended
// for them to be all in a single list, and we report the error
// as well.)
type _ interface {
	~int|~int /* ERROR overlapping terms ~int */
	~int|int /* ERROR overlapping terms int */
	int|int /* ERROR overlapping terms int */
}

type _ interface {
	~struct{f int} | ~struct{g int} | ~struct /* ERROR overlapping terms */ {f int}
}

// Interface type lists can contain any type, incl. *Named types.
// Verify that we use the underlying type to compute the operational type.
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 type lists leads to interfaces
// with type lists that are the intersection of the embedded
// type 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 /* ERROR does not satisfy I012 */ ]
var _ = f012[bool /* ERROR does not satisfy I012 */ ]
var _ = f012[string /* ERROR does not satisfy I012 */ ]
var _ = f012[float64 /* ERROR does not satisfy I012 */ ]

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_ */ ]