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