aboutsummaryrefslogtreecommitdiff
path: root/test/escape_indir.go
diff options
context:
space:
mode:
authorDmitry Vyukov <dvyukov@google.com>2015-02-19 17:39:17 +0300
committerDmitry Vyukov <dvyukov@google.com>2015-03-28 13:38:45 +0000
commita21537ff74e8d873a9dcf55412a467769ddc3bd5 (patch)
tree293268165ec8809eb32a2a64fc28fa894c8ad45a /test/escape_indir.go
parent097b1e0b733dd425f77c5ad7ef410ed521f31e31 (diff)
downloadgo-a21537ff74e8d873a9dcf55412a467769ddc3bd5.tar.gz
go-a21537ff74e8d873a9dcf55412a467769ddc3bd5.zip
test: add tests for escape analysis when assigning to indirections
False positives (var incorrectly escapes) are marked with BAD. Change-Id: I0114d87ee467fe1e3b27642f8c5a04d4a9664211 Reviewed-on: https://go-review.googlesource.com/5295 Reviewed-by: Keith Randall <khr@golang.org>
Diffstat (limited to 'test/escape_indir.go')
-rw-r--r--test/escape_indir.go153
1 files changed, 153 insertions, 0 deletions
diff --git a/test/escape_indir.go b/test/escape_indir.go
new file mode 100644
index 0000000000..91aac77d73
--- /dev/null
+++ b/test/escape_indir.go
@@ -0,0 +1,153 @@
+// 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 when assigning to indirections.
+
+package escape
+
+var sink interface{}
+
+type ConstPtr struct {
+ p *int
+ c ConstPtr2
+ x **ConstPtr
+}
+
+type ConstPtr2 struct {
+ p *int
+ i int
+}
+
+func constptr0() {
+ i := 0 // ERROR "moved to heap: i"
+ x := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
+ // BAD: i should not escape here
+ x.p = &i // ERROR "&i escapes to heap"
+ _ = x
+}
+
+func constptr01() *ConstPtr {
+ i := 0 // ERROR "moved to heap: i"
+ x := &ConstPtr{} // ERROR "&ConstPtr literal escapes to heap"
+ x.p = &i // ERROR "&i escapes to heap"
+ return x
+}
+
+func constptr02() ConstPtr {
+ i := 0 // ERROR "moved to heap: i"
+ x := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
+ x.p = &i // ERROR "&i escapes to heap"
+ return *x
+}
+
+func constptr03() **ConstPtr {
+ i := 0 // ERROR "moved to heap: i"
+ x := &ConstPtr{} // ERROR "&ConstPtr literal escapes to heap" "moved to heap: x"
+ x.p = &i // ERROR "&i escapes to heap"
+ return &x // ERROR "&x escapes to heap"
+}
+
+func constptr1() {
+ i := 0 // ERROR "moved to heap: i"
+ x := &ConstPtr{} // ERROR "&ConstPtr literal escapes to heap"
+ x.p = &i // ERROR "&i escapes to heap"
+ sink = x
+}
+
+func constptr2() {
+ i := 0 // ERROR "moved to heap: i"
+ x := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
+ x.p = &i // ERROR "&i escapes to heap"
+ sink = *x
+}
+
+func constptr4() *ConstPtr {
+ p := new(ConstPtr) // ERROR "new\(ConstPtr\) escapes to heap"
+ *p = *&ConstPtr{} // ERROR "&ConstPtr literal does not escape"
+ return p
+}
+
+func constptr5() *ConstPtr {
+ p := new(ConstPtr) // ERROR "new\(ConstPtr\) escapes to heap"
+ p1 := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
+ *p = *p1
+ return p
+}
+
+// BAD: p should not escape here
+func constptr6(p *ConstPtr) { // ERROR "leaking param: p"
+ p1 := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
+ *p1 = *p
+ _ = p1
+}
+
+func constptr7() **ConstPtr {
+ p := new(ConstPtr) // ERROR "new\(ConstPtr\) escapes to heap" "moved to heap: p"
+ var tmp ConstPtr2
+ p1 := &tmp // ERROR "&tmp does not escape"
+ p.c = *p1
+ return &p // ERROR "&p escapes to heap"
+}
+
+func constptr8() *ConstPtr {
+ p := new(ConstPtr) // ERROR "new\(ConstPtr\) escapes to heap"
+ var tmp ConstPtr2
+ p.c = *&tmp // ERROR "&tmp does not escape"
+ return p
+}
+
+func constptr9() ConstPtr {
+ p := new(ConstPtr) // ERROR "new\(ConstPtr\) does not escape"
+ var p1 ConstPtr2
+ i := 0 // ERROR "moved to heap: i"
+ p1.p = &i // ERROR "&i escapes to heap"
+ p.c = p1
+ return *p
+}
+
+func constptr10() ConstPtr {
+ x := &ConstPtr{} // ERROR "moved to heap: x" "&ConstPtr literal escapes to heap"
+ i := 0 // ERROR "moved to heap: i"
+ var p *ConstPtr
+ p = &ConstPtr{p: &i, x: &x} // ERROR "&i escapes to heap" "&x escapes to heap" "&ConstPtr literal does not escape"
+ var pp **ConstPtr
+ pp = &p // ERROR "&p does not escape"
+ return **pp
+}
+
+func constptr11() *ConstPtr {
+ i := 0 // ERROR "moved to heap: i"
+ p := new(ConstPtr) // ERROR "new\(ConstPtr\) escapes to heap"
+ p1 := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
+ p1.p = &i // ERROR "&i escapes to heap"
+ *p = *p1
+ return p
+}
+
+func foo(p **int) { // ERROR "foo p does not escape"
+ i := 0 // ERROR "moved to heap: i"
+ y := p
+ *y = &i // ERROR "&i escapes to heap"
+}
+
+func foo1(p *int) { // ERROR "p does not escape"
+ i := 0 // ERROR "moved to heap: i"
+ y := &p // ERROR "&p does not escape"
+ *y = &i // ERROR "&i escapes to heap"
+}
+
+func foo2() {
+ type Z struct {
+ f **int
+ }
+ x := new(int) // ERROR "moved to heap: x" "new\(int\) escapes to heap"
+ sink = &x // ERROR "&x escapes to heap"
+ var z Z
+ z.f = &x // ERROR "&x does not escape"
+ p := z.f
+ i := 0 // ERROR "moved to heap: i"
+ *p = &i // ERROR "&i escapes to heap"
+}