2018-09-24 10:48:54 -06:00
// +build !gcflags_noopt
2018-09-06 04:28:17 -06:00
// 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
2019-04-01 12:58:33 -06:00
b . WriteString ( "123" )
2020-09-08 22:09:01 -06:00
b . Write ( [ ] byte { '4' } ) // ERROR "\[\]byte{...} does not escape$"
2019-04-01 12:58:33 -06:00
return b . String ( ) // ERROR "inlining call to bytes.\(\*Buffer\).String$" "string\(bytes.b.buf\[bytes.b.off:\]\) escapes to heap$"
2018-09-06 04:28:17 -06:00
}
2019-09-12 11:18:03 -06:00
func bufferNoEscape2 ( xs [ ] string ) int { // ERROR "xs does not escape$"
2020-09-08 22:09:01 -06:00
b := bytes . NewBuffer ( make ( [ ] byte , 0 , 64 ) ) // ERROR "&bytes.Buffer{...} does not escape$" "make\(\[\]byte, 0, 64\) does not escape$" "inlining call to bytes.NewBuffer$"
2018-09-06 04:28:17 -06:00
for _ , x := range xs {
b . WriteString ( x )
}
2018-11-30 06:36:00 -07:00
return b . Len ( ) // ERROR "inlining call to bytes.\(\*Buffer\).Len$"
2018-09-06 04:28:17 -06:00
}
2019-09-12 11:18:03 -06:00
func bufferNoEscape3 ( xs [ ] string ) string { // ERROR "xs does not escape$"
2020-09-08 22:09:01 -06:00
b := bytes . NewBuffer ( make ( [ ] byte , 0 , 64 ) ) // ERROR "&bytes.Buffer{...} does not escape$" "make\(\[\]byte, 0, 64\) does not escape$" "inlining call to bytes.NewBuffer$"
2018-09-06 04:28:17 -06:00
for _ , x := range xs {
b . WriteString ( x )
b . WriteByte ( ',' )
}
2018-11-30 06:36:00 -07:00
return b . String ( ) // ERROR "inlining call to bytes.\(\*Buffer\).String$" "string\(bytes.b.buf\[bytes.b.off:\]\) escapes to heap$"
2018-09-06 04:28:17 -06:00
}
2018-09-07 11:36:56 -06:00
func bufferNoEscape4 ( ) [ ] byte {
var b bytes . Buffer
[dev.regabi] cmd/compile: convert OPANIC argument to interface{} during typecheck
Currently, typecheck leaves arguments to OPANIC as their original
type. This CL changes it to insert implicit OCONVIFACE operations to
convert arguments to `interface{}` like how any other function call
would be handled.
No immediate benefits, other than getting to remove a tiny bit of
special-case logic in order.go's handling of OPANICs. Instead, the
generic code path for handling OCONVIFACE is used, if necessary.
Longer term, this should be marginally helpful for #43753, as it
reduces the number of cases where we need values to be addressable for
runtime calls.
However, this does require adding some hacks to appease existing
tests:
1. We need yet another kludge in inline budgeting, to ensure that
reflect.flag.mustBe stays inlinable for cmd/compile/internal/test's
TestIntendedInlining.
2. Since the OCONVIFACE expressions are now being introduced during
typecheck, they're now visible to escape analysis. So expressions like
"panic(1)" are now seen as "panic(interface{}(1))", and escape
analysis warns that the "interface{}(1)" escapes to the heap. These
have always escaped to heap, just now we're accurately reporting about
it.
(Also, unfortunately fmt.go hides implicit conversions by default in
diagnostics messages, so instead of reporting "interface{}(1) escapes
to heap", it actually reports "1 escapes to heap", which is
confusing. However, this confusing messaging also isn't new.)
Change-Id: Icedf60e1d2e464e219441b8d1233a313770272af
Reviewed-on: https://go-review.googlesource.com/c/go/+/284412
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Trust: Matthew Dempsky <mdempsky@google.com>
2021-01-17 17:14:48 -07:00
b . Grow ( 64 ) // ERROR "bufferNoEscape4 ignoring self-assignment in bytes.b.buf = bytes.b.buf\[:bytes.m\]$" "inlining call to bytes.\(\*Buffer\).Grow$" "string\(.*\) escapes to heap"
2019-04-01 12:58:33 -06:00
useBuffer ( & b )
return b . Bytes ( ) // ERROR "inlining call to bytes.\(\*Buffer\).Bytes$"
2018-09-07 11:36:56 -06:00
}
2018-11-30 06:36:00 -07:00
func bufferNoEscape5 ( ) { / / ERROR "can inline bufferNoEscape5$"
2020-09-08 22:09:01 -06:00
b := bytes . NewBuffer ( make ( [ ] byte , 0 , 128 ) ) // ERROR "&bytes.Buffer{...} does not escape$" "make\(\[\]byte, 0, 128\) does not escape$" "inlining call to bytes.NewBuffer$"
2018-09-07 11:36:56 -06:00
useBuffer ( b )
}
//go:noinline
2019-09-12 11:18:03 -06:00
func useBuffer ( b * bytes . Buffer ) { // ERROR "b does not escape$"
2018-09-07 11:36:56 -06:00
b . WriteString ( "1234" )
}