3 //go:build !gcflags_noopt && !goexperiment.newinliner
5 // Copyright 2018 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
13 // In order to get desired results, we need a combination of
14 // both escape analysis and inlining.
16 func bufferNotEscape() string {
17 // b itself does not escape, only its buf field will be
18 // copied during String() call, but object "handle" itself
19 // can be stack-allocated.
22 b.Write([]byte{'4'}) // ERROR "\[\]byte{...} does not escape$"
23 return b.String() // ERROR "inlining call to bytes.\(\*Buffer\).String$" "string\(bytes.b.buf\[bytes.b.off:\]\) escapes to heap$"
26 func bufferNoEscape2(xs []string) int { // ERROR "xs does not escape$"
27 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$"
28 for _, x := range xs {
31 return b.Len() // ERROR "inlining call to bytes.\(\*Buffer\).Len$"
34 func bufferNoEscape3(xs []string) string { // ERROR "xs does not escape$"
35 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$"
36 for _, x := range xs {
40 return b.String() // ERROR "inlining call to bytes.\(\*Buffer\).String$" "string\(bytes.b.buf\[bytes.b.off:\]\) escapes to heap$"
43 func bufferNoEscape4() []byte {
45 b.Grow(64) // ERROR "bufferNoEscape4 ignoring self-assignment in bytes.b.buf = bytes.b.buf\[:bytes.m\]$" "inlining call to bytes.\(\*Buffer\).Grow$" `".+" escapes to heap`
47 return b.Bytes() // ERROR "inlining call to bytes.\(\*Buffer\).Bytes$"
50 func bufferNoEscape5() { // ERROR "can inline bufferNoEscape5$"
51 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$"
56 func useBuffer(b *bytes.Buffer) { // ERROR "b does not escape$"