Skip to content

Commit

Permalink
cmd/compile: fix lexical scope of escaped variables
Browse files Browse the repository at this point in the history
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 <[email protected]>
TryBot-Result: Gobot Gobot <[email protected]>
Reviewed-by: Josh Bleecher Snyder <[email protected]>
Reviewed-by: David Chase <[email protected]>
Reviewed-by: Matthew Dempsky <[email protected]>
  • Loading branch information
aarzilli authored and mdempsky committed Jul 10, 2017
1 parent 123fd46 commit 6f83b75
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/cmd/compile/internal/gc/esc.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
11 changes: 11 additions & 0 deletions src/cmd/compile/internal/gc/scope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }"},
Expand Down Expand Up @@ -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()"},
Expand All @@ -173,6 +183,7 @@ var testfile = []testline{
{line: " TestBlock()"},
{line: " TestDiscontiguousRanges()"},
{line: " TestClosureScope()"},
{line: " TestEscape()"},
{line: "}"},
}

Expand Down

0 comments on commit 6f83b75

Please sign in to comment.