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$"
25 func foo2(yy *int) { // ERROR "leaking param: yy$"
29 func foo3(x int) *int { // ERROR "moved to heap: x$"
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$"
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 to result ~r0 level=0$" "leaking param: yy to result ~r0 level=0$"
67 func foo10(xx, yy *int) { // ERROR "xx does not escape$" "yy 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 "yyy does not escape$"
95 func foo15(yy *int) { // ERROR "moved to heap: yy$"
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$"
121 return &Bar{42, nil} // ERROR "&Bar{...} escapes to heap$"
124 func NewBarp(x *int) *Bar { // ERROR "leaking param: x$"
125 return &Bar{42, x} // ERROR "&Bar{...} escapes to heap$"
128 func NewBarp2(x *int) *Bar { // ERROR "x does not escape$"
129 return &Bar{*x, nil} // ERROR "&Bar{...} escapes to heap$"
132 func (b *Bar) NoLeak() int { // ERROR "b does not escape$"
136 func (b *Bar) Leak() *int { // ERROR "leaking param: b to result ~r0 level=0$"
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$"
154 func (b *Bar) LeaksABit() *int { // ERROR "leaking param: b to result ~r0 level=1$"
155 v := 0 // ERROR "moved to heap: v$"
160 func (b Bar) StillNoLeak() int { // ERROR "b 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{...} escapes to heap$"
179 func (b *Bar2) NoLeak() int { // ERROR "b does not escape$"
183 func (b *Bar2) Leak() []int { // ERROR "leaking param: b to result ~r0 level=0$"
187 func (b *Bar2) AlsoNoLeak() []int { // ERROR "leaking param: b to result ~r0 level=1$"
191 func (b Bar2) AgainNoLeak() [12]int { // ERROR "b does not escape$"
195 func (b *Bar2) LeakSelf() { // ERROR "leaking param: b$"
199 func (b *Bar2) LeakSelf2() { // ERROR "leaking param: b$"
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$"
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 "func literal escapes to heap$" "moved to heap: f$"
245 func foo23c(x int) func() int { // ERROR "moved to heap: x$"
246 return func() int { // ERROR "func literal 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$"
273 func foo32(x int) int {
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$"
305 func (f *Foo) foo42(x int) { // ERROR "f does not escape$" "moved to heap: x$"
309 func foo43(f *Foo, x int) { // ERROR "f does not escape$" "moved to heap: x$"
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 content: f$"
326 func (f *Foo) foo47() { // ERROR "leaking param: f$"
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$"
346 func indaddr2(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$"
350 func indaddr3(x *int32) *int { // ERROR "leaking param: x to result ~r0 level=0$"
351 return *(**int)(unsafe.Pointer(&x))
354 // From package math:
356 func Float32bits(f float32) uint32 {
357 return *(*uint32)(unsafe.Pointer(&f))
360 func Float32frombits(b uint32) float32 {
361 return *(*float32)(unsafe.Pointer(&b))
364 func Float64bits(f float64) uint64 {
365 return *(*uint64)(unsafe.Pointer(&f))
368 func Float64frombits(b uint64) float64 {
369 return *(*float64)(unsafe.Pointer(&b))
373 func float64bitsptr(f float64) *uint64 { // ERROR "moved to heap: f$"
374 return (*uint64)(unsafe.Pointer(&f))
377 func float64ptrbitsptr(f *float64) *uint64 { // ERROR "leaking param: f to result ~r0 level=0$"
378 return (*uint64)(unsafe.Pointer(f))
381 func typesw(i interface{}) *int { // ERROR "leaking param: i to result ~r0 level=0$"
382 switch val := i.(type) {
386 v := int(*val) // ERROR "moved to heap: v$"
392 func exprsw(i *int) *int { // ERROR "leaking param: i to result ~r0 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 ~r0 level=0$"
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 to result ~r0 level=0$"
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$"
472 var mv MV // ERROR "moved to heap: mv$"
478 foo63(mv) // ERROR "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$"
496 func foo71(x *int) []*int { // ERROR "leaking param: x$"
502 func foo71a(x int) []*int { // ERROR "moved to heap: x$"
514 func foo72aa() [10]*int {
515 var x int // ERROR "moved to heap: x$"
523 for i := 0; i < 10; i++ {
525 x := i // ERROR "moved to heap: x$"
531 func foo72b() [10]*int {
533 for i := 0; i < 10; i++ {
534 x := i // ERROR "moved to heap: x$"
542 s := []int{3, 2, 1} // ERROR "\[\]int{...} 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 "\[\]int{...} 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$"
565 s := []int{3, 2, 1} // ERROR "\[\]int{...} 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 "\[\]int{...} 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$"
592 s := []int{3, 2, 1} // ERROR "\[\]int{...} 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 "\[\]int{...} 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$"
614 func myprint(y *int, x ...interface{}) *int { // ERROR "leaking param: y to result ~r0 level=0$" "x does not escape$"
618 func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "leaking param: x to result ~r0 level=0$" "y does not escape$"
622 func foo75(z *int) { // ERROR "z does not escape$"
623 myprint(z, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$"
626 func foo75a(z *int) { // ERROR "z does not escape$"
627 myprint1(z, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$"
630 func foo75esc(z *int) { // ERROR "leaking param: z$"
631 gxx = myprint(z, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$"
634 func foo75aesc(z *int) { // ERROR "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 "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$"
643 func foo76(z *int) { // ERROR "z does not escape"
644 myprint(nil, z) // ERROR "... argument does not escape$"
647 func foo76a(z *int) { // ERROR "z does not escape"
648 myprint1(nil, z) // ERROR "... argument does not escape$"
652 myprint(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$"
656 myprint1(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$"
660 defer myprint(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$"
664 defer myprint1(nil, 1, 2, 3) // ERROR "1 does not escape" "2 does not escape" "3 does not escape" "... argument does not escape$"
669 // TODO: This one really only escapes its scope, but we don't distinguish yet.
670 defer myprint(nil, 1, 2, 3) // ERROR "... argument does not escape$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$"
676 defer myprint1(nil, 1, 2, 3) // ERROR "... argument does not escape$" "1 escapes to heap$" "2 escapes to heap$" "3 escapes to heap$"
680 func foo77(z []interface{}) { // ERROR "z does not escape$"
681 myprint(nil, z...) // z does not escape
684 func foo77a(z []interface{}) { // ERROR "z does not escape$"
688 func foo77b(z []interface{}) { // ERROR "leaking param: z$"
689 var ppi **interface{}
690 *ppi = myprint1(nil, z...)
693 func foo77c(z []interface{}) { // ERROR "leaking param: z$"
694 sink = myprint1(nil, z...)
699 myprint(nil, &i) // ERROR "... argument does not escape$"
702 myprint1(nil, &j) // ERROR "... argument does not escape$"
705 func foo78(z int) *int { // ERROR "moved to heap: z$"
709 func foo78a(z int) *int { // ERROR "moved to heap: z$"
712 return *x // really return y
716 return new(int) // ERROR "new\(int\) escapes to heap$"
722 // Really just escapes its scope but we don't distinguish
723 z = new(int) // ERROR "new\(int\) escapes to heap$"
731 z := new(int) // ERROR "new\(int\) does not escape$"
737 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$"
739 func noop(x, y *int) {} // ERROR "x does not escape$" "y does not escape$"
742 var x, y, z int // ERROR "moved to heap: x$" "moved to heap: y$" "moved to heap: z$"
746 var u, v, w int // ERROR "moved to heap: u$" "moved to heap: v$" "moved to heap: w$"
752 type Fooer interface {
756 type LimitedFooer struct {
761 func LimitFooer(r Fooer, n int64) Fooer { // ERROR "leaking param: r$"
762 return &LimitedFooer{r, n} // ERROR "&LimitedFooer{...} escapes to heap$"
765 func foo90(x *int) map[*int]*int { // ERROR "leaking param: x$"
766 return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} escapes to heap$"
769 func foo91(x *int) map[*int]*int { // ERROR "leaking param: x$"
770 return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} escapes to heap$"
773 func foo92(x *int) [2]*int { // ERROR "leaking param: x to result ~r0 level=0$"
774 return [2]*int{x, nil}
778 func foo93(c chan *int) *int { // ERROR "c does not escape$"
786 func foo94(m map[*int]*int, b bool) *int { // ERROR "leaking param: m to result ~r0 level=1"
787 for k, v := range m {
797 func foo95(m map[*int]*int, x *int) { // ERROR "m does not escape$" "leaking param: x$"
801 // does not leak m but does leak content
802 func foo96(m []*int) *int { // ERROR "leaking param: m to result ~r0 level=1"
807 func foo97(m [1]*int) *int { // ERROR "leaking param: m to result ~r0 level=0$"
812 func foo98(m map[int]*int) *int { // ERROR "m does not escape$"
817 func foo99(m *[1]*int) []*int { // ERROR "leaking param: m to result ~r0 level=0$"
822 func foo100(m []*int) *int { // ERROR "leaking param: m to result ~r0 level=1"
823 for _, v := range m {
830 func foo101(m [1]*int) *int { // ERROR "leaking param: m to result ~r0 level=0$"
831 for _, v := range m {
838 func foo101a(m [1]*int) *int { // ERROR "m does not escape$"
839 for i := range m { // ERROR "moved to heap: i$"
846 func foo102(m []*int, x *int) { // ERROR "m does not escape$" "leaking param: x$"
851 func foo103(m [1]*int, x *int) { // ERROR "m does not escape$" "x does not escape$"
857 // does not leak x but does leak content
858 func foo104(x []*int) { // ERROR "leaking param content: x"
862 // does not leak x but does leak content
863 func foo105(x []*int) { // ERROR "leaking param content: x"
868 func foo106(x *int) { // ERROR "leaking param: x$"
872 func foo107(x *int) map[*int]*int { // ERROR "leaking param: x$"
873 return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} escapes to heap$"
876 func foo108(x *int) map[*int]*int { // ERROR "leaking param: x$"
877 return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} escapes to heap$"
880 func foo109(x *int) *int { // ERROR "leaking param: x$"
881 m := map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int{...} does not escape$"
882 for k, _ := range m {
888 func foo110(x *int) *int { // ERROR "leaking param: x$"
889 m := map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int{...} does not escape$"
893 func foo111(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0"
894 m := []*int{x} // ERROR "\[\]\*int{...} does not escape$"
898 func foo112(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$"
903 func foo113(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$"
908 func foo114(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$"
909 m := &Bar{ii: x} // ERROR "&Bar{...} does not escape$"
913 func foo115(x *int) *int { // ERROR "leaking param: x to result ~r0 level=0$"
914 return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1))
917 func foo116(b bool) *int {
919 x := 1 // ERROR "moved to heap: x$"
922 y := 1 // ERROR "moved to heap: y$"
928 func foo117(unknown func(interface{})) { // ERROR "unknown does not escape$"
929 x := 1 // ERROR "moved to heap: x$"
933 func foo118(unknown func(*int)) { // ERROR "unknown does not escape$"
934 x := 1 // ERROR "moved to heap: x$"
940 func foo119(x *int) { // ERROR "leaking param: x$"
945 // formerly exponential time analysis
1046 // use the labels to silence compiler errors
1150 for i := 0; i < 10; i++ {
1151 defer myprint(nil, i) // ERROR "... argument does not escape$" "i escapes to heap$"
1152 go myprint(nil, i) // ERROR "... argument does not escape$" "i escapes to heap$"
1156 // same as foo121 but check across import
1158 for i := 0; i < 10; i++ {
1159 defer fmt.Printf("%d", i) // ERROR "... argument does not escape$" "i escapes to heap$"
1160 go fmt.Printf("%d", i) // ERROR "... argument does not escape$" "i escapes to heap$"
1164 // a harmless forward jump
1170 i = new(int) // ERROR "new\(int\) does not escape$"
1174 // a backward jump, increases loopdepth
1179 i = new(int) // ERROR "new\(int\) escapes to heap$"
1185 func foo124(x **int) { // ERROR "x does not escape$"
1186 var i int // ERROR "moved to heap: i$"
1188 func() { // ERROR "func literal does not escape$"
1193 func foo125(ch chan *int) { // ERROR "ch does not escape$"
1194 var i int // ERROR "moved to heap: i$"
1196 func() { // ERROR "func literal does not escape$"
1202 var px *int // loopdepth 0
1205 var i int // ERROR "moved to heap: i$"
1206 func() { // ERROR "func literal does not escape$"
1216 var i int // ERROR "moved to heap: i$"
1230 var i int // ERROR "moved to heap: i$"
1232 func() { // ERROR "func literal does not escape$"
1234 func() { // ERROR "func literal does not escape$"
1243 var i int // ERROR "moved to heap: i$"
1244 func() { // ERROR "func literal does not escape$"
1251 var i int // ERROR "moved to heap: i$"
1252 func() { // ERROR "func literal does not escape$"
1258 var i int // ERROR "moved to heap: i$"
1259 go func() { // ERROR "func literal escapes to heap$"
1265 var i int // ERROR "moved to heap: i$"
1266 defer func() { // ERROR "func literal does not escape$"
1274 func() { // ERROR "func literal does not escape$"
1276 func() { // ERROR "func literal does not escape$"
1284 var i int // ERROR "moved to heap: i$"
1286 go func() { // ERROR "func literal escapes to heap$"
1288 func() { // ERROR "func literal does not escape$"
1296 var i int // ERROR "moved to heap: i$"
1298 go func() { // ERROR "func literal escapes to heap$"
1300 func() { // ERROR "func literal does not escape$"
1308 var i int // ERROR "moved to heap: i$"
1310 func() { // ERROR "func literal does not escape$"
1312 go func() { // ERROR "func literal escapes to heap$"
1319 func foo138() *byte {
1323 t := new(T) // ERROR "new\(T\) escapes to heap$"
1327 func foo139() *byte {
1333 t := new(T) // ERROR "new\(T\) escapes to heap$"
1338 func foo140() interface{} {
1346 t := &T{} // ERROR "&T{} escapes to heap$"
1347 return U{ // ERROR "U{...} escapes to heap$"
1361 func F3(x []byte) // ERROR "x does not escape$"
1363 func F4(x []byte) // ERROR "leaking param: x$"
1369 var buf2 [10]byte // ERROR "moved to heap: buf2$"
1375 var buf4 [10]byte // ERROR "moved to heap: buf4$"
1383 func (t *Tm) M() { // ERROR "t does not escape$"
1389 t := new(Tm) // ERROR "new\(Tm\) does not escape$"
1390 f = t.M // ERROR "t.M does not escape$"
1397 t := new(Tm) // ERROR "new\(Tm\) escapes to heap$"
1398 gf = t.M // ERROR "t.M escapes to heap$"
1403 for i := 0; i < 1000; i++ {
1404 func() { // ERROR "func literal does not escape$"
1405 for i := 0; i < 1; i++ {
1414 // Check that annotations take effect regardless of whether they
1415 // are before or after the use in the source code.
1432 // issue 7313: for loop init should not be treated as "in loop"
1438 func foo145(l List) { // ERROR "l does not escape$"
1440 for p = &l; p.Next != nil; p = p.Next {
1444 func foo146(l List) { // ERROR "l does not escape$"
1447 for ; p.Next != nil; p = p.Next {
1451 func foo147(l List) { // ERROR "l does not escape$"
1459 func foo148(l List) { // ERROR "l does not escape$"
1460 for p := &l; p.Next != nil; p = p.Next {
1464 // related: address of variable should have depth of variable, not of loop
1466 func foo149(l List) { // ERROR "l does not escape$"
1469 for p = &l; p.Next != nil; p = p.Next {
1474 // issue 7934: missed ... if element type had no pointers
1478 func foo150(x ...byte) { // ERROR "leaking param: x$"
1483 foo150(1, 2, 3) // ERROR "... argument escapes to heap$"
1486 // issue 7931: bad handling of slice of array
1490 func foo151(x *int) { // ERROR "leaking param: x$"
1495 var a [64]int // ERROR "moved to heap: a$"
1497 foo151(&(&a)[4:8][0])
1501 var a [10]int // ERROR "moved to heap: a$"
1507 var a [64]int // ERROR "moved to heap: a$"
1509 foo151(&(&a)[4:8:8][0])
1513 var a [10]int // ERROR "moved to heap: a$"
1515 foo151(&b[4:8:8][0])
1524 func (u *U) String() *string { // ERROR "leaking param: u to result ~r0 level=1$"
1532 func NewV(u U) *V { // ERROR "leaking param: u$"
1533 return &V{u.String()} // ERROR "&V{...} escapes to heap$"
1537 a := "a" // ERROR "moved to heap: a$"
1543 // issue 8176 - &x in type switch body not marked as escaping
1545 func foo153(v interface{}) *int { // ERROR "v does not escape"
1546 switch x := v.(type) {
1547 case int: // ERROR "moved to heap: x$"
1550 panic(0) // ERROR "0 escapes to heap"
1553 // issue 8185 - &result escaping into result
1555 func f() (x int, y *int) { // ERROR "moved to heap: x$"
1560 func g() (x interface{}) { // ERROR "moved to heap: x$"
1565 var sink interface{}
1571 func ptrlitNoescape() {
1572 // Both literal and element do not escape.
1574 x := &Lit{&i} // ERROR "&Lit{...} does not escape$"
1578 func ptrlitNoEscape2() {
1579 // Literal does not escape, but element does.
1580 i := 0 // ERROR "moved to heap: i$"
1581 x := &Lit{&i} // ERROR "&Lit{...} does not escape$"
1585 func ptrlitEscape() {
1586 // Both literal and element escape.
1587 i := 0 // ERROR "moved to heap: i$"
1588 x := &Lit{&i} // ERROR "&Lit{...} escapes to heap$"
1594 type Buffer struct {
1603 func (b *Buffer) foo() { // ERROR "b does not escape$"
1604 b.buf1 = b.buf1[1:2] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf1\[1:2\]$"
1605 b.buf1 = b.buf1[1:2:3] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf1\[1:2:3\]$"
1606 b.buf1 = b.buf2[1:2] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf2\[1:2\]$"
1607 b.buf1 = b.buf2[1:2:3] // ERROR "\(\*Buffer\).foo ignoring self-assignment in b.buf1 = b.buf2\[1:2:3\]$"
1610 func (b *Buffer) bar() { // ERROR "leaking param: b$"
1614 func (b *Buffer) arrayPtr() { // ERROR "b does not escape"
1615 b.buf1 = b.arrPtr[1:2] // ERROR "\(\*Buffer\).arrayPtr ignoring self-assignment in b.buf1 = b.arrPtr\[1:2\]$"
1616 b.buf1 = b.arrPtr[1:2:3] // ERROR "\(\*Buffer\).arrayPtr ignoring self-assignment in b.buf1 = b.arrPtr\[1:2:3\]$"
1619 func (b *Buffer) baz() { // ERROR "b does not escape$"
1620 b.str1 = b.str1[1:2] // ERROR "\(\*Buffer\).baz ignoring self-assignment in b.str1 = b.str1\[1:2\]$"
1621 b.str1 = b.str2[1:2] // ERROR "\(\*Buffer\).baz ignoring self-assignment in b.str1 = b.str2\[1:2\]$"
1624 func (b *Buffer) bat() { // ERROR "leaking param content: b$"
1625 o := new(Buffer) // ERROR "new\(Buffer\) escapes to heap$"
1626 o.buf1 = b.buf1[1:2]
1630 func quux(sp *string, bp *[]byte) { // ERROR "bp does not escape$" "sp does not escape$"
1631 *sp = (*sp)[1:2] // ERROR "quux ignoring self-assignment in \*sp = \(\*sp\)\[1:2\]$"
1632 *bp = (*bp)[1:2] // ERROR "quux ignoring self-assignment in \*bp = \(\*bp\)\[1:2\]$"
1635 type StructWithString struct {
1640 // This is escape analysis false negative.
1641 // We assign the pointer to x.p but leak x.s. Escape analysis coarsens flows
1642 // to just x, and thus &i looks escaping.
1643 func fieldFlowTracking() {
1644 var x StructWithString
1645 i := 0 // ERROR "moved to heap: i$"
1647 sink = x.s // ERROR "x.s escapes to heap$"
1650 // String operations.
1652 func slicebytetostring0() {
1653 b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$"
1654 s := string(b) // ERROR "string\(b\) does not escape$"
1658 func slicebytetostring1() {
1659 b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$"
1660 s := string(b) // ERROR "string\(b\) does not escape$"
1665 func slicebytetostring2() {
1666 b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$"
1667 s := string(b) // ERROR "string\(b\) escapes to heap$"
1668 s1 := s[0:1] // ERROR "moved to heap: s1$"
1672 func slicebytetostring3() {
1673 b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$"
1674 s := string(b) // ERROR "string\(b\) escapes to heap$"
1676 sink = s1 // ERROR "s1 escapes to heap$"
1682 s := s0 + s1 // ERROR "s0 \+ s1 does not escape$"
1690 s += s0 + s1 // ERROR "s0 \+ s1 does not escape$"
1695 b := make([]byte, 20) // ERROR "make\(\[\]byte, 20\) does not escape$"
1697 s := string(b) + s0 // ERROR "string\(b\) \+ s0 does not escape$" "string\(b\) does not escape$"
1704 s := s0 + s1 // ERROR "s0 \+ s1 escapes to heap$"
1706 sink = s2 // ERROR "s2 escapes to heap$"
1709 func intstring0() bool {
1710 // string does not escape
1712 s := string(x) // ERROR "string\(x\) does not escape$"
1716 func intstring1() string {
1717 // string does not escape, but the buffer does
1719 s := string(x) // ERROR "string\(x\) escapes to heap$"
1724 // string escapes to heap
1726 s := string(x) // ERROR "moved to heap: s$" "string\(x\) escapes to heap$"
1730 func stringtoslicebyte0() {
1732 x := []byte(s) // ERROR "\(\[\]byte\)\(s\) does not escape$" "zero-copy string->\[\]byte conversion"
1736 func stringtoslicebyte1() []byte {
1738 return []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap$"
1741 func stringtoslicebyte2() {
1743 sink = []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap$"
1746 func stringtoslicerune0() {
1748 x := []rune(s) // ERROR "\(\[\]rune\)\(s\) does not escape$"
1752 func stringtoslicerune1() []rune {
1754 return []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap$"
1757 func stringtoslicerune2() {
1759 sink = []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap$"
1762 func slicerunetostring0() {
1763 r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$"
1764 s := string(r) // ERROR "string\(r\) does not escape$"
1768 func slicerunetostring1() string {
1769 r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$"
1770 return string(r) // ERROR "string\(r\) escapes to heap$"
1773 func slicerunetostring2() {
1774 r := []rune{1, 2, 3} // ERROR "\[\]rune{...} does not escape$"
1775 sink = string(r) // ERROR "string\(r\) escapes to heap$"
1779 m := make(map[int]int) // ERROR "make\(map\[int\]int\) does not escape$"
1783 sink = m[0] // ERROR "m\[0\] escapes to heap$"
1786 func makemap1() map[int]int {
1787 return make(map[int]int) // ERROR "make\(map\[int\]int\) escapes to heap$"
1791 m := make(map[int]int) // ERROR "make\(map\[int\]int\) escapes to heap$"
1795 func nonescapingEface(m map[interface{}]bool) bool { // ERROR "m does not escape$"
1796 return m["foo"] // ERROR ".foo. does not escape$"
1799 func nonescapingIface(m map[M]bool) bool { // ERROR "m does not escape$"
1800 return m[MV(0)] // ERROR "MV\(0\) does not escape$"
1804 x := new(int) // ERROR "new\(int\) escapes to heap$"
1808 func issue10353a(x *int) func() { // ERROR "leaking param: x$"
1809 return func() { // ERROR "func literal escapes to heap$"
1814 func issue10353b() {
1817 x := new(int) // ERROR "new\(int\) escapes to heap$"
1818 f = func() { // ERROR "func literal escapes to heap$"
1825 func issue11387(x int) func() int {
1826 f := func() int { return x } // ERROR "func literal escapes to heap"
1827 slice1 := []func() int{f} // ERROR "\[\].* does not escape"
1828 slice2 := make([]func() int, 1) // ERROR "make\(.*\) does not escape"
1829 copy(slice2, slice1)
1833 func issue12397(x, y int) { // ERROR "moved to heap: y$"
1834 // x does not escape below, because all relevant code is dead.