OSTRARRAYBYTE,
OADDSTR,
ONEW,
- OCLOSURE,
OCALLPART,
ORUNESTR,
OCONVIFACE:
escflows(e, dst, src)
+ case OCLOSURE:
+ // OCLOSURE is lowered to OPTRLIT,
+ // insert OADDR to account for the additional indirection.
+ a := Nod(OADDR, src, nil)
+ a.Lineno = src.Lineno
+ a.Escloopdepth = src.Escloopdepth
+ escflows(e, dst, a)
+
// Flowing multiple returns to a single dst happens when
// analyzing "go f(g())": here g() flows to sink (issue 4529).
case OCALLMETH, OCALLFUNC, OCALLINTER:
src.Esc = EscHeap
addrescapes(src.Left)
if Debug['m'] != 0 {
- Warnl(int(src.Lineno), "%v escapes to heap", Nconv(src, obj.FmtShort))
+ p := src
+ if p.Left.Op == OCLOSURE {
+ p = p.Left // merely to satisfy error messages in tests
+ }
+ Warnl(int(src.Lineno), "%v escapes to heap", Nconv(p, obj.FmtShort))
}
}
func nonescapingIface(m map[M]bool) bool { // ERROR "m does not escape"
return m[MV(0)] // ERROR "MV\(0\) does not escape"
}
+
+func issue10353() {
+ x := new(int) // ERROR "new\(int\) escapes to heap"
+ issue10353a(x)()
+}
+
+func issue10353a(x *int) func() { // ERROR "leaking param: x"
+ return func() { // ERROR "func literal escapes to heap"
+ println(*x)
+ }
+}
+
+func issue10353b() {
+ var f func()
+ for {
+ x := new(int) // ERROR "new\(int\) escapes to heap"
+ f = func() { // ERROR "func literal escapes to heap"
+ println(*x)
+ }
+ }
+ _ = f
+}
func nonescapingIface(m map[M]bool) bool { // ERROR "m does not escape"
return m[MV(0)] // ERROR "MV\(0\) does not escape"
}
+
+func issue10353() {
+ x := new(int) // ERROR "new\(int\) escapes to heap"
+ issue10353a(x)()
+}
+
+func issue10353a(x *int) func() { // ERROR "leaking param: x"
+ return func() { // ERROR "func literal escapes to heap"
+ println(*x)
+ }
+}
+
+func issue10353b() {
+ var f func()
+ for {
+ x := new(int) // ERROR "new\(int\) escapes to heap"
+ f = func() { // ERROR "func literal escapes to heap"
+ println(*x)
+ }
+ }
+ _ = f
+}
--- /dev/null
+// run
+
+// Copyright 2015 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.
+
+// issue 10253: cmd/gc: incorrect escape analysis of closures
+// Partial call x.foo was not promoted to heap.
+
+package main
+
+func main() {
+ c := make(chan bool)
+ // Create a new goroutine to get a default-size stack segment.
+ go func() {
+ x := new(X)
+ clos(x.foo)()
+ c <- true
+ }()
+ <-c
+}
+
+type X int
+
+func (x *X) foo() {
+}
+
+func clos(x func()) func() {
+ f := func() {
+ print("")
+ x() // This statement crashed, because the partial call was allocated on the old stack.
+ }
+ // Grow stack so that partial call x becomes invalid if allocated on stack.
+ growstack(10000)
+ c := make(chan bool)
+ // Spoil the previous stack segment.
+ go func() {
+ c <- true
+ }()
+ <-c
+ return f
+}
+
+func growstack(x int) {
+ if x == 0 {
+ return
+ }
+ growstack(x-1)
+}