1 // Copyright 2021 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 assignability
6
7 // See the end of this package for the declarations
8 // of the types and variables used in these tests.
9
10 // "x's type is identical to T"
11 func _[TP any](X TP) {
12 b = b
13 a = a
14 l = l
15 s = s
16 p = p
17 f = f
18 i = i
19 m = m
20 c = c
21 d = d
22
23 B = B
24 A = A
25 L = L
26 S = S
27 P = P
28 F = F
29 I = I
30 M = M
31 C = C
32 D = D
33 X = X
34 }
35
36 // "x's type V and T have identical underlying types
37 // and at least one of V or T is not a named type."
38 // (here a named type is a type with a name)
39 func _[TP1, TP2 Interface](X1 TP1, X2 TP2) {
40 b = B // ERROR cannot use B .* as int value
41 a = A
42 l = L
43 s = S
44 p = P
45 f = F
46 i = I
47 m = M
48 c = C
49 d = D
50
51 B = b // ERROR cannot use b .* as Basic value
52 A = a
53 L = l
54 S = s
55 P = p
56 F = f
57 I = i
58 M = m
59 C = c
60 D = d
61 X1 = i // ERROR cannot use i .* as TP1 value
62 X1 = X2 // ERROR cannot use X2 .* as TP1 value
63 }
64
65 // "T is an interface type and x implements T and T is not a type parameter"
66 func _[TP Interface](X TP) {
67 i = d // ERROR missing method m
68 i = D
69 i = X
70 X = i // ERROR cannot use i .* as TP value
71 }
72
73 // "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"
74 // (here a named type is a type with a name)
75 type (
76 _SendChan = chan<- int
77 _RecvChan = <-chan int
78
79 SendChan _SendChan
80 RecvChan _RecvChan
81 )
82
83 func _[
84 _CC ~_Chan,
85 _SC ~_SendChan,
86 _RC ~_RecvChan,
87
88 CC Chan,
89 SC SendChan,
90 RC RecvChan,
91 ]() {
92 var (
93 _ _SendChan = c
94 _ _RecvChan = c
95 _ _Chan = c
96
97 _ _SendChan = C
98 _ _RecvChan = C
99 _ _Chan = C
100
101 _ SendChan = c
102 _ RecvChan = c
103 _ Chan = c
104
105 _ SendChan = C // ERROR cannot use C .* as SendChan value
106 _ RecvChan = C // ERROR cannot use C .* as RecvChan value
107 _ Chan = C
108 _ Chan = make /* ERROR cannot use make\(chan Basic\) .* as Chan value */ (chan Basic)
109 )
110
111 var (
112 _ _CC = C // ERROR cannot use C .* as _CC value
113 _ _SC = C // ERROR cannot use C .* as _SC value
114 _ _RC = C // ERROR cannot use C .* as _RC value
115
116 _ CC = _CC /* ERROR cannot use _CC\(nil\) .* as CC value */ (nil)
117 _ SC = _CC /* ERROR cannot use _CC\(nil\) .* as SC value */ (nil)
118 _ RC = _CC /* ERROR cannot use _CC\(nil\) .* as RC value */ (nil)
119
120 _ CC = C // ERROR cannot use C .* as CC value
121 _ SC = C // ERROR cannot use C .* as SC value
122 _ RC = C // ERROR cannot use C .* as RC value
123 )
124 }
125
126 // "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."
127 func _[
128 TP0 any,
129 TP1 ~_Chan,
130 TP2 ~chan int | ~chan byte,
131 ]() {
132 var (
133 _ TP0 = c // ERROR cannot use c .* as TP0 value
134 _ TP0 = C // ERROR cannot use C .* as TP0 value
135 _ TP1 = c
136 _ TP1 = C // ERROR cannot use C .* as TP1 value
137 _ TP2 = c // ERROR .* cannot assign chan int to chan byte
138 )
139 }
140
141 // "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."
142 func _[
143 TP0 Interface,
144 TP1 ~_Chan,
145 TP2 ~chan int | ~chan byte,
146 ](X0 TP0, X1 TP1, X2 TP2) {
147 i = X0
148 I = X0
149 c = X1
150 C = X1 // ERROR cannot use X1 .* as Chan value
151 c = X2 // ERROR .* cannot assign chan byte \(in TP2\) to chan int
152 }
153
154 // "x is the predeclared identifier nil and T is a pointer, function, slice, map, channel, or interface type"
155 // TODO(rfindley) error messages about untyped nil diverge from types2 here.
156 // Consider aligning them.
157 func _[TP Interface](X TP) {
158 b = nil // ERROR cannot use nil
159 a = nil // ERROR cannot use nil
160 l = nil
161 s = nil // ERROR cannot use nil
162 p = nil
163 f = nil
164 i = nil
165 m = nil
166 c = nil
167 d = nil // ERROR cannot use nil
168
169 B = nil // ERROR cannot use nil
170 A = nil // ERROR cannot use nil
171 L = nil
172 S = nil // ERROR cannot use nil
173 P = nil
174 F = nil
175 I = nil
176 M = nil
177 C = nil
178 D = nil // ERROR cannot use nil
179 X = nil // ERROR cannot use nil
180 }
181
182 // "x is an untyped constant representable by a value of type T"
183 func _[
184 Int8 ~int8,
185 Int16 ~int16,
186 Int32 ~int32,
187 Int64 ~int64,
188 Int8_16 ~int8 | ~int16,
189 ](
190 i8 Int8,
191 i16 Int16,
192 i32 Int32,
193 i64 Int64,
194 i8_16 Int8_16,
195 ) {
196 b = 42
197 b = 42.0
198 // etc.
199
200 i8 = -1 << 7
201 i8 = 1<<7 - 1
202 i16 = -1 << 15
203 i16 = 1<<15 - 1
204 i32 = -1 << 31
205 i32 = 1<<31 - 1
206 i64 = -1 << 63
207 i64 = 1<<63 - 1
208
209 i8_16 = -1 << 7
210 i8_16 = 1<<7 - 1
211 i8_16 = - /* ERROR cannot use .* as Int8_16 */ 1 << 15
212 i8_16 = 1 /* ERROR cannot use .* as Int8_16 */ <<15 - 1
213 }
214
215 // proto-types for tests
216
217 type (
218 _Basic = int
219 _Array = [10]int
220 _Slice = []int
221 _Struct = struct{ f int }
222 _Pointer = *int
223 _Func = func(x int) string
224 _Interface = interface{ m() int }
225 _Map = map[string]int
226 _Chan = chan int
227
228 Basic _Basic
229 Array _Array
230 Slice _Slice
231 Struct _Struct
232 Pointer _Pointer
233 Func _Func
234 Interface _Interface
235 Map _Map
236 Chan _Chan
237 Defined _Struct
238 )
239
240 func (Defined) m() int
241
242 // proto-variables for tests
243
244 var (
245 b _Basic
246 a _Array
247 l _Slice
248 s _Struct
249 p _Pointer
250 f _Func
251 i _Interface
252 m _Map
253 c _Chan
254 d _Struct
255
256 B Basic
257 A Array
258 L Slice
259 S Struct
260 P Pointer
261 F Func
262 I Interface
263 M Map
264 C Chan
265 D Defined
266 )
267
View as plain text