2015-02-19 15:14:13 +00:00
|
|
|
// errorcheck -0 -m -l
|
|
|
|
|
2016-04-10 21:32:26 +00:00
|
|
|
// Copyright 2015 The Go Authors. All rights reserved.
|
2015-02-19 15:14:13 +00:00
|
|
|
// 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() {
|
2015-03-28 14:08:53 +00:00
|
|
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
|
2015-02-19 15:14:13 +00:00
|
|
|
// BAD: i should not escape
|
|
|
|
i := 0 // ERROR "moved to heap: i"
|
|
|
|
// BAD: j should not escape
|
2020-09-09 04:09:01 +00:00
|
|
|
j := 0 // ERROR "moved to heap: j"
|
2019-04-01 18:58:33 +00:00
|
|
|
m[&i] = &j
|
2015-02-19 15:14:13 +00:00
|
|
|
_ = m
|
|
|
|
}
|
|
|
|
|
|
|
|
func map1() *int {
|
2015-03-28 14:08:53 +00:00
|
|
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
|
2015-02-19 15:14:13 +00:00
|
|
|
// BAD: i should not escape
|
2020-09-09 04:09:01 +00:00
|
|
|
i := 0 // ERROR "moved to heap: i"
|
|
|
|
j := 0 // ERROR "moved to heap: j"
|
2019-04-01 18:58:33 +00:00
|
|
|
m[&i] = &j
|
|
|
|
return m[&i]
|
2015-02-19 15:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func map2() map[*int]*int {
|
2015-03-28 14:08:53 +00:00
|
|
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) escapes to heap"
|
2015-02-19 15:14:13 +00:00
|
|
|
i := 0 // ERROR "moved to heap: i"
|
|
|
|
j := 0 // ERROR "moved to heap: j"
|
2019-04-01 18:58:33 +00:00
|
|
|
m[&i] = &j
|
2015-02-19 15:14:13 +00:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func map3() []*int {
|
2015-03-28 14:08:53 +00:00
|
|
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
|
2015-02-19 15:14:13 +00:00
|
|
|
i := 0 // ERROR "moved to heap: i"
|
|
|
|
// BAD: j should not escape
|
2020-09-09 04:09:01 +00:00
|
|
|
j := 0 // ERROR "moved to heap: j"
|
2019-04-01 18:58:33 +00:00
|
|
|
m[&i] = &j
|
2015-02-19 15:14:13 +00:00
|
|
|
var r []*int
|
|
|
|
for k := range m {
|
|
|
|
r = append(r, k)
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func map4() []*int {
|
2015-03-28 14:08:53 +00:00
|
|
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
|
2015-02-19 15:14:13 +00:00
|
|
|
// BAD: i should not escape
|
2020-09-09 04:09:01 +00:00
|
|
|
i := 0 // ERROR "moved to heap: i"
|
|
|
|
j := 0 // ERROR "moved to heap: j"
|
2019-04-01 18:58:33 +00:00
|
|
|
m[&i] = &j
|
2015-02-19 15:14:13 +00:00
|
|
|
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"
|
2020-09-09 04:09:01 +00:00
|
|
|
i := 0 // ERROR "moved to heap: i"
|
|
|
|
j := 0 // ERROR "moved to heap: j"
|
2019-04-01 18:58:33 +00:00
|
|
|
m[&i] = &j
|
2015-02-19 15:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func map6(m map[*int]*int) { // ERROR "m does not escape"
|
|
|
|
if m != nil {
|
2015-03-28 14:08:53 +00:00
|
|
|
m = make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
|
2015-02-19 15:14:13 +00:00
|
|
|
}
|
2020-09-09 04:09:01 +00:00
|
|
|
i := 0 // ERROR "moved to heap: i"
|
|
|
|
j := 0 // ERROR "moved to heap: j"
|
2019-04-01 18:58:33 +00:00
|
|
|
m[&i] = &j
|
2015-02-19 15:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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"
|
2020-09-09 04:09:01 +00:00
|
|
|
m := map[*int]*int{&i: &j} // ERROR "map\[\*int\]\*int{...} does not escape"
|
2015-02-19 15:14:13 +00:00
|
|
|
_ = m
|
|
|
|
}
|
|
|
|
|
|
|
|
func map8() {
|
|
|
|
i := 0 // ERROR "moved to heap: i"
|
|
|
|
j := 0 // ERROR "moved to heap: j"
|
2020-09-09 04:09:01 +00:00
|
|
|
m := map[*int]*int{&i: &j} // ERROR "map\[\*int\]\*int{...} escapes to heap"
|
cmd/compile: silence esc diagnostics about directiface OCONVIFACEs
In general, a conversion to interface type may require values to be
boxed, which in turn necessitates escape analysis to determine whether
the boxed representation can be stack allocated.
However, esc.go used to unconditionally print escape analysis
decisions about OCONVIFACE, even for conversions that don't require
boxing (e.g., pointers, channels, maps, functions).
For test compatibility with esc.go, escape.go similarly printed these
useless diagnostics. This CL removes the diagnostics, and updates test
expectations accordingly.
Change-Id: I97c57a4a08e44d265bba516c78426ff4f2bf1e12
Reviewed-on: https://go-review.googlesource.com/c/go/+/192697
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
2019-08-30 17:56:30 +00:00
|
|
|
sink = m
|
2015-02-19 15:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func map9() *int {
|
|
|
|
// BAD: i should not escape
|
|
|
|
i := 0 // ERROR "moved to heap: i"
|
|
|
|
j := 0 // ERROR "moved to heap: j"
|
2020-09-09 04:09:01 +00:00
|
|
|
m := map[*int]*int{&i: &j} // ERROR "map\[\*int\]\*int{...} does not escape"
|
2015-02-19 15:14:13 +00:00
|
|
|
return m[nil]
|
|
|
|
}
|