github.com/stingnevermore/go@v0.0.0-20180120041312-3810f5bfed72/test/escape_calls.go (about) 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 } 45 46 // Test for bug where func var f used prototype's escape analysis results. 47 func prototype(xyz []string) {} // ERROR "prototype xyz does not escape" 48 func 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 }