aboutsummaryrefslogtreecommitdiff
path: root/test/escape_slice.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2015-02-19 18:30:08 +0300
committerDmitry Vyukov <dvyukov@google.com>2015-03-28 13:18:42 +0000
commit0558f12123d8838575acb64fc7d9d58c59511b89 (patch)
treedd754adc2072383a8a3c77a9e49c6799f727fcbf /test/escape_slice.go
parent1f5617e37c9078dbe082151c9e17d5dc900cb5a0 (diff)
downloadgo-0558f12123d8838575acb64fc7d9d58c59511b89.tar.gz
go-0558f12123d8838575acb64fc7d9d58c59511b89.zip
test: add tests for escape analysis of slices
False positives (var incorrectly escapes) are marked with BAD. Change-Id: I9e9a3f71b060520103bcf289829a2efdf6f2b517 Reviewed-on: https://go-review.googlesource.com/5298 Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'test/escape_slice.go')
-rw-r--r--test/escape_slice.go90
1 files changed, 90 insertions, 0 deletions
diff --git a/test/escape_slice.go b/test/escape_slice.go
new file mode 100644
index 0000000000..9315e27682
--- /dev/null
+++ b/test/escape_slice.go
@@ -0,0 +1,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
+}