Skip to content

Commit bfa97ba

Browse files
committed
[dev.regabi] test: add another closure test case
When deciding whether a captured variable can be passed by value, the compiler is sensitive to the order that the OCLOSURE node is typechecked relative to the order that the variable is passed to "checkassign". Today, for an assignment like: q, g = 2, func() int { return q } we get this right because we always typecheck the full RHS expression list before calling checkassign on any LHS expression. But I nearly made a change that would interleave this ordering, causing us to call checkassign on q before typechecking the function literal. And alarmingly, there weren't any tests that caught this. So this commit adds one. Change-Id: I66cacd61066c7a229070861a7d973bcc434904cc Reviewed-on: https://go-review.googlesource.com/c/go/+/280998 Trust: Matthew Dempsky <mdempsky@google.com> Run-TryBot: Matthew Dempsky <mdempsky@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
1 parent 67ad695 commit bfa97ba

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

test/closure2.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
package main
1111

12+
var never bool
13+
1214
func main() {
1315
{
1416
type X struct {
@@ -115,4 +117,16 @@ func main() {
115117
panic("g() != 2")
116118
}
117119
}
120+
121+
{
122+
var g func() int
123+
q := 0
124+
q, g = 1, func() int { return q }
125+
if never {
126+
g = func() int { return 2 }
127+
}
128+
if g() != 1 {
129+
panic("g() != 1")
130+
}
131+
}
118132
}

0 commit comments

Comments
 (0)