From: Alessandro Arzilli Date: Sun, 9 Jul 2017 15:03:45 +0000 (+0200) Subject: cmd/compile: fix lexical scope of escaped variables X-Git-Tag: go1.9rc1~82 X-Git-Url: http://www.git.cypherpunks.su/?a=commitdiff_plain;h=6f83b75be2bf038a3a919ad7bd64eda2ee9a934a;p=gostls13.git cmd/compile: fix lexical scope of escaped variables 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 TryBot-Result: Gobot Gobot Reviewed-by: Josh Bleecher Snyder Reviewed-by: David Chase Reviewed-by: Matthew Dempsky --- diff --git a/src/cmd/compile/internal/gc/esc.go b/src/cmd/compile/internal/gc/esc.go index c25eb544cf..87a5b7f29f 100644 --- a/src/cmd/compile/internal/gc/esc.go +++ b/src/cmd/compile/internal/gc/esc.go @@ -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. diff --git a/src/cmd/compile/internal/gc/scope_test.go b/src/cmd/compile/internal/gc/scope_test.go index f08e900193..9113afe279 100644 --- a/src/cmd/compile/internal/gc/scope_test.go +++ b/src/cmd/compile/internal/gc/scope_test.go @@ -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: "}"}, }