1 // errorcheck -0 -N -m -l
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.
9 // Registerization is disabled too (-N), which should
10 // have no effect on escape analysis.
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 "xx does not escape" "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 "xx does not escape" "yy does not escape"
46 xx = &yy // ERROR "&yy does not escape"
49 func foo6(xx **int, yy *int) { // ERROR "xx does not escape" "leaking param: yy"
53 func foo7(xx **int, yy *int) { // ERROR "xx does not escape" "yy does not escape"
57 func foo8(xx, yy *int) int { // ERROR "xx does not escape" "yy does not escape"
62 func foo9(xx, yy *int) *int { // ERROR "leaking param: xx" "leaking param: yy"
67 func foo10(xx, yy *int) { // ERROR "xx does not escape" "yy does not escape"
73 xx := &x // ERROR "&x does not escape"
74 yy := &y // ERROR "&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: yyy"
91 func foo14(yyy **int) { // ERROR "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 "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"
125 return &Bar{42, x} // ERROR "&Bar literal escapes to heap"
128 func NewBarp2(x *int) *Bar { // ERROR "x does not escape"
129 return &Bar{*x, nil} // ERROR "&Bar literal escapes to heap"
132 func (b *Bar) NoLeak() int { // ERROR "b does not escape"
136 func (b *Bar) Leak() *int { // ERROR "leaking param: b"
137 return &b.i // ERROR "&b.i escapes to heap"
140 func (b *Bar) AlsoNoLeak() *int { // ERROR "leaking param b content to result ~r0"
144 func (b Bar) AlsoLeak() *int { // ERROR "leaking param: b"
148 func (b Bar) LeaksToo() *int { // ERROR "leaking param: b"
149 v := 0 // ERROR "moved to heap: v"
150 b.ii = &v // ERROR "&v escapes"
154 func (b *Bar) LeaksABit() *int { // ERROR "leaking param b content to result ~r0"
155 v := 0 // ERROR "moved to heap: v"
156 b.ii = &v // ERROR "&v escapes"
160 func (b Bar) StillNoLeak() int { // ERROR "b does not escape"
162 b.ii = &v // ERROR "&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 "b does not escape"
183 func (b *Bar2) Leak() []int { // ERROR "leaking param: b"
184 return b.i[:] // ERROR "b.i escapes to heap"
187 func (b *Bar2) AlsoNoLeak() []int { // ERROR "leaking param b content to result ~r0"
191 func (b Bar2) AgainNoLeak() [12]int { // ERROR "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 "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 "moved to heap: f" "func literal escapes to heap"
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 "func literal does not escape"
260 func fooleak(xx *int) int { // ERROR "leaking param: xx"
265 func foonoleak(xx *int) int { // ERROR "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 "&x does not escape"
285 func (f *Foo) fooleak() { // ERROR "leaking param: f"
289 func (f *Foo) foonoleak() { // ERROR "f does not escape"
293 func (f *Foo) Leak() { // ERROR "leaking param: f"
297 func (f *Foo) NoLeak() { // ERROR "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 "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 "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 "f does not escape"
321 // See foo13 above for explanation of why f leaks.
322 func (f *Foo) foo46() { // ERROR "leaking param: 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"
347 return *&x // ERROR "&x does not escape"
350 func indaddr3(x *int32) *int { // ERROR "leaking param: x"
351 return *(**int)(unsafe.Pointer(&x)) // ERROR "&x does not escape"
354 // From package math:
356 func Float32bits(f float32) uint32 {
357 return *(*uint32)(unsafe.Pointer(&f)) // ERROR "&f does not escape"
360 func Float32frombits(b uint32) float32 {
361 return *(*float32)(unsafe.Pointer(&b)) // ERROR "&b does not escape"
364 func Float64bits(f float64) uint64 {
365 return *(*uint64)(unsafe.Pointer(&f)) // ERROR "&f does not escape"
368 func Float64frombits(b uint64) float64 {
369 return *(*float64)(unsafe.Pointer(&b)) // ERROR "&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"
378 return (*uint64)(unsafe.Pointer(f))
381 func typesw(i interface{}) *int { // ERROR "leaking param: i"
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"
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"
410 func foo60a(i *int) *int { // ERROR "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"
426 func foo61a(i *int) *int { // ERROR "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 "new[(]S[)] does not escape"
451 func foo63(m M) { // ERROR "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 "&mv does not escape"
472 var mv MV // ERROR "moved to heap: mv"
473 foo64(&mv) // ERROR "&mv escapes to heap"
483 foo64(mv) // escapes but it's an int so irrelevant
486 func foo69(m M) { // ERROR "leaking param: m"
490 func foo70(mv1 *MV, m M) { // ERROR "leaking param: mv1" "leaking param: m"
495 func foo71(x *int) []*int { // ERROR "leaking param: x"
501 func foo71a(x int) []*int { // ERROR "moved to heap: x"
503 y = append(y, &x) // ERROR "&x escapes to heap"
510 y[0] = &x // ERROR "&x does not escape"
513 func foo72aa() [10]*int {
514 var x int // ERROR "moved to heap: x"
516 y[0] = &x // ERROR "&x escapes to heap"
522 for i := 0; i < 10; i++ {
524 x := i // ERROR "moved to heap: x"
525 y[i] = &x // ERROR "&x escapes to heap"
530 func foo72b() [10]*int {
532 for i := 0; i < 10; i++ {
533 x := i // ERROR "moved to heap: x"
534 y[i] = &x // ERROR "&x escapes to heap"
541 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
542 for _, v := range s {
544 // actually just escapes its scope
545 defer func() { // ERROR "func literal escapes to heap"
552 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
553 for _, v := range s {
554 vv := v // ERROR "moved to heap: vv"
555 // actually just escapes its scope
556 defer func() { // ERROR "func literal escapes to heap"
557 vv = 42 // ERROR "&vv escapes to heap"
564 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
565 for _, v := range s {
567 // actually just escapes its scope
568 fn := func() { // ERROR "func literal escapes to heap"
576 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
577 for _, v := range s {
578 vv := v // ERROR "moved to heap: vv"
579 // actually just escapes its scope
580 fn := func() { // ERROR "func literal escapes to heap"
581 vv += 1 // ERROR "&vv escapes to heap"
591 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
592 for i, v := range s {
594 // actually just escapes its scope
595 array[i] = func() { // ERROR "func literal escapes to heap"
603 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
604 for i, v := range s {
605 vv := v // ERROR "moved to heap: vv"
606 // actually just escapes its scope
607 array[i] = func() { // ERROR "func literal escapes to heap"
608 println(&vv) // ERROR "&vv escapes to heap" "&vv does not escape"
613 func myprint(y *int, x ...interface{}) *int { // ERROR "x does not escape" "leaking param: y"
617 func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "y does not escape" "leaking param: x"
618 return &x[0] // ERROR "&x.0. escapes to heap"
621 func foo75(z *int) { // ERROR "z does not escape"
622 myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
625 func foo75a(z *int) { // ERROR "z does not escape"
626 myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
629 func foo75esc(z *int) { // ERROR "leaking param: z"
630 gxx = myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
633 func foo75aesc(z *int) { // ERROR "z does not escape"
634 var ppi **interface{} // assignments to pointer dereferences lose track
635 *ppi = myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
638 func foo76(z *int) { // ERROR "leaking param: z"
639 myprint(nil, z) // ERROR "[.][.][.] argument does not escape"
642 func foo76a(z *int) { // ERROR "leaking param: z"
643 myprint1(nil, z) // ERROR "[.][.][.] argument does not escape"
647 myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
651 myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
655 defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
659 defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
664 // TODO: This one really only escapes its scope, but we don't distinguish yet.
665 defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
671 defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
675 func foo77(z []interface{}) { // ERROR "z does not escape"
676 myprint(nil, z...) // z does not escape
679 func foo77a(z []interface{}) { // ERROR "z does not escape"
683 func foo77b(z []interface{}) { // ERROR "leaking param: z"
684 var ppi **interface{}
685 *ppi = myprint1(nil, z...)
688 func foo78(z int) *int { // ERROR "moved to heap: z"
689 return &z // ERROR "&z escapes to heap"
692 func foo78a(z int) *int { // ERROR "moved to heap: z"
693 y := &z // ERROR "&z escapes to heap"
694 x := &y // ERROR "&y does not escape"
695 return *x // really return y
699 return new(int) // ERROR "new[(]int[)] escapes to heap"
705 // Really just escapes its scope but we don't distinguish
706 z = new(int) // ERROR "new[(]int[)] escapes to heap"
714 z := new(int) // ERROR "new[(]int[)] does not escape"
720 func tee(p *int) (x, y *int) { return p, p } // ERROR "leaking param"
722 func noop(x, y *int) {} // ERROR "does not escape"
725 var x, y, z int // ERROR "moved to heap"
726 go noop(tee(&z)) // ERROR "&z escapes to heap"
727 go noop(&x, &y) // ERROR "escapes to heap"
729 var u, v, w int // ERROR "moved to heap"
730 defer noop(tee(&u)) // ERROR "&u escapes to heap"
731 defer noop(&v, &w) // ERROR "escapes to heap"
735 type Fooer interface {
739 type LimitedFooer struct {
744 func LimitFooer(r Fooer, n int64) Fooer { // ERROR "leaking param: r"
745 return &LimitedFooer{r, n} // ERROR "&LimitedFooer literal escapes to heap"
748 func foo90(x *int) map[*int]*int { // ERROR "leaking param: x"
749 return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int literal escapes to heap"
752 func foo91(x *int) map[*int]*int { // ERROR "leaking param: x"
753 return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int literal escapes to heap"
756 func foo92(x *int) [2]*int { // ERROR "leaking param: x"
757 return [2]*int{x, nil}
761 func foo93(c chan *int) *int { // ERROR "c does not escape"
769 func foo94(m map[*int]*int, b bool) *int { // ERROR "m does not escape"
770 for k, v := range m {
780 func foo95(m map[*int]*int, x *int) { // ERROR "m does not escape" "leaking param: x"
785 func foo96(m []*int) *int { // ERROR "m does not escape"
790 func foo97(m [1]*int) *int { // ERROR "leaking param: m"
795 func foo98(m map[int]*int) *int { // ERROR "m does not escape"
800 func foo99(m *[1]*int) []*int { // ERROR "leaking param: m"
805 func foo100(m []*int) *int { // ERROR "m does not escape"
806 for _, v := range m {
813 func foo101(m [1]*int) *int { // ERROR "leaking param: m"
814 for _, v := range m {
821 func foo101a(m [1]*int) *int { // ERROR "m does not escape"
822 for i := range m { // ERROR "moved to heap: i"
823 return &i // ERROR "&i escapes to heap"
829 func foo102(m []*int, x *int) { // ERROR "m does not escape" "leaking param: x"
834 func foo103(m [1]*int, x *int) { // ERROR "m does not escape" "x does not escape"
841 func foo104(x []*int) { // ERROR "x does not escape"
846 func foo105(x []*int) { // ERROR "x does not escape"
851 func foo106(x *int) { // ERROR "leaking param: x"
855 func foo107(x *int) map[*int]*int { // ERROR "leaking param: x"
856 return map[*int]*int{x: nil} // ERROR "map.* literal escapes to heap"
859 func foo108(x *int) map[*int]*int { // ERROR "leaking param: x"
860 return map[*int]*int{nil: x} // ERROR "map.* literal escapes to heap"
863 func foo109(x *int) *int { // ERROR "leaking param: x"
864 m := map[*int]*int{x: nil} // ERROR "map.* literal does not escape"
865 for k, _ := range m {
871 func foo110(x *int) *int { // ERROR "leaking param: x"
872 m := map[*int]*int{nil: x} // ERROR "map.* literal does not escape"
876 func foo111(x *int) *int { // ERROR "leaking param: x"
877 m := []*int{x} // ERROR "\[\]\*int literal does not escape"
881 func foo112(x *int) *int { // ERROR "leaking param: x"
886 func foo113(x *int) *int { // ERROR "leaking param: x"
891 func foo114(x *int) *int { // ERROR "leaking param: x"
892 m := &Bar{ii: x} // ERROR "&Bar literal does not escape"
896 func foo115(x *int) *int { // ERROR "leaking param: x"
897 return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1))
900 func foo116(b bool) *int {
902 x := 1 // ERROR "moved to heap: x"
903 return &x // ERROR "&x escapes to heap"
905 y := 1 // ERROR "moved to heap: y"
906 return &y // ERROR "&y escapes to heap"
911 func foo117(unknown func(interface{})) { // ERROR "unknown does not escape"
912 x := 1 // ERROR "moved to heap: x"
913 unknown(&x) // ERROR "&x escapes to heap"
916 func foo118(unknown func(*int)) { // ERROR "unknown does not escape"
917 x := 1 // ERROR "moved to heap: x"
918 unknown(&x) // ERROR "&x escapes to heap"
923 func foo119(x *int) { // ERROR "leaking param: x"
928 // formerly exponential time analysis
1029 // use the labels to silence compiler errors
1133 for i := 0; i < 10; i++ {
1134 defer myprint(nil, i) // ERROR "[.][.][.] argument escapes to heap"
1135 go myprint(nil, i) // ERROR "[.][.][.] argument escapes to heap"
1139 // same as foo121 but check across import
1141 for i := 0; i < 10; i++ {
1142 defer fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes to heap"
1143 go fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes to heap"
1147 // a harmless forward jump
1153 i = new(int) // ERROR "new.int. does not escape"
1157 // a backward jump, increases loopdepth
1162 i = new(int) // ERROR "new.int. escapes to heap"
1168 func foo124(x **int) { // ERROR "x does not escape"
1169 var i int // ERROR "moved to heap: i"
1170 p := &i // ERROR "&i escapes"
1171 func() { // ERROR "func literal does not escape"
1172 *x = p // ERROR "leaking closure reference p"
1176 func foo125(ch chan *int) { // ERROR "does not escape"
1177 var i int // ERROR "moved to heap"
1178 p := &i // ERROR "&i escapes to heap"
1179 func() { // ERROR "func literal does not escape"
1180 ch <- p // ERROR "leaking closure reference p"
1185 var px *int // loopdepth 0
1188 var i int // ERROR "moved to heap"
1189 func() { // ERROR "func literal does not escape"
1190 px = &i // ERROR "&i escapes"
1199 var i int // ERROR "moved to heap: i"
1200 p := &i // ERROR "&i escapes to heap"
1207 p := &i // ERROR "&i does not escape"
1213 var i int // ERROR "moved to heap: i"
1214 p := &i // ERROR "&i escapes to heap"
1215 func() { // ERROR "func literal does not escape"
1216 q := p // ERROR "leaking closure reference p"
1217 func() { // ERROR "func literal does not escape"
1218 r := q // ERROR "leaking closure reference q"
1226 var i int // ERROR "moved to heap"
1227 func() { // ERROR "func literal does not escape"
1228 px = &i // ERROR "&i escapes" "leaking closure reference i"
1234 var i int // ERROR "moved to heap"
1235 func() { // ERROR "func literal does not escape"
1236 px = &i // ERROR "&i escapes" "leaking closure reference i"
1241 var i int // ERROR "moved to heap"
1242 go func() { // ERROR "func literal escapes to heap"
1243 px = &i // ERROR "&i escapes" "leaking closure reference i"
1248 var i int // ERROR "moved to heap"
1249 defer func() { // ERROR "func literal does not escape"
1250 px = &i // ERROR "&i escapes" "leaking closure reference i"
1256 p := &i // ERROR "&i does not escape"
1257 func() { // ERROR "func literal does not escape"
1259 func() { // ERROR "func literal does not escape"
1267 var i int // ERROR "moved to heap: i"
1268 p := &i // ERROR "&i escapes to heap"
1269 go func() { // ERROR "func literal escapes to heap"
1271 func() { // ERROR "func literal does not escape"
1279 var i int // ERROR "moved to heap: i"
1280 p := &i // ERROR "&i escapes to heap"
1281 go func() { // ERROR "func literal escapes to heap"
1282 q := p // ERROR "leaking closure reference p"
1283 func() { // ERROR "func literal does not escape"
1284 r := q // ERROR "leaking closure reference q"
1291 var i int // ERROR "moved to heap: i"
1292 p := &i // ERROR "&i escapes to heap"
1293 func() { // ERROR "func literal does not escape"
1294 q := p // ERROR "leaking closure reference p"
1295 go func() { // ERROR "func literal escapes to heap"
1302 func foo138() *byte {
1306 t := new(T) // ERROR "new.T. escapes to heap"
1307 return &t.x[0] // ERROR "&t.x.0. escapes to heap"
1310 func foo139() *byte {
1316 t := new(T) // ERROR "new.T. escapes to heap"
1317 return &t.x.y // ERROR "&t.x.y escapes to heap"
1321 func foo140() interface{} {
1329 t := &T{} // ERROR "&T literal escapes to heap"
1344 func F3(x []byte) // ERROR "F3 x does not escape"
1350 F1(buf1[:]) // ERROR "buf1 does not escape"
1352 var buf2 [10]byte // ERROR "moved to heap: buf2"
1353 F2(buf2[:]) // ERROR "buf2 escapes to heap"
1356 F3(buf3[:]) // ERROR "buf3 does not escape"
1358 var buf4 [10]byte // ERROR "moved to heap: buf4"
1359 F4(buf4[:]) // ERROR "buf4 escapes to heap"
1366 func (t *Tm) M() { // ERROR "t does not escape"
1372 t := new(Tm) // ERROR "escapes to heap"
1373 f = t.M // ERROR "t.M does not escape"
1380 t := new(Tm) // ERROR "escapes to heap"
1381 gf = t.M // ERROR "t.M escapes to heap"
1386 for i := 0; i < 1000; i++ {
1387 func() { // ERROR "func literal does not escape"
1388 for i := 0; i < 1; i++ {
1390 t.M() // ERROR "t does not escape"
1397 // Check that annotations take effect regardless of whether they
1398 // are before or after the use in the source code.
1406 foo144a(&x) // ERROR "&x does not escape"
1408 foo144b(&y) // ERROR "&y does not escape"
1415 // issue 7313: for loop init should not be treated as "in loop"
1421 func foo145(l List) { // ERROR "l does not escape"
1423 for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1427 func foo146(l List) { // ERROR "l does not escape"
1429 p = &l // ERROR "&l does not escape"
1430 for ; p.Next != nil; p = p.Next {
1434 func foo147(l List) { // ERROR "l does not escape"
1436 p = &l // ERROR "&l does not escape"
1442 func foo148(l List) { // ERROR " l does not escape"
1443 for p := &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1447 // related: address of variable should have depth of variable, not of loop
1449 func foo149(l List) { // ERROR " l does not escape"
1452 for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1457 // issue 7934: missed ... if element type had no pointers
1461 func foo150(x ...byte) { // ERROR "leaking param: x"
1466 foo150(1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
1469 // issue 7931: bad handling of slice of array
1473 func foo151(x *int) { // ERROR "leaking param: x"
1478 var a [64]int // ERROR "moved to heap: a"
1480 foo151(&(&a)[4:8][0]) // ERROR "&\(&a\)\[4:8\]\[0\] escapes to heap" "&a escapes to heap"
1484 var a [10]int // ERROR "moved to heap: a"
1485 b := a[:] // ERROR "a escapes to heap"
1486 foo151(&b[4:8][0]) // ERROR "&b\[4:8\]\[0\] escapes to heap"
1490 var a [64]int // ERROR "moved to heap: a"
1492 foo151(&(&a)[4:8:8][0]) // ERROR "&\(&a\)\[4:8:8\]\[0\] escapes to heap" "&a escapes to heap"
1496 var a [10]int // ERROR "moved to heap: a"
1497 b := a[:] // ERROR "a escapes to heap"
1498 foo151(&b[4:8:8][0]) // ERROR "&b\[4:8:8\]\[0\] escapes to heap"
1507 func (u *U) String() *string { // ERROR "leaking param u content to result ~r0"
1515 func NewV(u U) *V { // ERROR "leaking param: u"
1516 return &V{u.String()} // ERROR "&V literal escapes to heap" "u does not escape"
1520 a := "a" // ERROR "moved to heap: a"
1521 u := U{&a} // ERROR "&a escapes to heap"
1526 // issue 8176 - &x in type switch body not marked as escaping
1528 func foo153(v interface{}) *int { // ERROR "leaking param: v"
1529 switch x := v.(type) {
1530 case int: // ERROR "moved to heap: x"
1531 return &x // ERROR "&x escapes to heap"
1536 // issue 8185 - &result escaping into result
1538 func f() (x int, y *int) { // ERROR "moved to heap: x"
1539 y = &x // ERROR "&x escapes to heap"
1543 func g() (x interface{}) { // ERROR "moved to heap: x"
1544 x = &x // ERROR "&x escapes to heap"
1548 var sink interface{}
1554 func ptrlitNoescape() {
1555 // Both literal and element do not escape.
1557 x := &Lit{&i} // ERROR "&Lit literal does not escape" "&i does not escape"
1561 func ptrlitNoEscape2() {
1562 // Literal does not escape, but element does.
1563 i := 0 // ERROR "moved to heap: i"
1564 x := &Lit{&i} // ERROR "&Lit literal does not escape" "&i escapes to heap"
1568 func ptrlitEscape() {
1569 // Both literal and element escape.
1570 i := 0 // ERROR "moved to heap: i"
1571 x := &Lit{&i} // ERROR "&Lit literal escapes to heap" "&i escapes to heap"
1577 type Buffer struct {
1585 func (b *Buffer) foo() { // ERROR "b does not escape"
1586 b.buf1 = b.buf1[1:2] // ERROR "ignoring self-assignment to b.buf1"
1587 b.buf1 = b.buf1[1:2:3] // ERROR "ignoring self-assignment to b.buf1"
1588 b.buf1 = b.buf2[1:2] // ERROR "ignoring self-assignment to b.buf1"
1589 b.buf1 = b.buf2[1:2:3] // ERROR "ignoring self-assignment to b.buf1"
1592 func (b *Buffer) bar() { // ERROR "leaking param: b"
1593 b.buf1 = b.arr[1:2] // ERROR "b.arr escapes to heap"
1596 func (b *Buffer) baz() { // ERROR "b does not escape"
1597 b.str1 = b.str1[1:2] // ERROR "ignoring self-assignment to b.str1"
1598 b.str1 = b.str2[1:2] // ERROR "ignoring self-assignment to b.str1"
1601 func (b *Buffer) bat() { // ERROR "leaking param: b"
1602 o := new(Buffer) // ERROR "new\(Buffer\) escapes to heap"
1603 o.buf1 = b.buf1[1:2]
1607 func quux(sp *string, bp *[]byte) { // ERROR "sp does not escape" "bp does not escape"
1608 *sp = (*sp)[1:2] // ERROR "quux ignoring self-assignment to \*sp"
1609 *bp = (*bp)[1:2] // ERROR "quux ignoring self-assignment to \*bp"
1612 type StructWithString struct {
1617 // This is escape analysis false negative.
1618 // We assign the pointer to x.p but leak x.s. Escape analysis coarsens flows
1619 // to just x, and thus &i looks escaping.
1620 func fieldFlowTracking() {
1621 var x StructWithString
1622 i := 0 // ERROR "moved to heap: i"
1623 x.p = &i // ERROR "&i escapes to heap"
1627 // String operations.
1629 func slicebytetostring0() {
1630 b := make([]byte, 20) // ERROR "does not escape"
1631 s := string(b) // ERROR "string\(b\) does not escape"
1635 func slicebytetostring1() {
1636 b := make([]byte, 20) // ERROR "does not escape"
1637 s := string(b) // ERROR "string\(b\) does not escape"
1642 func slicebytetostring2() {
1643 b := make([]byte, 20) // ERROR "does not escape"
1644 s := string(b) // ERROR "string\(b\) escapes to heap"
1645 s1 := s[0:1] // ERROR "moved to heap: s1"
1646 sink = &s1 // ERROR "&s1 escapes to heap"
1649 func slicebytetostring3() {
1650 b := make([]byte, 20) // ERROR "does not escape"
1651 s := string(b) // ERROR "string\(b\) escapes to heap"
1659 s := s0 + s1 // ERROR "s0 \+ s1 does not escape"
1667 s += s0 + s1 // ERROR "s0 \+ s1 does not escape"
1672 b := make([]byte, 20) // ERROR "does not escape"
1674 s := string(b) + s0 // ERROR "string\(b\) does not escape" "string\(b\) \+ s0 does not escape"
1681 s := s0 + s1 // ERROR "s0 \+ s1 escapes to heap"
1686 func intstring0() bool {
1687 // string does not escape
1689 s := string(x) // ERROR "string\(x\) does not escape"
1693 func intstring1() string {
1694 // string does not escape, but the buffer does
1696 s := string(x) // ERROR "string\(x\) escapes to heap"
1701 // string escapes to heap
1703 s := string(x) // ERROR "string\(x\) escapes to heap" "moved to heap: s"
1704 sink = &s // ERROR "&s escapes to heap"
1707 func stringtoslicebyte0() {
1709 x := []byte(s) // ERROR "\(\[\]byte\)\(s\) does not escape"
1713 func stringtoslicebyte1() []byte {
1715 return []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap"
1718 func stringtoslicebyte2() {
1720 sink = []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap"
1723 func stringtoslicerune0() {
1725 x := []rune(s) // ERROR "\(\[\]rune\)\(s\) does not escape"
1729 func stringtoslicerune1() []rune {
1731 return []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap"
1734 func stringtoslicerune2() {
1736 sink = []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap"
1739 func slicerunetostring0() {
1740 r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape"
1741 s := string(r) // ERROR "string\(r\) does not escape"
1745 func slicerunetostring1() string {
1746 r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape"
1747 return string(r) // ERROR "string\(r\) escapes to heap"
1750 func slicerunetostring2() {
1751 r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape"
1752 sink = string(r) // ERROR "string\(r\) escapes to heap"
1756 m := make(map[int]int) // ERROR "make\(map\[int\]int\, 0\) does not escape"
1763 func makemap1() map[int]int {
1764 return make(map[int]int) // ERROR "make\(map\[int\]int\, 0\) escapes to heap"
1768 m := make(map[int]int) // ERROR "make\(map\[int\]int\, 0\) escapes to heap"