diff options
author | Cherry Zhang <cherryyz@google.com> | 2018-04-06 16:21:26 -0400 |
---|---|---|
committer | Cherry Zhang <cherryyz@google.com> | 2018-04-13 14:48:23 +0000 |
commit | 5a91c83ce82509551d353b1b5ea25cd72b44fec3 (patch) | |
tree | ed1806673f2c6d98847dbb89aa0c7d2cfc45f1c5 /test/escape5.go | |
parent | eb4f33243ece5874d2690c4b9bc72cb47f8da772 (diff) | |
download | go-5a91c83ce82509551d353b1b5ea25cd72b44fec3.tar.gz go-5a91c83ce82509551d353b1b5ea25cd72b44fec3.zip |
cmd/compile: in escape analysis, propagate loop depth to field
The escape analysis models "loop depth". If the address of an
expression is assigned to something defined at a lower (outer)
loop depth, the escape analysis decides it escapes. However, it
uses the loop depth of the address operator instead of where
the RHS is defined. This causes an unnecessary escape if there is
an assignment inside a loop but the RHS is defined outside the
loop. This CL propagates the loop depth.
Fixes #24730.
Change-Id: I5ff1530688bdfd90561a7b39c8be9bfc009a9dae
Reviewed-on: https://go-review.googlesource.com/105257
Run-TryBot: Cherry Zhang <cherryyz@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David Chase <drchase@google.com>
Diffstat (limited to 'test/escape5.go')
-rw-r--r-- | test/escape5.go | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/test/escape5.go b/test/escape5.go index 0bae1e8401..d02f735f8f 100644 --- a/test/escape5.go +++ b/test/escape5.go @@ -175,3 +175,22 @@ func _() { u.M() // ERROR "u does not escape" u.N() // ERROR "u does not escape" } + +// Issue 24730: taking address in a loop causes unnecessary escape +type T24730 struct { + x [64]byte +} + +func (t *T24730) g() { // ERROR "t does not escape" + y := t.x[:] // ERROR "t\.x does not escape" + for i := range t.x[:] { // ERROR "t\.x does not escape" + y = t.x[:] // ERROR "t\.x does not escape" + y[i] = 1 + } + + var z *byte + for i := range t.x[:] { // ERROR "t\.x does not escape" + z = &t.x[i] // ERROR "t\.x\[i\] does not escape" + *z = 2 + } +} |