mirror of
https://github.com/golang/go
synced 2024-11-22 19:34:59 -07:00
test: add escape analysis tests form maps
False positives (var incorrectly escapes) are marked with BAD. Change-Id: I8383fa3f77e2156d781d994968636ba9a17bb975 Reviewed-on: https://go-review.googlesource.com/5297 Reviewed-by: Keith Randall <khr@golang.org>
This commit is contained in:
parent
a21537ff74
commit
130538bb9a
107
test/escape_map.go
Normal file
107
test/escape_map.go
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
// 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 maps.
|
||||||
|
|
||||||
|
package escape
|
||||||
|
|
||||||
|
var sink interface{}
|
||||||
|
|
||||||
|
func map0() {
|
||||||
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int, 0\) does not escape"
|
||||||
|
// BAD: i should not escape
|
||||||
|
i := 0 // ERROR "moved to heap: i"
|
||||||
|
// BAD: j should not escape
|
||||||
|
j := 0 // ERROR "moved to heap: j"
|
||||||
|
m[&i] = &j // ERROR "&i escapes to heap" "&j escapes to heap"
|
||||||
|
_ = m
|
||||||
|
}
|
||||||
|
|
||||||
|
func map1() *int {
|
||||||
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int, 0\) does not escape"
|
||||||
|
// BAD: i should not escape
|
||||||
|
i := 0 // ERROR "moved to heap: i"
|
||||||
|
j := 0 // ERROR "moved to heap: j"
|
||||||
|
m[&i] = &j // ERROR "&i escapes to heap" "&j escapes to heap"
|
||||||
|
return m[&i] // ERROR "&i does not escape"
|
||||||
|
}
|
||||||
|
|
||||||
|
func map2() map[*int]*int {
|
||||||
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int, 0\) escapes to heap"
|
||||||
|
i := 0 // ERROR "moved to heap: i"
|
||||||
|
j := 0 // ERROR "moved to heap: j"
|
||||||
|
m[&i] = &j // ERROR "&i escapes to heap" "&j escapes to heap"
|
||||||
|
return m
|
||||||
|
}
|
||||||
|
|
||||||
|
func map3() []*int {
|
||||||
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int, 0\) does not escape"
|
||||||
|
i := 0 // ERROR "moved to heap: i"
|
||||||
|
// BAD: j should not escape
|
||||||
|
j := 0 // ERROR "moved to heap: j"
|
||||||
|
m[&i] = &j // ERROR "&i escapes to heap" "&j escapes to heap"
|
||||||
|
var r []*int
|
||||||
|
for k := range m {
|
||||||
|
r = append(r, k)
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func map4() []*int {
|
||||||
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int, 0\) does not escape"
|
||||||
|
// BAD: i should not escape
|
||||||
|
i := 0 // ERROR "moved to heap: i"
|
||||||
|
j := 0 // ERROR "moved to heap: j"
|
||||||
|
m[&i] = &j // ERROR "&i escapes to heap" "&j escapes to heap"
|
||||||
|
var r []*int
|
||||||
|
for k, v := range m {
|
||||||
|
// We want to test exactly "for k, v := range m" rather than "for _, v := range m".
|
||||||
|
// The following if is merely to use (but not leak) k.
|
||||||
|
if k != nil {
|
||||||
|
r = append(r, v)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func map5(m map[*int]*int) { // ERROR "m does not escape"
|
||||||
|
i := 0 // ERROR "moved to heap: i"
|
||||||
|
j := 0 // ERROR "moved to heap: j"
|
||||||
|
m[&i] = &j // ERROR "&i escapes to heap" "&j escapes to heap"
|
||||||
|
}
|
||||||
|
|
||||||
|
func map6(m map[*int]*int) { // ERROR "m does not escape"
|
||||||
|
if m != nil {
|
||||||
|
m = make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int, 0\) does not escape"
|
||||||
|
}
|
||||||
|
i := 0 // ERROR "moved to heap: i"
|
||||||
|
j := 0 // ERROR "moved to heap: j"
|
||||||
|
m[&i] = &j // ERROR "&i escapes to heap" "&j escapes to heap"
|
||||||
|
}
|
||||||
|
|
||||||
|
func map7() {
|
||||||
|
// BAD: i should not escape
|
||||||
|
i := 0 // ERROR "moved to heap: i"
|
||||||
|
// BAD: j should not escape
|
||||||
|
j := 0 // ERROR "moved to heap: j"
|
||||||
|
m := map[*int]*int{&i: &j} // ERROR "&i escapes to heap" "&j escapes to heap" "literal does not escape"
|
||||||
|
_ = m
|
||||||
|
}
|
||||||
|
|
||||||
|
func map8() {
|
||||||
|
i := 0 // ERROR "moved to heap: i"
|
||||||
|
j := 0 // ERROR "moved to heap: j"
|
||||||
|
m := map[*int]*int{&i: &j} // ERROR "&i escapes to heap" "&j escapes to heap" "literal escapes to heap"
|
||||||
|
sink = m
|
||||||
|
}
|
||||||
|
|
||||||
|
func map9() *int {
|
||||||
|
// BAD: i should not escape
|
||||||
|
i := 0 // ERROR "moved to heap: i"
|
||||||
|
j := 0 // ERROR "moved to heap: j"
|
||||||
|
m := map[*int]*int{&i: &j} // ERROR "&i escapes to heap" "&j escapes to heap" "literal does not escape"
|
||||||
|
return m[nil]
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user