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.
17 func noleak(p *int) int { // ERROR "p does not escape"
21 func leaktoret(p *int) *int { // ERROR "leaking param: p to result"
25 func leaktoret2(p *int) (*int, *int) { // ERROR "leaking param: p to result ~r0" "leaking param: p to result ~r1"
29 func leaktoret22(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r0" "leaking param: q to result ~r1"
33 func leaktoret22b(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r1" "leaking param: q to result ~r0"
34 return leaktoret22(q, p)
37 func leaktoret22c(p, q *int) (*int, *int) { // ERROR "leaking param: p to result ~r1" "leaking param: q to result ~r0"
38 r, s := leaktoret22(q, p)
42 func leaktoret22d(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 leaktoret22e(p, q *int) (r, s *int) { // ERROR "leaking param: p to result s" "leaking param: q to result r"
48 r, s = leaktoret22(q, p)
52 func leaktoret22f(p, q *int) (r, s *int) { // ERROR "leaking param: p to result s" "leaking param: q to result r"
53 rr, ss := leaktoret22(q, p)
59 func leaktosink(p *int) *int { // ERROR "leaking param: p"
77 var x int // ERROR "moved to heap: x"
83 var x int // ERROR "moved to heap: x"
84 p, q := leaktoret2(&x)
91 leaktoret22(leaktoret2(&x))
95 var x int // ERROR "moved to heap: x"
96 px1, px2 := leaktoret22(leaktoret2(&x))
101 type T struct{ x int }
103 func (t *T) Foo(u int) (*T, bool) { // ERROR "leaking param: t to result"
109 r, _ := new(T).Foo(42) // ERROR "new.T. escapes to heap"
113 func leakrecursive1(p, q *int) (*int, *int) { // ERROR "leaking param: p" "leaking param: q"
114 return leakrecursive2(q, p)
117 func leakrecursive2(p, q *int) (*int, *int) { // ERROR "leaking param: p" "leaking param: q"
119 return leakrecursive1(q, p)
121 // without this, leakrecursive? are safe for p and q, b/c in fact their graph does not have leaking edges.
125 var global interface{}
135 func f8(p *T1) (k T2) { // ERROR "leaking param: p$"
141 // should make p leak always
147 var j T1 // ERROR "moved to heap: j"
152 // These don't escape but are too big for the stack
153 var x [1 << 30]byte // ERROR "moved to heap: x"
154 var y = make([]byte, 1<<30) // ERROR "make\(\[\]byte, 1073741824\) escapes to heap"
158 // Test for issue 19687 (passing to unnamed parameters does not escape).
167 runtime.KeepAlive(&x)
170 // Test for issue 24305 (passing to unnamed receivers does not escape).
188 // Issue 24730: taking address in a loop causes unnecessary escape
193 func (t *T24730) g() { // ERROR "t does not escape"
195 for i := range t.x[:] {
201 for i := range t.x[:] {
207 // Issue 15730: copy causes unnecessary escape
213 func f15730a(args ...interface{}) { // ERROR "args does not escape"
214 for _, arg := range args {
215 switch a := arg.(type) {
222 func f15730b(args ...interface{}) { // ERROR "args does not escape"
223 for _, arg := range args {
224 switch a := arg.(type) {
231 func f15730c(args ...interface{}) { // ERROR "leaking param content: args"
232 for _, arg := range args {
233 switch a := arg.(type) {
235 // copy pointerful data should cause escape
241 // Issue 29000: unnamed parameter is not handled correctly
243 var sink4 interface{}
244 var alwaysFalse = false
246 func f29000(_ int, x interface{}) { // ERROR "leaking param: x"
255 f29000(2, x) // ERROR "x escapes to heap"
258 // Issue 28369: taking an address of a parameter and converting it into a uintptr causes an
259 // unnecessary escape.
261 var sink28369 uintptr
263 func f28369(n int) int {
265 sink28369 = uintptr(unsafe.Pointer(&n))
269 return 1 + f28369(n-1)
272 // Issue 44614: parameters that flow to a heap-allocated result
273 // parameter must be recorded as a heap-flow rather than a
276 // N.B., must match "leaking param: p",
277 // but *not* "leaking param: p to result r level=0".
278 func f(p *int) (r *int) { // ERROR "leaking param: p$" "moved to heap: r"