From 54af752865d4759eb49437904f3a2d04d3779cc8 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 24 Sep 2012 15:53:12 -0400 Subject: [PATCH] cmd/gc: fix escape analysis bug Was not handling &x.y[0] and &x.y.z correctly where y is an array or struct-valued field (not a pointer). R=ken2 CC=golang-dev https://golang.org/cl/6551059 --- src/cmd/gc/esc.c | 8 +++++++- test/escape2.go | 18 ++++++++++++++++++ test/escape4.go | 18 ++++++++++++++++++ 3 files changed, 43 insertions(+), 1 deletion(-) diff --git a/src/cmd/gc/esc.c b/src/cmd/gc/esc.c index bd197ab23e..c5faa041c8 100644 --- a/src/cmd/gc/esc.c +++ b/src/cmd/gc/esc.c @@ -926,9 +926,15 @@ escwalk(EscState *e, int level, Node *dst, Node *src) } break; + case ODOT: + escwalk(e, level, dst, src->left); + break; + case OINDEX: - if(isfixedarray(src->type)) + if(isfixedarray(src->left->type)) { + escwalk(e, level, dst, src->left); break; + } // fall through case OSLICE: case ODOTPTR: diff --git a/test/escape2.go b/test/escape2.go index 8f48ef7bd6..8db12d9913 100644 --- a/test/escape2.go +++ b/test/escape2.go @@ -1211,3 +1211,21 @@ func foo137() { }() }() } + +func foo138() *byte { + type T struct { + x [1]byte + } + t := new(T) // ERROR "new.T. escapes to heap" + return &t.x[0] // ERROR "&t.x.0. escapes to heap" +} + +func foo139() *byte { + type T struct { + x struct { + y byte + } + } + t := new(T) // ERROR "new.T. escapes to heap" + return &t.x.y // ERROR "&t.x.y escapes to heap" +} diff --git a/test/escape4.go b/test/escape4.go index cc3ac78f04..83bc8eb123 100644 --- a/test/escape4.go +++ b/test/escape4.go @@ -37,3 +37,21 @@ func f2() {} // ERROR "can inline f2" // No inline for panic, recover. func f3() { panic(1) } func f4() { recover() } + +func f5() *byte { + type T struct { + x [1]byte + } + t := new(T) // ERROR "new.T. escapes to heap" + return &t.x[0] // ERROR "&t.x.0. escapes to heap" +} + +func f6() *byte { + type T struct { + x struct { + y byte + } + } + t := new(T) // ERROR "new.T. escapes to heap" + return &t.x.y // ERROR "&t.x.y escapes to heap" +} -- 2.48.1