mirror of
https://github.com/golang/go
synced 2024-11-12 04:00:23 -07:00
abefcac10a
For most nodes (e.g., OPTRLIT, OMAKESLICE, OCONVIFACE), escape analysis prints "escapes to heap" or "does not escape" to indicate whether that node's allocation can be heap or stack allocated. These messages are also emitted for OADDR, even though OADDR does not actually allocate anything itself. Moreover, it's redundant because escape analysis already prints "moved to heap" diagnostics when an OADDR node like "&x" causes x to require heap allocation. Because OADDR nodes don't allocate memory, my escape analysis rewrite doesn't naturally emit the "escapes to heap" / "does not escape" diagnostics for them. It's also non-trivial to replicate the exact semantics esc.go uses for OADDR. Since there are so many of these messages, I'm disabling them in this CL by themselves. I modified esc.go to suppress the Warnl calls without any other behavior changes, and then used a shell script to automatically remove any ERROR messages mentioned by run.go in "missing error" or "no match for" lines. Fixes #16300. Updates #23109. Change-Id: I3993e2743c3ff83ccd0893f4e73b366ff8871a57 Reviewed-on: https://go-review.googlesource.com/c/go/+/170319 Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Cherry Zhang <cherryyz@google.com> Reviewed-by: David Chase <drchase@google.com>
108 lines
2.8 KiB
Go
108 lines
2.8 KiB
Go
// 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\) 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
|
|
_ = m
|
|
}
|
|
|
|
func map1() *int {
|
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) 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
|
|
return m[&i]
|
|
}
|
|
|
|
func map2() map[*int]*int {
|
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) escapes to heap"
|
|
i := 0 // ERROR "moved to heap: i"
|
|
j := 0 // ERROR "moved to heap: j"
|
|
m[&i] = &j
|
|
return m
|
|
}
|
|
|
|
func map3() []*int {
|
|
m := make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) 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
|
|
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\) 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
|
|
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
|
|
}
|
|
|
|
func map6(m map[*int]*int) { // ERROR "m does not escape"
|
|
if m != nil {
|
|
m = make(map[*int]*int) // ERROR "make\(map\[\*int\]\*int\) does not escape"
|
|
}
|
|
i := 0 // ERROR "moved to heap: i"
|
|
j := 0 // ERROR "moved to heap: j"
|
|
m[&i] = &j
|
|
}
|
|
|
|
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 "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 "literal escapes to heap"
|
|
sink = m // ERROR "m escapes to heap"
|
|
}
|
|
|
|
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 "literal does not escape"
|
|
return m[nil]
|
|
}
|