1// errorcheck -0 -N -m -l
2
3// Copyright 2016 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// The escape analyzer needs to run till its root set settles
8// (this is not that often, it turns out).
9// This test is likely to become stale because the leak depends
10// on a spurious-escape bug -- return an interface as a named
11// output parameter appears to cause the called closure to escape,
12// where returning it as a regular type does not.
13
14package main
15
16import (
17	"fmt"
18)
19
20type closure func(i, j int) ent
21
22type ent int
23
24func (e ent) String() string {
25	return fmt.Sprintf("%d", int(e)) // ERROR "... argument does not escape$" "int\(e\) escapes to heap$"
26}
27
28//go:noinline
29func foo(ops closure, j int) (err fmt.Stringer) { // ERROR "ops does not escape"
30	enqueue := func(i int) fmt.Stringer { // ERROR "func literal does not escape"
31		return ops(i, j) // ERROR "ops\(i, j\) escapes to heap$"
32	}
33	err = enqueue(4)
34	if err != nil {
35		return err
36	}
37	return // return result of enqueue, a fmt.Stringer
38}
39
40func main() {
41	// 3 identical functions, to get different escape behavior.
42	f := func(i, j int) ent { // ERROR "func literal does not escape"
43		return ent(i + j)
44	}
45	i := foo(f, 3).(ent)
46	fmt.Printf("foo(f,3)=%d\n", int(i)) // ERROR "int\(i\) escapes to heap$" "... argument does not escape$"
47}
48