3 // Copyright 2012 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.
12 func noleak(p *int) int { // ERROR "p does not escape"
16 func leaktoret(p *int) *int { // ERROR "leaking param: p to result"
20 func leaktoret2(p *int) (*int, *int) { // ERROR "leaking param: p to result ~r1" "leaking param: p to result ~r2"
24 func leaktoret22(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r2" "leaking param: q to result ~r3"
28 func leaktoret22b(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r3" "leaking param: q to result ~r2"
29 return leaktoret22(q, p)
32 func leaktoret22c(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r3" "leaking param: q to result ~r2"
33 r, s := leaktoret22(q, p)
37 func leaktoret22d(p, q *int) (r, s *int) { // ERROR "leaking param: p to result s" "leaking param: q to result r"
38 r, s = leaktoret22(q, p)
42 func leaktoret22e(p, q *int) (r, s *int) { // ERROR "leaking param: p to result s" "leaking param: q to result r"
43 r, s = leaktoret22(q, p)
47 func leaktoret22f(p, q *int) (r, s *int) { // ERROR "leaking param: p to result s" "leaking param: q to result r"
48 rr, ss := leaktoret22(q, p)
54 func leaktosink(p *int) *int { // ERROR "leaking param: p"
61 p := noleak(&x) // ERROR "&x does not escape"
67 p := leaktoret(&x) // ERROR "&x does not escape"
72 var x int // ERROR "moved to heap: x"
73 p := leaktoret(&x) // ERROR "&x escapes to heap"
78 var x int // ERROR "moved to heap: x"
79 p, q := leaktoret2(&x) // ERROR "&x escapes to heap"
86 leaktoret22(leaktoret2(&x)) // ERROR "&x does not escape"
90 var x int // ERROR "moved to heap: x"
91 px1, px2 := leaktoret22(leaktoret2(&x)) // ERROR "&x escapes to heap"
96 type T struct{ x int }
98 func (t *T) Foo(u int) (*T, bool) { // ERROR "leaking param: t to result"
104 r, _ := new(T).Foo(42) // ERROR "new.T. escapes to heap"
108 func leakrecursive1(p, q *int) (*int, *int) { // ERROR "leaking param: p" "leaking param: q"
109 return leakrecursive2(q, p)
112 func leakrecursive2(p, q *int) (*int, *int) { // ERROR "leaking param: p" "leaking param: q"
114 return leakrecursive1(q, p)
116 // without this, leakrecursive? are safe for p and q, b/c in fact their graph does not have leaking edges.
120 var global interface{}
130 func f8(p *T1) (k T2) { // ERROR "leaking param: p to result k" "leaking param: p"
136 // should make p leak always
137 global = p // ERROR "p escapes to heap"
142 var j T1 // ERROR "moved to heap: j"
143 f8(&j) // ERROR "&j escapes to heap"
147 // These don't escape but are too big for the stack
148 var x [1 << 30]byte // ERROR "moved to heap: x"
149 var y = make([]byte, 1<<30) // ERROR "make\(\[\]byte, 1 << 30\) escapes to heap"