-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathescape_slice.go
90 lines (75 loc) · 2.12 KB
/
escape_slice.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// errorcheck -0 -m -l
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Test escape analysis for slices.
package escape
var sink interface{}
func slice0() {
var s []*int
// BAD: i should not escape
i := 0 // ERROR "moved to heap: i"
s = append(s, &i) // ERROR "&i escapes to heap"
_ = s
}
func slice1() *int {
var s []*int
i := 0 // ERROR "moved to heap: i"
s = append(s, &i) // ERROR "&i escapes to heap"
return s[0]
}
func slice2() []*int {
var s []*int
i := 0 // ERROR "moved to heap: i"
s = append(s, &i) // ERROR "&i escapes to heap"
return s
}
func slice3() *int {
var s []*int
i := 0 // ERROR "moved to heap: i"
s = append(s, &i) // ERROR "&i escapes to heap"
for _, p := range s {
return p
}
return nil
}
func slice4(s []*int) { // ERROR "s does not escape"
i := 0 // ERROR "moved to heap: i"
s[0] = &i // ERROR "&i escapes to heap"
}
func slice5(s []*int) { // ERROR "s does not escape"
if s != nil {
s = make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
}
i := 0 // ERROR "moved to heap: i"
s[0] = &i // ERROR "&i escapes to heap"
}
func slice6() {
s := make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
// BAD: i should not escape
i := 0 // ERROR "moved to heap: i"
s[0] = &i // ERROR "&i escapes to heap"
_ = s
}
func slice7() *int {
s := make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
i := 0 // ERROR "moved to heap: i"
s[0] = &i // ERROR "&i escapes to heap"
return s[0]
}
func slice8() {
// BAD: i should not escape here
i := 0 // ERROR "moved to heap: i"
s := []*int{&i} // ERROR "&i escapes to heap" "literal does not escape"
_ = s
}
func slice9() *int {
i := 0 // ERROR "moved to heap: i"
s := []*int{&i} // ERROR "&i escapes to heap" "literal does not escape"
return s[0]
}
func slice10() []*int {
i := 0 // ERROR "moved to heap: i"
s := []*int{&i} // ERROR "&i escapes to heap" "literal escapes to heap"
return s
}