Home | History | Annotate | Download | only in fixedbugs
      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 
     14 package main
     15 
     16 import (
     17 	"fmt"
     18 )
     19 
     20 type closure func(i, j int) ent
     21 
     22 type ent int
     23 
     24 func (e ent) String() string {
     25 	return fmt.Sprintf("%d", int(e)) // ERROR "ent.String ... argument does not escape$" "int\(e\) escapes to heap$"
     26 }
     27 
     28 //go:noinline
     29 func foo(ops closure, j int) (err fmt.Stringer) { // ERROR "leaking param: ops$" "leaking param: ops to result err level=0$"
     30 	enqueue := func(i int) fmt.Stringer { // ERROR "func literal escapes to heap$"
     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 
     40 func main() {
     41 	// 3 identical functions, to get different escape behavior.
     42 	f := func(i, j int) ent { // ERROR "func literal escapes to heap$"
     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$" "main ... argument does not escape$"
     47 }
     48