summaryrefslogtreecommitdiffstats
path: root/src/internal/types/testdata/spec/assignability.go
blob: 0ab8eb3e93df2d09a8de5f8d6472c75a3670a8c8 (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
258
259
260
261
262
263
264
// Copyright 2021 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 assignability

// See the end of this package for the declarations
// of the types and variables used in these tests.

// "x's type is identical to T"
func _[TP any](X TP) {
	b = b
	a = a
	l = l
	s = s
	p = p
	f = f
	i = i
	m = m
	c = c
	d = d

	B = B
	A = A
	L = L
	S = S
	P = P
	F = F
	I = I
	M = M
	C = C
	D = D
	X = X
}

// "x's type V and T have identical underlying types
// and at least one of V or T is not a named type."
// (here a named type is a type with a name)
func _[TP1, TP2 Interface](X1 TP1, X2 TP2) {
	b = B // ERROR cannot use B .* as int value
	a = A
	l = L
	s = S
	p = P
	f = F
	i = I
	m = M
	c = C
	d = D

	B = b // ERROR cannot use b .* as Basic value
	A = a
	L = l
	S = s
	P = p
	F = f
	I = i
	M = m
	C = c
	D = d
	X1 = i  // ERROR cannot use i .* as TP1 value
	X1 = X2 // ERROR cannot use X2 .* as TP1 value
}

// "T is an interface type and x implements T and T is not a type parameter"
func _[TP Interface](X TP) {
	i = d // ERROR missing method m
	i = D
	i = X
	X = i // ERROR cannot use i .* as TP value
}

// "x is a bidirectional channel value, T is a channel type, x's type V and T have identical element types, and at least one of V or T is not a named type"
// (here a named type is a type with a name)
type (
	_SendChan = chan<- int
	_RecvChan = <-chan int

	SendChan _SendChan
	RecvChan _RecvChan
)

func _[
	_CC ~_Chan,
	_SC ~_SendChan,
	_RC ~_RecvChan,

	CC Chan,
	SC SendChan,
	RC RecvChan,
]() {
	var (
		_ _SendChan = c
		_ _RecvChan = c
		_ _Chan     = c

		_ _SendChan = C
		_ _RecvChan = C
		_ _Chan     = C

		_ SendChan = c
		_ RecvChan = c
		_ Chan     = c

		_ SendChan = C // ERROR cannot use C .* as SendChan value
		_ RecvChan = C // ERROR cannot use C .* as RecvChan value
		_ Chan     = C
		_ Chan     = make /* ERROR cannot use make\(chan Basic\) .* as Chan value */ (chan Basic)
	)

	var (
		_ _CC = C // ERROR cannot use C .* as _CC value
		_ _SC = C // ERROR cannot use C .* as _SC value
		_ _RC = C // ERROR cannot use C .* as _RC value

		_ CC = _CC /* ERROR cannot use _CC\(nil\) .* as CC value */ (nil)
		_ SC = _CC /* ERROR cannot use _CC\(nil\) .* as SC value */ (nil)
		_ RC = _CC /* ERROR cannot use _CC\(nil\) .* as RC value */ (nil)

		_ CC = C // ERROR cannot use C .* as CC value
		_ SC = C // ERROR cannot use C .* as SC value
		_ RC = C // ERROR cannot use C .* as RC value
	)
}

// "x's type V is not a named type and T is a type parameter, and x is assignable to each specific type in T's type set."
func _[
	TP0 any,
	TP1 ~_Chan,
	TP2 ~chan int | ~chan byte,
]() {
	var (
		_ TP0 = c // ERROR cannot use c .* as TP0 value
		_ TP0 = C // ERROR cannot use C .* as TP0 value
		_ TP1 = c
		_ TP1 = C // ERROR cannot use C .* as TP1 value
		_ TP2 = c // ERROR .* cannot assign chan int to chan byte
	)
}

// "x's type V is a type parameter and T is not a named type, and values x' of each specific type in V's type set are assignable to T."
func _[
	TP0 Interface,
	TP1 ~_Chan,
	TP2 ~chan int | ~chan byte,
](X0 TP0, X1 TP1, X2 TP2) {
	i = X0
	I = X0
	c = X1
	C = X1 // ERROR cannot use X1 .* as Chan value
	c = X2 // ERROR .* cannot assign chan byte \(in TP2\) to chan int
}

// "x is the predeclared identifier nil and T is a pointer, function, slice, map, channel, or interface type"
func _[TP Interface](X TP) {
	b = nil // ERROR cannot use nil
	a = nil // ERROR cannot use nil
	l = nil
	s = nil // ERROR cannot use nil
	p = nil
	f = nil
	i = nil
	m = nil
	c = nil
	d = nil // ERROR cannot use nil

	B = nil // ERROR cannot use nil
	A = nil // ERROR cannot use nil
	L = nil
	S = nil // ERROR cannot use nil
	P = nil
	F = nil
	I = nil
	M = nil
	C = nil
	D = nil // ERROR cannot use nil
	X = nil // ERROR cannot use nil
}

// "x is an untyped constant representable by a value of type T"
func _[
	Int8 ~int8,
	Int16 ~int16,
	Int32 ~int32,
	Int64 ~int64,
	Int8_16 ~int8 | ~int16,
](
	i8 Int8,
	i16 Int16,
	i32 Int32,
	i64 Int64,
	i8_16 Int8_16,
) {
	b = 42
	b = 42.0
	// etc.

	i8 = -1 << 7
	i8 = 1<<7 - 1
	i16 = -1 << 15
	i16 = 1<<15 - 1
	i32 = -1 << 31
	i32 = 1<<31 - 1
	i64 = -1 << 63
	i64 = 1<<63 - 1

	i8_16 = -1 << 7
	i8_16 = 1<<7 - 1
	i8_16 = - /* ERROR cannot use .* as Int8_16 */ 1 << 15
	i8_16 = 1 /* ERROR cannot use .* as Int8_16 */ <<15 - 1
}

// proto-types for tests

type (
	_Basic     = int
	_Array     = [10]int
	_Slice     = []int
	_Struct    = struct{ f int }
	_Pointer   = *int
	_Func      = func(x int) string
	_Interface = interface{ m() int }
	_Map       = map[string]int
	_Chan      = chan int

	Basic     _Basic
	Array     _Array
	Slice     _Slice
	Struct    _Struct
	Pointer   _Pointer
	Func      _Func
	Interface _Interface
	Map       _Map
	Chan      _Chan
	Defined   _Struct
)

func (Defined) m() int

// proto-variables for tests

var (
	b _Basic
	a _Array
	l _Slice
	s _Struct
	p _Pointer
	f _Func
	i _Interface
	m _Map
	c _Chan
	d _Struct

	B Basic
	A Array
	L Slice
	S Struct
	P Pointer
	F Func
	I Interface
	M Map
	C Chan
	D Defined
)