LEFT | RIGHT |
1 // errchk -0 $G -m $D/$F.go | 1 // errchk -0 $G -m $D/$F.go |
2 | 2 |
3 // Copyright 2010 The Go Authors. All rights reserved. | 3 // Copyright 2010 The Go Authors. All rights reserved. |
4 // Use of this source code is governed by a BSD-style | 4 // Use of this source code is governed by a BSD-style |
5 // license that can be found in the LICENSE file. | 5 // license that can be found in the LICENSE file. |
6 | 6 |
7 package foo | 7 package foo |
8 | 8 |
9 import ( | 9 import ( |
10 "fmt" | 10 "fmt" |
11 "unsafe" | 11 "unsafe" |
12 ) | 12 ) |
13 | 13 |
14 var gxx *int | 14 var gxx *int |
15 | 15 |
16 func foo1(x int) { // ERROR "moved to heap: x" | 16 func foo1(x int) { // ERROR "moved to heap: x" |
17 » gxx = &x // ERROR "&x escapes to heap" | 17 » gxx = &x // ERROR "&x escapes to heap" |
18 } | 18 } |
19 | 19 |
20 func foo2(yy *int) { // ERROR "leaking param: yy" | 20 func foo2(yy *int) { // ERROR "leaking param: yy" |
21 gxx = yy | 21 gxx = yy |
22 } | 22 } |
23 | 23 |
24 func foo3(x int) *int { // ERROR "moved to heap: x" | 24 func foo3(x int) *int { // ERROR "moved to heap: x" |
25 » return &x // ERROR "&x escapes to heap" | 25 » return &x // ERROR "&x escapes to heap" |
26 } | 26 } |
27 | 27 |
28 type T *T | 28 type T *T |
29 | 29 |
30 func foo3b(t T) { // ERROR "leaking param: t" | 30 func foo3b(t T) { // ERROR "leaking param: t" |
31 *t = t | 31 *t = t |
32 } | 32 } |
33 | 33 |
34 // xx isn't going anywhere, so use of yy is ok | 34 // xx isn't going anywhere, so use of yy is ok |
35 func foo4(xx, yy *int) { // ERROR "xx does not escape" "yy does not escape" | 35 func foo4(xx, yy *int) { // ERROR "xx does not escape" "yy does not escape" |
36 xx = yy | 36 xx = yy |
37 } | 37 } |
38 | 38 |
39 // xx isn't going anywhere, so taking address of yy is ok | 39 // xx isn't going anywhere, so taking address of yy is ok |
40 func foo5(xx **int, yy *int) { // ERROR "xx does not escape" "yy does not escape
" | 40 func foo5(xx **int, yy *int) { // ERROR "xx does not escape" "yy does not escape
" |
41 » xx = &yy // ERROR "&yy does not escape" | 41 » xx = &yy // ERROR "&yy does not escape" |
42 } | 42 } |
43 | 43 |
44 func foo6(xx **int, yy *int) { // ERROR "xx does not escape" "leaking param: yy" | 44 func foo6(xx **int, yy *int) { // ERROR "xx does not escape" "leaking param: yy" |
45 *xx = yy | 45 *xx = yy |
46 } | 46 } |
47 | 47 |
48 func foo7(xx **int, yy *int) { // ERROR "xx does not escape" "yy does not escape
" | 48 func foo7(xx **int, yy *int) { // ERROR "xx does not escape" "yy does not escape
" |
49 **xx = *yy | 49 **xx = *yy |
50 } | 50 } |
51 | 51 |
52 func foo8(xx, yy *int) int { // ERROR "xx does not escape" "yy does not escape" | 52 func foo8(xx, yy *int) int { // ERROR "xx does not escape" "yy does not escape" |
53 xx = yy | 53 xx = yy |
54 return *xx | 54 return *xx |
55 } | 55 } |
56 | 56 |
57 func foo9(xx, yy *int) *int { // ERROR "leaking param: xx" "leaking param: yy" | 57 func foo9(xx, yy *int) *int { // ERROR "leaking param: xx" "leaking param: yy" |
58 xx = yy | 58 xx = yy |
59 return xx | 59 return xx |
60 } | 60 } |
61 | 61 |
62 func foo10(xx, yy *int) { // ERROR "xx does not escape" "yy does not escape" | 62 func foo10(xx, yy *int) { // ERROR "xx does not escape" "yy does not escape" |
63 *xx = *yy | 63 *xx = *yy |
64 } | 64 } |
65 | 65 |
66 func foo11() int { | 66 func foo11() int { |
67 x, y := 0, 42 | 67 x, y := 0, 42 |
68 » xx := &x // ERROR "&x does not escape" | 68 » xx := &x // ERROR "&x does not escape" |
69 » yy := &y // ERROR "&y does not escape" | 69 » yy := &y // ERROR "&y does not escape" |
70 *xx = *yy | 70 *xx = *yy |
71 return x | 71 return x |
72 } | 72 } |
73 | 73 |
74 var xxx **int | 74 var xxx **int |
75 | 75 |
76 func foo12(yyy **int) { // ERROR "leaking param: yyy" | 76 func foo12(yyy **int) { // ERROR "leaking param: yyy" |
77 xxx = yyy | 77 xxx = yyy |
78 } | 78 } |
79 | 79 |
80 func foo13(yyy **int) { // ERROR "yyy does not escape" | 80 func foo13(yyy **int) { // ERROR "yyy does not escape" |
81 *xxx = *yyy | 81 *xxx = *yyy |
82 } | 82 } |
83 | 83 |
84 func foo14(yyy **int) { // ERROR "yyy does not escape" | 84 func foo14(yyy **int) { // ERROR "yyy does not escape" |
85 **xxx = **yyy | 85 **xxx = **yyy |
86 } | 86 } |
87 | 87 |
88 func foo15(yy *int) { // ERROR "moved to heap: yy" | 88 func foo15(yy *int) { // ERROR "moved to heap: yy" |
89 » xxx = &yy // ERROR "&yy escapes to heap" | 89 » xxx = &yy // ERROR "&yy escapes to heap" |
90 } | 90 } |
91 | 91 |
92 func foo16(yy *int) { // ERROR "leaking param: yy" | 92 func foo16(yy *int) { // ERROR "leaking param: yy" |
93 *xxx = yy | 93 *xxx = yy |
94 } | 94 } |
95 | 95 |
96 func foo17(yy *int) { // ERROR "yy does not escape" | 96 func foo17(yy *int) { // ERROR "yy does not escape" |
97 **xxx = *yy | 97 **xxx = *yy |
98 } | 98 } |
99 | 99 |
100 func foo18(y int) { // ERROR "moved to heap: "y" | 100 func foo18(y int) { // ERROR "moved to heap: "y" |
101 » *xxx = &y // ERROR "&y escapes to heap" | 101 » *xxx = &y // ERROR "&y escapes to heap" |
102 } | 102 } |
103 | 103 |
104 func foo19(y int) { | 104 func foo19(y int) { |
105 **xxx = y | 105 **xxx = y |
106 } | 106 } |
107 | 107 |
108 type Bar struct { | 108 type Bar struct { |
109 i int | 109 i int |
110 ii *int | 110 ii *int |
111 } | 111 } |
(...skipping 11 matching lines...) Expand all Loading... |
123 } | 123 } |
124 | 124 |
125 func (b *Bar) NoLeak() int { // ERROR "b does not escape" | 125 func (b *Bar) NoLeak() int { // ERROR "b does not escape" |
126 return *(b.ii) | 126 return *(b.ii) |
127 } | 127 } |
128 | 128 |
129 func (b *Bar) AlsoNoLeak() *int { // ERROR "b does not escape" | 129 func (b *Bar) AlsoNoLeak() *int { // ERROR "b does not escape" |
130 return b.ii | 130 return b.ii |
131 } | 131 } |
132 | 132 |
133 func goLeak(b *Bar) { // ERROR "leaking param: b" | 133 func goLeak(b *Bar) { // ERROR "leaking param: b" |
134 go b.NoLeak() | 134 go b.NoLeak() |
135 } | 135 } |
136 | 136 |
137 type Bar2 struct { | 137 type Bar2 struct { |
138 i [12]int | 138 i [12]int |
139 ii []int | 139 ii []int |
140 } | 140 } |
141 | 141 |
142 func NewBar2() *Bar2 { | 142 func NewBar2() *Bar2 { |
143 return &Bar2{[12]int{42}, nil} // ERROR "&Bar2 literal escapes to heap" | 143 return &Bar2{[12]int{42}, nil} // ERROR "&Bar2 literal escapes to heap" |
144 } | 144 } |
145 | 145 |
146 func (b *Bar2) NoLeak() int { // ERROR "b does not escape" | 146 func (b *Bar2) NoLeak() int { // ERROR "b does not escape" |
147 return b.i[0] | 147 return b.i[0] |
148 } | 148 } |
149 | 149 |
150 func (b *Bar2) Leak() []int { // ERROR "leaking param: b" | 150 func (b *Bar2) Leak() []int { // ERROR "leaking param: b" |
151 » return b.i[:] // ERROR "&b.i escapes to heap" | 151 » return b.i[:] // ERROR "&b.i escapes to heap" |
152 } | 152 } |
153 | 153 |
154 func (b *Bar2) AlsoNoLeak() []int { // ERROR "b does not escape" | 154 func (b *Bar2) AlsoNoLeak() []int { // ERROR "b does not escape" |
155 return b.ii[0:1] | 155 return b.ii[0:1] |
156 } | 156 } |
157 | 157 |
158 func (b *Bar2) LeakSelf() { // ERROR "leaking param: b" | 158 func (b *Bar2) LeakSelf() { // ERROR "leaking param: b" |
159 » b.ii = b.i[0:4] // ERROR "&b.i escapes to heap" | 159 » b.ii = b.i[0:4] // ERROR "&b.i escapes to heap" |
160 } | 160 } |
161 | 161 |
162 func (b *Bar2) LeakSelf2() { // ERROR "leaking param: b" | 162 func (b *Bar2) LeakSelf2() { // ERROR "leaking param: b" |
163 var buf []int | 163 var buf []int |
164 » buf = b.i[0:] // ERROR "&b.i escapes to heap" | 164 » buf = b.i[0:] // ERROR "&b.i escapes to heap" |
165 b.ii = buf | 165 b.ii = buf |
166 } | 166 } |
167 | 167 |
168 func foo21() func() int { | 168 func foo21() func() int { |
169 » x := 42 // ERROR "moved to heap: x" | 169 » x := 42 // ERROR "moved to heap: x" |
170 » return func() int { // ERROR "func literal escapes to heap" | 170 » return func() int { // ERROR "func literal escapes to heap" |
171 » » return x // ERROR "&x escapes to heap" | 171 » » return x // ERROR "&x escapes to heap" |
172 } | 172 } |
173 } | 173 } |
174 | 174 |
175 func foo22() int { | 175 func foo22() int { |
176 x := 42 | 176 x := 42 |
177 » return func() int { // ERROR "func literal does not escape" | 177 » return func() int { // ERROR "func literal does not escape" |
178 return x | 178 return x |
179 }() | 179 }() |
180 } | 180 } |
181 | 181 |
182 func foo23(x int) func() int { // ERROR "moved to heap: x" | 182 func foo23(x int) func() int { // ERROR "moved to heap: x" |
183 » return func() int { // ERROR "func literal escapes to heap" | 183 » return func() int { // ERROR "func literal escapes to heap" |
184 » » return x // ERROR "&x escapes to heap" | 184 » » return x // ERROR "&x escapes to heap" |
185 } | 185 } |
186 } | 186 } |
187 | 187 |
188 func foo23a(x int) func() int { // ERROR "moved to heap: x" | 188 func foo23a(x int) func() int { // ERROR "moved to heap: x" |
189 » f := func() int { // ERROR "func literal escapes to heap" | 189 » f := func() int { // ERROR "func literal escapes to heap" |
190 » » return x // ERROR "&x escapes to heap" | 190 » » return x // ERROR "&x escapes to heap" |
191 } | 191 } |
192 return f | 192 return f |
193 } | 193 } |
194 | 194 |
195 func foo23b(x int) *(func() int) { // ERROR "moved to heap: x" | 195 func foo23b(x int) *(func() int) { // ERROR "moved to heap: x" |
196 f := func() int { return x } // ERROR "moved to heap: f" "func literal e
scapes to heap" "&x escapes to heap" | 196 f := func() int { return x } // ERROR "moved to heap: f" "func literal e
scapes to heap" "&x escapes to heap" |
197 » return &f // ERROR "&f escapes to heap" | 197 » return &f // ERROR "&f escapes to heap" |
198 } | 198 } |
199 | 199 |
200 func foo24(x int) int { | 200 func foo24(x int) int { |
201 » return func() int { // ERROR "func literal does not escape" | 201 » return func() int { // ERROR "func literal does not escape" |
202 return x | 202 return x |
203 }() | 203 }() |
204 } | 204 } |
205 | 205 |
206 var x *int | 206 var x *int |
207 | 207 |
208 func fooleak(xx *int) int { // ERROR "leaking param: xx" | 208 func fooleak(xx *int) int { // ERROR "leaking param: xx" |
209 x = xx | 209 x = xx |
210 return *x | 210 return *x |
211 } | 211 } |
212 | 212 |
213 func foonoleak(xx *int) int { // ERROR "xx does not escape" | 213 func foonoleak(xx *int) int { // ERROR "xx does not escape" |
214 return *x + *xx | 214 return *x + *xx |
215 } | 215 } |
216 | 216 |
217 func foo31(x int) int { // ERROR "moved to heap: x" | 217 func foo31(x int) int { // ERROR "moved to heap: x" |
218 » return fooleak(&x) // ERROR "&x escapes to heap" | 218 » return fooleak(&x) // ERROR "&x escapes to heap" |
219 } | 219 } |
220 | 220 |
221 func foo32(x int) int { | 221 func foo32(x int) int { |
222 » return foonoleak(&x) // ERROR "&x does not escape" | 222 » return foonoleak(&x) // ERROR "&x does not escape" |
223 } | 223 } |
224 | 224 |
225 type Foo struct { | 225 type Foo struct { |
226 xx *int | 226 xx *int |
227 x int | 227 x int |
228 } | 228 } |
229 | 229 |
230 var F Foo | 230 var F Foo |
231 var pf *Foo | 231 var pf *Foo |
232 | 232 |
233 func (f *Foo) fooleak() { // ERROR "leaking param: f" | 233 func (f *Foo) fooleak() { // ERROR "leaking param: f" |
234 pf = f | 234 pf = f |
235 } | 235 } |
236 | 236 |
237 func (f *Foo) foonoleak() { // ERROR "f does not escape" | 237 func (f *Foo) foonoleak() { // ERROR "f does not escape" |
238 F.x = f.x | 238 F.x = f.x |
239 } | 239 } |
240 | 240 |
241 func (f *Foo) Leak() { // ERROR "leaking param: f" | 241 func (f *Foo) Leak() { // ERROR "leaking param: f" |
242 f.fooleak() | 242 f.fooleak() |
243 } | 243 } |
244 | 244 |
245 func (f *Foo) NoLeak() { // ERROR "f does not escape" | 245 func (f *Foo) NoLeak() { // ERROR "f does not escape" |
246 f.foonoleak() | 246 f.foonoleak() |
247 } | 247 } |
248 | 248 |
249 func foo41(x int) { // ERROR "moved to heap: x" | 249 func foo41(x int) { // ERROR "moved to heap: x" |
250 » F.xx = &x // ERROR "&x escapes to heap" | 250 » F.xx = &x // ERROR "&x escapes to heap" |
251 } | 251 } |
252 | 252 |
253 func (f *Foo) foo42(x int) { // ERROR "f does not escape" "moved to heap: x" | 253 func (f *Foo) foo42(x int) { // ERROR "f does not escape" "moved to heap: x" |
254 » f.xx = &x // ERROR "&x escapes to heap" | 254 » f.xx = &x // ERROR "&x escapes to heap" |
255 } | 255 } |
256 | 256 |
257 func foo43(f *Foo, x int) { // ERROR "f does not escape" "moved to heap: x" | 257 func foo43(f *Foo, x int) { // ERROR "f does not escape" "moved to heap: x" |
258 » f.xx = &x // ERROR "&x escapes to heap" | 258 » f.xx = &x // ERROR "&x escapes to heap" |
259 } | 259 } |
260 | 260 |
261 func foo44(yy *int) { // ERROR "leaking param: yy" | 261 func foo44(yy *int) { // ERROR "leaking param: yy" |
262 F.xx = yy | 262 F.xx = yy |
263 } | 263 } |
264 | 264 |
265 func (f *Foo) foo45() { // ERROR "f does not escape" | 265 func (f *Foo) foo45() { // ERROR "f does not escape" |
266 F.x = f.x | 266 F.x = f.x |
267 } | 267 } |
268 | 268 |
269 func (f *Foo) foo46() { // ERROR "f does not escape" | 269 func (f *Foo) foo46() { // ERROR "f does not escape" |
270 F.xx = f.xx | 270 F.xx = f.xx |
271 } | 271 } |
272 | 272 |
273 func (f *Foo) foo47() { // ERROR "leaking param: f" | 273 func (f *Foo) foo47() { // ERROR "leaking param: f" |
274 » f.xx = &f.x // ERROR "&f.x escapes to heap" | 274 » f.xx = &f.x // ERROR "&f.x escapes to heap" |
275 } | 275 } |
276 | 276 |
277 var ptrSlice []*int | 277 var ptrSlice []*int |
278 | 278 |
279 func foo50(i *int) { // ERROR "leaking param: i" | 279 func foo50(i *int) { // ERROR "leaking param: i" |
280 ptrSlice[0] = i | 280 ptrSlice[0] = i |
281 } | 281 } |
282 | 282 |
283 var ptrMap map[*int]*int | 283 var ptrMap map[*int]*int |
284 | 284 |
285 func foo51(i *int) { // ERROR "leaking param: i" | 285 func foo51(i *int) { // ERROR "leaking param: i" |
286 ptrMap[i] = i | 286 ptrMap[i] = i |
287 } | 287 } |
288 | 288 |
289 func indaddr1(x int) *int { // ERROR "moved to heap: x" | 289 func indaddr1(x int) *int { // ERROR "moved to heap: x" |
290 » return &x // ERROR "&x escapes to heap" | 290 » return &x // ERROR "&x escapes to heap" |
291 } | 291 } |
292 | 292 |
293 func indaddr2(x *int) *int { // ERROR "leaking param: x" | 293 func indaddr2(x *int) *int { // ERROR "leaking param: x" |
294 » return *&x // ERROR "&x does not escape" | 294 » return *&x // ERROR "&x does not escape" |
295 } | 295 } |
296 | 296 |
297 func indaddr3(x *int32) *int { // ERROR "leaking param: x" | 297 func indaddr3(x *int32) *int { // ERROR "leaking param: x" |
298 » return *(**int)(unsafe.Pointer(&x)) // ERROR "&x does not escape" | 298 » return *(**int)(unsafe.Pointer(&x)) // ERROR "&x does not escape" |
299 } | 299 } |
300 | 300 |
301 // From package math: | 301 // From package math: |
302 | 302 |
303 func Float32bits(f float32) uint32 { | 303 func Float32bits(f float32) uint32 { |
304 » return *(*uint32)(unsafe.Pointer(&f)) // ERROR "&f does not escape" | 304 » return *(*uint32)(unsafe.Pointer(&f)) // ERROR "&f does not escape" |
305 } | 305 } |
306 | 306 |
307 func Float32frombits(b uint32) float32 { | 307 func Float32frombits(b uint32) float32 { |
308 » return *(*float32)(unsafe.Pointer(&b)) // ERROR "&b does not escape" | 308 » return *(*float32)(unsafe.Pointer(&b)) // ERROR "&b does not escape" |
309 } | 309 } |
310 | 310 |
311 func Float64bits(f float64) uint64 { | 311 func Float64bits(f float64) uint64 { |
312 » return *(*uint64)(unsafe.Pointer(&f)) // ERROR "&f does not escape" | 312 » return *(*uint64)(unsafe.Pointer(&f)) // ERROR "&f does not escape" |
313 } | 313 } |
314 | 314 |
315 func Float64frombits(b uint64) float64 { | 315 func Float64frombits(b uint64) float64 { |
316 » return *(*float64)(unsafe.Pointer(&b)) // ERROR "&b does not escape" | 316 » return *(*float64)(unsafe.Pointer(&b)) // ERROR "&b does not escape" |
317 } | 317 } |
318 | 318 |
319 // contrast with | 319 // contrast with |
320 func float64bitsptr(f float64) *uint64 { // ERROR "moved to heap: f" | 320 func float64bitsptr(f float64) *uint64 { // ERROR "moved to heap: f" |
321 » return (*uint64)(unsafe.Pointer(&f)) // ERROR "&f escapes to heap" | 321 » return (*uint64)(unsafe.Pointer(&f)) // ERROR "&f escapes to heap" |
322 } | 322 } |
323 | 323 |
324 func float64ptrbitsptr(f *float64) *uint64 { // ERROR "leaking param: f" | 324 func float64ptrbitsptr(f *float64) *uint64 { // ERROR "leaking param: f" |
325 return (*uint64)(unsafe.Pointer(f)) | 325 return (*uint64)(unsafe.Pointer(f)) |
326 } | 326 } |
327 | 327 |
328 func typesw(i interface{}) *int { // ERROR "leaking param: i" | 328 func typesw(i interface{}) *int { // ERROR "leaking param: i" |
329 switch val := i.(type) { | 329 switch val := i.(type) { |
330 case *int: | 330 case *int: |
331 return val | 331 return val |
332 case *int8: | 332 case *int8: |
333 v := int(*val) // ERROR "moved to heap: v" | 333 v := int(*val) // ERROR "moved to heap: v" |
334 » » return &v // ERROR "&v escapes to heap" | 334 » » return &v // ERROR "&v escapes to heap" |
335 } | 335 } |
336 return nil | 336 return nil |
337 } | 337 } |
338 | 338 |
339 func exprsw(i *int) *int { // ERROR "leaking param: i" | 339 func exprsw(i *int) *int { // ERROR "leaking param: i" |
340 switch j := i; *j + 110 { | 340 switch j := i; *j + 110 { |
341 case 12: | 341 case 12: |
342 return j | 342 return j |
343 case 42: | 343 case 42: |
344 return nil | 344 return nil |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
405 func foo64b(m M) { // ERROR "leaking param: m" | 405 func foo64b(m M) { // ERROR "leaking param: m" |
406 defer m.M() | 406 defer m.M() |
407 } | 407 } |
408 | 408 |
409 type MV int | 409 type MV int |
410 | 410 |
411 func (MV) M() {} | 411 func (MV) M() {} |
412 | 412 |
413 func foo65() { | 413 func foo65() { |
414 var mv MV | 414 var mv MV |
415 » foo63(&mv) // ERROR "&mv does not escape" | 415 » foo63(&mv) // ERROR "&mv does not escape" |
416 } | 416 } |
417 | 417 |
418 func foo66() { | 418 func foo66() { |
419 » var mv MV // ERROR "moved to heap: mv" | 419 » var mv MV // ERROR "moved to heap: mv" |
420 » foo64(&mv) // ERROR "&mv escapes to heap" | 420 » foo64(&mv) // ERROR "&mv escapes to heap" |
421 } | 421 } |
422 | 422 |
423 func foo67() { | 423 func foo67() { |
424 var mv MV | 424 var mv MV |
425 foo63(mv) | 425 foo63(mv) |
426 } | 426 } |
427 | 427 |
428 func foo68() { | 428 func foo68() { |
429 var mv MV | 429 var mv MV |
430 foo64(mv) // escapes but it's an int so irrelevant | 430 foo64(mv) // escapes but it's an int so irrelevant |
431 } | 431 } |
432 | 432 |
433 func foo69(m M) { // ERROR "leaking param: m" | 433 func foo69(m M) { // ERROR "leaking param: m" |
434 foo64(m) | 434 foo64(m) |
435 } | 435 } |
436 | 436 |
437 func foo70(mv1 *MV, m M) { // ERROR "leaking param: mv1" "leaking param: m" | 437 func foo70(mv1 *MV, m M) { // ERROR "leaking param: mv1" "leaking param: m" |
438 m = mv1 | 438 m = mv1 |
439 foo64(m) | 439 foo64(m) |
440 } | 440 } |
441 | 441 |
442 func foo71(x *int) []*int { // ERROR "leaking param: x" | 442 func foo71(x *int) []*int { // ERROR "leaking param: x" |
443 var y []*int | 443 var y []*int |
444 y = append(y, x) | 444 y = append(y, x) |
445 return y | 445 return y |
446 } | 446 } |
447 | 447 |
448 func foo71a(x int) []*int { // ERROR "moved to heap: x" | 448 func foo71a(x int) []*int { // ERROR "moved to heap: x" |
449 var y []*int | 449 var y []*int |
450 » y = append(y, &x) // ERROR "&x escapes to heap" | 450 » y = append(y, &x) // ERROR "&x escapes to heap" |
451 return y | 451 return y |
452 } | 452 } |
453 | 453 |
454 func foo72() { | 454 func foo72() { |
455 var x int | 455 var x int |
456 var y [1]*int | 456 var y [1]*int |
457 » y[0] = &x // ERROR "&x does not escape" | 457 » y[0] = &x // ERROR "&x does not escape" |
458 } | 458 } |
459 | 459 |
460 func foo72aa() [10]*int { | 460 func foo72aa() [10]*int { |
461 var x int // ERROR "moved to heap: x" | 461 var x int // ERROR "moved to heap: x" |
462 var y [10]*int | 462 var y [10]*int |
463 » y[0] = &x // ERROR "&x escapes to heap" | 463 » y[0] = &x // ERROR "&x escapes to heap" |
464 return y | 464 return y |
465 } | 465 } |
466 | 466 |
467 func foo72a() { | 467 func foo72a() { |
468 var y [10]*int | 468 var y [10]*int |
469 for i := 0; i < 10; i++ { | 469 for i := 0; i < 10; i++ { |
470 // escapes its scope | 470 // escapes its scope |
471 » » x := i // ERROR "moved to heap: x" | 471 » » x := i // ERROR "moved to heap: x" |
472 y[i] = &x // ERROR "&x escapes to heap" | 472 y[i] = &x // ERROR "&x escapes to heap" |
473 } | 473 } |
474 return | 474 return |
475 } | 475 } |
476 | 476 |
477 func foo72b() [10]*int { | 477 func foo72b() [10]*int { |
478 var y [10]*int | 478 var y [10]*int |
479 for i := 0; i < 10; i++ { | 479 for i := 0; i < 10; i++ { |
480 » » x := i // ERROR "moved to heap: x" | 480 » » x := i // ERROR "moved to heap: x" |
481 » » y[i] = &x // ERROR "&x escapes to heap" | 481 » » y[i] = &x // ERROR "&x escapes to heap" |
482 } | 482 } |
483 return y | 483 return y |
484 } | 484 } |
485 | 485 |
486 // issue 2145 | 486 // issue 2145 |
487 func foo73() { | 487 func foo73() { |
488 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape" | 488 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape" |
489 for _, v := range s { | 489 for _, v := range s { |
490 » » vv := v // ERROR "moved to heap: vv" | 490 » » vv := v // ERROR "moved to heap: vv" |
491 // actually just escapes its scope | 491 // actually just escapes its scope |
492 defer func() { // ERROR "func literal escapes to heap" | 492 defer func() { // ERROR "func literal escapes to heap" |
493 » » » println(vv) // ERROR "&vv escapes to heap" | 493 » » » println(vv) // ERROR "&vv escapes to heap" |
494 }() | 494 }() |
495 } | 495 } |
496 } | 496 } |
497 | 497 |
498 func foo74() { | 498 func foo74() { |
499 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape" | 499 s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape" |
500 for _, v := range s { | 500 for _, v := range s { |
501 » » vv := v // ERROR "moved to heap: vv" | 501 » » vv := v // ERROR "moved to heap: vv" |
502 // actually just escapes its scope | 502 // actually just escapes its scope |
503 fn := func() { // ERROR "func literal escapes to heap" | 503 fn := func() { // ERROR "func literal escapes to heap" |
504 » » » println(vv) // ERROR "&vv escapes to heap" | 504 » » » println(vv) // ERROR "&vv escapes to heap" |
505 } | 505 } |
506 defer fn() | 506 defer fn() |
507 } | 507 } |
508 } | 508 } |
509 | 509 |
510 func myprint(y *int, x ...interface{}) *int { // ERROR "x does not escape" "leak
ing param: y" | 510 func myprint(y *int, x ...interface{}) *int { // ERROR "x does not escape" "leak
ing param: y" |
511 return y | 511 return y |
512 } | 512 } |
513 | 513 |
514 func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "y does not esca
pe" "leaking param: x" | 514 func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "y does not esca
pe" "leaking param: x" |
515 » return &x[0] // ERROR "&x.0. escapes to heap" | 515 » return &x[0] // ERROR "&x.0. escapes to heap" |
516 } | 516 } |
517 | 517 |
518 func foo75(z *int) { // ERROR "leaking param: z" | 518 func foo75(z *int) { // ERROR "leaking param: z" |
519 myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" | 519 myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" |
520 } | 520 } |
521 | 521 |
522 func foo75a(z *int) { // ERROR "z does not escape" | 522 func foo75a(z *int) { // ERROR "z does not escape" |
523 myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" | 523 myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" |
524 } | 524 } |
525 | 525 |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
562 | 562 |
563 func foo77(z []interface{}) { // ERROR "z does not escape" | 563 func foo77(z []interface{}) { // ERROR "z does not escape" |
564 myprint(nil, z...) // z does not escape | 564 myprint(nil, z...) // z does not escape |
565 } | 565 } |
566 | 566 |
567 func foo77a(z []interface{}) { // ERROR "leaking param: z" | 567 func foo77a(z []interface{}) { // ERROR "leaking param: z" |
568 myprint1(nil, z...) | 568 myprint1(nil, z...) |
569 } | 569 } |
570 | 570 |
571 func foo78(z int) *int { // ERROR "moved to heap: z" | 571 func foo78(z int) *int { // ERROR "moved to heap: z" |
572 » return &z // ERROR "&z escapes to heap" | 572 » return &z // ERROR "&z escapes to heap" |
573 } | 573 } |
574 | 574 |
575 func foo78a(z int) *int { // ERROR "moved to heap: z" | 575 func foo78a(z int) *int { // ERROR "moved to heap: z" |
576 » y := &z // ERROR "&z escapes to heap" | 576 » y := &z // ERROR "&z escapes to heap" |
577 » x := &y // ERROR "&y does not escape" | 577 » x := &y // ERROR "&y does not escape" |
578 return *x // really return y | 578 return *x // really return y |
579 } | 579 } |
580 | 580 |
581 func foo79() *int { | 581 func foo79() *int { |
582 return new(int) // ERROR "new[(]int[)] escapes to heap" | 582 return new(int) // ERROR "new[(]int[)] escapes to heap" |
583 } | 583 } |
584 | 584 |
585 func foo80() *int { | 585 func foo80() *int { |
586 var z *int | 586 var z *int |
587 for { | 587 for { |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
681 func foo101(m [1]*int) *int { // ERROR "leaking param: m" | 681 func foo101(m [1]*int) *int { // ERROR "leaking param: m" |
682 for _, v := range m { | 682 for _, v := range m { |
683 return v | 683 return v |
684 } | 684 } |
685 return nil | 685 return nil |
686 } | 686 } |
687 | 687 |
688 // does not leak m | 688 // does not leak m |
689 func foo101a(m [1]*int) *int { // ERROR "m does not escape" | 689 func foo101a(m [1]*int) *int { // ERROR "m does not escape" |
690 for i := range m { // ERROR "moved to heap: i" | 690 for i := range m { // ERROR "moved to heap: i" |
691 » » return &i // ERROR "&i escapes to heap" | 691 » » return &i // ERROR "&i escapes to heap" |
692 } | 692 } |
693 return nil | 693 return nil |
694 } | 694 } |
695 | 695 |
696 // does leak x | 696 // does leak x |
697 func foo102(m []*int, x *int) { // ERROR "m does not escape" "leaking param: x" | 697 func foo102(m []*int, x *int) { // ERROR "m does not escape" "leaking param: x" |
698 m[0] = x | 698 m[0] = x |
699 } | 699 } |
700 | 700 |
701 // does not leak x | 701 // does not leak x |
702 func foo103(m [1]*int, x *int) { // ERROR "m does not escape" "x does not escape
" | 702 func foo103(m [1]*int, x *int) { // ERROR "m does not escape" "x does not escape
" |
703 m[0] = x | 703 m[0] = x |
704 } | 704 } |
705 | 705 |
706 var y []*int | 706 var y []*int |
707 | 707 |
708 // does not leak x | 708 // does not leak x |
709 func foo104(x []*int) { // ERROR "x does not escape" | 709 func foo104(x []*int) { // ERROR "x does not escape" |
710 copy(y, x) | 710 copy(y, x) |
711 } | 711 } |
712 | 712 |
713 // does not leak x | 713 // does not leak x |
714 func foo105(x []*int) { // ERROR "x does not escape" | 714 func foo105(x []*int) { // ERROR "x does not escape" |
715 _ = append(y, x...) | 715 _ = append(y, x...) |
716 } | 716 } |
717 | 717 |
718 // does leak x | 718 // does leak x |
719 func foo106(x *int) { // ERROR "leaking param: x" | 719 func foo106(x *int) { // ERROR "leaking param: x" |
720 _ = append(y, x) | 720 _ = append(y, x) |
721 } | 721 } |
722 | 722 |
723 func foo107(x *int) map[*int]*int { // ERROR "leaking param: x" | 723 func foo107(x *int) map[*int]*int { // ERROR "leaking param: x" |
724 return map[*int]*int{x: nil} // ERROR "map.* literal escapes to heap" | 724 return map[*int]*int{x: nil} // ERROR "map.* literal escapes to heap" |
725 } | 725 } |
726 | 726 |
727 func foo108(x *int) map[*int]*int { // ERROR "leaking param: x" | 727 func foo108(x *int) map[*int]*int { // ERROR "leaking param: x" |
728 return map[*int]*int{nil: x} // ERROR "map.* literal escapes to heap" | 728 return map[*int]*int{nil: x} // ERROR "map.* literal escapes to heap" |
729 } | 729 } |
730 | 730 |
731 func foo109(x *int) *int { // ERROR "leaking param: x" | 731 func foo109(x *int) *int { // ERROR "leaking param: x" |
732 » m := map[*int]*int{x: nil} // ERROR "map.* literal does not escape" | 732 » m := map[*int]*int{x: nil} // ERROR "map.* literal does not escape" |
733 for k, _ := range m { | 733 for k, _ := range m { |
734 return k | 734 return k |
735 } | 735 } |
736 return nil | 736 return nil |
737 } | 737 } |
738 | 738 |
739 func foo110(x *int) *int { // ERROR "leaking param: x" | 739 func foo110(x *int) *int { // ERROR "leaking param: x" |
740 » m := map[*int]*int{nil: x} // ERROR "map.* literal does not escape" | 740 » m := map[*int]*int{nil: x} // ERROR "map.* literal does not escape" |
741 return m[nil] | 741 return m[nil] |
742 } | 742 } |
743 | 743 |
744 func foo111(x *int) *int { // ERROR "leaking param: x" | 744 func foo111(x *int) *int { // ERROR "leaking param: x" |
745 » m := []*int{x} // ERROR "\[\]\*int literal does not escape" | 745 » m := []*int{x} // ERROR "\[\]\*int literal does not escape" |
746 return m[0] | 746 return m[0] |
747 } | 747 } |
748 | 748 |
749 func foo112(x *int) *int { // ERROR "leaking param: x" | 749 func foo112(x *int) *int { // ERROR "leaking param: x" |
750 m := [1]*int{x} | 750 m := [1]*int{x} |
751 return m[0] | 751 return m[0] |
752 } | 752 } |
753 | 753 |
754 func foo113(x *int) *int { // ERROR "leaking param: x" | 754 func foo113(x *int) *int { // ERROR "leaking param: x" |
755 m := Bar{ii: x} | 755 m := Bar{ii: x} |
756 return m.ii | 756 return m.ii |
757 } | 757 } |
758 | 758 |
759 func foo114(x *int) *int { // ERROR "leaking param: x" | 759 func foo114(x *int) *int { // ERROR "leaking param: x" |
760 » m := &Bar{ii: x} // ERROR "&Bar literal does not escape" | 760 » m := &Bar{ii: x} // ERROR "&Bar literal does not escape" |
761 return m.ii | 761 return m.ii |
762 } | 762 } |
763 | 763 |
764 func foo115(x *int) *int { // ERROR "leaking param: x" | 764 func foo115(x *int) *int { // ERROR "leaking param: x" |
765 return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1)) | 765 return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1)) |
766 } | 766 } |
767 | 767 |
768 func foo116(b bool) *int { | 768 func foo116(b bool) *int { |
769 if b { | 769 if b { |
770 » » x := 1 // ERROR "moved to heap: x" | 770 » » x := 1 // ERROR "moved to heap: x" |
771 » » return &x // ERROR "&x escapes to heap" | 771 » » return &x // ERROR "&x escapes to heap" |
772 } else { | 772 } else { |
773 » » y := 1 // ERROR "moved to heap: y" | 773 » » y := 1 // ERROR "moved to heap: y" |
774 » » return &y // ERROR "&y escapes to heap" | 774 » » return &y // ERROR "&y escapes to heap" |
775 » } | 775 » } |
776 » return nil | 776 » return nil |
777 } | 777 } |
778 | 778 |
779 func foo117(unknown func(interface{})) { // ERROR "unknown does not escape" | 779 func foo117(unknown func(interface{})) { // ERROR "unknown does not escape" |
780 » x := 1 // ERROR "moved to heap: x" | 780 » x := 1 // ERROR "moved to heap: x" |
781 unknown(&x) // ERROR "&x escapes to heap" | 781 unknown(&x) // ERROR "&x escapes to heap" |
782 } | 782 } |
783 | 783 |
784 func foo118(unknown func(*int)) { // ERROR "unknown does not escape" | 784 func foo118(unknown func(*int)) { // ERROR "unknown does not escape" |
785 » x := 1 // ERROR "moved to heap: x" | 785 » x := 1 // ERROR "moved to heap: x" |
786 unknown(&x) // ERROR "&x escapes to heap" | 786 unknown(&x) // ERROR "&x escapes to heap" |
787 } | 787 } |
788 | 788 |
789 func external(*int) | 789 func external(*int) |
790 | 790 |
791 func foo119(x *int) { // ERROR "leaking param: x" | 791 func foo119(x *int) { // ERROR "leaking param: x" |
792 external(x) | 792 external(x) |
793 } | 793 } |
794 | 794 |
795 func foo120() { | 795 func foo120() { |
796 // formerly exponential time analysis | 796 // formerly exponential time analysis |
797 L1: | 797 L1: |
798 L2: | 798 L2: |
799 L3: | 799 L3: |
800 L4: | 800 L4: |
801 L5: | 801 L5: |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
993 goto L96 | 993 goto L96 |
994 goto L97 | 994 goto L97 |
995 goto L98 | 995 goto L98 |
996 goto L99 | 996 goto L99 |
997 goto L100 | 997 goto L100 |
998 } | 998 } |
999 | 999 |
1000 func foo121() { | 1000 func foo121() { |
1001 for i := 0; i < 10; i++ { | 1001 for i := 0; i < 10; i++ { |
1002 defer myprint(nil, i) // ERROR "[.][.][.] argument escapes to he
ap" | 1002 defer myprint(nil, i) // ERROR "[.][.][.] argument escapes to he
ap" |
1003 » » go myprint(nil, i) // ERROR "[.][.][.] argument escapes to heap" | 1003 » » go myprint(nil, i) // ERROR "[.][.][.] argument escapes to he
ap" |
1004 } | 1004 } |
1005 } | 1005 } |
1006 | 1006 |
1007 // same as foo121 but check across import | 1007 // same as foo121 but check across import |
1008 func foo121b() { | 1008 func foo121b() { |
1009 for i := 0; i < 10; i++ { | 1009 for i := 0; i < 10; i++ { |
1010 defer fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes t
o heap" | 1010 defer fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes t
o heap" |
1011 » » go fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes to h
eap" | 1011 » » go fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes t
o heap" |
1012 » } | 1012 » } |
1013 } | 1013 } |
1014 | |
LEFT | RIGHT |