]> Cypherpunks.ru repositories - gostls13.git/blob - test/escape.go
test: explanatory comments [c-g]*
[gostls13.git] / test / escape.go
1 // run
2
3 // Copyright 2009 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 package main
8
9 // Test for correct heap-moving of escaped variables.
10 // It is hard to check for the allocations, but it is easy
11 // to check that if you call the function twice at the
12 // same stack level, the pointers returned should be
13 // different.
14
15 var bad = false
16
17 var allptr = make([]*int, 0, 100)
18
19 func noalias(p, q *int, s string) {
20         n := len(allptr)
21         *p = -(n+1)
22         *q = -(n+2)
23         allptr = allptr[0:n+2]
24         allptr[n] = p
25         allptr[n+1] = q
26         n += 2
27         for i := 0; i < n; i++ {
28                 if allptr[i] != nil && *allptr[i] != -(i+1) {
29                         println("aliased pointers", -(i+1), *allptr[i], "after", s)
30                         allptr[i] = nil
31                         bad = true
32                 }
33         }
34 }
35
36 func val(p, q *int, v int, s string) {
37         if *p != v {
38                 println("wrong value want", v, "got", *p, "after", s)
39                 bad = true
40         }
41         if *q != v+1 {
42                 println("wrong value want", v+1, "got", *q, "after", s)
43                 bad = true
44         }
45 }
46
47 func chk(p, q *int, v int, s string) {
48         val(p, q, v, s)
49         noalias(p, q, s)
50 }
51
52 func chkalias(p, q *int, v int, s string) {
53         if p != q {
54                 println("want aliased pointers but got different after", s)
55         }
56         if *q != v+1 {
57                 println("wrong value want", v+1, "got", *q, "after", s)
58         }
59 }
60
61 func i_escapes(x int) *int {
62         var i int
63         i = x
64         return &i
65 }
66
67 func j_escapes(x int) *int {
68         var j int = x
69         j = x
70         return &j
71 }
72
73 func k_escapes(x int) *int {
74         k := x
75         return &k
76 }
77
78 func in_escapes(x int) *int {
79         return &x
80 }
81
82 func send(c chan int, x int) {
83         c <- x
84 }
85
86 func select_escapes(x int) *int {
87         c := make(chan int)
88         go send(c, x)
89         select {
90         case req := <-c:
91                 return &req
92         }
93         return nil
94 }
95
96 func select_escapes1(x int, y int) (*int, *int) {
97         c := make(chan int)
98         var a [2]int
99         var p [2]*int
100         a[0] = x
101         a[1] = y
102         for i := 0; i < 2; i++ {
103                 go send(c, a[i])
104                 select {
105                 case req := <-c:
106                         p[i] = &req
107                 }
108         }
109         return p[0], p[1]
110 }
111
112 func range_escapes(x int) *int {
113         var a [1]int
114         a[0] = x
115         for _, v := range a {
116                 return &v
117         }
118         return nil
119 }
120
121 // *is* aliased
122 func range_escapes2(x, y int) (*int, *int) {
123         var a [2]int
124         var p [2]*int
125         a[0] = x
126         a[1] = y
127         for k, v := range a {
128                 p[k] = &v
129         }
130         return p[0], p[1]
131 }
132
133 // *is* aliased
134 func for_escapes2(x int, y int) (*int, *int) {
135         var p [2]*int
136         n := 0
137         for i := x; n < 2; i = y {
138                 p[n] = &i
139                 n++
140         }
141         return p[0], p[1]
142 }
143
144 func out_escapes(i int) (x int, p *int) {
145         x = i
146         p = &x  // ERROR "address of out parameter"
147         return
148 }
149
150 func out_escapes_2(i int) (x int, p *int) {
151         x = i
152         return x, &x    // ERROR "address of out parameter"
153 }
154
155 func defer1(i int) (x int) {
156         c := make(chan int)
157         go func() { x = i; c <- 1 }()
158         <-c
159         return
160 }
161
162 func main() {
163         p, q := i_escapes(1), i_escapes(2)
164         chk(p, q, 1, "i_escapes")
165
166         p, q = j_escapes(3), j_escapes(4)
167         chk(p, q, 3, "j_escapes")
168
169         p, q = k_escapes(5), k_escapes(6)
170         chk(p, q, 5, "k_escapes")
171
172         p, q = in_escapes(7), in_escapes(8)
173         chk(p, q, 7, "in_escapes")
174
175         p, q = select_escapes(9), select_escapes(10)
176         chk(p, q, 9, "select_escapes")
177
178         p, q = select_escapes1(11, 12)
179         chk(p, q, 11, "select_escapes1")
180
181         p, q = range_escapes(13), range_escapes(14)
182         chk(p, q, 13, "range_escapes")
183
184         p, q = range_escapes2(101, 102)
185         chkalias(p, q, 101, "range_escapes2")
186
187         p, q = for_escapes2(103, 104)
188         chkalias(p, q, 103, "for_escapes2")
189
190         _, p = out_escapes(15)
191         _, q = out_escapes(16)
192         chk(p, q, 15, "out_escapes")
193
194         _, p = out_escapes_2(17)
195         _, q = out_escapes_2(18)
196         chk(p, q, 17, "out_escapes_2")
197
198         x := defer1(20)
199         if x != 20 {
200                 println("defer failed", x)
201                 bad = true
202         }
203
204         if bad {
205                 panic("BUG: no escape")
206         }
207 }