]> Cypherpunks.ru repositories - gostls13.git/blob - test/escape_slice.go
test: add tests for escape analysis of slices
[gostls13.git] / test / escape_slice.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 slices.
8
9 package escape
10
11 var sink interface{}
12
13 func slice0() {
14         var s []*int
15         // BAD: i should not escape
16         i := 0            // ERROR "moved to heap: i"
17         s = append(s, &i) // ERROR "&i escapes to heap"
18         _ = s
19 }
20
21 func slice1() *int {
22         var s []*int
23         i := 0            // ERROR "moved to heap: i"
24         s = append(s, &i) // ERROR "&i escapes to heap"
25         return s[0]
26 }
27
28 func slice2() []*int {
29         var s []*int
30         i := 0            // ERROR "moved to heap: i"
31         s = append(s, &i) // ERROR "&i escapes to heap"
32         return s
33 }
34
35 func slice3() *int {
36         var s []*int
37         i := 0            // ERROR "moved to heap: i"
38         s = append(s, &i) // ERROR "&i escapes to heap"
39         for _, p := range s {
40                 return p
41         }
42         return nil
43 }
44
45 func slice4(s []*int) { // ERROR "s does not escape"
46         i := 0    // ERROR "moved to heap: i"
47         s[0] = &i // ERROR "&i escapes to heap"
48 }
49
50 func slice5(s []*int) { // ERROR "s does not escape"
51         if s != nil {
52                 s = make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
53         }
54         i := 0    // ERROR "moved to heap: i"
55         s[0] = &i // ERROR "&i escapes to heap"
56 }
57
58 func slice6() {
59         s := make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
60         // BAD: i should not escape
61         i := 0    // ERROR "moved to heap: i"
62         s[0] = &i // ERROR "&i escapes to heap"
63         _ = s
64 }
65
66 func slice7() *int {
67         s := make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
68         i := 0                // ERROR "moved to heap: i"
69         s[0] = &i             // ERROR "&i escapes to heap"
70         return s[0]
71 }
72
73 func slice8() {
74         // BAD: i should not escape here
75         i := 0          // ERROR "moved to heap: i"
76         s := []*int{&i} // ERROR "&i escapes to heap" "literal does not escape"
77         _ = s
78 }
79
80 func slice9() *int {
81         i := 0          // ERROR "moved to heap: i"
82         s := []*int{&i} // ERROR "&i escapes to heap" "literal does not escape"
83         return s[0]
84 }
85
86 func slice10() []*int {
87         i := 0          // ERROR "moved to heap: i"
88         s := []*int{&i} // ERROR "&i escapes to heap" "literal escapes to heap"
89         return s
90 }