cmd/compile: fix lexical block of captured variables

Variables captured by a closure were always assigned to the root scope
in their declaration function. Using decl.Name.Defn.Pos will result in
the correct scope for both the declaration function and the capturing
function.

Fixes #21515

Change-Id: I3960aface3c4fc97e15b36191a74a7bed5b5ebc1
Reviewed-on: https://go-review.googlesource.com/56830
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Alessandro Arzilli 2017-08-18 10:22:19 +02:00 committed by Matthew Dempsky
parent a72e26f246
commit e1cf2be7a8
2 changed files with 31 additions and 8 deletions

View file

@ -173,6 +173,18 @@ var testfile = []testline{
{line: " fi(p)", scopes: []int{1}},
{line: " }"},
{line: "}"},
{line: "func TestCaptureVar(flag bool) func() int {"},
{line: " a := 1", vars: []string{"arg flag bool", "arg ~r1 func() int", "var a int"}},
{line: " if flag {"},
{line: " b := 2", scopes: []int{1}, vars: []string{"var b int", "var f func() int"}},
{line: " f := func() int {", scopes: []int{1, 0}},
{line: " return b + 1"},
{line: " }"},
{line: " return f", scopes: []int{1}},
{line: " }"},
{line: " f1(a)"},
{line: " return nil"},
{line: "}"},
{line: "func main() {"},
{line: " TestNestedFor()"},
{line: " TestOas2()"},
@ -184,6 +196,7 @@ var testfile = []testline{
{line: " TestDiscontiguousRanges()"},
{line: " TestClosureScope()"},
{line: " TestEscape()"},
{line: " TestCaptureVar(true)"},
{line: "}"},
}