aboutsummaryrefslogtreecommitdiff
path: root/test/escape_closure.go
diff options
context:
space:
mode:
authorMatthew Dempsky <mdempsky@google.com>2019-04-02 14:44:13 -0700
committerMatthew Dempsky <mdempsky@google.com>2019-04-16 16:20:39 +0000
commita9831633be548c039ada30a2fb9d7290c35ac0c0 (patch)
tree511937efe6dea2b8132e61eab191fde0f871379d /test/escape_closure.go
parent94e720059f902339699b8bf7b2b10897311b50f8 (diff)
downloadgo-a9831633be548c039ada30a2fb9d7290c35ac0c0.tar.gz
go-a9831633be548c039ada30a2fb9d7290c35ac0c0.zip
cmd/compile: update escape analysis tests for newescape
The new escape analysis implementation tries to emit debugging diagnostics that are compatible with the existing implementation, but there's a handful of cases that are easier to handle by updating the test expectations instead. For regress tests that need updating, the original file is copied to oldescapeXXX.go.go with -newescape=false added to the //errorcheck line, while the file is updated in place with -newescape=true and new test requirements. Notable test changes: 1) escape_because.go looks for a lot of detailed internal debugging messages that are fairly particular to how esc.go works and that I haven't attempted to port over to escape.go yet. 2) There are a lot of "leaking param: x to result ~r1 level=-1" messages for code like func(p *int) *T { return &T{p} } that were simply wrong. Here &T must be heap allocated unconditionally (because it's being returned); and since p is stored into it, p escapes unconditionally too. esc.go incorrectly reports that p escapes conditionally only if the returned pointer escaped. 3) esc.go used to print each "leaking param" analysis result as it discovered them, which could lead to redundant messages (e.g., that a param leaks at level=0 and level=1). escape.go instead prints everything at the end, once it knows the shortest path to each sink. 4) esc.go didn't precisely model direct-interface types, resulting in some values unnecessarily escaping to the heap when stored into non-escaping interface values. 5) For functions written in assembly, esc.go only printed "does not escape" messages, whereas escape.go prints "does not escape" or "leaking param" as appropriate, consistent with the behavior for functions written in Go. 6) 12 tests included "BAD" annotations identifying cases where esc.go was unnecessarily heap allocating something. These are all fixed by escape.go. Updates #23109. Change-Id: Iabc9eb14c94c9cadde3b183478d1fd54f013502f Reviewed-on: https://go-review.googlesource.com/c/go/+/170447 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'test/escape_closure.go')
-rw-r--r--test/escape_closure.go41
1 files changed, 17 insertions, 24 deletions
diff --git a/test/escape_closure.go b/test/escape_closure.go
index 93efe94ed7..cf055d3b34 100644
--- a/test/escape_closure.go
+++ b/test/escape_closure.go
@@ -1,4 +1,4 @@
-// errorcheck -0 -m -l
+// errorcheck -0 -m -l -newescape=true
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
@@ -11,27 +11,24 @@ package escape
var sink interface{}
func ClosureCallArgs0() {
- x := 0 // ERROR "moved to heap: x"
+ x := 0
func(p *int) { // ERROR "p does not escape" "func literal does not escape"
*p = 1
- // BAD: x should not escape to heap here
}(&x)
}
func ClosureCallArgs1() {
- x := 0 // ERROR "moved to heap: x"
+ x := 0
for {
func(p *int) { // ERROR "p does not escape" "func literal does not escape"
*p = 1
- // BAD: x should not escape to heap here
}(&x)
}
}
func ClosureCallArgs2() {
for {
- // BAD: x should not escape here
- x := 0 // ERROR "moved to heap: x"
+ x := 0
func(p *int) { // ERROR "p does not escape" "func literal does not escape"
*p = 1
}(&x)
@@ -46,8 +43,7 @@ func ClosureCallArgs3() {
}
func ClosureCallArgs4() {
- // BAD: x should not leak here
- x := 0 // ERROR "moved to heap: x"
+ x := 0
_ = func(p *int) *int { // ERROR "leaking param: p to result ~r1" "func literal does not escape"
return p
}(&x)
@@ -55,7 +51,9 @@ func ClosureCallArgs4() {
func ClosureCallArgs5() {
x := 0 // ERROR "moved to heap: x"
- sink = func(p *int) *int { // ERROR "leaking param: p to result ~r1" "func literal does not escape" "\(func literal\)\(&x\) escapes to heap"
+ // TODO(mdempsky): We get "leaking param: p" here because the new escape analysis pass
+ // can tell that p flows directly to sink, but it's a little weird. Re-evaluate.
+ sink = func(p *int) *int { // ERROR "leaking param: p" "func literal does not escape" "\(func literal\)\(&x\) escapes to heap"
return p
}(&x)
}
@@ -79,10 +77,9 @@ func ClosureCallArgs7() {
}
func ClosureCallArgs8() {
- x := 0 // ERROR "moved to heap: x"
+ x := 0
defer func(p *int) { // ERROR "p does not escape" "func literal does not escape"
*p = 1
- // BAD: x should not escape to heap here
}(&x)
}
@@ -113,8 +110,7 @@ func ClosureCallArgs11() {
}
func ClosureCallArgs12() {
- // BAD: x should not leak
- x := 0 // ERROR "moved to heap: x"
+ x := 0
defer func(p *int) *int { // ERROR "leaking param: p to result ~r1" "func literal does not escape"
return p
}(&x)
@@ -128,21 +124,18 @@ func ClosureCallArgs13() {
}
func ClosureCallArgs14() {
- x := 0 // ERROR "moved to heap: x"
- // BAD: &x should not escape here
- p := &x // ERROR "moved to heap: p"
+ x := 0
+ p := &x
_ = func(p **int) *int { // ERROR "leaking param: p to result ~r1 level=1" "func literal does not escape"
return *p
- // BAD: p should not escape here
}(&p)
}
func ClosureCallArgs15() {
x := 0 // ERROR "moved to heap: x"
- p := &x // ERROR "moved to heap: p"
- sink = func(p **int) *int { // ERROR "leaking param: p to result ~r1 level=1" "func literal does not escape" "\(func literal\)\(&p\) escapes to heap"
+ p := &x
+ sink = func(p **int) *int { // ERROR "leaking param content: p" "func literal does not escape" "\(func literal\)\(&p\) escapes to heap"
return *p
- // BAD: p should not escape here
}(&p)
}
@@ -152,7 +145,7 @@ func ClosureLeak1(s string) string { // ERROR "ClosureLeak1 s does not escape"
}
// See #14409 -- returning part of captured var leaks it.
-func ClosureLeak1a(a ...string) string { // ERROR "leaking param: a to result ~r1 level=1"
+func ClosureLeak1a(a ...string) string { // ERROR "leaking param: a to result ~r1 level=1$"
return func() string { // ERROR "ClosureLeak1a func literal does not escape"
return a[0]
}()
@@ -163,11 +156,11 @@ func ClosureLeak2(s string) string { // ERROR "ClosureLeak2 s does not escape"
c := ClosureLeak2a(t) // ERROR "ClosureLeak2 ... argument does not escape"
return c
}
-func ClosureLeak2a(a ...string) string { // ERROR "leaking param: a to result ~r1 level=1"
+func ClosureLeak2a(a ...string) string { // ERROR "leaking param content: a"
return ClosureLeak2b(func() string { // ERROR "ClosureLeak2a func literal does not escape"
return a[0]
})
}
-func ClosureLeak2b(f func() string) string { // ERROR "leaking param: f to result ~r1 level=1"
+func ClosureLeak2b(f func() string) string { // ERROR "ClosureLeak2b f does not escape"
return f()
}