1 // Copyright 2020 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 // This file tests built-in calls on generic types.
6
7 package builtins
8
9 import "unsafe"
10
11 // close
12
13 type C0 interface{ int }
14 type C1 interface{ chan int }
15 type C2 interface{ chan int | <-chan int }
16 type C3 interface{ chan int | chan float32 }
17 type C4 interface{ chan int | chan<- int }
18 type C5[T any] interface{ ~chan T | chan<- T }
19
20 func _[T any](ch T) {
21 close(ch /* ERROR cannot close non-channel */)
22 }
23
24 func _[T C0](ch T) {
25 close(ch /* ERROR cannot close non-channel */)
26 }
27
28 func _[T C1](ch T) {
29 close(ch)
30 }
31
32 func _[T C2](ch T) {
33 close(ch /* ERROR cannot close receive-only channel */)
34 }
35
36 func _[T C3](ch T) {
37 close(ch)
38 }
39
40 func _[T C4](ch T) {
41 close(ch)
42 }
43
44 func _[T C5[X], X any](ch T) {
45 close(ch)
46 }
47
48 // copy
49
50 func _[T any](x, y T) {
51 copy(x /* ERROR copy expects slice arguments */ , y)
52 }
53
54 func _[T ~[]byte](x, y T) {
55 copy(x, y)
56 copy(x, "foo")
57 copy("foo" /* ERROR expects slice arguments */ , y)
58
59 var x2 []byte
60 copy(x2, y) // element types are identical
61 copy(y, x2) // element types are identical
62
63 type myByte byte
64 var x3 []myByte
65 copy(x3 /* ERROR different element types */ , y)
66 copy(y /* ERROR different element types */ , x3)
67 }
68
69 func _[T ~[]E, E any](x T, y []E) {
70 copy(x, y)
71 copy(x /* ERROR different element types */ , "foo")
72 }
73
74 func _[T ~string](x []byte, y T) {
75 copy(x, y)
76 copy(y /* ERROR expects slice arguments */ , x)
77 }
78
79 func _[T ~[]byte|~string](x T, y []byte) {
80 copy(x /* ERROR expects slice arguments */ , y)
81 copy(y, x)
82 }
83
84 type L0 []int
85 type L1 []int
86
87 func _[T L0 | L1](x, y T) {
88 copy(x, y)
89 }
90
91 // delete
92
93 type M0 interface{ int }
94 type M1 interface{ map[string]int }
95 type M2 interface { map[string]int | map[string]float64 }
96 type M3 interface{ map[string]int | map[rune]int }
97 type M4[K comparable, V any] interface{ map[K]V | map[rune]V }
98
99 func _[T any](m T) {
100 delete(m /* ERROR not a map */, "foo")
101 }
102
103 func _[T M0](m T) {
104 delete(m /* ERROR not a map */, "foo")
105 }
106
107 func _[T M1](m T) {
108 delete(m, "foo")
109 }
110
111 func _[T M2](m T) {
112 delete(m, "foo")
113 delete(m, 0 /* ERROR cannot use .* as string */)
114 }
115
116 func _[T M3](m T) {
117 delete(m /* ERROR must have identical key types */, "foo")
118 }
119
120 func _[T M4[rune, V], V any](m T) {
121 delete(m, 'k')
122 }
123
124 func _[T M4[K, V], K comparable, V any](m T) {
125 delete(m /* ERROR must have identical key types */, "foo")
126 }
127
128 // make
129
130 type myChan chan int
131
132 func _[
133 S1 ~[]int,
134 S2 ~[]int | ~chan int,
135
136 M1 ~map[string]int,
137 M2 ~map[string]int | ~chan int,
138
139 C1 ~chan int,
140 C2 ~chan int | ~chan string,
141 C3 chan int | myChan, // single underlying type
142 ]() {
143 type S0 []int
144 _ = make([]int, 10)
145 _ = make(S0, 10)
146 _ = make(S1, 10)
147 _ = make() /* ERROR not enough arguments */
148 _ = make /* ERROR expects 2 or 3 arguments */ (S1)
149 _ = make(S1, 10, 20)
150 _ = make /* ERROR expects 2 or 3 arguments */ (S1, 10, 20, 30)
151 _ = make(S2 /* ERROR cannot make S2: no core type */ , 10)
152
153 type M0 map[string]int
154 _ = make(map[string]int)
155 _ = make(M0)
156 _ = make(M1)
157 _ = make(M1, 10)
158 _ = make/* ERROR expects 1 or 2 arguments */(M1, 10, 20)
159 _ = make(M2 /* ERROR cannot make M2: no core type */ )
160
161 type C0 chan int
162 _ = make(chan int)
163 _ = make(C0)
164 _ = make(C1)
165 _ = make(C1, 10)
166 _ = make/* ERROR expects 1 or 2 arguments */(C1, 10, 20)
167 _ = make(C2 /* ERROR cannot make C2: no core type */ )
168 _ = make(C3)
169 }
170
171 // unsafe.Alignof
172
173 func _[T comparable]() {
174 var (
175 b int64
176 a [10]T
177 s struct{ f T }
178 p *T
179 l []T
180 f func(T)
181 i interface{ m() T }
182 c chan T
183 m map[T]T
184 t T
185 )
186
187 const bb = unsafe.Alignof(b)
188 assert(bb == 8)
189 const _ = unsafe /* ERROR not constant */ .Alignof(a)
190 const _ = unsafe /* ERROR not constant */ .Alignof(s)
191 const pp = unsafe.Alignof(p)
192 assert(pp == 8)
193 const ll = unsafe.Alignof(l)
194 assert(ll == 8)
195 const ff = unsafe.Alignof(f)
196 assert(ff == 8)
197 const ii = unsafe.Alignof(i)
198 assert(ii == 8)
199 const cc = unsafe.Alignof(c)
200 assert(cc == 8)
201 const mm = unsafe.Alignof(m)
202 assert(mm == 8)
203 const _ = unsafe /* ERROR not constant */ .Alignof(t)
204 }
205
206 // unsafe.Offsetof
207
208 func _[T comparable]() {
209 var (
210 b struct{ _, f int64 }
211 a struct{ _, f [10]T }
212 s struct{ _, f struct{ f T } }
213 p struct{ _, f *T }
214 l struct{ _, f []T }
215 f struct{ _, f func(T) }
216 i struct{ _, f interface{ m() T } }
217 c struct{ _, f chan T }
218 m struct{ _, f map[T]T }
219 t struct{ _, f T }
220 )
221
222 const bb = unsafe.Offsetof(b.f)
223 assert(bb == 8)
224 const _ = unsafe /* ERROR not constant */ .Alignof(a)
225 const _ = unsafe /* ERROR not constant */ .Alignof(s)
226 const pp = unsafe.Offsetof(p.f)
227 assert(pp == 8)
228 const ll = unsafe.Offsetof(l.f)
229 assert(ll == 24)
230 const ff = unsafe.Offsetof(f.f)
231 assert(ff == 8)
232 const ii = unsafe.Offsetof(i.f)
233 assert(ii == 16)
234 const cc = unsafe.Offsetof(c.f)
235 assert(cc == 8)
236 const mm = unsafe.Offsetof(m.f)
237 assert(mm == 8)
238 const _ = unsafe /* ERROR not constant */ .Alignof(t)
239 }
240
241 // unsafe.Sizeof
242
243 func _[T comparable]() {
244 var (
245 b int64
246 a [10]T
247 s struct{ f T }
248 p *T
249 l []T
250 f func(T)
251 i interface{ m() T }
252 c chan T
253 m map[T]T
254 t T
255 )
256
257 const bb = unsafe.Sizeof(b)
258 assert(bb == 8)
259 const _ = unsafe /* ERROR not constant */ .Alignof(a)
260 const _ = unsafe /* ERROR not constant */ .Alignof(s)
261 const pp = unsafe.Sizeof(p)
262 assert(pp == 8)
263 const ll = unsafe.Sizeof(l)
264 assert(ll == 24)
265 const ff = unsafe.Sizeof(f)
266 assert(ff == 8)
267 const ii = unsafe.Sizeof(i)
268 assert(ii == 16)
269 const cc = unsafe.Sizeof(c)
270 assert(cc == 8)
271 const mm = unsafe.Sizeof(m)
272 assert(mm == 8)
273 const _ = unsafe /* ERROR not constant */ .Alignof(t)
274 }
275
View as plain text