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.
7 // Test, using compiler diagnostic flags, that the escape analysis is working.
8 // Compiles but does not run. Inlining is disabled.
10 // escape2n.go contains all the same tests but compiles with -N.
21 func foo1(x int) { // ERROR "moved to heap: x$"
22 gxx = &x // ERROR "&x escapes to heap$"
25 func foo2(yy *int) { // ERROR "leaking param: yy$"
29 func foo3(x int) *int { // ERROR "moved to heap: x$"
30 return &x // ERROR "&x escapes to heap$"
35 func foo3b(t T) { // ERROR "leaking param: t$"
39 // xx isn't going anywhere, so use of yy is ok
40 func foo4(xx, yy *int) { // ERROR "foo4 xx does not escape$" "foo4 yy does not escape$"
44 // xx isn't going anywhere, so taking address of yy is ok
45 func foo5(xx **int, yy *int) { // ERROR "foo5 xx does not escape$" "foo5 yy does not escape$"
46 xx = &yy // ERROR "foo5 &yy does not escape$"
49 func foo6(xx **int, yy *int) { // ERROR "foo6 xx does not escape$" "leaking param: yy$"
53 func foo7(xx **int, yy *int) { // ERROR "foo7 xx does not escape$" "foo7 yy does not escape$"
57 func foo8(xx, yy *int) int { // ERROR "foo8 xx does not escape$" "foo8 yy does not escape$"
62 func foo9(xx, yy *int) *int { // ERROR "leaking param: xx to result ~r2 level=0$" "leaking param: yy to result ~r2 level=0$"
67 func foo10(xx, yy *int) { // ERROR "foo10 xx does not escape$" "foo10 yy does not escape$"
73 xx := &x // ERROR "foo11 &x does not escape$"
74 yy := &y // ERROR "foo11 &y does not escape$"
81 func foo12(yyy **int) { // ERROR "leaking param: yyy$"
85 // Must treat yyy as leaking because *yyy leaks, and the escape analysis
86 // summaries in exported metadata do not distinguish these two cases.
87 func foo13(yyy **int) { // ERROR "leaking param content: yyy$"
91 func foo14(yyy **int) { // ERROR "foo14 yyy does not escape$"
95 func foo15(yy *int) { // ERROR "moved to heap: yy$"
96 xxx = &yy // ERROR "&yy escapes to heap$"
99 func foo16(yy *int) { // ERROR "leaking param: yy$"
103 func foo17(yy *int) { // ERROR "foo17 yy does not escape$"
107 func foo18(y int) { // ERROR "moved to heap: y$"
108 *xxx = &y // ERROR "&y escapes to heap$"
121 return &Bar{42, nil} // ERROR "&Bar literal escapes to heap$"
124 func NewBarp(x *int) *Bar { // ERROR "leaking param: x to result ~r1 level=-1$"
125 return &Bar{42, x} // ERROR "&Bar literal escapes to heap$"
128 func NewBarp2(x *int) *Bar { // ERROR "NewBarp2 x does not escape$"
129 return &Bar{*x, nil} // ERROR "&Bar literal escapes to heap$"
132 func (b *Bar) NoLeak() int { // ERROR "\(\*Bar\).NoLeak b does not escape$"
136 func (b *Bar) Leak() *int { // ERROR "leaking param: b to result ~r0 level=0$"
137 return &b.i // ERROR "&b.i escapes to heap$"
140 func (b *Bar) AlsoNoLeak() *int { // ERROR "leaking param: b to result ~r0 level=1$"
144 func (b Bar) AlsoLeak() *int { // ERROR "leaking param: b to result ~r0 level=0$"
148 func (b Bar) LeaksToo() *int { // ERROR "leaking param: b to result ~r0 level=0$"
149 v := 0 // ERROR "moved to heap: v$"
150 b.ii = &v // ERROR "&v escapes to heap$"
154 func (b *Bar) LeaksABit() *int { // ERROR "leaking param: b to result ~r0 level=1$"
155 v := 0 // ERROR "moved to heap: v$"
156 b.ii = &v // ERROR "&v escapes to heap$"
160 func (b Bar) StillNoLeak() int { // ERROR "Bar.StillNoLeak b does not escape$"
162 b.ii = &v // ERROR "Bar.StillNoLeak &v does not escape$"
166 func goLeak(b *Bar) { // ERROR "leaking param: b$"
175 func NewBar2() *Bar2 {
176 return &Bar2{[12]int{42}, nil} // ERROR "&Bar2 literal escapes to heap$"
179 func (b *Bar2) NoLeak() int { // ERROR "\(\*Bar2\).NoLeak b does not escape$"
183 func (b *Bar2) Leak() []int { // ERROR "leaking param: b to result ~r0 level=0$"
184 return b.i[:] // ERROR "b.i escapes to heap$"
187 func (b *Bar2) AlsoNoLeak() []int { // ERROR "leaking param: b to result ~r0 level=1$"
191 func (b Bar2) AgainNoLeak() [12]int { // ERROR "Bar2.AgainNoLeak b does not escape$"
195 func (b *Bar2) LeakSelf() { // ERROR "leaking param: b$"
196 b.ii = b.i[0:4] // ERROR "b.i escapes to heap$"
199 func (b *Bar2) LeakSelf2() { // ERROR "leaking param: b$"
201 buf = b.i[0:] // ERROR "b.i escapes to heap$"
205 func foo21() func() int {
207 return func() int { // ERROR "func literal escapes to heap$"
212 func foo21a() func() int {
213 x := 42 // ERROR "moved to heap: x$"
214 return func() int { // ERROR "func literal escapes to heap$"
215 x++ // ERROR "&x escapes to heap$"
222 return func() int { // ERROR "foo22 func literal does not escape$"
227 func foo23(x int) func() int {
228 return func() int { // ERROR "func literal escapes to heap$"
233 func foo23a(x int) func() int {
234 f := func() int { // ERROR "func literal escapes to heap$"
240 func foo23b(x int) *(func() int) {
241 f := func() int { return x } // ERROR "func literal escapes to heap$" "moved to heap: f$"
242 return &f // ERROR "&f escapes to heap$"
245 func foo23c(x int) func() int { // ERROR "moved to heap: x$"
246 return func() int { // ERROR "func literal escapes to heap$"
247 x++ // ERROR "&x escapes to heap$"
252 func foo24(x int) int {
253 return func() int { // ERROR "foo24 func literal does not escape$"
260 func fooleak(xx *int) int { // ERROR "leaking param: xx$"
265 func foonoleak(xx *int) int { // ERROR "foonoleak xx does not escape$"
269 func foo31(x int) int { // ERROR "moved to heap: x$"
270 return fooleak(&x) // ERROR "&x escapes to heap$"
273 func foo32(x int) int {
274 return foonoleak(&x) // ERROR "foo32 &x does not escape$"
285 func (f *Foo) fooleak() { // ERROR "leaking param: f$"
289 func (f *Foo) foonoleak() { // ERROR "\(\*Foo\).foonoleak f does not escape$"
293 func (f *Foo) Leak() { // ERROR "leaking param: f$"
297 func (f *Foo) NoLeak() { // ERROR "\(\*Foo\).NoLeak f does not escape$"
301 func foo41(x int) { // ERROR "moved to heap: x$"
302 F.xx = &x // ERROR "&x escapes to heap$"
305 func (f *Foo) foo42(x int) { // ERROR "\(\*Foo\).foo42 f does not escape$" "moved to heap: x$"
306 f.xx = &x // ERROR "&x escapes to heap$"
309 func foo43(f *Foo, x int) { // ERROR "foo43 f does not escape$" "moved to heap: x$"
310 f.xx = &x // ERROR "&x escapes to heap$"
313 func foo44(yy *int) { // ERROR "leaking param: yy$"
317 func (f *Foo) foo45() { // ERROR "\(\*Foo\).foo45 f does not escape$"
321 // See foo13 above for explanation of why f leaks.
322 func (f *Foo) foo46() { // ERROR "leaking param content: f$"
326 func (f *Foo) foo47() { // ERROR "leaking param: f$"
327 f.xx = &f.x // ERROR "&f.x escapes to heap$"
332 func foo50(i *int) { // ERROR "leaking param: i$"
336 var ptrMap map[*int]*int
338 func foo51(i *int) { // ERROR "leaking param: i$"
342 func indaddr1(x int) *int { // ERROR "moved to heap: x$"
343 return &x // ERROR "&x escapes to heap$"
346 func indaddr2(x *int) *int { // ERROR "leaking param: x to result ~r1 level=0$"
347 return *&x // ERROR "indaddr2 &x does not escape$"
350 func indaddr3(x *int32) *int { // ERROR "leaking param: x to result ~r1 level=0$"
351 return *(**int)(unsafe.Pointer(&x)) // ERROR "indaddr3 &x does not escape$"
354 // From package math:
356 func Float32bits(f float32) uint32 {
357 return *(*uint32)(unsafe.Pointer(&f)) // ERROR "Float32bits &f does not escape$"
360 func Float32frombits(b uint32) float32 {
361 return *(*float32)(unsafe.Pointer(&b)) // ERROR "Float32frombits &b does not escape$"
364 func Float64bits(f float64) uint64 {
365 return *(*uint64)(unsafe.Pointer(&f)) // ERROR "Float64bits &f does not escape$"
368 func Float64frombits(b uint64) float64 {
369 return *(*float64)(unsafe.Pointer(&b)) // ERROR "Float64frombits &b does not escape$"
373 func float64bitsptr(f float64) *uint64 { // ERROR "moved to heap: f$"
374 return (*uint64)(unsafe.Pointer(&f)) // ERROR "&f escapes to heap$"
377 func float64ptrbitsptr(f *float64) *uint64 { // ERROR "leaking param: f to result ~r1 level=0$"
378 return (*uint64)(unsafe.Pointer(f))
381 func typesw(i interface{}) *int { // ERROR "leaking param: i to result ~r1 level=0$"
382 switch val := i.(type) {
386 v := int(*val) // ERROR "moved to heap: v$"
387 return &v // ERROR "&v escapes to heap$"
392 func exprsw(i *int) *int { // ERROR "leaking param: i to result ~r1 level=0$"
393 switch j := i; *j + 110 {
403 // assigning to an array element is like assigning to the array
404 func foo60(i *int) *int { // ERROR "leaking param: i to result ~r1 level=0$"
410 func foo60a(i *int) *int { // ERROR "foo60a i does not escape$"
416 // assigning to a struct field is like assigning to the struct
417 func foo61(i *int) *int { // ERROR "leaking param: i to result ~r1 level=0$"
426 func foo61a(i *int) *int { // ERROR "foo61a i does not escape$"
435 // assigning to a struct field is like assigning to the struct but
436 // here this subtlety is lost, since s.a counts as an assignment to a
437 // track-losing dereference.
438 func foo62(i *int) *int { // ERROR "leaking param: i$"
442 s := new(S) // ERROR "foo62 new\(S\) does not escape$"
451 func foo63(m M) { // ERROR "foo63 m does not escape$"
454 func foo64(m M) { // ERROR "leaking param: m$"
458 func foo64b(m M) { // ERROR "leaking param: m$"
468 foo63(&mv) // ERROR "foo65 &mv does not escape$"
472 var mv MV // ERROR "moved to heap: mv$"
473 foo64(&mv) // ERROR "&mv escapes to heap$"
478 foo63(mv) // ERROR "foo67 mv does not escape$"
483 // escapes but it's an int so irrelevant
484 foo64(mv) // ERROR "mv escapes to heap$"
487 func foo69(m M) { // ERROR "leaking param: m$"
491 func foo70(mv1 *MV, m M) { // ERROR "leaking param: m$" "leaking param: mv1$"
492 m = mv1 // ERROR "mv1 escapes to heap$"
496 func foo71(x *int) []*int { // ERROR "leaking param: x$"
502 func foo71a(x int) []*int { // ERROR "moved to heap: x$"
504 y = append(y, &x) // ERROR "&x escapes to heap$"
511 y[0] = &x // ERROR "foo72 &x does not escape$"
514 func foo72aa() [10]*int {
515 var x int // ERROR "moved to heap: x$"
517 y[0] = &x // ERROR "&x escapes to heap$"
523 for i := 0; i < 10; i++ {
525 x := i // ERROR "moved to heap: x$"
526 y[i] = &x // ERROR "&x escapes to heap$"
531 func foo72b() [10]*int {
533 for i := 0; i < 10; i++ {
534 x := i // ERROR "moved to heap: x$"
535 y[i] = &x // ERROR "&x escapes to heap$"
542 s := []int{3, 2, 1} // ERROR "foo73 \[\]int literal does not escape$"
543 for _, v := range s {
545 // actually just escapes its scope
546 defer func() { // ERROR "func literal escapes to heap$"
553 s := []int{3, 2, 1} // ERROR "foo731 \[\]int literal does not escape$"
554 for _, v := range s {
555 vv := v // ERROR "moved to heap: vv$"
556 // actually just escapes its scope
557 defer func() { // ERROR "func literal escapes to heap$"
558 vv = 42 // ERROR "&vv escapes to heap$"
565 s := []int{3, 2, 1} // ERROR "foo74 \[\]int literal does not escape$"
566 for _, v := range s {
568 // actually just escapes its scope
569 fn := func() { // ERROR "func literal escapes to heap$"
577 s := []int{3, 2, 1} // ERROR "foo74a \[\]int literal does not escape$"
578 for _, v := range s {
579 vv := v // ERROR "moved to heap: vv$"
580 // actually just escapes its scope
581 fn := func() { // ERROR "func literal escapes to heap$"
582 vv += 1 // ERROR "&vv escapes to heap$"
592 s := []int{3, 2, 1} // ERROR "foo74b \[\]int literal does not escape$"
593 for i, v := range s {
595 // actually just escapes its scope
596 array[i] = func() { // ERROR "func literal escapes to heap$"
604 s := []int{3, 2, 1} // ERROR "foo74c \[\]int literal does not escape$"
605 for i, v := range s {
606 vv := v // ERROR "moved to heap: vv$"
607 // actually just escapes its scope
608 array[i] = func() { // ERROR "func literal escapes to heap$"
609 println(&vv) // ERROR "&vv escapes to heap$" "<S> &vv does not escape$"
614 func myprint(y *int, x ...interface{}) *int { // ERROR "leaking param: y to result ~r2 level=0$" "myprint x does not escape$"
618 func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "leaking param: x to result ~r2 level=0$" "myprint1 y does not escape$"
619 return &x[0] // ERROR "&x\[0\] escapes to heap$"
622 func foo75(z *int) { // ERROR "foo75 z does not escape$"
623 myprint(z, 1, 2, 3) // ERROR "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" "foo75 ... argument does not escape$"
626 func foo75a(z *int) { // ERROR "foo75a z does not escape$"
627 myprint1(z, 1, 2, 3) // ERROR "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" "foo75a ... argument does not escape$"
630 func foo75esc(z *int) { // ERROR "leaking param: z$"
631 gxx = myprint(z, 1, 2, 3) // ERROR "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" "foo75esc ... argument does not escape$"
634 func foo75aesc(z *int) { // ERROR "foo75aesc z does not escape$"
635 var ppi **interface{} // assignments to pointer dereferences lose track
636 *ppi = myprint1(z, 1, 2, 3) // ERROR "... argument escapes to heap$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$"
639 func foo75aesc1(z *int) { // ERROR "foo75aesc1 z does not escape$"
640 sink = myprint1(z, 1, 2, 3) // ERROR "... argument escapes to heap$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" "myprint1\(z, 1, 2, 3\) escapes to heap$"
643 // BAD: z does not escape here
644 func foo76(z *int) { // ERROR "leaking param: z$"
645 myprint(nil, z) // ERROR "foo76 ... argument does not escape$" "z escapes to heap$"
648 // BAD: z does not escape here
649 func foo76a(z *int) { // ERROR "leaking param: z$"
650 myprint1(nil, z) // ERROR "foo76a ... argument does not escape$" "z escapes to heap$"
654 myprint(nil, 1, 2, 3) // ERROR "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" "foo76b ... argument does not escape$"
658 myprint1(nil, 1, 2, 3) // ERROR "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" "foo76c ... argument does not escape$"
662 defer myprint(nil, 1, 2, 3) // ERROR "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" "foo76d ... argument does not escape$"
666 defer myprint1(nil, 1, 2, 3) // ERROR "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$" "foo76e ... argument does not escape$"
671 // TODO: This one really only escapes its scope, but we don't distinguish yet.
672 defer myprint(nil, 1, 2, 3) // ERROR "... argument escapes to heap$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$"
678 defer myprint1(nil, 1, 2, 3) // ERROR "... argument escapes to heap$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$"
682 func foo77(z []interface{}) { // ERROR "foo77 z does not escape$"
683 myprint(nil, z...) // z does not escape
686 func foo77a(z []interface{}) { // ERROR "foo77a z does not escape$"
690 func foo77b(z []interface{}) { // ERROR "leaking param: z$"
691 var ppi **interface{}
692 *ppi = myprint1(nil, z...)
695 func foo77c(z []interface{}) { // ERROR "leaking param: z$"
696 sink = myprint1(nil, z...) // ERROR "myprint1\(nil, z...\) escapes to heap$"
700 // BAD: i should not escape here
701 i := 0 // ERROR "moved to heap: i$"
702 myprint(nil, &i) // ERROR "&i escapes to heap$" "dotdotdot ... argument does not escape$"
704 // BAD: j should not escape here
705 j := 0 // ERROR "moved to heap: j$"
706 myprint1(nil, &j) // ERROR "&j escapes to heap$" "dotdotdot ... argument does not escape$"
709 func foo78(z int) *int { // ERROR "moved to heap: z$"
710 return &z // ERROR "&z escapes to heap$"
713 func foo78a(z int) *int { // ERROR "moved to heap: z$"
714 y := &z // ERROR "&z escapes to heap$"
715 x := &y // ERROR "foo78a &y does not escape$"
716 return *x // really return y
720 return new(int) // ERROR "new\(int\) escapes to heap$"
726 // Really just escapes its scope but we don't distinguish
727 z = new(int) // ERROR "new\(int\) escapes to heap$"
735 z := new(int) // ERROR "foo81 new\(int\) does not escape$"
741 func tee(p *int) (x, y *int) { return p, p } // ERROR "leaking param: p to result x level=0$" "leaking param: p to result y level=0$"
743 func noop(x, y *int) {} // ERROR "noop x does not escape$" "noop y does not escape$"
746 var x, y, z int // ERROR "moved to heap: x$" "moved to heap: y$" "moved to heap: z$"
747 go noop(tee(&z)) // ERROR "&z escapes to heap$"
748 go noop(&x, &y) // ERROR "&x escapes to heap$" "&y escapes to heap$"
750 var u, v, w int // ERROR "moved to heap: u$" "moved to heap: v$" "moved to heap: w$"
751 defer noop(tee(&u)) // ERROR "&u escapes to heap$"
752 defer noop(&v, &w) // ERROR "&v escapes to heap$" "&w escapes to heap$"
756 type Fooer interface {
760 type LimitedFooer struct {
765 func LimitFooer(r Fooer, n int64) Fooer { // ERROR "leaking param: r to result ~r2 level=-1$"
766 return &LimitedFooer{r, n} // ERROR "&LimitedFooer literal escapes to heap$"
769 func foo90(x *int) map[*int]*int { // ERROR "leaking param: x$"
770 return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int literal escapes to heap$"
773 func foo91(x *int) map[*int]*int { // ERROR "leaking param: x$"
774 return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int literal escapes to heap$"
777 func foo92(x *int) [2]*int { // ERROR "leaking param: x to result ~r1 level=0$"
778 return [2]*int{x, nil}
782 func foo93(c chan *int) *int { // ERROR "foo93 c does not escape$"
790 func foo94(m map[*int]*int, b bool) *int { // ERROR "leaking param: m to result ~r2 level=1"
791 for k, v := range m {
801 func foo95(m map[*int]*int, x *int) { // ERROR "foo95 m does not escape$" "leaking param: x$"
805 // does not leak m but does leak content
806 func foo96(m []*int) *int { // ERROR "leaking param: m to result ~r1 level=1"
811 func foo97(m [1]*int) *int { // ERROR "leaking param: m to result ~r1 level=0$"
816 func foo98(m map[int]*int) *int { // ERROR "foo98 m does not escape$"
821 func foo99(m *[1]*int) []*int { // ERROR "leaking param: m to result ~r1 level=0$"
826 func foo100(m []*int) *int { // ERROR "leaking param: m to result ~r1 level=1"
827 for _, v := range m {
834 func foo101(m [1]*int) *int { // ERROR "leaking param: m to result ~r1 level=0$"
835 for _, v := range m {
842 func foo101a(m [1]*int) *int { // ERROR "foo101a m does not escape$"
843 for i := range m { // ERROR "moved to heap: i$"
844 return &i // ERROR "&i escapes to heap$"
850 func foo102(m []*int, x *int) { // ERROR "foo102 m does not escape$" "leaking param: x$"
855 func foo103(m [1]*int, x *int) { // ERROR "foo103 m does not escape$" "foo103 x does not escape$"
862 func foo104(x []*int) { // ERROR "foo104 x does not escape$"
866 // does not leak x but does leak content
867 func foo105(x []*int) { // ERROR "leaking param content: x"
872 func foo106(x *int) { // ERROR "leaking param: x$"
876 func foo107(x *int) map[*int]*int { // ERROR "leaking param: x$"
877 return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int literal escapes to heap$"
880 func foo108(x *int) map[*int]*int { // ERROR "leaking param: x$"
881 return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int literal escapes to heap$"
884 func foo109(x *int) *int { // ERROR "leaking param: x$"
885 m := map[*int]*int{x: nil} // ERROR "foo109 map\[\*int\]\*int literal does not escape$"
886 for k, _ := range m {
892 func foo110(x *int) *int { // ERROR "leaking param: x$"
893 m := map[*int]*int{nil: x} // ERROR "foo110 map\[\*int\]\*int literal does not escape$"
897 func foo111(x *int) *int { // ERROR "leaking param: x to result ~r1 level=0"
898 m := []*int{x} // ERROR "foo111 \[\]\*int literal does not escape$"
902 func foo112(x *int) *int { // ERROR "leaking param: x to result ~r1 level=0$"
907 func foo113(x *int) *int { // ERROR "leaking param: x to result ~r1 level=0$"
912 func foo114(x *int) *int { // ERROR "leaking param: x to result ~r1 level=0$"
913 m := &Bar{ii: x} // ERROR "foo114 &Bar literal does not escape$"
917 func foo115(x *int) *int { // ERROR "leaking param: x to result ~r1 level=0$"
918 return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1))
921 func foo116(b bool) *int {
923 x := 1 // ERROR "moved to heap: x$"
924 return &x // ERROR "&x escapes to heap$"
926 y := 1 // ERROR "moved to heap: y$"
927 return &y // ERROR "&y escapes to heap$"
932 func foo117(unknown func(interface{})) { // ERROR "foo117 unknown does not escape$"
933 x := 1 // ERROR "moved to heap: x$"
934 unknown(&x) // ERROR "&x escapes to heap$"
937 func foo118(unknown func(*int)) { // ERROR "foo118 unknown does not escape$"
938 x := 1 // ERROR "moved to heap: x$"
939 unknown(&x) // ERROR "&x escapes to heap$"
944 func foo119(x *int) { // ERROR "leaking param: x$"
949 // formerly exponential time analysis
1050 // use the labels to silence compiler errors
1154 for i := 0; i < 10; i++ {
1155 defer myprint(nil, i) // ERROR "... argument escapes to heap$" "i escapes to heap$"
1156 go myprint(nil, i) // ERROR "... argument escapes to heap$" "i escapes to heap$"
1160 // same as foo121 but check across import
1162 for i := 0; i < 10; i++ {
1163 defer fmt.Printf("%d", i) // ERROR "... argument escapes to heap$" "i escapes to heap$"
1164 go fmt.Printf("%d", i) // ERROR "... argument escapes to heap$" "i escapes to heap$"
1168 // a harmless forward jump
1174 i = new(int) // ERROR "foo122 new\(int\) does not escape$"
1178 // a backward jump, increases loopdepth
1183 i = new(int) // ERROR "new\(int\) escapes to heap$"
1189 func foo124(x **int) { // ERROR "foo124 x does not escape$"
1190 var i int // ERROR "moved to heap: i$"
1191 p := &i // ERROR "&i escapes to heap$"
1192 func() { // ERROR "foo124 func literal does not escape$"
1193 *x = p // ERROR "leaking closure reference p$"
1197 func foo125(ch chan *int) { // ERROR "foo125 ch does not escape$"
1198 var i int // ERROR "moved to heap: i$"
1199 p := &i // ERROR "&i escapes to heap$"
1200 func() { // ERROR "foo125 func literal does not escape$"
1201 ch <- p // ERROR "leaking closure reference p$"
1206 var px *int // loopdepth 0
1209 var i int // ERROR "moved to heap: i$"
1210 func() { // ERROR "foo126 func literal does not escape$"
1211 px = &i // ERROR "&i escapes to heap$"
1220 var i int // ERROR "moved to heap: i$"
1221 p := &i // ERROR "&i escapes to heap$"
1228 p := &i // ERROR "foo128 &i does not escape$"
1234 var i int // ERROR "moved to heap: i$"
1235 p := &i // ERROR "&i escapes to heap$"
1236 func() { // ERROR "foo129 func literal does not escape$"
1237 q := p // ERROR "leaking closure reference p$"
1238 func() { // ERROR "<S> func literal does not escape$"
1239 r := q // ERROR "leaking closure reference q$"
1247 var i int // ERROR "moved to heap: i$"
1248 func() { // ERROR "foo130 func literal does not escape$"
1249 px = &i // ERROR "&i escapes to heap$" "leaking closure reference i$"
1255 var i int // ERROR "moved to heap: i$"
1256 func() { // ERROR "foo131 func literal does not escape$"
1257 px = &i // ERROR "&i escapes to heap$" "leaking closure reference i$"
1262 var i int // ERROR "moved to heap: i$"
1263 go func() { // ERROR "func literal escapes to heap$"
1264 px = &i // ERROR "&i escapes to heap$" "leaking closure reference i$"
1269 var i int // ERROR "moved to heap: i$"
1270 defer func() { // ERROR "foo133 func literal does not escape$"
1271 px = &i // ERROR "&i escapes to heap$" "leaking closure reference i$"
1277 p := &i // ERROR "foo134 &i does not escape$"
1278 func() { // ERROR "foo134 func literal does not escape$"
1280 func() { // ERROR "<S> func literal does not escape$"
1288 var i int // ERROR "moved to heap: i$"
1289 p := &i // ERROR "&i escapes to heap$"
1290 go func() { // ERROR "func literal escapes to heap$"
1292 func() { // ERROR "<S> func literal does not escape$"
1300 var i int // ERROR "moved to heap: i$"
1301 p := &i // ERROR "&i escapes to heap$"
1302 go func() { // ERROR "func literal escapes to heap$"
1303 q := p // ERROR "leaking closure reference p$"
1304 func() { // ERROR "<S> func literal does not escape$"
1305 r := q // ERROR "leaking closure reference q$"
1312 var i int // ERROR "moved to heap: i$"
1313 p := &i // ERROR "&i escapes to heap$"
1314 func() { // ERROR "foo137 func literal does not escape$"
1315 q := p // ERROR "leaking closure reference p$"
1316 go func() { // ERROR "func literal escapes to heap$"
1323 func foo138() *byte {
1327 t := new(T) // ERROR "new\(T\) escapes to heap$"
1328 return &t.x[0] // ERROR "&t.x\[0\] escapes to heap$"
1331 func foo139() *byte {
1337 t := new(T) // ERROR "new\(T\) escapes to heap$"
1338 return &t.x.y // ERROR "&t.x.y escapes to heap$"
1342 func foo140() interface{} {
1350 t := &T{} // ERROR "&T literal escapes to heap$"
1351 return U{ // ERROR "U literal escapes to heap$"
1365 func F3(x []byte) // ERROR "F3 x does not escape$"
1371 F1(buf1[:]) // ERROR "G buf1 does not escape$"
1373 var buf2 [10]byte // ERROR "moved to heap: buf2$"
1374 F2(buf2[:]) // ERROR "buf2 escapes to heap$"
1377 F3(buf3[:]) // ERROR "G buf3 does not escape$"
1379 var buf4 [10]byte // ERROR "moved to heap: buf4$"
1380 F4(buf4[:]) // ERROR "buf4 escapes to heap$"
1387 func (t *Tm) M() { // ERROR "\(\*Tm\).M t does not escape$"
1393 t := new(Tm) // ERROR "new\(Tm\) escapes to heap$"
1394 f = t.M // ERROR "foo141 t.M does not escape$"
1401 t := new(Tm) // ERROR "new\(Tm\) escapes to heap$"
1402 gf = t.M // ERROR "t.M escapes to heap$"
1407 for i := 0; i < 1000; i++ {
1408 func() { // ERROR "foo143 func literal does not escape$"
1409 for i := 0; i < 1; i++ {
1411 t.M() // ERROR "<S> t does not escape$"
1418 // Check that annotations take effect regardless of whether they
1419 // are before or after the use in the source code.
1427 foo144a(&x) // ERROR "foo144 &x does not escape$"
1429 foo144b(&y) // ERROR "foo144 &y does not escape$"
1436 // issue 7313: for loop init should not be treated as "in loop"
1442 func foo145(l List) { // ERROR "foo145 l does not escape$"
1444 for p = &l; p.Next != nil; p = p.Next { // ERROR "foo145 &l does not escape$"
1448 func foo146(l List) { // ERROR "foo146 l does not escape$"
1450 p = &l // ERROR "foo146 &l does not escape$"
1451 for ; p.Next != nil; p = p.Next {
1455 func foo147(l List) { // ERROR "foo147 l does not escape$"
1457 p = &l // ERROR "foo147 &l does not escape$"
1463 func foo148(l List) { // ERROR "foo148 l does not escape$"
1464 for p := &l; p.Next != nil; p = p.Next { // ERROR "foo148 &l does not escape$"
1468 // related: address of variable should have depth of variable, not of loop
1470 func foo149(l List) { // ERROR "foo149 l does not escape$"
1473 for p = &l; p.Next != nil; p = p.Next { // ERROR "foo149 &l does not escape$"
1478 // issue 7934: missed ... if element type had no pointers
1482 func foo150(x ...byte) { // ERROR "leaking param: x$"
1487 foo150(1, 2, 3) // ERROR "... argument escapes to heap$"
1490 // issue 7931: bad handling of slice of array
1494 func foo151(x *int) { // ERROR "leaking param: x$"
1499 var a [64]int // ERROR "moved to heap: a$"
1501 foo151(&(&a)[4:8][0]) // ERROR "&\(&a\)\[4:8\]\[0\] escapes to heap$" "&a escapes to heap$"
1505 var a [10]int // ERROR "moved to heap: a$"
1506 b := a[:] // ERROR "a escapes to heap$"
1507 foo151(&b[4:8][0]) // ERROR "&b\[4:8\]\[0\] escapes to heap$"
1511 var a [64]int // ERROR "moved to heap: a$"
1513 foo151(&(&a)[4:8:8][0]) // ERROR "&\(&a\)\[4:8:8\]\[0\] escapes to heap$" "&a escapes to heap$"
1517 var a [10]int // ERROR "moved to heap: a$"
1518 b := a[:] // ERROR "a escapes to heap$"
1519 foo151(&b[4:8:8][0]) // ERROR "&b\[4:8:8\]\[0\] escapes to heap$"
1528 func (u *U) String() *string { // ERROR "leaking param: u to result ~r0 level=1$"
1536 // BAD -- level of leak ought to be 0
1537 func NewV(u U) *V { // ERROR "leaking param: u to result ~r1 level=-1"
1538 return &V{u.String()} // ERROR "&V literal escapes to heap$" "NewV u does not escape"
1542 a := "a" // ERROR "moved to heap: a$"
1543 u := U{&a} // ERROR "&a escapes to heap$"
1548 // issue 8176 - &x in type switch body not marked as escaping
1550 func foo153(v interface{}) *int { // ERROR "leaking param: v to result ~r1 level=-1$"
1551 switch x := v.(type) {
1552 case int: // ERROR "moved to heap: x$"
1553 return &x // ERROR "&x escapes to heap$"
1558 // issue 8185 - &result escaping into result
1560 func f() (x int, y *int) { // ERROR "moved to heap: x$"
1561 y = &x // ERROR "&x escapes to heap$"
1565 func g() (x interface{}) { // ERROR "moved to heap: x$"
1566 x = &x // ERROR "&x escapes to heap$"
1570 var sink interface{}
1576 func ptrlitNoescape() {
1577 // Both literal and element do not escape.
1579 x := &Lit{&i} // ERROR "ptrlitNoescape &Lit literal does not escape$" "ptrlitNoescape &i does not escape$"
1583 func ptrlitNoEscape2() {
1584 // Literal does not escape, but element does.
1585 i := 0 // ERROR "moved to heap: i$"
1586 x := &Lit{&i} // ERROR "&i escapes to heap$" "ptrlitNoEscape2 &Lit literal does not escape$"
1587 sink = *x // ERROR "\*x escapes to heap$"
1590 func ptrlitEscape() {
1591 // Both literal and element escape.
1592 i := 0 // ERROR "moved to heap: i$"
1593 x := &Lit{&i} // ERROR "&Lit literal escapes to heap$" "&i escapes to heap$"
1594 sink = x // ERROR "x escapes to heap$"
1599 type Buffer struct {
1607 func (b *Buffer) foo() { // ERROR "\(\*Buffer\).foo b does not escape$"
1608 b.buf1 = b.buf1[1:2] // ERROR "\(\*Buffer\).foo ignoring self-assignment to b.buf1$"
1609 b.buf1 = b.buf1[1:2:3] // ERROR "\(\*Buffer\).foo ignoring self-assignment to b.buf1$"
1610 b.buf1 = b.buf2[1:2] // ERROR "\(\*Buffer\).foo ignoring self-assignment to b.buf1$"
1611 b.buf1 = b.buf2[1:2:3] // ERROR "\(\*Buffer\).foo ignoring self-assignment to b.buf1$"
1614 func (b *Buffer) bar() { // ERROR "leaking param: b$"
1615 b.buf1 = b.arr[1:2] // ERROR "b.arr escapes to heap$"
1618 func (b *Buffer) baz() { // ERROR "\(\*Buffer\).baz b does not escape$"
1619 b.str1 = b.str1[1:2] // ERROR "\(\*Buffer\).baz ignoring self-assignment to b.str1$"
1620 b.str1 = b.str2[1:2] // ERROR "\(\*Buffer\).baz ignoring self-assignment to b.str1$"
1623 func (b *Buffer) bat() { // ERROR "leaking param content: b$"
1624 o := new(Buffer) // ERROR "new\(Buffer\) escapes to heap$"
1625 o.buf1 = b.buf1[1:2]
1626 sink = o // ERROR "o escapes to heap$"
1629 func quux(sp *string, bp *[]byte) { // ERROR "quux bp does not escape$" "quux sp does not escape$"
1630 *sp = (*sp)[1:2] // ERROR "quux ignoring self-assignment to \*sp$"
1631 *bp = (*bp)[1:2] // ERROR "quux ignoring self-assignment to \*bp$"
1634 type StructWithString struct {
1639 // This is escape analysis false negative.
1640 // We assign the pointer to x.p but leak x.s. Escape analysis coarsens flows
1641 // to just x, and thus &i looks escaping.
1642 func fieldFlowTracking() {
1643 var x StructWithString
1644 i := 0 // ERROR "moved to heap: i$"
1645 x.p = &i // ERROR "&i escapes to heap$"
1646 sink = x.s // ERROR "x.s escapes to heap$"
1649 // String operations.
1651 func slicebytetostring0() {
1652 b := make([]byte, 20) // ERROR "slicebytetostring0 make\(\[\]byte, 20\) does not escape$"
1653 s := string(b) // ERROR "slicebytetostring0 string\(b\) does not escape$"
1657 func slicebytetostring1() {
1658 b := make([]byte, 20) // ERROR "slicebytetostring1 make\(\[\]byte, 20\) does not escape$"
1659 s := string(b) // ERROR "slicebytetostring1 string\(b\) does not escape$"
1664 func slicebytetostring2() {
1665 b := make([]byte, 20) // ERROR "slicebytetostring2 make\(\[\]byte, 20\) does not escape$"
1666 s := string(b) // ERROR "string\(b\) escapes to heap$"
1667 s1 := s[0:1] // ERROR "moved to heap: s1$"
1668 sink = &s1 // ERROR "&s1 escapes to heap$"
1671 func slicebytetostring3() {
1672 b := make([]byte, 20) // ERROR "slicebytetostring3 make\(\[\]byte, 20\) does not escape$"
1673 s := string(b) // ERROR "string\(b\) escapes to heap$"
1675 sink = s1 // ERROR "s1 escapes to heap$"
1681 s := s0 + s1 // ERROR "addstr0 s0 \+ s1 does not escape$"
1689 s += s0 + s1 // ERROR "addstr1 s0 \+ s1 does not escape$"
1694 b := make([]byte, 20) // ERROR "addstr2 make\(\[\]byte, 20\) does not escape$"
1696 s := string(b) + s0 // ERROR "addstr2 string\(b\) \+ s0 does not escape$" "addstr2 string\(b\) does not escape$"
1703 s := s0 + s1 // ERROR "s0 \+ s1 escapes to heap$"
1705 sink = s2 // ERROR "s2 escapes to heap$"
1708 func intstring0() bool {
1709 // string does not escape
1711 s := string(x) // ERROR "intstring0 string\(x\) does not escape$"
1715 func intstring1() string {
1716 // string does not escape, but the buffer does
1718 s := string(x) // ERROR "string\(x\) escapes to heap$"
1723 // string escapes to heap
1725 s := string(x) // ERROR "moved to heap: s$" "string\(x\) escapes to heap$"
1726 sink = &s // ERROR "&s escapes to heap$"
1729 func stringtoslicebyte0() {
1731 x := []byte(s) // ERROR "stringtoslicebyte0 \(\[\]byte\)\(s\) does not escape$"
1735 func stringtoslicebyte1() []byte {
1737 return []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap$"
1740 func stringtoslicebyte2() {
1742 sink = []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap$"
1745 func stringtoslicerune0() {
1747 x := []rune(s) // ERROR "stringtoslicerune0 \(\[\]rune\)\(s\) does not escape$"
1751 func stringtoslicerune1() []rune {
1753 return []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap$"
1756 func stringtoslicerune2() {
1758 sink = []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap$"
1761 func slicerunetostring0() {
1762 r := []rune{1, 2, 3} // ERROR "slicerunetostring0 \[\]rune literal does not escape$"
1763 s := string(r) // ERROR "slicerunetostring0 string\(r\) does not escape$"
1767 func slicerunetostring1() string {
1768 r := []rune{1, 2, 3} // ERROR "slicerunetostring1 \[\]rune literal does not escape$"
1769 return string(r) // ERROR "string\(r\) escapes to heap$"
1772 func slicerunetostring2() {
1773 r := []rune{1, 2, 3} // ERROR "slicerunetostring2 \[\]rune literal does not escape$"
1774 sink = string(r) // ERROR "string\(r\) escapes to heap$"
1778 m := make(map[int]int) // ERROR "makemap0 make\(map\[int\]int\) does not escape$"
1782 sink = m[0] // ERROR "m\[0\] escapes to heap$"
1785 func makemap1() map[int]int {
1786 return make(map[int]int) // ERROR "make\(map\[int\]int\) escapes to heap$"
1790 m := make(map[int]int) // ERROR "make\(map\[int\]int\) escapes to heap$"
1791 sink = m // ERROR "m escapes to heap$"
1794 func nonescapingEface(m map[interface{}]bool) bool { // ERROR "nonescapingEface m does not escape$"
1795 return m["foo"] // ERROR "nonescapingEface .foo. does not escape$"
1798 func nonescapingIface(m map[M]bool) bool { // ERROR "nonescapingIface m does not escape$"
1799 return m[MV(0)] // ERROR "nonescapingIface MV\(0\) does not escape$"
1803 x := new(int) // ERROR "new\(int\) escapes to heap$"
1807 func issue10353a(x *int) func() { // ERROR "leaking param: x to result ~r1 level=-1$"
1808 return func() { // ERROR "func literal escapes to heap$"
1813 func issue10353b() {
1816 x := new(int) // ERROR "new\(int\) escapes to heap$"
1817 f = func() { // ERROR "func literal escapes to heap$"