1// errorcheck -0 -m -l 2 3// Copyright 2010 The Go Authors. All rights reserved. 4// Use of this source code is governed by a BSD-style 5// license that can be found in the LICENSE file. 6 7// Test, using compiler diagnostic flags, that the escape analysis is working. 8// Compiles but does not run. Inlining is disabled. 9 10package foo 11 12import ( 13 "fmt" 14 "unsafe" 15) 16 17var gxx *int 18 19func foo1(x int) { // ERROR "moved to heap: x" 20 gxx = &x // ERROR "&x escapes to heap" 21} 22 23func foo2(yy *int) { // ERROR "leaking param: yy" 24 gxx = yy 25} 26 27func foo3(x int) *int { // ERROR "moved to heap: x" 28 return &x // ERROR "&x escapes to heap" 29} 30 31type T *T 32 33func foo3b(t T) { // ERROR "leaking param: t" 34 *t = t 35} 36 37// xx isn't going anywhere, so use of yy is ok 38func foo4(xx, yy *int) { // ERROR "xx does not escape" "yy does not escape" 39 xx = yy 40} 41 42// xx isn't going anywhere, so taking address of yy is ok 43func foo5(xx **int, yy *int) { // ERROR "xx does not escape" "yy does not escape" 44 xx = &yy // ERROR "&yy does not escape" 45} 46 47func foo6(xx **int, yy *int) { // ERROR "xx does not escape" "leaking param: yy" 48 *xx = yy 49} 50 51func foo7(xx **int, yy *int) { // ERROR "xx does not escape" "yy does not escape" 52 **xx = *yy 53} 54 55func foo8(xx, yy *int) int { // ERROR "xx does not escape" "yy does not escape" 56 xx = yy 57 return *xx 58} 59 60func foo9(xx, yy *int) *int { // ERROR "leaking param: xx" "leaking param: yy" 61 xx = yy 62 return xx 63} 64 65func foo10(xx, yy *int) { // ERROR "xx does not escape" "yy does not escape" 66 *xx = *yy 67} 68 69func foo11() int { 70 x, y := 0, 42 71 xx := &x // ERROR "&x does not escape" 72 yy := &y // ERROR "&y does not escape" 73 *xx = *yy 74 return x 75} 76 77var xxx **int 78 79func foo12(yyy **int) { // ERROR "leaking param: yyy" 80 xxx = yyy 81} 82 83func foo13(yyy **int) { // ERROR "yyy does not escape" 84 *xxx = *yyy 85} 86 87func foo14(yyy **int) { // ERROR "yyy does not escape" 88 **xxx = **yyy 89} 90 91func foo15(yy *int) { // ERROR "moved to heap: yy" 92 xxx = &yy // ERROR "&yy escapes to heap" 93} 94 95func foo16(yy *int) { // ERROR "leaking param: yy" 96 *xxx = yy 97} 98 99func foo17(yy *int) { // ERROR "yy does not escape" 100 **xxx = *yy 101} 102 103func foo18(y int) { // ERROR "moved to heap: "y" 104 *xxx = &y // ERROR "&y escapes to heap" 105} 106 107func foo19(y int) { 108 **xxx = y 109} 110 111type Bar struct { 112 i int 113 ii *int 114} 115 116func NewBar() *Bar { 117 return &Bar{42, nil} // ERROR "&Bar literal escapes to heap" 118} 119 120func NewBarp(x *int) *Bar { // ERROR "leaking param: x" 121 return &Bar{42, x} // ERROR "&Bar literal escapes to heap" 122} 123 124func NewBarp2(x *int) *Bar { // ERROR "x does not escape" 125 return &Bar{*x, nil} // ERROR "&Bar literal escapes to heap" 126} 127 128func (b *Bar) NoLeak() int { // ERROR "b does not escape" 129 return *(b.ii) 130} 131 132func (b *Bar) Leak() *int { // ERROR "leaking param: b" 133 return &b.i // ERROR "&b.i escapes to heap" 134} 135 136func (b *Bar) AlsoNoLeak() *int { // ERROR "b does not escape" 137 return b.ii 138} 139 140func (b Bar) AlsoLeak() *int { // ERROR "leaking param: b" 141 return b.ii 142} 143 144func (b Bar) LeaksToo() *int { // ERROR "leaking param: b" 145 v := 0 // ERROR "moved to heap: v" 146 b.ii = &v // ERROR "&v escapes" 147 return b.ii 148} 149 150func (b *Bar) LeaksABit() *int { // ERROR "b does not escape" 151 v := 0 // ERROR "moved to heap: v" 152 b.ii = &v // ERROR "&v escapes" 153 return b.ii 154} 155 156func (b Bar) StillNoLeak() int { // ERROR "b does not escape" 157 v := 0 158 b.ii = &v // ERROR "&v does not escape" 159 return b.i 160} 161 162func goLeak(b *Bar) { // ERROR "leaking param: b" 163 go b.NoLeak() 164} 165 166type Bar2 struct { 167 i [12]int 168 ii []int 169} 170 171func NewBar2() *Bar2 { 172 return &Bar2{[12]int{42}, nil} // ERROR "&Bar2 literal escapes to heap" 173} 174 175func (b *Bar2) NoLeak() int { // ERROR "b does not escape" 176 return b.i[0] 177} 178 179func (b *Bar2) Leak() []int { // ERROR "leaking param: b" 180 return b.i[:] // ERROR "b.i escapes to heap" 181} 182 183func (b *Bar2) AlsoNoLeak() []int { // ERROR "b does not escape" 184 return b.ii[0:1] 185} 186 187func (b Bar2) AgainNoLeak() [12]int { // ERROR "b does not escape" 188 return b.i 189} 190 191func (b *Bar2) LeakSelf() { // ERROR "leaking param: b" 192 b.ii = b.i[0:4] // ERROR "b.i escapes to heap" 193} 194 195func (b *Bar2) LeakSelf2() { // ERROR "leaking param: b" 196 var buf []int 197 buf = b.i[0:] // ERROR "b.i escapes to heap" 198 b.ii = buf 199} 200 201func foo21() func() int { 202 x := 42 // ERROR "moved to heap: x" 203 return func() int { // ERROR "func literal escapes to heap" 204 return x // ERROR "&x escapes to heap" 205 } 206} 207 208func foo22() int { 209 x := 42 210 return func() int { // ERROR "func literal does not escape" 211 return x 212 }() 213} 214 215func foo23(x int) func() int { // ERROR "moved to heap: x" 216 return func() int { // ERROR "func literal escapes to heap" 217 return x // ERROR "&x escapes to heap" 218 } 219} 220 221func foo23a(x int) func() int { // ERROR "moved to heap: x" 222 f := func() int { // ERROR "func literal escapes to heap" 223 return x // ERROR "&x escapes to heap" 224 } 225 return f 226} 227 228func foo23b(x int) *(func() int) { // ERROR "moved to heap: x" 229 f := func() int { return x } // ERROR "moved to heap: f" "func literal escapes to heap" "&x escapes to heap" 230 return &f // ERROR "&f escapes to heap" 231} 232 233func foo24(x int) int { 234 return func() int { // ERROR "func literal does not escape" 235 return x 236 }() 237} 238 239var x *int 240 241func fooleak(xx *int) int { // ERROR "leaking param: xx" 242 x = xx 243 return *x 244} 245 246func foonoleak(xx *int) int { // ERROR "xx does not escape" 247 return *x + *xx 248} 249 250func foo31(x int) int { // ERROR "moved to heap: x" 251 return fooleak(&x) // ERROR "&x escapes to heap" 252} 253 254func foo32(x int) int { 255 return foonoleak(&x) // ERROR "&x does not escape" 256} 257 258type Foo struct { 259 xx *int 260 x int 261} 262 263var F Foo 264var pf *Foo 265 266func (f *Foo) fooleak() { // ERROR "leaking param: f" 267 pf = f 268} 269 270func (f *Foo) foonoleak() { // ERROR "f does not escape" 271 F.x = f.x 272} 273 274func (f *Foo) Leak() { // ERROR "leaking param: f" 275 f.fooleak() 276} 277 278func (f *Foo) NoLeak() { // ERROR "f does not escape" 279 f.foonoleak() 280} 281 282func foo41(x int) { // ERROR "moved to heap: x" 283 F.xx = &x // ERROR "&x escapes to heap" 284} 285 286func (f *Foo) foo42(x int) { // ERROR "f does not escape" "moved to heap: x" 287 f.xx = &x // ERROR "&x escapes to heap" 288} 289 290func foo43(f *Foo, x int) { // ERROR "f does not escape" "moved to heap: x" 291 f.xx = &x // ERROR "&x escapes to heap" 292} 293 294func foo44(yy *int) { // ERROR "leaking param: yy" 295 F.xx = yy 296} 297 298func (f *Foo) foo45() { // ERROR "f does not escape" 299 F.x = f.x 300} 301 302func (f *Foo) foo46() { // ERROR "f does not escape" 303 F.xx = f.xx 304} 305 306func (f *Foo) foo47() { // ERROR "leaking param: f" 307 f.xx = &f.x // ERROR "&f.x escapes to heap" 308} 309 310var ptrSlice []*int 311 312func foo50(i *int) { // ERROR "leaking param: i" 313 ptrSlice[0] = i 314} 315 316var ptrMap map[*int]*int 317 318func foo51(i *int) { // ERROR "leaking param: i" 319 ptrMap[i] = i 320} 321 322func indaddr1(x int) *int { // ERROR "moved to heap: x" 323 return &x // ERROR "&x escapes to heap" 324} 325 326func indaddr2(x *int) *int { // ERROR "leaking param: x" 327 return *&x // ERROR "&x does not escape" 328} 329 330func indaddr3(x *int32) *int { // ERROR "leaking param: x" 331 return *(**int)(unsafe.Pointer(&x)) // ERROR "&x does not escape" 332} 333 334// From package math: 335 336func Float32bits(f float32) uint32 { 337 return *(*uint32)(unsafe.Pointer(&f)) // ERROR "&f does not escape" 338} 339 340func Float32frombits(b uint32) float32 { 341 return *(*float32)(unsafe.Pointer(&b)) // ERROR "&b does not escape" 342} 343 344func Float64bits(f float64) uint64 { 345 return *(*uint64)(unsafe.Pointer(&f)) // ERROR "&f does not escape" 346} 347 348func Float64frombits(b uint64) float64 { 349 return *(*float64)(unsafe.Pointer(&b)) // ERROR "&b does not escape" 350} 351 352// contrast with 353func float64bitsptr(f float64) *uint64 { // ERROR "moved to heap: f" 354 return (*uint64)(unsafe.Pointer(&f)) // ERROR "&f escapes to heap" 355} 356 357func float64ptrbitsptr(f *float64) *uint64 { // ERROR "leaking param: f" 358 return (*uint64)(unsafe.Pointer(f)) 359} 360 361func typesw(i interface{}) *int { // ERROR "leaking param: i" 362 switch val := i.(type) { 363 case *int: 364 return val 365 case *int8: 366 v := int(*val) // ERROR "moved to heap: v" 367 return &v // ERROR "&v escapes to heap" 368 } 369 return nil 370} 371 372func exprsw(i *int) *int { // ERROR "leaking param: i" 373 switch j := i; *j + 110 { 374 case 12: 375 return j 376 case 42: 377 return nil 378 } 379 return nil 380 381} 382 383// assigning to an array element is like assigning to the array 384func foo60(i *int) *int { // ERROR "leaking param: i" 385 var a [12]*int 386 a[0] = i 387 return a[1] 388} 389 390func foo60a(i *int) *int { // ERROR "i does not escape" 391 var a [12]*int 392 a[0] = i 393 return nil 394} 395 396// assigning to a struct field is like assigning to the struct 397func foo61(i *int) *int { // ERROR "leaking param: i" 398 type S struct { 399 a, b *int 400 } 401 var s S 402 s.a = i 403 return s.b 404} 405 406func foo61a(i *int) *int { // ERROR "i does not escape" 407 type S struct { 408 a, b *int 409 } 410 var s S 411 s.a = i 412 return nil 413} 414 415// assigning to a struct field is like assigning to the struct but 416// here this subtlety is lost, since s.a counts as an assignment to a 417// track-losing dereference. 418func foo62(i *int) *int { // ERROR "leaking param: i" 419 type S struct { 420 a, b *int 421 } 422 s := new(S) // ERROR "new[(]S[)] does not escape" 423 s.a = i 424 return nil // s.b 425} 426 427type M interface { 428 M() 429} 430 431func foo63(m M) { // ERROR "m does not escape" 432} 433 434func foo64(m M) { // ERROR "leaking param: m" 435 m.M() 436} 437 438func foo64b(m M) { // ERROR "leaking param: m" 439 defer m.M() 440} 441 442type MV int 443 444func (MV) M() {} 445 446func foo65() { 447 var mv MV 448 foo63(&mv) // ERROR "&mv does not escape" 449} 450 451func foo66() { 452 var mv MV // ERROR "moved to heap: mv" 453 foo64(&mv) // ERROR "&mv escapes to heap" 454} 455 456func foo67() { 457 var mv MV 458 foo63(mv) 459} 460 461func foo68() { 462 var mv MV 463 foo64(mv) // escapes but it's an int so irrelevant 464} 465 466func foo69(m M) { // ERROR "leaking param: m" 467 foo64(m) 468} 469 470func foo70(mv1 *MV, m M) { // ERROR "leaking param: mv1" "leaking param: m" 471 m = mv1 472 foo64(m) 473} 474 475func foo71(x *int) []*int { // ERROR "leaking param: x" 476 var y []*int 477 y = append(y, x) 478 return y 479} 480 481func foo71a(x int) []*int { // ERROR "moved to heap: x" 482 var y []*int 483 y = append(y, &x) // ERROR "&x escapes to heap" 484 return y 485} 486 487func foo72() { 488 var x int 489 var y [1]*int 490 y[0] = &x // ERROR "&x does not escape" 491} 492 493func foo72aa() [10]*int { 494 var x int // ERROR "moved to heap: x" 495 var y [10]*int 496 y[0] = &x // ERROR "&x escapes to heap" 497 return y 498} 499 500func foo72a() { 501 var y [10]*int 502 for i := 0; i < 10; i++ { 503 // escapes its scope 504 x := i // ERROR "moved to heap: x" 505 y[i] = &x // ERROR "&x escapes to heap" 506 } 507 return 508} 509 510func foo72b() [10]*int { 511 var y [10]*int 512 for i := 0; i < 10; i++ { 513 x := i // ERROR "moved to heap: x" 514 y[i] = &x // ERROR "&x escapes to heap" 515 } 516 return y 517} 518 519// issue 2145 520func foo73() { 521 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape" 522 for _, v := range s { 523 vv := v // ERROR "moved to heap: vv" 524 // actually just escapes its scope 525 defer func() { // ERROR "func literal escapes to heap" 526 println(vv) // ERROR "&vv escapes to heap" 527 }() 528 } 529} 530 531func foo74() { 532 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape" 533 for _, v := range s { 534 vv := v // ERROR "moved to heap: vv" 535 // actually just escapes its scope 536 fn := func() { // ERROR "func literal escapes to heap" 537 println(vv) // ERROR "&vv escapes to heap" 538 } 539 defer fn() 540 } 541} 542 543// issue 3975 544func foo74b() { 545 var array [3]func() 546 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape" 547 for i, v := range s { 548 vv := v // ERROR "moved to heap: vv" 549 // actually just escapes its scope 550 array[i] = func() { // ERROR "func literal escapes to heap" 551 println(vv) // ERROR "&vv escapes to heap" 552 } 553 } 554} 555 556func myprint(y *int, x ...interface{}) *int { // ERROR "x does not escape" "leaking param: y" 557 return y 558} 559 560func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "y does not escape" "leaking param: x" 561 return &x[0] // ERROR "&x.0. escapes to heap" 562} 563 564func foo75(z *int) { // ERROR "leaking param: z" 565 myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" 566} 567 568func foo75a(z *int) { // ERROR "z does not escape" 569 myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" 570} 571 572func foo76(z *int) { // ERROR "leaking param: z" 573 myprint(nil, z) // ERROR "[.][.][.] argument does not escape" 574} 575 576func foo76a(z *int) { // ERROR "leaking param: z" 577 myprint1(nil, z) // ERROR "[.][.][.] argument escapes to heap" 578} 579 580func foo76b() { 581 myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" 582} 583 584func foo76c() { 585 myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" 586} 587 588func foo76d() { 589 defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" 590} 591 592func foo76e() { 593 defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" 594} 595 596func foo76f() { 597 for { 598 // TODO: This one really only escapes its scope, but we don't distinguish yet. 599 defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" 600 } 601} 602 603func foo76g() { 604 for { 605 defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" 606 } 607} 608 609func foo77(z []interface{}) { // ERROR "z does not escape" 610 myprint(nil, z...) // z does not escape 611} 612 613func foo77a(z []interface{}) { // ERROR "leaking param: z" 614 myprint1(nil, z...) 615} 616 617func foo78(z int) *int { // ERROR "moved to heap: z" 618 return &z // ERROR "&z escapes to heap" 619} 620 621func foo78a(z int) *int { // ERROR "moved to heap: z" 622 y := &z // ERROR "&z escapes to heap" 623 x := &y // ERROR "&y does not escape" 624 return *x // really return y 625} 626 627func foo79() *int { 628 return new(int) // ERROR "new[(]int[)] escapes to heap" 629} 630 631func foo80() *int { 632 var z *int 633 for { 634 // Really just escapes its scope but we don't distinguish 635 z = new(int) // ERROR "new[(]int[)] escapes to heap" 636 } 637 _ = z 638 return nil 639} 640 641func foo81() *int { 642 for { 643 z := new(int) // ERROR "new[(]int[)] does not escape" 644 _ = z 645 } 646 return nil 647} 648 649type Fooer interface { 650 Foo() 651} 652 653type LimitedFooer struct { 654 Fooer 655 N int64 656} 657 658func LimitFooer(r Fooer, n int64) Fooer { // ERROR "leaking param: r" 659 return &LimitedFooer{r, n} // ERROR "&LimitedFooer literal escapes to heap" 660} 661 662func foo90(x *int) map[*int]*int { // ERROR "leaking param: x" 663 return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int literal escapes to heap" 664} 665 666func foo91(x *int) map[*int]*int { // ERROR "leaking param: x" 667 return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int literal escapes to heap" 668} 669 670func foo92(x *int) [2]*int { // ERROR "leaking param: x" 671 return [2]*int{x, nil} 672} 673 674// does not leak c 675func foo93(c chan *int) *int { // ERROR "c does not escape" 676 for v := range c { 677 return v 678 } 679 return nil 680} 681 682// does not leak m 683func foo94(m map[*int]*int, b bool) *int { // ERROR "m does not escape" 684 for k, v := range m { 685 if b { 686 return k 687 } 688 return v 689 } 690 return nil 691} 692 693// does leak x 694func foo95(m map[*int]*int, x *int) { // ERROR "m does not escape" "leaking param: x" 695 m[x] = x 696} 697 698// does not leak m 699func foo96(m []*int) *int { // ERROR "m does not escape" 700 return m[0] 701} 702 703// does leak m 704func foo97(m [1]*int) *int { // ERROR "leaking param: m" 705 return m[0] 706} 707 708// does not leak m 709func foo98(m map[int]*int) *int { // ERROR "m does not escape" 710 return m[0] 711} 712 713// does leak m 714func foo99(m *[1]*int) []*int { // ERROR "leaking param: m" 715 return m[:] 716} 717 718// does not leak m 719func foo100(m []*int) *int { // ERROR "m does not escape" 720 for _, v := range m { 721 return v 722 } 723 return nil 724} 725 726// does leak m 727func foo101(m [1]*int) *int { // ERROR "leaking param: m" 728 for _, v := range m { 729 return v 730 } 731 return nil 732} 733 734// does not leak m 735func foo101a(m [1]*int) *int { // ERROR "m does not escape" 736 for i := range m { // ERROR "moved to heap: i" 737 return &i // ERROR "&i escapes to heap" 738 } 739 return nil 740} 741 742// does leak x 743func foo102(m []*int, x *int) { // ERROR "m does not escape" "leaking param: x" 744 m[0] = x 745} 746 747// does not leak x 748func foo103(m [1]*int, x *int) { // ERROR "m does not escape" "x does not escape" 749 m[0] = x 750} 751 752var y []*int 753 754// does not leak x 755func foo104(x []*int) { // ERROR "x does not escape" 756 copy(y, x) 757} 758 759// does not leak x 760func foo105(x []*int) { // ERROR "x does not escape" 761 _ = append(y, x...) 762} 763 764// does leak x 765func foo106(x *int) { // ERROR "leaking param: x" 766 _ = append(y, x) 767} 768 769func foo107(x *int) map[*int]*int { // ERROR "leaking param: x" 770 return map[*int]*int{x: nil} // ERROR "map.* literal escapes to heap" 771} 772 773func foo108(x *int) map[*int]*int { // ERROR "leaking param: x" 774 return map[*int]*int{nil: x} // ERROR "map.* literal escapes to heap" 775} 776 777func foo109(x *int) *int { // ERROR "leaking param: x" 778 m := map[*int]*int{x: nil} // ERROR "map.* literal does not escape" 779 for k, _ := range m { 780 return k 781 } 782 return nil 783} 784 785func foo110(x *int) *int { // ERROR "leaking param: x" 786 m := map[*int]*int{nil: x} // ERROR "map.* literal does not escape" 787 return m[nil] 788} 789 790func foo111(x *int) *int { // ERROR "leaking param: x" 791 m := []*int{x} // ERROR "\[\]\*int literal does not escape" 792 return m[0] 793} 794 795func foo112(x *int) *int { // ERROR "leaking param: x" 796 m := [1]*int{x} 797 return m[0] 798} 799 800func foo113(x *int) *int { // ERROR "leaking param: x" 801 m := Bar{ii: x} 802 return m.ii 803} 804 805func foo114(x *int) *int { // ERROR "leaking param: x" 806 m := &Bar{ii: x} // ERROR "&Bar literal does not escape" 807 return m.ii 808} 809 810func foo115(x *int) *int { // ERROR "leaking param: x" 811 return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1)) 812} 813 814func foo116(b bool) *int { 815 if b { 816 x := 1 // ERROR "moved to heap: x" 817 return &x // ERROR "&x escapes to heap" 818 } else { 819 y := 1 // ERROR "moved to heap: y" 820 return &y // ERROR "&y escapes to heap" 821 } 822 return nil 823} 824 825func foo117(unknown func(interface{})) { // ERROR "unknown does not escape" 826 x := 1 // ERROR "moved to heap: x" 827 unknown(&x) // ERROR "&x escapes to heap" 828} 829 830func foo118(unknown func(*int)) { // ERROR "unknown does not escape" 831 x := 1 // ERROR "moved to heap: x" 832 unknown(&x) // ERROR "&x escapes to heap" 833} 834 835func external(*int) 836 837func foo119(x *int) { // ERROR "leaking param: x" 838 external(x) 839} 840 841func foo120() { 842 // formerly exponential time analysis 843L1: 844L2: 845L3: 846L4: 847L5: 848L6: 849L7: 850L8: 851L9: 852L10: 853L11: 854L12: 855L13: 856L14: 857L15: 858L16: 859L17: 860L18: 861L19: 862L20: 863L21: 864L22: 865L23: 866L24: 867L25: 868L26: 869L27: 870L28: 871L29: 872L30: 873L31: 874L32: 875L33: 876L34: 877L35: 878L36: 879L37: 880L38: 881L39: 882L40: 883L41: 884L42: 885L43: 886L44: 887L45: 888L46: 889L47: 890L48: 891L49: 892L50: 893L51: 894L52: 895L53: 896L54: 897L55: 898L56: 899L57: 900L58: 901L59: 902L60: 903L61: 904L62: 905L63: 906L64: 907L65: 908L66: 909L67: 910L68: 911L69: 912L70: 913L71: 914L72: 915L73: 916L74: 917L75: 918L76: 919L77: 920L78: 921L79: 922L80: 923L81: 924L82: 925L83: 926L84: 927L85: 928L86: 929L87: 930L88: 931L89: 932L90: 933L91: 934L92: 935L93: 936L94: 937L95: 938L96: 939L97: 940L98: 941L99: 942L100: 943 // use the labels to silence compiler errors 944 goto L1 945 goto L2 946 goto L3 947 goto L4 948 goto L5 949 goto L6 950 goto L7 951 goto L8 952 goto L9 953 goto L10 954 goto L11 955 goto L12 956 goto L13 957 goto L14 958 goto L15 959 goto L16 960 goto L17 961 goto L18 962 goto L19 963 goto L20 964 goto L21 965 goto L22 966 goto L23 967 goto L24 968 goto L25 969 goto L26 970 goto L27 971 goto L28 972 goto L29 973 goto L30 974 goto L31 975 goto L32 976 goto L33 977 goto L34 978 goto L35 979 goto L36 980 goto L37 981 goto L38 982 goto L39 983 goto L40 984 goto L41 985 goto L42 986 goto L43 987 goto L44 988 goto L45 989 goto L46 990 goto L47 991 goto L48 992 goto L49 993 goto L50 994 goto L51 995 goto L52 996 goto L53 997 goto L54 998 goto L55 999 goto L56 1000 goto L57 1001 goto L58 1002 goto L59 1003 goto L60 1004 goto L61 1005 goto L62 1006 goto L63 1007 goto L64 1008 goto L65 1009 goto L66 1010 goto L67 1011 goto L68 1012 goto L69 1013 goto L70 1014 goto L71 1015 goto L72 1016 goto L73 1017 goto L74 1018 goto L75 1019 goto L76 1020 goto L77 1021 goto L78 1022 goto L79 1023 goto L80 1024 goto L81 1025 goto L82 1026 goto L83 1027 goto L84 1028 goto L85 1029 goto L86 1030 goto L87 1031 goto L88 1032 goto L89 1033 goto L90 1034 goto L91 1035 goto L92 1036 goto L93 1037 goto L94 1038 goto L95 1039 goto L96 1040 goto L97 1041 goto L98 1042 goto L99 1043 goto L100 1044} 1045 1046func foo121() { 1047 for i := 0; i < 10; i++ { 1048 defer myprint(nil, i) // ERROR "[.][.][.] argument escapes to heap" 1049 go myprint(nil, i) // ERROR "[.][.][.] argument escapes to heap" 1050 } 1051} 1052 1053// same as foo121 but check across import 1054func foo121b() { 1055 for i := 0; i < 10; i++ { 1056 defer fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes to heap" 1057 go fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes to heap" 1058 } 1059} 1060 1061// a harmless forward jump 1062func foo122() { 1063 var i *int 1064 1065 goto L1 1066L1: 1067 i = new(int) // ERROR "new.int. does not escape" 1068 _ = i 1069} 1070 1071// a backward jump, increases loopdepth 1072func foo123() { 1073 var i *int 1074 1075L1: 1076 i = new(int) // ERROR "new.int. escapes to heap" 1077 1078 goto L1 1079 _ = i 1080} 1081 1082func foo124(x **int) { // ERROR "x does not escape" 1083 var i int // ERROR "moved to heap: i" 1084 p := &i // ERROR "&i escapes" 1085 func() { // ERROR "func literal does not escape" 1086 *x = p // ERROR "leaking closure reference p" 1087 }() 1088} 1089 1090func foo125(ch chan *int) { // ERROR "does not escape" 1091 var i int // ERROR "moved to heap" 1092 p := &i // ERROR "&i escapes to heap" 1093 func() { // ERROR "func literal does not escape" 1094 ch <- p // ERROR "leaking closure reference p" 1095 }() 1096} 1097 1098func foo126() { 1099 var px *int // loopdepth 0 1100 for { 1101 // loopdepth 1 1102 var i int // ERROR "moved to heap" 1103 func() { // ERROR "func literal does not escape" 1104 px = &i // ERROR "&i escapes" 1105 }() 1106 } 1107} 1108 1109var px *int 1110 1111func foo127() { 1112 var i int // ERROR "moved to heap: i" 1113 p := &i // ERROR "&i escapes to heap" 1114 q := p 1115 px = q 1116} 1117 1118func foo128() { 1119 var i int 1120 p := &i // ERROR "&i does not escape" 1121 q := p 1122 _ = q 1123} 1124 1125func foo129() { 1126 var i int // ERROR "moved to heap: i" 1127 p := &i // ERROR "&i escapes to heap" 1128 func() { // ERROR "func literal does not escape" 1129 q := p // ERROR "leaking closure reference p" 1130 func() { // ERROR "func literal does not escape" 1131 r := q // ERROR "leaking closure reference q" 1132 px = r 1133 }() 1134 }() 1135} 1136 1137func foo130() { 1138 for { 1139 var i int // ERROR "moved to heap" 1140 func() { // ERROR "func literal does not escape" 1141 px = &i // ERROR "&i escapes" "leaking closure reference i" 1142 }() 1143 } 1144} 1145 1146func foo131() { 1147 var i int // ERROR "moved to heap" 1148 func() { // ERROR "func literal does not escape" 1149 px = &i // ERROR "&i escapes" "leaking closure reference i" 1150 }() 1151} 1152 1153func foo132() { 1154 var i int // ERROR "moved to heap" 1155 go func() { // ERROR "func literal escapes to heap" 1156 px = &i // ERROR "&i escapes" "leaking closure reference i" 1157 }() 1158} 1159 1160func foo133() { 1161 var i int // ERROR "moved to heap" 1162 defer func() { // ERROR "func literal does not escape" 1163 px = &i // ERROR "&i escapes" "leaking closure reference i" 1164 }() 1165} 1166 1167func foo134() { 1168 var i int 1169 p := &i // ERROR "&i does not escape" 1170 func() { // ERROR "func literal does not escape" 1171 q := p 1172 func() { // ERROR "func literal does not escape" 1173 r := q 1174 _ = r 1175 }() 1176 }() 1177} 1178 1179func foo135() { 1180 var i int // ERROR "moved to heap: i" 1181 p := &i // ERROR "&i escapes to heap" "moved to heap: p" 1182 go func() { // ERROR "func literal escapes to heap" 1183 q := p // ERROR "&p escapes to heap" 1184 func() { // ERROR "func literal does not escape" 1185 r := q 1186 _ = r 1187 }() 1188 }() 1189} 1190 1191func foo136() { 1192 var i int // ERROR "moved to heap: i" 1193 p := &i // ERROR "&i escapes to heap" "moved to heap: p" 1194 go func() { // ERROR "func literal escapes to heap" 1195 q := p // ERROR "&p escapes to heap" "leaking closure reference p" 1196 func() { // ERROR "func literal does not escape" 1197 r := q // ERROR "leaking closure reference q" 1198 px = r 1199 }() 1200 }() 1201} 1202 1203func foo137() { 1204 var i int // ERROR "moved to heap: i" 1205 p := &i // ERROR "&i escapes to heap" 1206 func() { // ERROR "func literal does not escape" 1207 q := p // ERROR "leaking closure reference p" "moved to heap: q" 1208 go func() { // ERROR "func literal escapes to heap" 1209 r := q // ERROR "&q escapes to heap" 1210 _ = r 1211 }() 1212 }() 1213} 1214 1215func foo138() *byte { 1216 type T struct { 1217 x [1]byte 1218 } 1219 t := new(T) // ERROR "new.T. escapes to heap" 1220 return &t.x[0] // ERROR "&t.x.0. escapes to heap" 1221} 1222 1223func foo139() *byte { 1224 type T struct { 1225 x struct { 1226 y byte 1227 } 1228 } 1229 t := new(T) // ERROR "new.T. escapes to heap" 1230 return &t.x.y // ERROR "&t.x.y escapes to heap" 1231} 1232