Skip to content

Commit 6f83b75

Browse files
aarzillimdempsky
authored andcommitted
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 <[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]>
1 parent 123fd46 commit 6f83b75

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

src/cmd/compile/internal/gc/esc.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2100,6 +2100,7 @@ func moveToHeap(n *Node) {
21002100
heapaddr := temp(types.NewPtr(n.Type))
21012101
heapaddr.Sym = lookup("&" + n.Sym.Name)
21022102
heapaddr.Orig.Sym = heapaddr.Sym
2103+
heapaddr.Pos = n.Pos
21032104

21042105
// Unset AutoTemp to persist the &foo variable name through SSA to
21052106
// liveness analysis.

src/cmd/compile/internal/gc/scope_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ var testfile = []testline{
4949
{line: "func f4(x int) { }"},
5050
{line: "func f5(x int) { }"},
5151
{line: "func f6(x int) { }"},
52+
{line: "func fi(x interface{}) { if a, ok := x.(error); ok { a.Error() } }"},
5253
{line: "func gret1() int { return 2 }"},
5354
{line: "func gretbool() bool { return true }"},
5455
{line: "func gret3() (int, int, int) { return 0, 1, 2 }"},
@@ -163,6 +164,15 @@ var testfile = []testline{
163164
{line: " }"},
164165
{line: " f(3); f1(b)"},
165166
{line: "}"},
167+
{line: "func TestEscape() {"},
168+
{line: " a := 1", vars: []string{"var a int"}},
169+
{line: " {"},
170+
{line: " b := 2", scopes: []int{1}, vars: []string{"var &b *int", "var p *int"}},
171+
{line: " p := &b", scopes: []int{1}},
172+
{line: " f1(a)", scopes: []int{1}},
173+
{line: " fi(p)", scopes: []int{1}},
174+
{line: " }"},
175+
{line: "}"},
166176
{line: "func main() {"},
167177
{line: " TestNestedFor()"},
168178
{line: " TestOas2()"},
@@ -173,6 +183,7 @@ var testfile = []testline{
173183
{line: " TestBlock()"},
174184
{line: " TestDiscontiguousRanges()"},
175185
{line: " TestClosureScope()"},
186+
{line: " TestEscape()"},
176187
{line: "}"},
177188
}
178189

0 commit comments

Comments
 (0)