]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: fix lexical scope of escaped variables
authorAlessandro Arzilli <alessandro.arzilli@gmail.com>
Sun, 9 Jul 2017 15:03:45 +0000 (17:03 +0200)
committerMatthew Dempsky <mdempsky@google.com>
Mon, 10 Jul 2017 20:09:00 +0000 (20:09 +0000)
When a local variable is moved to the heap the declaration position
should be preserved so that later on we can assign it to the correct
DW_TAG_lexical_block.

Fixes #20959

Change-Id: I3700ef53c68ccd506d0633f11374ad88a52b2898
Reviewed-on: https://go-review.googlesource.com/47852
Run-TryBot: Emmanuel Odeke <emm.odeke@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Josh Bleecher Snyder <josharian@gmail.com>
Reviewed-by: David Chase <drchase@google.com>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
src/cmd/compile/internal/gc/esc.go
src/cmd/compile/internal/gc/scope_test.go

index c25eb544cf9faa017e69dbc8de0eaafa26b81c60..87a5b7f29f52c455ab5d8c8d2072e77c188f6624 100644 (file)
@@ -2100,6 +2100,7 @@ func moveToHeap(n *Node) {
        heapaddr := temp(types.NewPtr(n.Type))
        heapaddr.Sym = lookup("&" + n.Sym.Name)
        heapaddr.Orig.Sym = heapaddr.Sym
+       heapaddr.Pos = n.Pos
 
        // Unset AutoTemp to persist the &foo variable name through SSA to
        // liveness analysis.
index f08e900193d749e1df65958a8a83c11538e98109..9113afe279bebe64d8a7d8028e05166c20931e06 100644 (file)
@@ -49,6 +49,7 @@ var testfile = []testline{
        {line: "func f4(x int) { }"},
        {line: "func f5(x int) { }"},
        {line: "func f6(x int) { }"},
+       {line: "func fi(x interface{}) { if a, ok := x.(error); ok { a.Error() } }"},
        {line: "func gret1() int { return 2 }"},
        {line: "func gretbool() bool { return true }"},
        {line: "func gret3() (int, int, int) { return 0, 1, 2 }"},
@@ -163,6 +164,15 @@ var testfile = []testline{
        {line: "        }"},
        {line: "        f(3); f1(b)"},
        {line: "}"},
+       {line: "func TestEscape() {"},
+       {line: "        a := 1", vars: []string{"var a int"}},
+       {line: "        {"},
+       {line: "                b := 2", scopes: []int{1}, vars: []string{"var &b *int", "var p *int"}},
+       {line: "                p := &b", scopes: []int{1}},
+       {line: "                f1(a)", scopes: []int{1}},
+       {line: "                fi(p)", scopes: []int{1}},
+       {line: "        }"},
+       {line: "}"},
        {line: "func main() {"},
        {line: "        TestNestedFor()"},
        {line: "        TestOas2()"},
@@ -173,6 +183,7 @@ var testfile = []testline{
        {line: "        TestBlock()"},
        {line: "        TestDiscontiguousRanges()"},
        {line: "        TestClosureScope()"},
+       {line: "        TestEscape()"},
        {line: "}"},
 }