Debian Codegraph @

src/internal/types/testdata/check/builtins0.go

raw
1 // Copyright 2012 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 // builtin calls
6
7 package builtins
8
9 import "unsafe"
10
11 func f0() {}
12
13 func append1() {
14 var b byte
15 var x int
16 var s []byte
17 _ = append() // ERROR "not enough arguments"
18 _ = append("foo" /* ERROR "must be a slice" */ )
19 _ = append(nil /* ERROR "must be a slice" */ , s)
20 _ = append(x /* ERROR "must be a slice" */ , s)
21 _ = append(s)
22 _ = append(s, nil...)
23 append /* ERROR "not used" */ (s)
24
25 _ = append(s, b)
26 _ = append(s, x /* ERROR "cannot use x" */ )
27 _ = append(s, s /* ERROR "cannot use s" */ )
28 _ = append(s...) /* ERROR "not enough arguments" */
29 _ = append(s, b, s /* ERROR "too many arguments" */ ...)
30 _ = append(s, 1, 2, 3)
31 _ = append(s, 1, 2, 3, x /* ERROR "cannot use x" */ , 5, 6, 6)
32 _ = append(s, 1, 2 /* ERROR "too many arguments" */, s...)
33 _ = append([]interface{}(nil), 1, 2, "foo", x, 3.1425, false)
34
35 type S []byte
36 type T string
37 var t T
38 _ = append(s, "foo" /* ERRORx `cannot use .* in argument to append` */ )
39 _ = append(s, "foo"...)
40 _ = append(S(s), "foo" /* ERRORx `cannot use .* in argument to append` */ )
41 _ = append(S(s), "foo"...)
42 _ = append(s, t /* ERROR "cannot use t" */ )
43 _ = append(s, t...)
44 _ = append(s, T("foo")...)
45 _ = append(S(s), t /* ERROR "cannot use t" */ )
46 _ = append(S(s), t...)
47 _ = append(S(s), T("foo")...)
48 _ = append([]string{}, t /* ERROR "cannot use t" */ , "foo")
49 _ = append([]T{}, t, "foo")
50 }
51
52 // from the spec
53 func append2() {
54 s0 := []int{0, 0}
55 s1 := append(s0, 2) // append a single element s1 == []int{0, 0, 2}
56 s2 := append(s1, 3, 5, 7) // append multiple elements s2 == []int{0, 0, 2, 3, 5, 7}
57 s3 := append(s2, s0...) // append a slice s3 == []int{0, 0, 2, 3, 5, 7, 0, 0}
58 s4 := append(s3[3:6], s3[2:]...) // append overlapping slice s4 == []int{3, 5, 7, 2, 3, 5, 7, 0, 0}
59
60 var t []interface{}
61 t = append(t, 42, 3.1415, "foo") // t == []interface{}{42, 3.1415, "foo"}
62
63 var b []byte
64 b = append(b, "bar"...) // append string contents b == []byte{'b', 'a', 'r' }
65
66 _ = s4
67 }
68
69 func append3() {
70 f1 := func() (s []int) { return }
71 f2 := func() (s []int, x int) { return }
72 f3 := func() (s []int, x, y int) { return }
73 f5 := func() (s []interface{}, x int, y float32, z string, b bool) { return }
74 ff := func() (int, float32) { return 0, 0 }
75 _ = append(f0 /* ERROR "used as value" */ ())
76 _ = append(f1())
77 _ = append(f2())
78 _ = append(f3())
79 _ = append(f5())
80 _ = append(ff /* ERROR "must be a slice" */ ()) // TODO(gri) better error message
81 }
82
83 func cap1() {
84 var a [10]bool
85 var p *[20]int
86 var c chan string
87 _ = cap() // ERROR "not enough arguments"
88 _ = cap(1, 2) // ERROR "too many arguments"
89 _ = cap(42 /* ERROR "invalid" */)
90 const _3 = cap(a)
91 assert(_3 == 10)
92 const _4 = cap(p)
93 assert(_4 == 20)
94 _ = cap(c)
95 cap /* ERROR "not used" */ (c)
96
97 // issue 4744
98 type T struct{ a [10]int }
99 const _ = cap(((*T)(nil)).a)
100
101 var s [][]byte
102 _ = cap(s)
103 _ = cap(s... /* ERROR "invalid use of ... with built-in cap" */ )
104
105 var x int
106 _ = cap(x /* ERROR "invalid argument: x (variable of type int) for built-in cap" */ )
107 }
108
109 func cap2() {
110 f1a := func() (a [10]int) { return }
111 f1s := func() (s []int) { return }
112 f2 := func() (s []int, x int) { return }
113 _ = cap(f0 /* ERROR "used as value" */ ())
114 _ = cap(f1a())
115 _ = cap(f1s())
116 _ = cap(f2()) // ERROR "too many arguments"
117 }
118
119 // test cases for issue 7387
120 func cap3() {
121 var f = func() int { return 0 }
122 var x = f()
123 const (
124 _ = cap([4]int{})
125 _ = cap([4]int{x})
126 _ = cap /* ERROR "not constant" */ ([4]int{f()})
127 _ = cap /* ERROR "not constant" */ ([4]int{cap([]int{})})
128 _ = cap([4]int{cap([4]int{})})
129 )
130 var y float64
131 var z complex128
132 const (
133 _ = cap([4]float64{})
134 _ = cap([4]float64{y})
135 _ = cap([4]float64{real(2i)})
136 _ = cap /* ERROR "not constant" */ ([4]float64{real(z)})
137 )
138 var ch chan [10]int
139 const (
140 _ = cap /* ERROR "not constant" */ (<-ch)
141 _ = cap /* ERROR "not constant" */ ([4]int{(<-ch)[0]})
142 )
143 }
144
145 func clear1() {
146 var a [10]int
147 var m map[float64]string
148 var s []byte
149 clear(a /* ERROR "cannot clear a" */)
150 clear(&/* ERROR "cannot clear &a" */a)
151 clear(m)
152 clear(s)
153 clear([]int{})
154 }
155
156 func close1() {
157 var c chan int
158 var r <-chan int
159 close() // ERROR "not enough arguments"
160 close(1, 2) // ERROR "too many arguments"
161 close(42 /* ERROR "cannot close non-channel" */)
162 close(r /* ERROR "receive-only channel" */)
163 close(c)
164 _ = close /* ERROR "used as value" */ (c)
165
166 var s []chan int
167 close(s... /* ERROR "invalid use of ..." */ )
168 }
169
170 func close2() {
171 f1 := func() (ch chan int) { return }
172 f2 := func() (ch chan int, x int) { return }
173 close(f0 /* ERROR "used as value" */ ())
174 close(f1())
175 close(f2()) // ERROR "too many arguments"
176 }
177
178 func complex1() {
179 var i32 int32
180 var f32 float32
181 var f64 float64
182 var c64 complex64
183 var c128 complex128
184 _ = complex() // ERROR "not enough arguments"
185 _ = complex(1) // ERROR "not enough arguments"
186 _ = complex(true /* ERROR "mismatched types" */ , 0)
187 _ = complex(i32 /* ERROR "expected floating-point" */ , 0)
188 _ = complex("foo" /* ERROR "mismatched types" */ , 0)
189 _ = complex(c64 /* ERROR "expected floating-point" */ , 0)
190 _ = complex(0 /* ERROR "mismatched types" */ , true)
191 _ = complex(0 /* ERROR "expected floating-point" */ , i32)
192 _ = complex(0 /* ERROR "mismatched types" */ , "foo")
193 _ = complex(0 /* ERROR "expected floating-point" */ , c64)
194 _ = complex(f32, f32)
195 _ = complex(f32, 1)
196 _ = complex(f32, 1.0)
197 _ = complex(f32, 'a')
198 _ = complex(f64, f64)
199 _ = complex(f64, 1)
200 _ = complex(f64, 1.0)
201 _ = complex(f64, 'a')
202 _ = complex(f32 /* ERROR "mismatched types" */ , f64)
203 _ = complex(f64 /* ERROR "mismatched types" */ , f32)
204 _ = complex(1, 1)
205 _ = complex(1, 1.1)
206 _ = complex(1, 'a')
207 complex /* ERROR "not used" */ (1, 2)
208
209 var _ complex64 = complex(f32, f32)
210 var _ complex64 = complex /* ERRORx `cannot use .* in variable declaration` */ (f64, f64)
211
212 var _ complex128 = complex /* ERRORx `cannot use .* in variable declaration` */ (f32, f32)
213 var _ complex128 = complex(f64, f64)
214
215 // untyped constants
216 const _ int = complex(1, 0)
217 const _ float32 = complex(1, 0)
218 const _ complex64 = complex(1, 0)
219 const _ complex128 = complex(1, 0)
220 const _ = complex(0i, 0i)
221 const _ = complex(0i, 0)
222 const _ int = 1.0 + complex(1, 0i)
223
224 const _ int = complex /* ERROR "int" */ (1.1, 0)
225 const _ float32 = complex /* ERROR "float32" */ (1, 2)
226
227 // untyped values
228 var s uint
229 _ = complex(1 /* ERROR "integer" */ <<s, 0)
230 const _ = complex /* ERROR "not constant" */ (1 /* ERROR "integer" */ <<s, 0)
231 var _ int = complex /* ERRORx `cannot use .* in variable declaration` */ (1 /* ERROR "integer" */ <<s, 0)
232
233 // floating-point argument types must be identical
234 type F32 float32
235 type F64 float64
236 var x32 F32
237 var x64 F64
238 c64 = complex(x32, x32)
239 _ = complex(x32 /* ERROR "mismatched types" */ , f32)
240 _ = complex(f32 /* ERROR "mismatched types" */ , x32)
241 c128 = complex(x64, x64)
242 _ = c128
243 _ = complex(x64 /* ERROR "mismatched types" */ , f64)
244 _ = complex(f64 /* ERROR "mismatched types" */ , x64)
245
246 var t []float32
247 _ = complex(t... /* ERROR "invalid use of ..." */ )
248 }
249
250 func complex2() {
251 f1 := func() (x float32) { return }
252 f2 := func() (x, y float32) { return }
253 f3 := func() (x, y, z float32) { return }
254 _ = complex(f0 /* ERROR "used as value" */ ())
255 _ = complex(f1()) // ERROR "not enough arguments"
256 _ = complex(f2())
257 _ = complex(f3()) // ERROR "too many arguments"
258 }
259
260 func copy1() {
261 copy() // ERROR "not enough arguments"
262 copy("foo") // ERROR "not enough arguments"
263 copy([ /* ERROR "invalid copy: argument must be a slice; have [...]int{} (value of type [0]int)" */ ...]int{}, []int{})
264 copy([]int{}, [ /* ERROR "invalid copy: argument must be a slice; have [...]int{} (value of type [0]int)" */ ...]int{})
265 copy([ /* ERROR "invalid copy: arguments []int8{} (value of type []int8) and \"foo\" (untyped string constant) have different element types int8 and byte" */ ]int8{}, "foo")
266
267 // spec examples
268 var a = [...]int{0, 1, 2, 3, 4, 5, 6, 7}
269 var s = make([]int, 6)
270 var b = make([]byte, 5)
271 n1 := copy(s, a[0:]) // n1 == 6, s == []int{0, 1, 2, 3, 4, 5}
272 n2 := copy(s, s[2:]) // n2 == 4, s == []int{2, 3, 4, 5, 4, 5}
273 n3 := copy(b, "Hello, World!") // n3 == 5, b == []byte("Hello")
274 _, _, _ = n1, n2, n3
275
276 var t [][]int
277 copy(t, t)
278 copy(t, nil /* ERROR "invalid copy: argument must be a slice; have untyped nil" */ )
279 copy(nil /* ERROR "invalid copy: argument must be a slice; have untyped nil" */ , t)
280 copy(nil /* ERROR "invalid copy: argument must be a slice; have untyped nil" */ , nil)
281 copy(t... /* ERROR "invalid use of ..." */ )
282 }
283
284 func copy2() {
285 f1 := func() (a []int) { return }
286 f2 := func() (a, b []int) { return }
287 f3 := func() (a, b, c []int) { return }
288 copy(f0 /* ERROR "used as value" */ ())
289 copy(f1()) // ERROR "not enough arguments"
290 copy(f2())
291 copy(f3()) // ERROR "too many arguments"
292 }
293
294 func delete1() {
295 var m map[string]int
296 var s string
297 delete() // ERROR "not enough arguments"
298 delete(1) // ERROR "not enough arguments"
299 delete(1, 2, 3) // ERROR "too many arguments"
300 delete(m, 0 /* ERROR "cannot use" */)
301 delete(m, s)
302 _ = delete /* ERROR "used as value" */ (m, s)
303
304 var t []map[string]string
305 delete(t... /* ERROR "invalid use of ..." */ )
306 }
307
308 func delete2() {
309 f1 := func() (m map[string]int) { return }
310 f2 := func() (m map[string]int, k string) { return }
311 f3 := func() (m map[string]int, k string, x float32) { return }
312 delete(f0 /* ERROR "used as value" */ ())
313 delete(f1()) // ERROR "not enough arguments"
314 delete(f2())
315 delete(f3()) // ERROR "too many arguments"
316 }
317
318 func imag1() {
319 var f32 float32
320 var f64 float64
321 var c64 complex64
322 var c128 complex128
323 _ = imag() // ERROR "not enough arguments"
324 _ = imag(1, 2) // ERROR "too many arguments"
325 _ = imag(10)
326 _ = imag(2.7182818)
327 _ = imag("foo" /* ERROR "expected complex" */)
328 _ = imag('a')
329 const _5 = imag(1 + 2i)
330 assert(_5 == 2)
331 f32 = _5
332 f64 = _5
333 const _6 = imag(0i)
334 assert(_6 == 0)
335 f32 = imag(c64)
336 f64 = imag(c128)
337 f32 = imag /* ERRORx `cannot use .* in assignment` */ (c128)
338 f64 = imag /* ERRORx `cannot use .* in assignment` */ (c64)
339 imag /* ERROR "not used" */ (c64)
340 _, _ = f32, f64
341
342 // complex type may not be predeclared
343 type C64 complex64
344 type C128 complex128
345 var x64 C64
346 var x128 C128
347 f32 = imag(x64)
348 f64 = imag(x128)
349
350 var a []complex64
351 _ = imag(a... /* ERROR "invalid use of ..." */ )
352
353 // if argument is untyped, result is untyped
354 const _ byte = imag(1.2 + 3i)
355 const _ complex128 = imag(1.2 + 3i)
356
357 // lhs constant shift operands are typed as complex128
358 var s uint
359 _ = imag(1 /* ERROR "must be integer" */ << s)
360 }
361
362 func imag2() {
363 f1 := func() (x complex128) { return }
364 f2 := func() (x, y complex128) { return }
365 _ = imag(f0 /* ERROR "used as value" */ ())
366 _ = imag(f1())
367 _ = imag(f2()) // ERROR "too many arguments"
368 }
369
370 func len1() {
371 const c = "foobar"
372 var a [10]bool
373 var p *[20]int
374 var m map[string]complex128
375 _ = len() // ERROR "not enough arguments"
376 _ = len(1, 2) // ERROR "too many arguments"
377 _ = len(42 /* ERROR "invalid" */)
378 const _3 = len(c)
379 assert(_3 == 6)
380 const _4 = len(a)
381 assert(_4 == 10)
382 const _5 = len(p)
383 assert(_5 == 20)
384 _ = len(m)
385 len /* ERROR "not used" */ (c)
386
387 // esoteric case
388 var t string
389 var hash map[interface{}][]*[10]int
390 const n = len /* ERROR "not constant" */ (hash[recover()][len(t)])
391 assert(n == 10) // ok because n has unknown value and no error is reported
392 var ch <-chan int
393 const nn = len /* ERROR "not constant" */ (hash[<-ch][len(t)])
394
395 // issue 4744
396 type T struct{ a [10]int }
397 const _ = len(((*T)(nil)).a)
398
399 var s [][]byte
400 _ = len(s)
401 _ = len(s... /* ERROR "invalid use of ..." */ )
402 }
403
404 func len2() {
405 f1 := func() (x []int) { return }
406 f2 := func() (x, y []int) { return }
407 _ = len(f0 /* ERROR "used as value" */ ())
408 _ = len(f1())
409 _ = len(f2()) // ERROR "too many arguments"
410 }
411
412 // test cases for issue 7387
413 func len3() {
414 var f = func() int { return 0 }
415 var x = f()
416 const (
417 _ = len([4]int{})
418 _ = len([4]int{x})
419 _ = len /* ERROR "not constant" */ ([4]int{f()})
420 _ = len /* ERROR "not constant" */ ([4]int{len([]int{})})
421 _ = len([4]int{len([4]int{})})
422 )
423 var y float64
424 var z complex128
425 const (
426 _ = len([4]float64{})
427 _ = len([4]float64{y})
428 _ = len([4]float64{real(2i)})
429 _ = len /* ERROR "not constant" */ ([4]float64{real(z)})
430 )
431 var ch chan [10]int
432 const (
433 _ = len /* ERROR "not constant" */ (<-ch)
434 _ = len /* ERROR "not constant" */ ([4]int{(<-ch)[0]})
435 )
436 }
437
438 func make1() {
439 var n int
440 var m float32
441 var s uint
442
443 _ = make() // ERROR "not enough arguments"
444 _ = make(1 /* ERROR "not a type" */)
445 _ = make(int /* ERROR "cannot make" */)
446
447 // slices
448 _ = make/* ERROR "arguments" */ ([]int)
449 _ = make/* ERROR "arguments" */ ([]int, 2, 3, 4)
450 _ = make([]int, int /* ERROR "not an expression" */)
451 _ = make([]int, 10, float32 /* ERROR "not an expression" */)
452 _ = make([]int, "foo" /* ERROR "cannot convert" */)
453 _ = make([]int, 10, 2.3 /* ERROR "truncated" */)
454 _ = make([]int, 5, 10.0)
455 _ = make([]int, 0i)
456 _ = make([]int, 1.0)
457 _ = make([]int, 1.0<<s)
458 _ = make([]int, 1.1 /* ERROR "int" */ <<s)
459 _ = make([]int, - /* ERROR "must not be negative" */ 1, 10)
460 _ = make([]int, 0, - /* ERROR "must not be negative" */ 1)
461 _ = make([]int, - /* ERROR "must not be negative" */ 1, - /* ERROR "must not be negative" */ 1)
462 _ = make([]int, 1 /* ERROR "overflows" */ <<100, 1 /* ERROR "overflows" */ <<100)
463 _ = make([]int, 10 /* ERROR "length and capacity swapped" */ , 9)
464 _ = make([]int, 1 /* ERROR "overflows" */ <<100, 12345)
465 _ = make([]int, m /* ERROR "must be integer" */ )
466 _ = &make /* ERROR "cannot take address" */ ([]int, 0)
467
468 // maps
469 _ = make /* ERROR "arguments" */ (map[int]string, 10, 20)
470 _ = make(map[int]float32, int /* ERROR "not an expression" */)
471 _ = make(map[int]float32, "foo" /* ERROR "cannot convert" */)
472 _ = make(map[int]float32, 10)
473 _ = make(map[int]float32, n)
474 _ = make(map[int]float32, int64(n))
475 _ = make(map[string]bool, 10.0)
476 _ = make(map[string]bool, 10.0<<s)
477 _ = &make /* ERROR "cannot take address" */ (map[string]bool)
478
479 // channels
480 _ = make /* ERROR "arguments" */ (chan int, 10, 20)
481 _ = make(chan int, int /* ERROR "not an expression" */)
482 _ = make(chan<- int, "foo" /* ERROR "cannot convert" */)
483 _ = make(chan int, - /* ERROR "must not be negative" */ 10)
484 _ = make(<-chan float64, 10)
485 _ = make(chan chan int, n)
486 _ = make(chan string, int64(n))
487 _ = make(chan bool, 10.0)
488 _ = make(chan bool, 10.0<<s)
489 _ = &make /* ERROR "cannot take address" */ (chan bool)
490
491 make /* ERROR "not used" */ ([]int, 10)
492
493 var t []int
494 _ = make([]int, t[0], t[1])
495 _ = make([]int, t... /* ERROR "invalid use of ..." */ )
496 }
497
498 func make2() {
499 f1 := func() (x []int) { return }
500 _ = make(f0 /* ERROR "not a type" */ ())
501 _ = make(f1 /* ERROR "not a type" */ ())
502 }
503
504 func max1() {
505 var b bool
506 var c complex128
507 var x int
508 var s string
509 type myint int
510 var m myint
511 _ = max() /* ERROR "not enough arguments" */
512 _ = max(b /* ERROR "cannot be ordered" */ )
513 _ = max(c /* ERROR "cannot be ordered" */ )
514 _ = max(x)
515 _ = max(s)
516 _ = max(x, x)
517 _ = max(x, x, x, x, x)
518 var _ int = max /* ERROR "cannot use max(m) (value of int type myint) as int value" */ (m)
519 _ = max(x, m /* ERROR "invalid argument: mismatched types int (previous argument) and myint (type of m)" */ , x)
520
521 _ = max(1, x)
522 _ = max(1.0, x)
523 _ = max(1.2 /* ERROR "1.2 (untyped float constant) truncated to int" */ , x)
524 _ = max(-10, 1.0, c /* ERROR "cannot be ordered" */ )
525
526 const (
527 _ = max /* ERROR "max(x) (value of type int) is not constant" */ (x)
528 _ = max(true /* ERROR "invalid argument: true (untyped bool constant) cannot be ordered" */ )
529 _ = max(1)
530 _ = max(1, 2.3, 'a')
531 _ = max(1, "foo" /* ERROR "mismatched types" */ )
532 _ = max(1, 0i /* ERROR "cannot be ordered" */ )
533 _ = max(1, 2 /* ERROR "cannot be ordered" */ + 3i )
534 )
535 }
536
537 func max2() {
538 _ = assert(max(0) == 0)
539 _ = assert(max(0, 1) == 1)
540 _ = assert(max(0, -10, 123456789) == 123456789)
541 _ = assert(max(-12345678901234567890, 0) == 0)
542
543 _ = assert(max(1, 2.3) == 2.3)
544 _ = assert(max(1, 2.3, 'a') == 'a')
545
546 _ = assert(max("", "a") == "a")
547 _ = assert(max("abcde", "xyz", "foo", "bar") == "xyz")
548
549 const (
550 _ int = max(1.0)
551 _ float32 = max(1, 2)
552 _ int = max /* ERROR "cannot use max(1, 2.3) (untyped float constant 2.3) as int value" */ (1, 2.3)
553 _ int = max(1.2, 3) // ok!
554 _ byte = max(1, 'a')
555 )
556 }
557
558 func min1() {
559 var b bool
560 var c complex128
561 var x int
562 var s string
563 type myint int
564 var m myint
565 _ = min() /* ERROR "not enough arguments" */
566 _ = min(b /* ERROR "cannot be ordered" */ )
567 _ = min(c /* ERROR "cannot be ordered" */ )
568 _ = min(x)
569 _ = min(s)
570 _ = min(x, x)
571 _ = min(x, x, x, x, x)
572 var _ int = min /* ERROR "cannot use min(m) (value of int type myint) as int value" */ (m)
573 _ = min(x, m /* ERROR "invalid argument: mismatched types int (previous argument) and myint (type of m)" */ , x)
574
575 _ = min(1, x)
576 _ = min(1.0, x)
577 _ = min(1.2 /* ERROR "1.2 (untyped float constant) truncated to int" */ , x)
578 _ = min(-10, 1.0, c /* ERROR "cannot be ordered" */ )
579
580 const (
581 _ = min /* ERROR "min(x) (value of type int) is not constant" */ (x)
582 _ = min(true /* ERROR "invalid argument: true (untyped bool constant) cannot be ordered" */ )
583 _ = min(1)
584 _ = min(1, 2.3, 'a')
585 _ = min(1, "foo" /* ERROR "mismatched types" */ )
586 _ = min(1, 0i /* ERROR "cannot be ordered" */ )
587 _ = min(1, 2 /* ERROR "cannot be ordered" */ + 3i )
588 )
589 }
590
591 func min2() {
592 _ = assert(min(0) == 0)
593 _ = assert(min(0, 1) == 0)
594 _ = assert(min(0, -10, 123456789) == -10)
595 _ = assert(min(-12345678901234567890, 0) == -12345678901234567890)
596
597 _ = assert(min(1, 2.3) == 1)
598 _ = assert(min(1, 2.3, 'a') == 1)
599
600 _ = assert(min("", "a") == "")
601 _ = assert(min("abcde", "xyz", "foo", "bar") == "abcde")
602
603 const (
604 _ int = min(1.0)
605 _ float32 = min(1, 2)
606 _ int = min(1, 2.3) // ok!
607 _ int = min /* ERROR "cannot use min(1.2, 3) (untyped float constant 1.2) as int value" */ (1.2, 3)
608 _ byte = min(1, 'a')
609 )
610 }
611
612
613 func new1() {
614 _ = new() // ERROR "not enough arguments"
615 _ = new(1, 2) // ERROR "too many arguments"
616 _ = new(unsafe /* ERROR "use of package unsafe not in selector" */ )
617
618 _ = new(struct{ x, y int })
619 p := new(float64)
620 q := new(*float64)
621 _ = *p == **q
622
623 type G[P any] struct{}
624 _ = new(G[int])
625 _ = new(G /* ERROR "cannot use generic type G without instantiation" */ )
626
627 new /* ERROR "not used" */ (int)
628 _ = &new /* ERROR "cannot take address" */ (int)
629 _ = new(int... /* ERROR "invalid use of ..." */)
630 _ = new(f0 /* ERROR "f0() (no value) used as value or type" */ ())
631 _ = new(len /* ERROR "len (built-in) must be called" */)
632 _ = new(1 /* ERROR "argument to new (overflows)" */ << 70)
633 }
634
635 func new2() {
636 // new(expr), added in go1.26
637 f1 := func() []int { panic(0) }
638 f2 := func() (int, int) { panic(0) }
639 var (
640 _ *[]int = new(f1())
641 _ *func() []int = new(f1)
642 _ *bool = new(false)
643 _ *bool = new(1 < 2)
644 _ *int = new(123)
645 _ *float64 = new(1.0)
646 _ *uint = new(uint(3))
647 _ *rune = new('a')
648 _ *string = new("A")
649 _ *struct{} = new(struct{}{})
650 _ *any = new(any)
651
652 _ = new(f2 /* ERRORx "multiple-value.*in single-value context" */ ())
653 _ = new(1 << /* ERROR "constant shift overflow" */ 1000)
654 _ = new(1e10000 /* ERROR "cannot use 1e10000 (untyped float constant 1e+10000) as float64 value in argument to new (overflows)" */ )
655 _ = new(nil /* ERROR "use of untyped nil in argument to new" */ )
656 _ = new(comparable /* ERROR "cannot use type comparable outside a type constraint" */ )
657 _ = new(new /* ERROR "new (built-in) must be called" */ )
658 _ = new(panic /* ERROR "panic(0) (no value) used as value or type" */ (0))
659
660 // from issue 43125
661 _ = new(-1)
662 _ = new(1 + 1)
663 )
664 }
665
666 func panic1() {
667 panic() // ERROR "not enough arguments"
668 panic(1, 2) // ERROR "too many arguments"
669 panic(0)
670 panic("foo")
671 panic(false)
672 panic(1<<10)
673 panic(1 << /* ERROR "constant shift overflow" */ 1000)
674 _ = panic /* ERROR "used as value" */ (0)
675
676 var s []byte
677 panic(s)
678 panic(s... /* ERROR "invalid use of ..." */ )
679 }
680
681 func panic2() {
682 f1 := func() (x int) { return }
683 f2 := func() (x, y int) { return }
684 panic(f0 /* ERROR "used as value" */ ())
685 panic(f1())
686 panic(f2()) // ERROR "too many arguments"
687 }
688
689 func print1() {
690 print()
691 print(1)
692 print(1, 2)
693 print("foo")
694 print(2.718281828)
695 print(false)
696 print(1<<10)
697 print(1 << /* ERROR "constant shift overflow" */ 1000)
698 println(nil /* ERROR "untyped nil" */ )
699
700 var s []int
701 print(s... /* ERROR "invalid use of ..." */ )
702 _ = print /* ERROR "used as value" */ ()
703 }
704
705 func print2() {
706 f1 := func() (x int) { return }
707 f2 := func() (x, y int) { return }
708 f3 := func() (x int, y float32, z string) { return }
709 print(f0 /* ERROR "used as value" */ ())
710 print(f1())
711 print(f2())
712 print(f3())
713 }
714
715 func println1() {
716 println()
717 println(1)
718 println(1, 2)
719 println("foo")
720 println(2.718281828)
721 println(false)
722 println(1<<10)
723 println(1 << /* ERROR "constant shift overflow" */ 1000)
724 println(nil /* ERROR "untyped nil" */ )
725
726 var s []int
727 println(s... /* ERROR "invalid use of ..." */ )
728 _ = println /* ERROR "used as value" */ ()
729 }
730
731 func println2() {
732 f1 := func() (x int) { return }
733 f2 := func() (x, y int) { return }
734 f3 := func() (x int, y float32, z string) { return }
735 println(f0 /* ERROR "used as value" */ ())
736 println(f1())
737 println(f2())
738 println(f3())
739 }
740
741 func real1() {
742 var f32 float32
743 var f64 float64
744 var c64 complex64
745 var c128 complex128
746 _ = real() // ERROR "not enough arguments"
747 _ = real(1, 2) // ERROR "too many arguments"
748 _ = real(10)
749 _ = real(2.7182818)
750 _ = real("foo" /* ERROR "expected complex" */)
751 const _5 = real(1 + 2i)
752 assert(_5 == 1)
753 f32 = _5
754 f64 = _5
755 const _6 = real(0i)
756 assert(_6 == 0)
757 f32 = real(c64)
758 f64 = real(c128)
759 f32 = real /* ERRORx `cannot use .* in assignment` */ (c128)
760 f64 = real /* ERRORx `cannot use .* in assignment` */ (c64)
761 real /* ERROR "not used" */ (c64)
762
763 // complex type may not be predeclared
764 type C64 complex64
765 type C128 complex128
766 var x64 C64
767 var x128 C128
768 f32 = imag(x64)
769 f64 = imag(x128)
770 _, _ = f32, f64
771
772 var a []complex64
773 _ = real(a... /* ERROR "invalid use of ..." */ )
774
775 // if argument is untyped, result is untyped
776 const _ byte = real(1 + 2.3i)
777 const _ complex128 = real(1 + 2.3i)
778
779 // lhs constant shift operands are typed as complex128
780 var s uint
781 _ = real(1 /* ERROR "must be integer" */ << s)
782 }
783
784 func real2() {
785 f1 := func() (x complex128) { return }
786 f2 := func() (x, y complex128) { return }
787 _ = real(f0 /* ERROR "used as value" */ ())
788 _ = real(f1())
789 _ = real(f2()) // ERROR "too many arguments"
790 }
791
792 func recover1() {
793 _ = recover()
794 _ = recover(10) // ERROR "too many arguments"
795 recover()
796
797 var s []int
798 recover(s... /* ERROR "invalid use of ..." */ )
799 }
800
801 func recover2() {
802 f1 := func() (x int) { return }
803 f2 := func() (x, y int) { return }
804 _ = recover(f0 /* ERROR "used as value" */ ())
805 _ = recover(f1()) // ERROR "too many arguments"
806 _ = recover(f2()) // ERROR "too many arguments"
807 }
808
809 // assuming types.DefaultPtrSize == 8
810 type S0 struct{ // offset
811 a bool // 0
812 b rune // 4
813 c *int // 8
814 d bool // 16
815 e complex128 // 24
816 } // 40
817
818 type S1 struct{ // offset
819 x float32 // 0
820 y string // 8
821 z *S1 // 24
822 S0 // 32
823 } // 72
824
825 type S2 struct{ // offset
826 *S1 // 0
827 } // 8
828
829 type S3 struct { // offset
830 a int64 // 0
831 b int32 // 8
832 } // 16
833
834 type S4 struct { // offset
835 S3 // 0
836 int32 // 12
837 } // 24
838
839 type S5 struct { // offset
840 a [3]int32 // 0
841 b int32 // 16
842 } // 16
843
844 func (S2) m() {}
845
846 func Alignof1() {
847 var x int
848 _ = unsafe.Alignof() // ERROR "not enough arguments"
849 _ = unsafe.Alignof(1, 2) // ERROR "too many arguments"
850 _ = unsafe.Alignof(int /* ERROR "not an expression" */)
851 _ = unsafe.Alignof(42)
852 _ = unsafe.Alignof(new(struct{}))
853 _ = unsafe.Alignof(1<<10)
854 _ = unsafe.Alignof(1 << /* ERROR "constant shift overflow" */ 1000)
855 _ = unsafe.Alignof(nil /* ERROR "untyped nil" */ )
856 unsafe /* ERROR "not used" */ .Alignof(x)
857
858 var y S0
859 assert(unsafe.Alignof(y.a) == 1)
860 assert(unsafe.Alignof(y.b) == 4)
861 assert(unsafe.Alignof(y.c) == 8)
862 assert(unsafe.Alignof(y.d) == 1)
863 assert(unsafe.Alignof(y.e) == 8)
864
865 var s []byte
866 _ = unsafe.Alignof(s)
867 _ = unsafe.Alignof(s... /* ERROR "invalid use of ..." */ )
868 }
869
870 func Alignof2() {
871 f1 := func() (x int32) { return }
872 f2 := func() (x, y int32) { return }
873 _ = unsafe.Alignof(f0 /* ERROR "used as value" */ ())
874 assert(unsafe.Alignof(f1()) == 4)
875 _ = unsafe.Alignof(f2()) // ERROR "too many arguments"
876 }
877
878 func Offsetof1() {
879 var x struct{ f int }
880 _ = unsafe.Offsetof() // ERROR "not enough arguments"
881 _ = unsafe.Offsetof(1, 2) // ERROR "too many arguments"
882 _ = unsafe.Offsetof(int /* ERROR "not a selector expression" */ )
883 _ = unsafe.Offsetof(x /* ERROR "not a selector expression" */ )
884 _ = unsafe.Offsetof(nil /* ERROR "not a selector expression" */ )
885 _ = unsafe.Offsetof(x.f)
886 _ = unsafe.Offsetof((x.f))
887 _ = unsafe.Offsetof((((((((x))).f)))))
888 unsafe /* ERROR "not used" */ .Offsetof(x.f)
889
890 var y0 S0
891 assert(unsafe.Offsetof(y0.a) == 0)
892 assert(unsafe.Offsetof(y0.b) == 4)
893 assert(unsafe.Offsetof(y0.c) == 8)
894 assert(unsafe.Offsetof(y0.d) == 16)
895 assert(unsafe.Offsetof(y0.e) == 24)
896
897 var y1 S1
898 assert(unsafe.Offsetof(y1.x) == 0)
899 assert(unsafe.Offsetof(y1.y) == 8)
900 assert(unsafe.Offsetof(y1.z) == 24)
901 assert(unsafe.Offsetof(y1.S0) == 32)
902
903 assert(unsafe.Offsetof(y1.S0.a) == 0) // relative to S0
904 assert(unsafe.Offsetof(y1.a) == 32) // relative to S1
905 assert(unsafe.Offsetof(y1.b) == 36) // relative to S1
906 assert(unsafe.Offsetof(y1.c) == 40) // relative to S1
907 assert(unsafe.Offsetof(y1.d) == 48) // relative to S1
908 assert(unsafe.Offsetof(y1.e) == 56) // relative to S1
909
910 var y1p *S1
911 assert(unsafe.Offsetof(y1p.S0) == 32)
912
913 type P *S1
914 var p P = y1p
915 assert(unsafe.Offsetof(p.S0) == 32)
916
917 var y2 S2
918 assert(unsafe.Offsetof(y2.S1) == 0)
919 _ = unsafe.Offsetof(y2 /* ERROR "embedded via a pointer" */ .x)
920 _ = unsafe.Offsetof(y2 /* ERROR "method value" */ .m)
921
922 var s []byte
923 _ = unsafe.Offsetof(s... /* ERROR "invalid use of ..." */ )
924 }
925
926 func Offsetof2() {
927 f1 := func() (x int32) { return }
928 f2 := func() (x, y int32) { return }
929 _ = unsafe.Offsetof(f0 /* ERROR "not a selector expression" */ ())
930 _ = unsafe.Offsetof(f1 /* ERROR "not a selector expression" */ ())
931 _ = unsafe.Offsetof(f2 /* ERROR "not a selector expression" */ ())
932 }
933
934 func Sizeof1() {
935 var x int
936 _ = unsafe.Sizeof() // ERROR "not enough arguments"
937 _ = unsafe.Sizeof(1, 2) // ERROR "too many arguments"
938 _ = unsafe.Sizeof(int /* ERROR "not an expression" */)
939 _ = unsafe.Sizeof(42)
940 _ = unsafe.Sizeof(new(complex128))
941 _ = unsafe.Sizeof(1<<10)
942 _ = unsafe.Sizeof(1 << /* ERROR "constant shift overflow" */ 1000)
943 _ = unsafe.Sizeof(nil /* ERROR "untyped nil" */ )
944 unsafe /* ERROR "not used" */ .Sizeof(x)
945
946 // basic types have size guarantees
947 assert(unsafe.Sizeof(byte(0)) == 1)
948 assert(unsafe.Sizeof(uint8(0)) == 1)
949 assert(unsafe.Sizeof(int8(0)) == 1)
950 assert(unsafe.Sizeof(uint16(0)) == 2)
951 assert(unsafe.Sizeof(int16(0)) == 2)
952 assert(unsafe.Sizeof(uint32(0)) == 4)
953 assert(unsafe.Sizeof(int32(0)) == 4)
954 assert(unsafe.Sizeof(float32(0)) == 4)
955 assert(unsafe.Sizeof(uint64(0)) == 8)
956 assert(unsafe.Sizeof(int64(0)) == 8)
957 assert(unsafe.Sizeof(float64(0)) == 8)
958 assert(unsafe.Sizeof(complex64(0)) == 8)
959 assert(unsafe.Sizeof(complex128(0)) == 16)
960
961 var y0 S0
962 assert(unsafe.Sizeof(y0.a) == 1)
963 assert(unsafe.Sizeof(y0.b) == 4)
964 assert(unsafe.Sizeof(y0.c) == 8)
965 assert(unsafe.Sizeof(y0.d) == 1)
966 assert(unsafe.Sizeof(y0.e) == 16)
967 assert(unsafe.Sizeof(y0) == 40)
968
969 var y1 S1
970 assert(unsafe.Sizeof(y1) == 72)
971
972 var y2 S2
973 assert(unsafe.Sizeof(y2) == 8)
974
975 var y3 S3
976 assert(unsafe.Sizeof(y3) == 16)
977
978 var y4 S4
979 assert(unsafe.Sizeof(y4) == 24)
980
981 var y5 S5
982 assert(unsafe.Sizeof(y5) == 16)
983
984 var a3 [10]S3
985 assert(unsafe.Sizeof(a3) == 160)
986
987 // test case for issue 5670
988 type T struct {
989 a int32
990 _ int32
991 c int32
992 }
993 assert(unsafe.Sizeof(T{}) == 12)
994
995 var s []byte
996 _ = unsafe.Sizeof(s)
997 _ = unsafe.Sizeof(s... /* ERROR "invalid use of ..." */ )
998 }
999
1000 func Sizeof2() {
1001 f1 := func() (x int64) { return }
1002 f2 := func() (x, y int64) { return }
1003 _ = unsafe.Sizeof(f0 /* ERROR "used as value" */ ())
1004 assert(unsafe.Sizeof(f1()) == 8)
1005 _ = unsafe.Sizeof(f2()) // ERROR "too many arguments"
1006 }
1007
1008 func Slice1() {
1009 var x int
1010 unsafe.Slice() // ERROR "not enough arguments"
1011 unsafe.Slice(1, 2, 3) // ERROR "too many arguments"
1012 unsafe.Slice(1 /* ERROR "is not a pointer" */ , 2)
1013 unsafe.Slice(nil /* ERROR "nil is not a pointer" */ , 0)
1014 unsafe.Slice(&x, "foo" /* ERRORx `cannot convert .* to type int` */ )
1015 unsafe.Slice(&x, 1.2 /* ERROR "truncated to int" */ )
1016 unsafe.Slice(&x, - /* ERROR "must not be negative" */ 1)
1017 unsafe /* ERROR "not used" */ .Slice(&x, 0)
1018 var _ []byte = unsafe /* ERROR "value of type []int" */ .Slice(&x, 0)
1019
1020 var _ []int = unsafe.Slice(&x, 0)
1021 _ = unsafe.Slice(&x, 1.0)
1022 _ = unsafe.Slice((*int)(nil), 0)
1023 }
1024
1025 func SliceData1() {
1026 var s []int
1027 unsafe.SliceData(0 /* ERROR "not a slice" */)
1028 unsafe /* ERROR "not used" */ .SliceData(s)
1029
1030 type S []int
1031 _ = unsafe.SliceData(s)
1032 _ = unsafe.SliceData(S{})
1033 }
1034
1035 func String1() {
1036 var b byte
1037 unsafe.String() // ERROR "not enough arguments"
1038 unsafe.String(1, 2, 3) // ERROR "too many arguments"
1039 unsafe.String(1 /* ERROR "cannot use 1" */ , 2)
1040 unsafe.String(&b, "foo" /* ERRORx `cannot convert .* to type int` */ )
1041 unsafe.String(&b, 1.2 /* ERROR "truncated to int" */ )
1042 unsafe.String(&b, - /* ERROR "must not be negative" */ 1)
1043 unsafe /* ERROR "not used" */ .String(&b, 0)
1044 var _ []byte = unsafe /* ERROR "value of type string" */ .String(&b, 0)
1045
1046 var _ string = unsafe.String(&b, 0)
1047 _ = unsafe.String(&b, 1.0)
1048 _ = unsafe.String(nil, 0) // here we allow nil as ptr argument (in contrast to unsafe.Slice)
1049 }
1050
1051 func StringData1() {
1052 var s string
1053 type S string
1054 unsafe.StringData(0 /* ERROR "cannot use 0" */)
1055 unsafe.StringData(S /* ERROR "cannot use S" */ ("foo"))
1056 unsafe /* ERROR "not used" */ .StringData(s)
1057
1058 _ = unsafe.StringData(s)
1059 _ = unsafe.StringData("foo")
1060 }
1061
1062 // self-testing only
1063 func assert1() {
1064 var x int
1065 assert() /* ERROR "not enough arguments" */
1066 assert(1, 2) /* ERROR "too many arguments" */
1067 assert("foo" /* ERROR "boolean constant" */ )
1068 assert(x /* ERROR "boolean constant" */)
1069 assert(true)
1070 assert /* ERROR "failed" */ (false)
1071 _ = assert(true)
1072
1073 var s []byte
1074 assert(s... /* ERROR "invalid use of ..." */ )
1075 }
1076
1077 func assert2() {
1078 f1 := func() (x bool) { return }
1079 f2 := func() (x bool) { return }
1080 assert(f0 /* ERROR "used as value" */ ())
1081 assert(f1 /* ERROR "boolean constant" */ ())
1082 assert(f2 /* ERROR "boolean constant" */ ())
1083 }
1084
1085 // self-testing only
1086 func trace1() {
1087 // Uncomment the code below to test trace - will produce console output
1088 // _ = trace /* ERROR "no value" */ ()
1089 // _ = trace(1)
1090 // _ = trace(true, 1.2, '\'', "foo", 42i, "foo" <= "bar")
1091
1092 var s []byte
1093 trace(s... /* ERROR "invalid use of ..." */ )
1094 }
1095
1096 func trace2() {
1097 f1 := func() (x int) { return }
1098 f2 := func() (x int, y string) { return }
1099 f3 := func() (x int, y string, z []int) { return }
1100 _ = f1
1101 _ = f2
1102 _ = f3
1103 // Uncomment the code below to test trace - will produce console output
1104 // trace(f0())
1105 // trace(f1())
1106 // trace(f2())
1107 // trace(f3())
1108 // trace(f0(), 1)
1109 // trace(f1(), 1, 2)
1110 // trace(f2(), 1, 2, 3)
1111 // trace(f3(), 1, 2, 3, 4)
1112 }