blob: 20cb6433346f748643245067b5f5cb6457154cd3 [file] [log] [blame]
Dan Willemsen09eb3b12015-09-16 14:34:17 -07001// errorcheck -0 -m -l
2
Dan Willemsen38f2dba2016-07-08 14:54:35 -07003// Copyright 2015 The Go Authors. All rights reserved.
Dan Willemsen09eb3b12015-09-16 14:34:17 -07004// 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
12package foo
13
14func f(buf []byte) []byte { // ERROR "leaking param: buf to result ~r1 level=0$"
15 return buf
16}
17
18func g(*byte) string
19
20func 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
25type Node struct {
26 s string
27 left, right *Node
28}
29
30func 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}
45
46// Test for bug where func var f used prototype's escape analysis results.
47func prototype(xyz []string) {} // ERROR "prototype xyz does not escape"
48func bar() {
49 var got [][]string
50 f := prototype
51 f = func(ss []string) { got = append(got, ss) } // ERROR "leaking param: ss" "func literal does not escape"
52 s := "string"
53 f([]string{s}) // ERROR "\[\]string literal escapes to heap"
54}