]> Cypherpunks.ru repositories - gostls13.git/commitdiff
cmd/compile: unnamed parameters do not escape
authorKeith Randall <keithr@alum.mit.edu>
Fri, 24 Mar 2017 16:00:17 +0000 (09:00 -0700)
committerKeith Randall <khr@golang.org>
Fri, 24 Mar 2017 17:14:00 +0000 (17:14 +0000)
Fixes #19687

Change-Id: I2e4769b4ec5812506df4ac5dc6bc6a7c5774ecb0
Reviewed-on: https://go-review.googlesource.com/38600
Run-TryBot: Keith Randall <khr@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
src/cmd/compile/internal/gc/esc.go
test/escape5.go

index 0a26bf4aae1492b83c76bb3f757ecbaef96dbacc..72b136fe11540eca1bdb495eb21593ce73c56728 100644 (file)
@@ -2118,4 +2118,13 @@ func (e *EscState) esctag(fn *Node) {
                case EscHeap: // touched by escflood, moved to heap
                }
        }
+
+       // Unnamed parameters are unused and therefore do not escape.
+       // (Unnamed parameters are not in the Dcl list in the loop above
+       // so we need to mark them separately.)
+       for _, f := range fn.Type.Params().Fields().Slice() {
+               if f.Sym == nil || isblanksym(f.Sym) {
+                       f.Note = mktag(EscNone)
+               }
+       }
 }
index c4bf17b2acf2e0ef1c8467a78cb07a0d2a55cc13..7d6ef554a53fd0852330ab0b7d75820a3f1ddbf6 100644 (file)
@@ -9,6 +9,8 @@
 
 package foo
 
+import "runtime"
+
 func noleak(p *int) int { // ERROR "p does not escape"
        return *p
 }
@@ -149,3 +151,15 @@ func f10() {
        var y = make([]byte, 1<<30) // ERROR "make\(\[\]byte, 1 << 30\) escapes to heap"
        _ = x[0] + y[0]
 }
+
+// Test for issue 19687 (passing to unnamed parameters does not escape).
+func f11(**int) {
+}
+func f12(_ **int) {
+}
+func f13() {
+       var x *int
+       f11(&x)               // ERROR "&x does not escape"
+       f12(&x)               // ERROR "&x does not escape"
+       runtime.KeepAlive(&x) // ERROR "&x does not escape"
+}