1
0
mirror of https://github.com/golang/go synced 2024-11-18 10:44:45 -07:00
go/test/fixedbugs/issue27557.go
Matthew Dempsky 2a2423bd05 cmd/compile: more precise analysis of method values
Previously for a method value "x.M", we always flowed x directly to
the heap, which led to the receiver argument generally needing to be
heap allocated.

This CL changes it to flow x to the closure and M's receiver
parameter. This allows receiver arguments to be stack allocated as
long as (1) the closure never escapes, *and* (2) method doesn't leak
its receiver parameter.

Within the standard library, this allows a handful of objects to be
stack allocated instead. Listed here are diagnostics that were
previously emitted by "go build -gcflags=-m std cmd" that are no
longer emitted:

archive/tar/writer.go:118:6: moved to heap: f
archive/tar/writer.go:208:6: moved to heap: f
archive/tar/writer.go:248:6: moved to heap: f
cmd/compile/internal/gc/initorder.go:252:2: moved to heap: d
cmd/compile/internal/gc/initorder.go:75:2: moved to heap: s
cmd/go/internal/generate/generate.go:206:7: &Generator literal escapes to heap
cmd/internal/obj/arm64/asm7.go:910:2: moved to heap: c
cmd/internal/obj/mips/asm0.go:415:2: moved to heap: c
cmd/internal/obj/pcln.go:294:22: new(pcinlineState) escapes to heap
cmd/internal/obj/s390x/asmz.go:459:2: moved to heap: c
crypto/tls/handshake_server.go:56:2: moved to heap: hs

Thanks to Cuong Manh Le for help coming up with this solution.

Fixes #27557.

Change-Id: I8c85d671d07fb9b53e11d2dd05949a34dbbd7e17
Reviewed-on: https://go-review.googlesource.com/c/go/+/228263
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
Reviewed-by: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2020-04-21 20:49:34 +00:00

42 lines
946 B
Go

// errorcheck -0 -l -m
// Copyright 2019 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 p
var sink interface{}
func _() {
var t T
f := t.noescape // ERROR "t.noescape does not escape"
f()
}
func _() {
var t T // ERROR "moved to heap"
f := t.escape // ERROR "t.escape does not escape"
f()
}
func _() {
var t T // ERROR "moved to heap"
f := t.returns // ERROR "t.returns does not escape"
sink = f()
}
type T struct{}
func (t *T) noescape() {} // ERROR "t does not escape"
func (t *T) escape() { sink = t } // ERROR "leaking param: t$"
func (t *T) returns() *T { return t } // ERROR "leaking param: t to result ~r0 level=0"
func (t *T) recursive() { // ERROR "leaking param: t$"
sink = t
var t2 T // ERROR "moved to heap"
f := t2.recursive // ERROR "t2.recursive does not escape"
f()
}