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 {
206 x := 42 // ERROR "moved to heap: x"
207 return func() int { // ERROR "func literal escapes to heap"
208 return x // ERROR "&x escapes to heap"
214 return func() int { // ERROR "func literal does not escape"
219 func foo23(x int) func() int { // ERROR "moved to heap: x"
220 return func() int { // ERROR "func literal escapes to heap"
221 return x // ERROR "&x escapes to heap"
225 func foo23a(x int) func() int { // ERROR "moved to heap: x"
226 f := func() int { // ERROR "func literal escapes to heap"
227 return x // ERROR "&x escapes to heap"
232 func foo23b(x int) *(func() int) { // ERROR "moved to heap: x"
233 f := func() int { return x } // ERROR "moved to heap: f" "func literal escapes to heap" "&x escapes to heap"
234 return &f // ERROR "&f escapes to heap"
237 func foo24(x int) int {
238 return func() int { // ERROR "func literal does not escape"
245 func fooleak(xx *int) int { // ERROR "leaking param: xx"
250 func foonoleak(xx *int) int { // ERROR "xx does not escape"
254 func foo31(x int) int { // ERROR "moved to heap: x"
255 return fooleak(&x) // ERROR "&x escapes to heap"
258 func foo32(x int) int {
259 return foonoleak(&x) // ERROR "&x does not escape"
270 func (f *Foo) fooleak() { // ERROR "leaking param: f"
274 func (f *Foo) foonoleak() { // ERROR "f does not escape"
278 func (f *Foo) Leak() { // ERROR "leaking param: f"
282 func (f *Foo) NoLeak() { // ERROR "f does not escape"
286 func foo41(x int) { // ERROR "moved to heap: x"
287 F.xx = &x // ERROR "&x escapes to heap"
290 func (f *Foo) foo42(x int) { // ERROR "f does not escape" "moved to heap: x"
291 f.xx = &x // ERROR "&x escapes to heap"
294 func foo43(f *Foo, x int) { // ERROR "f does not escape" "moved to heap: x"
295 f.xx = &x // ERROR "&x escapes to heap"
298 func foo44(yy *int) { // ERROR "leaking param: yy"
302 func (f *Foo) foo45() { // ERROR "f does not escape"
306 // See foo13 above for explanation of why f leaks.
307 func (f *Foo) foo46() { // ERROR "leaking param: f"
311 func (f *Foo) foo47() { // ERROR "leaking param: f"
312 f.xx = &f.x // ERROR "&f.x escapes to heap"
317 func foo50(i *int) { // ERROR "leaking param: i"
321 var ptrMap map[*int]*int
323 func foo51(i *int) { // ERROR "leaking param: i"
327 func indaddr1(x int) *int { // ERROR "moved to heap: x"
328 return &x // ERROR "&x escapes to heap"
331 func indaddr2(x *int) *int { // ERROR "leaking param: x"
332 return *&x // ERROR "&x does not escape"
335 func indaddr3(x *int32) *int { // ERROR "leaking param: x"
336 return *(**int)(unsafe.Pointer(&x)) // ERROR "&x does not escape"
339 // From package math:
341 func Float32bits(f float32) uint32 {
342 return *(*uint32)(unsafe.Pointer(&f)) // ERROR "&f does not escape"
345 func Float32frombits(b uint32) float32 {
346 return *(*float32)(unsafe.Pointer(&b)) // ERROR "&b does not escape"
349 func Float64bits(f float64) uint64 {
350 return *(*uint64)(unsafe.Pointer(&f)) // ERROR "&f does not escape"
353 func Float64frombits(b uint64) float64 {
354 return *(*float64)(unsafe.Pointer(&b)) // ERROR "&b does not escape"
358 func float64bitsptr(f float64) *uint64 { // ERROR "moved to heap: f"
359 return (*uint64)(unsafe.Pointer(&f)) // ERROR "&f escapes to heap"
362 func float64ptrbitsptr(f *float64) *uint64 { // ERROR "leaking param: f"
363 return (*uint64)(unsafe.Pointer(f))
366 func typesw(i interface{}) *int { // ERROR "leaking param: i"
367 switch val := i.(type) {
371 v := int(*val) // ERROR "moved to heap: v"
372 return &v // ERROR "&v escapes to heap"
377 func exprsw(i *int) *int { // ERROR "leaking param: i"
378 switch j := i; *j + 110 {
388 // assigning to an array element is like assigning to the array
389 func foo60(i *int) *int { // ERROR "leaking param: i"
395 func foo60a(i *int) *int { // ERROR "i does not escape"
401 // assigning to a struct field is like assigning to the struct
402 func foo61(i *int) *int { // ERROR "leaking param: i"
411 func foo61a(i *int) *int { // ERROR "i does not escape"
420 // assigning to a struct field is like assigning to the struct but
421 // here this subtlety is lost, since s.a counts as an assignment to a
422 // track-losing dereference.
423 func foo62(i *int) *int { // ERROR "leaking param: i"
427 s := new(S) // ERROR "new[(]S[)] does not escape"
436 func foo63(m M) { // ERROR "m does not escape"
439 func foo64(m M) { // ERROR "leaking param: m"
443 func foo64b(m M) { // ERROR "leaking param: m"
453 foo63(&mv) // ERROR "&mv does not escape"
457 var mv MV // ERROR "moved to heap: mv"
458 foo64(&mv) // ERROR "&mv escapes to heap"
468 foo64(mv) // escapes but it's an int so irrelevant
471 func foo69(m M) { // ERROR "leaking param: m"
475 func foo70(mv1 *MV, m M) { // ERROR "leaking param: mv1" "leaking param: m"
480 func foo71(x *int) []*int { // ERROR "leaking param: x"
486 func foo71a(x int) []*int { // ERROR "moved to heap: x"
488 y = append(y, &x) // ERROR "&x escapes to heap"
495 y[0] = &x // ERROR "&x does not escape"
498 func foo72aa() [10]*int {
499 var x int // ERROR "moved to heap: x"
501 y[0] = &x // ERROR "&x escapes to heap"
507 for i := 0; i < 10; i++ {
509 x := i // ERROR "moved to heap: x"
510 y[i] = &x // ERROR "&x escapes to heap"
515 func foo72b() [10]*int {
517 for i := 0; i < 10; i++ {
518 x := i // ERROR "moved to heap: x"
519 y[i] = &x // ERROR "&x escapes to heap"
526 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
527 for _, v := range s {
528 vv := v // ERROR "moved to heap: vv"
529 // actually just escapes its scope
530 defer func() { // ERROR "func literal escapes to heap"
531 println(vv) // ERROR "&vv escapes to heap"
537 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
538 for _, v := range s {
539 vv := v // ERROR "moved to heap: vv"
540 // actually just escapes its scope
541 fn := func() { // ERROR "func literal escapes to heap"
542 println(vv) // ERROR "&vv escapes to heap"
551 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
552 for i, v := range s {
553 vv := v // ERROR "moved to heap: vv"
554 // actually just escapes its scope
555 array[i] = func() { // ERROR "func literal escapes to heap"
556 println(vv) // ERROR "&vv escapes to heap"
561 func myprint(y *int, x ...interface{}) *int { // ERROR "x does not escape" "leaking param: y"
565 func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "y does not escape" "leaking param: x"
566 return &x[0] // ERROR "&x.0. escapes to heap"
569 func foo75(z *int) { // ERROR "z does not escape"
570 myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
573 func foo75a(z *int) { // ERROR "z does not escape"
574 myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
577 func foo75esc(z *int) { // ERROR "leaking param: z"
578 gxx = myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
581 func foo75aesc(z *int) { // ERROR "z does not escape"
582 var ppi **interface{} // assignments to pointer dereferences lose track
583 *ppi = myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
586 func foo76(z *int) { // ERROR "leaking param: z"
587 myprint(nil, z) // ERROR "[.][.][.] argument does not escape"
590 func foo76a(z *int) { // ERROR "leaking param: z"
591 myprint1(nil, z) // ERROR "[.][.][.] argument does not escape"
595 myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
599 myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
603 defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
607 defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
612 // TODO: This one really only escapes its scope, but we don't distinguish yet.
613 defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
619 defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
623 func foo77(z []interface{}) { // ERROR "z does not escape"
624 myprint(nil, z...) // z does not escape
627 func foo77a(z []interface{}) { // ERROR "z does not escape"
631 func foo77b(z []interface{}) { // ERROR "leaking param: z"
632 var ppi **interface{}
633 *ppi = myprint1(nil, z...)
636 func foo78(z int) *int { // ERROR "moved to heap: z"
637 return &z // ERROR "&z escapes to heap"
640 func foo78a(z int) *int { // ERROR "moved to heap: z"
641 y := &z // ERROR "&z escapes to heap"
642 x := &y // ERROR "&y does not escape"
643 return *x // really return y
647 return new(int) // ERROR "new[(]int[)] escapes to heap"
653 // Really just escapes its scope but we don't distinguish
654 z = new(int) // ERROR "new[(]int[)] escapes to heap"
662 z := new(int) // ERROR "new[(]int[)] does not escape"
668 func tee(p *int) (x, y *int) { return p, p } // ERROR "leaking param"
670 func noop(x, y *int) {} // ERROR "does not escape"
673 var x, y, z int // ERROR "moved to heap"
674 go noop(tee(&z)) // ERROR "&z escapes to heap"
675 go noop(&x, &y) // ERROR "escapes to heap"
677 var u, v, w int // ERROR "moved to heap"
678 defer noop(tee(&u)) // ERROR "&u escapes to heap"
679 defer noop(&v, &w) // ERROR "escapes to heap"
683 type Fooer interface {
687 type LimitedFooer struct {
692 func LimitFooer(r Fooer, n int64) Fooer { // ERROR "leaking param: r"
693 return &LimitedFooer{r, n} // ERROR "&LimitedFooer literal escapes to heap"
696 func foo90(x *int) map[*int]*int { // ERROR "leaking param: x"
697 return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int literal escapes to heap"
700 func foo91(x *int) map[*int]*int { // ERROR "leaking param: x"
701 return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int literal escapes to heap"
704 func foo92(x *int) [2]*int { // ERROR "leaking param: x"
705 return [2]*int{x, nil}
709 func foo93(c chan *int) *int { // ERROR "c does not escape"
717 func foo94(m map[*int]*int, b bool) *int { // ERROR "m does not escape"
718 for k, v := range m {
728 func foo95(m map[*int]*int, x *int) { // ERROR "m does not escape" "leaking param: x"
733 func foo96(m []*int) *int { // ERROR "m does not escape"
738 func foo97(m [1]*int) *int { // ERROR "leaking param: m"
743 func foo98(m map[int]*int) *int { // ERROR "m does not escape"
748 func foo99(m *[1]*int) []*int { // ERROR "leaking param: m"
753 func foo100(m []*int) *int { // ERROR "m does not escape"
754 for _, v := range m {
761 func foo101(m [1]*int) *int { // ERROR "leaking param: m"
762 for _, v := range m {
769 func foo101a(m [1]*int) *int { // ERROR "m does not escape"
770 for i := range m { // ERROR "moved to heap: i"
771 return &i // ERROR "&i escapes to heap"
777 func foo102(m []*int, x *int) { // ERROR "m does not escape" "leaking param: x"
782 func foo103(m [1]*int, x *int) { // ERROR "m does not escape" "x does not escape"
789 func foo104(x []*int) { // ERROR "x does not escape"
794 func foo105(x []*int) { // ERROR "x does not escape"
799 func foo106(x *int) { // ERROR "leaking param: x"
803 func foo107(x *int) map[*int]*int { // ERROR "leaking param: x"
804 return map[*int]*int{x: nil} // ERROR "map.* literal escapes to heap"
807 func foo108(x *int) map[*int]*int { // ERROR "leaking param: x"
808 return map[*int]*int{nil: x} // ERROR "map.* literal escapes to heap"
811 func foo109(x *int) *int { // ERROR "leaking param: x"
812 m := map[*int]*int{x: nil} // ERROR "map.* literal does not escape"
813 for k, _ := range m {
819 func foo110(x *int) *int { // ERROR "leaking param: x"
820 m := map[*int]*int{nil: x} // ERROR "map.* literal does not escape"
824 func foo111(x *int) *int { // ERROR "leaking param: x"
825 m := []*int{x} // ERROR "\[\]\*int literal does not escape"
829 func foo112(x *int) *int { // ERROR "leaking param: x"
834 func foo113(x *int) *int { // ERROR "leaking param: x"
839 func foo114(x *int) *int { // ERROR "leaking param: x"
840 m := &Bar{ii: x} // ERROR "&Bar literal does not escape"
844 func foo115(x *int) *int { // ERROR "leaking param: x"
845 return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1))
848 func foo116(b bool) *int {
850 x := 1 // ERROR "moved to heap: x"
851 return &x // ERROR "&x escapes to heap"
853 y := 1 // ERROR "moved to heap: y"
854 return &y // ERROR "&y escapes to heap"
859 func foo117(unknown func(interface{})) { // ERROR "unknown does not escape"
860 x := 1 // ERROR "moved to heap: x"
861 unknown(&x) // ERROR "&x escapes to heap"
864 func foo118(unknown func(*int)) { // ERROR "unknown does not escape"
865 x := 1 // ERROR "moved to heap: x"
866 unknown(&x) // ERROR "&x escapes to heap"
871 func foo119(x *int) { // ERROR "leaking param: x"
876 // formerly exponential time analysis
977 // use the labels to silence compiler errors
1081 for i := 0; i < 10; i++ {
1082 defer myprint(nil, i) // ERROR "[.][.][.] argument escapes to heap"
1083 go myprint(nil, i) // ERROR "[.][.][.] argument escapes to heap"
1087 // same as foo121 but check across import
1089 for i := 0; i < 10; i++ {
1090 defer fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes to heap"
1091 go fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes to heap"
1095 // a harmless forward jump
1101 i = new(int) // ERROR "new.int. does not escape"
1105 // a backward jump, increases loopdepth
1110 i = new(int) // ERROR "new.int. escapes to heap"
1116 func foo124(x **int) { // ERROR "x does not escape"
1117 var i int // ERROR "moved to heap: i"
1118 p := &i // ERROR "&i escapes"
1119 func() { // ERROR "func literal does not escape"
1120 *x = p // ERROR "leaking closure reference p"
1124 func foo125(ch chan *int) { // ERROR "does not escape"
1125 var i int // ERROR "moved to heap"
1126 p := &i // ERROR "&i escapes to heap"
1127 func() { // ERROR "func literal does not escape"
1128 ch <- p // ERROR "leaking closure reference p"
1133 var px *int // loopdepth 0
1136 var i int // ERROR "moved to heap"
1137 func() { // ERROR "func literal does not escape"
1138 px = &i // ERROR "&i escapes"
1147 var i int // ERROR "moved to heap: i"
1148 p := &i // ERROR "&i escapes to heap"
1155 p := &i // ERROR "&i does not escape"
1161 var i int // ERROR "moved to heap: i"
1162 p := &i // ERROR "&i escapes to heap"
1163 func() { // ERROR "func literal does not escape"
1164 q := p // ERROR "leaking closure reference p"
1165 func() { // ERROR "func literal does not escape"
1166 r := q // ERROR "leaking closure reference q"
1174 var i int // ERROR "moved to heap"
1175 func() { // ERROR "func literal does not escape"
1176 px = &i // ERROR "&i escapes" "leaking closure reference i"
1182 var i int // ERROR "moved to heap"
1183 func() { // ERROR "func literal does not escape"
1184 px = &i // ERROR "&i escapes" "leaking closure reference i"
1189 var i int // ERROR "moved to heap"
1190 go func() { // ERROR "func literal escapes to heap"
1191 px = &i // ERROR "&i escapes" "leaking closure reference i"
1196 var i int // ERROR "moved to heap"
1197 defer func() { // ERROR "func literal does not escape"
1198 px = &i // ERROR "&i escapes" "leaking closure reference i"
1204 p := &i // ERROR "&i does not escape"
1205 func() { // ERROR "func literal does not escape"
1207 func() { // ERROR "func literal does not escape"
1215 var i int // ERROR "moved to heap: i"
1216 p := &i // ERROR "&i escapes to heap" "moved to heap: p"
1217 go func() { // ERROR "func literal escapes to heap"
1218 q := p // ERROR "&p escapes to heap"
1219 func() { // ERROR "func literal does not escape"
1227 var i int // ERROR "moved to heap: i"
1228 p := &i // ERROR "&i escapes to heap" "moved to heap: p"
1229 go func() { // ERROR "func literal escapes to heap"
1230 q := p // ERROR "&p escapes to heap" "leaking closure reference p"
1231 func() { // ERROR "func literal does not escape"
1232 r := q // ERROR "leaking closure reference q"
1239 var i int // ERROR "moved to heap: i"
1240 p := &i // ERROR "&i escapes to heap"
1241 func() { // ERROR "func literal does not escape"
1242 q := p // ERROR "leaking closure reference p" "moved to heap: q"
1243 go func() { // ERROR "func literal escapes to heap"
1244 r := q // ERROR "&q escapes to heap"
1250 func foo138() *byte {
1254 t := new(T) // ERROR "new.T. escapes to heap"
1255 return &t.x[0] // ERROR "&t.x.0. escapes to heap"
1258 func foo139() *byte {
1264 t := new(T) // ERROR "new.T. escapes to heap"
1265 return &t.x.y // ERROR "&t.x.y escapes to heap"
1269 func foo140() interface{} {
1277 t := &T{} // ERROR "&T literal escapes to heap"
1292 func F3(x []byte) // ERROR "F3 x does not escape"
1298 F1(buf1[:]) // ERROR "buf1 does not escape"
1300 var buf2 [10]byte // ERROR "moved to heap: buf2"
1301 F2(buf2[:]) // ERROR "buf2 escapes to heap"
1304 F3(buf3[:]) // ERROR "buf3 does not escape"
1306 var buf4 [10]byte // ERROR "moved to heap: buf4"
1307 F4(buf4[:]) // ERROR "buf4 escapes to heap"
1314 func (t *Tm) M() { // ERROR "t does not escape"
1320 t := new(Tm) // ERROR "escapes to heap"
1321 f = t.M // ERROR "t.M does not escape"
1328 t := new(Tm) // ERROR "escapes to heap"
1329 gf = t.M // ERROR "t.M escapes to heap"
1334 for i := 0; i < 1000; i++ {
1335 func() { // ERROR "func literal does not escape"
1336 for i := 0; i < 1; i++ {
1338 t.M() // ERROR "t does not escape"
1345 // Check that annotations take effect regardless of whether they
1346 // are before or after the use in the source code.
1354 foo144a(&x) // ERROR "&x does not escape"
1356 foo144b(&y) // ERROR "&y does not escape"
1363 // issue 7313: for loop init should not be treated as "in loop"
1369 func foo145(l List) { // ERROR "l does not escape"
1371 for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1375 func foo146(l List) { // ERROR "l does not escape"
1377 p = &l // ERROR "&l does not escape"
1378 for ; p.Next != nil; p = p.Next {
1382 func foo147(l List) { // ERROR "l does not escape"
1384 p = &l // ERROR "&l does not escape"
1390 func foo148(l List) { // ERROR " l does not escape"
1391 for p := &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1395 // related: address of variable should have depth of variable, not of loop
1397 func foo149(l List) { // ERROR " l does not escape"
1400 for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1405 // issue 7934: missed ... if element type had no pointers
1409 func foo150(x ...byte) { // ERROR "leaking param: x"
1414 foo150(1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
1417 // issue 7931: bad handling of slice of array
1421 func foo151(x *int) { // ERROR "leaking param: x"
1426 var a [64]int // ERROR "moved to heap: a"
1428 foo151(&(&a)[4:8][0]) // ERROR "&\(&a\)\[4:8\]\[0\] escapes to heap" "&a escapes to heap"
1432 var a [10]int // ERROR "moved to heap: a"
1433 b := a[:] // ERROR "a escapes to heap"
1434 foo151(&b[4:8][0]) // ERROR "&b\[4:8\]\[0\] escapes to heap"
1438 var a [64]int // ERROR "moved to heap: a"
1440 foo151(&(&a)[4:8:8][0]) // ERROR "&\(&a\)\[4:8:8\]\[0\] escapes to heap" "&a escapes to heap"
1444 var a [10]int // ERROR "moved to heap: a"
1445 b := a[:] // ERROR "a escapes to heap"
1446 foo151(&b[4:8:8][0]) // ERROR "&b\[4:8:8\]\[0\] escapes to heap"
1455 func (u *U) String() *string { // ERROR "leaking param u content to result ~r0"
1463 func NewV(u U) *V { // ERROR "leaking param: u"
1464 return &V{u.String()} // ERROR "&V literal escapes to heap" "u does not escape"
1468 a := "a" // ERROR "moved to heap: a"
1469 u := U{&a} // ERROR "&a escapes to heap"
1474 // issue 8176 - &x in type switch body not marked as escaping
1476 func foo153(v interface{}) *int { // ERROR "leaking param: v"
1477 switch x := v.(type) {
1478 case int: // ERROR "moved to heap: x"
1479 return &x // ERROR "&x escapes to heap"
1484 // issue 8185 - &result escaping into result
1486 func f() (x int, y *int) { // ERROR "moved to heap: x"
1487 y = &x // ERROR "&x escapes to heap"
1491 func g() (x interface{}) { // ERROR "moved to heap: x"
1492 x = &x // ERROR "&x escapes to heap"
1496 var sink interface{}
1502 func ptrlitNoescape() {
1503 // Both literal and element do not escape.
1505 x := &Lit{&i} // ERROR "&Lit literal does not escape" "&i does not escape"
1509 func ptrlitNoEscape2() {
1510 // Literal does not escape, but element does.
1511 i := 0 // ERROR "moved to heap: i"
1512 x := &Lit{&i} // ERROR "&Lit literal does not escape" "&i escapes to heap"
1516 func ptrlitEscape() {
1517 // Both literal and element escape.
1518 i := 0 // ERROR "moved to heap: i"
1519 x := &Lit{&i} // ERROR "&Lit literal escapes to heap" "&i escapes to heap"
1525 type Buffer struct {
1533 func (b *Buffer) foo() { // ERROR "b does not escape"
1534 b.buf1 = b.buf1[1:2] // ERROR "ignoring self-assignment to b.buf1"
1535 b.buf1 = b.buf1[1:2:3] // ERROR "ignoring self-assignment to b.buf1"
1536 b.buf1 = b.buf2[1:2] // ERROR "ignoring self-assignment to b.buf1"
1537 b.buf1 = b.buf2[1:2:3] // ERROR "ignoring self-assignment to b.buf1"
1540 func (b *Buffer) bar() { // ERROR "leaking param: b"
1541 b.buf1 = b.arr[1:2] // ERROR "b.arr escapes to heap"
1544 func (b *Buffer) baz() { // ERROR "b does not escape"
1545 b.str1 = b.str1[1:2] // ERROR "ignoring self-assignment to b.str1"
1546 b.str1 = b.str2[1:2] // ERROR "ignoring self-assignment to b.str1"
1549 func (b *Buffer) bat() { // ERROR "leaking param: b"
1550 o := new(Buffer) // ERROR "new\(Buffer\) escapes to heap"
1551 o.buf1 = b.buf1[1:2]
1555 func quux(sp *string, bp *[]byte) { // ERROR "sp does not escape" "bp does not escape"
1556 *sp = (*sp)[1:2] // ERROR "quux ignoring self-assignment to \*sp"
1557 *bp = (*bp)[1:2] // ERROR "quux ignoring self-assignment to \*bp"
1560 type StructWithString struct {
1565 // This is escape analysis false negative.
1566 // We assign the pointer to x.p but leak x.s. Escape analysis coarsens flows
1567 // to just x, and thus &i looks escaping.
1568 func fieldFlowTracking() {
1569 var x StructWithString
1570 i := 0 // ERROR "moved to heap: i"
1571 x.p = &i // ERROR "&i escapes to heap"
1575 // String operations.
1577 func slicebytetostring0() {
1578 b := make([]byte, 20) // ERROR "does not escape"
1579 s := string(b) // ERROR "string\(b\) does not escape"
1583 func slicebytetostring1() {
1584 b := make([]byte, 20) // ERROR "does not escape"
1585 s := string(b) // ERROR "string\(b\) does not escape"
1590 func slicebytetostring2() {
1591 b := make([]byte, 20) // ERROR "does not escape"
1592 s := string(b) // ERROR "string\(b\) escapes to heap"
1593 s1 := s[0:1] // ERROR "moved to heap: s1"
1594 sink = &s1 // ERROR "&s1 escapes to heap"
1597 func slicebytetostring3() {
1598 b := make([]byte, 20) // ERROR "does not escape"
1599 s := string(b) // ERROR "string\(b\) escapes to heap"
1607 s := s0 + s1 // ERROR "s0 \+ s1 does not escape"
1615 s += s0 + s1 // ERROR "s0 \+ s1 does not escape"
1620 b := make([]byte, 20) // ERROR "does not escape"
1622 s := string(b) + s0 // ERROR "string\(b\) does not escape" "string\(b\) \+ s0 does not escape"
1629 s := s0 + s1 // ERROR "s0 \+ s1 escapes to heap"
1634 func intstring0() bool {
1635 // string does not escape
1637 s := string(x) // ERROR "string\(x\) does not escape"
1641 func intstring1() string {
1642 // string does not escape, but the buffer does
1644 s := string(x) // ERROR "string\(x\) escapes to heap"
1649 // string escapes to heap
1651 s := string(x) // ERROR "string\(x\) escapes to heap" "moved to heap: s"
1652 sink = &s // ERROR "&s escapes to heap"