]> Cypherpunks.ru repositories - gostls13.git/blob - test/escape_calls.go
[dev.ssa] Merge remote-tracking branch 'origin/master' into dev.ssa
[gostls13.git] / test / escape_calls.go
1 // errorcheck -0 -m -l
2
3 // Copyright 2015 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.
6
7 // Test escape analysis for function parameters.
8
9 // In this test almost everything is BAD except the simplest cases
10 // where input directly flows to output.
11
12 package foo
13
14 func f(buf []byte) []byte { // ERROR "leaking param: buf to result ~r1 level=0$"
15         return buf
16 }
17
18 func g(*byte) string
19
20 func h(e int) {
21         var x [32]byte // ERROR "moved to heap: x$"
22         g(&f(x[:])[0]) // ERROR "&f\(x\[:\]\)\[0\] escapes to heap$" "x escapes to heap$"
23 }
24
25 type Node struct {
26         s           string
27         left, right *Node
28 }
29
30 func walk(np **Node) int { // ERROR "leaking param content: np"
31         n := *np
32         w := len(n.s)
33         if n == nil {
34                 return 0
35         }
36         wl := walk(&n.left)  // ERROR "walk &n.left does not escape"
37         wr := walk(&n.right) // ERROR "walk &n.right does not escape"
38         if wl < wr {
39                 n.left, n.right = n.right, n.left
40                 wl, wr = wr, wl
41         }
42         *np = n
43         return w + wl + wr
44 }