mirror of
https://github.com/golang/go
synced 2024-11-05 16:36:10 -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>
58 lines
2.3 KiB
Go
58 lines
2.3 KiB
Go
// +build !gcflags_noopt
|
|
// errorcheck -0 -m
|
|
|
|
// Copyright 2018 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.
|
|
|
|
package foo
|
|
|
|
import "bytes"
|
|
|
|
// In order to get desired results, we need a combination of
|
|
// both escape analysis and inlining.
|
|
|
|
func bufferNotEscape() string {
|
|
// b itself does not escape, only its buf field will be
|
|
// copied during String() call, but object "handle" itself
|
|
// can be stack-allocated.
|
|
var b bytes.Buffer
|
|
b.WriteString("123")
|
|
b.Write([]byte{'4'}) // ERROR "bufferNotEscape \[\]byte literal does not escape$"
|
|
return b.String() // ERROR "inlining call to bytes.\(\*Buffer\).String$" "string\(bytes.b.buf\[bytes.b.off:\]\) escapes to heap$"
|
|
}
|
|
|
|
func bufferNoEscape2(xs []string) int { // ERROR "bufferNoEscape2 xs does not escape$"
|
|
b := bytes.NewBuffer(make([]byte, 0, 64)) // ERROR "bufferNoEscape2 &bytes.Buffer literal does not escape$" "bufferNoEscape2 make\(\[\]byte, 0, 64\) does not escape$" "inlining call to bytes.NewBuffer$"
|
|
for _, x := range xs {
|
|
b.WriteString(x)
|
|
}
|
|
return b.Len() // ERROR "inlining call to bytes.\(\*Buffer\).Len$"
|
|
}
|
|
|
|
func bufferNoEscape3(xs []string) string { // ERROR "bufferNoEscape3 xs does not escape$"
|
|
b := bytes.NewBuffer(make([]byte, 0, 64)) // ERROR "bufferNoEscape3 &bytes.Buffer literal does not escape$" "bufferNoEscape3 make\(\[\]byte, 0, 64\) does not escape$" "inlining call to bytes.NewBuffer$"
|
|
for _, x := range xs {
|
|
b.WriteString(x)
|
|
b.WriteByte(',')
|
|
}
|
|
return b.String() // ERROR "inlining call to bytes.\(\*Buffer\).String$" "string\(bytes.b.buf\[bytes.b.off:\]\) escapes to heap$"
|
|
}
|
|
|
|
func bufferNoEscape4() []byte {
|
|
var b bytes.Buffer
|
|
b.Grow(64) // ERROR "bufferNoEscape4 ignoring self-assignment in bytes.b.buf = bytes.b.buf\[:bytes.m·3\]$" "inlining call to bytes.\(\*Buffer\).Grow$"
|
|
useBuffer(&b)
|
|
return b.Bytes() // ERROR "inlining call to bytes.\(\*Buffer\).Bytes$"
|
|
}
|
|
|
|
func bufferNoEscape5() { // ERROR "can inline bufferNoEscape5$"
|
|
b := bytes.NewBuffer(make([]byte, 0, 128)) // ERROR "bufferNoEscape5 &bytes.Buffer literal does not escape$" "bufferNoEscape5 make\(\[\]byte, 0, 128\) does not escape$" "inlining call to bytes.NewBuffer$"
|
|
useBuffer(b)
|
|
}
|
|
|
|
//go:noinline
|
|
func useBuffer(b *bytes.Buffer) { // ERROR "useBuffer b does not escape$"
|
|
b.WriteString("1234")
|
|
}
|