]> Cypherpunks.ru repositories - gostls13.git/blob - test/closure2.go
test/closure2.go: correctly "use" tmp
[gostls13.git] / test / closure2.go
1 // run
2
3 // Copyright 2015 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
6
7 // Check that these do not use "by value" capturing,
8 // because changes are made to the value during the closure.
9
10 package main
11
12 func main() {
13         type X struct {
14                 v int
15         }
16         var x X
17         func() {
18                 x.v++
19         }()
20         if x.v != 1 {
21                 panic("x.v != 1")
22         }
23
24         type Y struct {
25                 X
26         }
27         var y Y
28         func() {
29                 y.v = 1
30         }()
31         if y.v != 1 {
32                 panic("y.v != 1")
33         }
34
35         type Z struct {
36                 a [3]byte
37         }
38         var z Z
39         func() {
40                 i := 0
41                 for z.a[1] = 1; i < 10; i++ {
42                 }
43         }()
44         if z.a[1] != 1 {
45                 panic("z.a[1] != 1")
46         }
47
48         w := 0
49         tmp := 0
50         f := func() {
51                 if w != 1 {
52                         panic("w != 1")
53                 }
54         }
55         func() {
56                 tmp = w // force capture of w, but do not write to it yet
57                 _ = tmp
58                 func() {
59                         func() {
60                                 w++ // write in a nested closure
61                         }()
62                 }()
63         }()
64         f()
65 }