]> Cypherpunks.ru repositories - gostls13.git/blob - test/escape2n.go
cmd/gc: fix escape analysis of closures
[gostls13.git] / test / escape2n.go
1 // errorcheck -0 -N -m -l
2
3 // Copyright 2010 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 // Test, using compiler diagnostic flags, that the escape analysis is working.
8 // Compiles but does not run.  Inlining is disabled.
9 // Registerization is disabled too (-N), which should
10 // have no effect on escape analysis.
11
12 package foo
13
14 import (
15         "fmt"
16         "unsafe"
17 )
18
19 var gxx *int
20
21 func foo1(x int) { // ERROR "moved to heap: x"
22         gxx = &x // ERROR "&x escapes to heap"
23 }
24
25 func foo2(yy *int) { // ERROR "leaking param: yy"
26         gxx = yy
27 }
28
29 func foo3(x int) *int { // ERROR "moved to heap: x"
30         return &x // ERROR "&x escapes to heap"
31 }
32
33 type T *T
34
35 func foo3b(t T) { // ERROR "leaking param: t"
36         *t = t
37 }
38
39 // xx isn't going anywhere, so use of yy is ok
40 func foo4(xx, yy *int) { // ERROR "xx does not escape" "yy does not escape"
41         xx = yy
42 }
43
44 // xx isn't going anywhere, so taking address of yy is ok
45 func foo5(xx **int, yy *int) { // ERROR "xx does not escape" "yy does not escape"
46         xx = &yy // ERROR "&yy does not escape"
47 }
48
49 func foo6(xx **int, yy *int) { // ERROR "xx does not escape" "leaking param: yy"
50         *xx = yy
51 }
52
53 func foo7(xx **int, yy *int) { // ERROR "xx does not escape" "yy does not escape"
54         **xx = *yy
55 }
56
57 func foo8(xx, yy *int) int { // ERROR "xx does not escape" "yy does not escape"
58         xx = yy
59         return *xx
60 }
61
62 func foo9(xx, yy *int) *int { // ERROR "leaking param: xx" "leaking param: yy"
63         xx = yy
64         return xx
65 }
66
67 func foo10(xx, yy *int) { // ERROR "xx does not escape" "yy does not escape"
68         *xx = *yy
69 }
70
71 func foo11() int {
72         x, y := 0, 42
73         xx := &x // ERROR "&x does not escape"
74         yy := &y // ERROR "&y does not escape"
75         *xx = *yy
76         return x
77 }
78
79 var xxx **int
80
81 func foo12(yyy **int) { // ERROR "leaking param: yyy"
82         xxx = yyy
83 }
84
85 // Must treat yyy as leaking because *yyy leaks, and the escape analysis
86 // summaries in exported metadata do not distinguish these two cases.
87 func foo13(yyy **int) { // ERROR "leaking param: yyy"
88         *xxx = *yyy
89 }
90
91 func foo14(yyy **int) { // ERROR "yyy does not escape"
92         **xxx = **yyy
93 }
94
95 func foo15(yy *int) { // ERROR "moved to heap: yy"
96         xxx = &yy // ERROR "&yy escapes to heap"
97 }
98
99 func foo16(yy *int) { // ERROR "leaking param: yy"
100         *xxx = yy
101 }
102
103 func foo17(yy *int) { // ERROR "yy does not escape"
104         **xxx = *yy
105 }
106
107 func foo18(y int) { // ERROR "moved to heap: "y"
108         *xxx = &y // ERROR "&y escapes to heap"
109 }
110
111 func foo19(y int) {
112         **xxx = y
113 }
114
115 type Bar struct {
116         i  int
117         ii *int
118 }
119
120 func NewBar() *Bar {
121         return &Bar{42, nil} // ERROR "&Bar literal escapes to heap"
122 }
123
124 func NewBarp(x *int) *Bar { // ERROR "leaking param: x"
125         return &Bar{42, x} // ERROR "&Bar literal escapes to heap"
126 }
127
128 func NewBarp2(x *int) *Bar { // ERROR "x does not escape"
129         return &Bar{*x, nil} // ERROR "&Bar literal escapes to heap"
130 }
131
132 func (b *Bar) NoLeak() int { // ERROR "b does not escape"
133         return *(b.ii)
134 }
135
136 func (b *Bar) Leak() *int { // ERROR "leaking param: b"
137         return &b.i // ERROR "&b.i escapes to heap"
138 }
139
140 func (b *Bar) AlsoNoLeak() *int { // ERROR "leaking param b content to result ~r0"
141         return b.ii
142 }
143
144 func (b Bar) AlsoLeak() *int { // ERROR "leaking param: b"
145         return b.ii
146 }
147
148 func (b Bar) LeaksToo() *int { // ERROR "leaking param: b"
149         v := 0    // ERROR "moved to heap: v"
150         b.ii = &v // ERROR "&v escapes"
151         return b.ii
152 }
153
154 func (b *Bar) LeaksABit() *int { // ERROR "leaking param b content to result ~r0"
155         v := 0    // ERROR "moved to heap: v"
156         b.ii = &v // ERROR "&v escapes"
157         return b.ii
158 }
159
160 func (b Bar) StillNoLeak() int { // ERROR "b does not escape"
161         v := 0
162         b.ii = &v // ERROR "&v does not escape"
163         return b.i
164 }
165
166 func goLeak(b *Bar) { // ERROR "leaking param: b"
167         go b.NoLeak()
168 }
169
170 type Bar2 struct {
171         i  [12]int
172         ii []int
173 }
174
175 func NewBar2() *Bar2 {
176         return &Bar2{[12]int{42}, nil} // ERROR "&Bar2 literal escapes to heap"
177 }
178
179 func (b *Bar2) NoLeak() int { // ERROR "b does not escape"
180         return b.i[0]
181 }
182
183 func (b *Bar2) Leak() []int { // ERROR "leaking param: b"
184         return b.i[:] // ERROR "b.i escapes to heap"
185 }
186
187 func (b *Bar2) AlsoNoLeak() []int { // ERROR "leaking param b content to result ~r0"
188         return b.ii[0:1]
189 }
190
191 func (b Bar2) AgainNoLeak() [12]int { // ERROR "b does not escape"
192         return b.i
193 }
194
195 func (b *Bar2) LeakSelf() { // ERROR "leaking param: b"
196         b.ii = b.i[0:4] // ERROR "b.i escapes to heap"
197 }
198
199 func (b *Bar2) LeakSelf2() { // ERROR "leaking param: b"
200         var buf []int
201         buf = b.i[0:] // ERROR "b.i escapes to heap"
202         b.ii = buf
203 }
204
205 func foo21() func() int {
206         x := 42
207         return func() int { // ERROR "func literal escapes to heap"
208                 return x
209         }
210 }
211
212 func foo21a() func() int {
213         x := 42             // ERROR "moved to heap: x"
214         return func() int { // ERROR "func literal escapes to heap"
215                 x++ // ERROR "&x escapes to heap"
216                 return x
217         }
218 }
219
220 func foo22() int {
221         x := 42
222         return func() int { // ERROR "func literal does not escape"
223                 return x
224         }()
225 }
226
227 func foo23(x int) func() int {
228         return func() int { // ERROR "func literal escapes to heap"
229                 return x
230         }
231 }
232
233 func foo23a(x int) func() int {
234         f := func() int { // ERROR "func literal escapes to heap"
235                 return x
236         }
237         return f
238 }
239
240 func foo23b(x int) *(func() int) {
241         f := func() int { return x } // ERROR "moved to heap: f" "func literal escapes to heap"
242         return &f                    // ERROR "&f escapes to heap"
243 }
244
245 func foo23c(x int) func() int { // ERROR "moved to heap: x"
246         return func() int { // ERROR "func literal escapes to heap"
247                 x++ // ERROR "&x escapes to heap"
248                 return x
249         }
250 }
251
252 func foo24(x int) int {
253         return func() int { // ERROR "func literal does not escape"
254                 return x
255         }()
256 }
257
258 var x *int
259
260 func fooleak(xx *int) int { // ERROR "leaking param: xx"
261         x = xx
262         return *x
263 }
264
265 func foonoleak(xx *int) int { // ERROR "xx does not escape"
266         return *x + *xx
267 }
268
269 func foo31(x int) int { // ERROR "moved to heap: x"
270         return fooleak(&x) // ERROR "&x escapes to heap"
271 }
272
273 func foo32(x int) int {
274         return foonoleak(&x) // ERROR "&x does not escape"
275 }
276
277 type Foo struct {
278         xx *int
279         x  int
280 }
281
282 var F Foo
283 var pf *Foo
284
285 func (f *Foo) fooleak() { // ERROR "leaking param: f"
286         pf = f
287 }
288
289 func (f *Foo) foonoleak() { // ERROR "f does not escape"
290         F.x = f.x
291 }
292
293 func (f *Foo) Leak() { // ERROR "leaking param: f"
294         f.fooleak()
295 }
296
297 func (f *Foo) NoLeak() { // ERROR "f does not escape"
298         f.foonoleak()
299 }
300
301 func foo41(x int) { // ERROR "moved to heap: x"
302         F.xx = &x // ERROR "&x escapes to heap"
303 }
304
305 func (f *Foo) foo42(x int) { // ERROR "f does not escape" "moved to heap: x"
306         f.xx = &x // ERROR "&x escapes to heap"
307 }
308
309 func foo43(f *Foo, x int) { // ERROR "f does not escape" "moved to heap: x"
310         f.xx = &x // ERROR "&x escapes to heap"
311 }
312
313 func foo44(yy *int) { // ERROR "leaking param: yy"
314         F.xx = yy
315 }
316
317 func (f *Foo) foo45() { // ERROR "f does not escape"
318         F.x = f.x
319 }
320
321 // See foo13 above for explanation of why f leaks.
322 func (f *Foo) foo46() { // ERROR "leaking param: f"
323         F.xx = f.xx
324 }
325
326 func (f *Foo) foo47() { // ERROR "leaking param: f"
327         f.xx = &f.x // ERROR "&f.x escapes to heap"
328 }
329
330 var ptrSlice []*int
331
332 func foo50(i *int) { // ERROR "leaking param: i"
333         ptrSlice[0] = i
334 }
335
336 var ptrMap map[*int]*int
337
338 func foo51(i *int) { // ERROR "leaking param: i"
339         ptrMap[i] = i
340 }
341
342 func indaddr1(x int) *int { // ERROR "moved to heap: x"
343         return &x // ERROR "&x escapes to heap"
344 }
345
346 func indaddr2(x *int) *int { // ERROR "leaking param: x"
347         return *&x // ERROR "&x does not escape"
348 }
349
350 func indaddr3(x *int32) *int { // ERROR "leaking param: x"
351         return *(**int)(unsafe.Pointer(&x)) // ERROR "&x does not escape"
352 }
353
354 // From package math:
355
356 func Float32bits(f float32) uint32 {
357         return *(*uint32)(unsafe.Pointer(&f)) // ERROR "&f does not escape"
358 }
359
360 func Float32frombits(b uint32) float32 {
361         return *(*float32)(unsafe.Pointer(&b)) // ERROR "&b does not escape"
362 }
363
364 func Float64bits(f float64) uint64 {
365         return *(*uint64)(unsafe.Pointer(&f)) // ERROR "&f does not escape"
366 }
367
368 func Float64frombits(b uint64) float64 {
369         return *(*float64)(unsafe.Pointer(&b)) // ERROR "&b does not escape"
370 }
371
372 // contrast with
373 func float64bitsptr(f float64) *uint64 { // ERROR "moved to heap: f"
374         return (*uint64)(unsafe.Pointer(&f)) // ERROR "&f escapes to heap"
375 }
376
377 func float64ptrbitsptr(f *float64) *uint64 { // ERROR "leaking param: f"
378         return (*uint64)(unsafe.Pointer(f))
379 }
380
381 func typesw(i interface{}) *int { // ERROR "leaking param: i"
382         switch val := i.(type) {
383         case *int:
384                 return val
385         case *int8:
386                 v := int(*val) // ERROR "moved to heap: v"
387                 return &v      // ERROR "&v escapes to heap"
388         }
389         return nil
390 }
391
392 func exprsw(i *int) *int { // ERROR "leaking param: i"
393         switch j := i; *j + 110 {
394         case 12:
395                 return j
396         case 42:
397                 return nil
398         }
399         return nil
400
401 }
402
403 // assigning to an array element is like assigning to the array
404 func foo60(i *int) *int { // ERROR "leaking param: i"
405         var a [12]*int
406         a[0] = i
407         return a[1]
408 }
409
410 func foo60a(i *int) *int { // ERROR "i does not escape"
411         var a [12]*int
412         a[0] = i
413         return nil
414 }
415
416 // assigning to a struct field  is like assigning to the struct
417 func foo61(i *int) *int { // ERROR "leaking param: i"
418         type S struct {
419                 a, b *int
420         }
421         var s S
422         s.a = i
423         return s.b
424 }
425
426 func foo61a(i *int) *int { // ERROR "i does not escape"
427         type S struct {
428                 a, b *int
429         }
430         var s S
431         s.a = i
432         return nil
433 }
434
435 // assigning to a struct field is like assigning to the struct but
436 // here this subtlety is lost, since s.a counts as an assignment to a
437 // track-losing dereference.
438 func foo62(i *int) *int { // ERROR "leaking param: i"
439         type S struct {
440                 a, b *int
441         }
442         s := new(S) // ERROR "new[(]S[)] does not escape"
443         s.a = i
444         return nil // s.b
445 }
446
447 type M interface {
448         M()
449 }
450
451 func foo63(m M) { // ERROR "m does not escape"
452 }
453
454 func foo64(m M) { // ERROR "leaking param: m"
455         m.M()
456 }
457
458 func foo64b(m M) { // ERROR "leaking param: m"
459         defer m.M()
460 }
461
462 type MV int
463
464 func (MV) M() {}
465
466 func foo65() {
467         var mv MV
468         foo63(&mv) // ERROR "&mv does not escape"
469 }
470
471 func foo66() {
472         var mv MV  // ERROR "moved to heap: mv"
473         foo64(&mv) // ERROR "&mv escapes to heap"
474 }
475
476 func foo67() {
477         var mv MV
478         foo63(mv) // ERROR "mv does not escape"
479 }
480
481 func foo68() {
482         var mv MV
483         // escapes but it's an int so irrelevant
484         foo64(mv) // ERROR "mv escapes to heap"
485 }
486
487 func foo69(m M) { // ERROR "leaking param: m"
488         foo64(m)
489 }
490
491 func foo70(mv1 *MV, m M) { // ERROR "leaking param: mv1" "leaking param: m"
492         m = mv1 // ERROR "mv1 escapes to heap"
493         foo64(m)
494 }
495
496 func foo71(x *int) []*int { // ERROR "leaking param: x"
497         var y []*int
498         y = append(y, x)
499         return y
500 }
501
502 func foo71a(x int) []*int { // ERROR "moved to heap: x"
503         var y []*int
504         y = append(y, &x) // ERROR "&x escapes to heap"
505         return y
506 }
507
508 func foo72() {
509         var x int
510         var y [1]*int
511         y[0] = &x // ERROR "&x does not escape"
512 }
513
514 func foo72aa() [10]*int {
515         var x int // ERROR "moved to heap: x"
516         var y [10]*int
517         y[0] = &x // ERROR "&x escapes to heap"
518         return y
519 }
520
521 func foo72a() {
522         var y [10]*int
523         for i := 0; i < 10; i++ {
524                 // escapes its scope
525                 x := i    // ERROR "moved to heap: x"
526                 y[i] = &x // ERROR "&x escapes to heap"
527         }
528         return
529 }
530
531 func foo72b() [10]*int {
532         var y [10]*int
533         for i := 0; i < 10; i++ {
534                 x := i    // ERROR "moved to heap: x"
535                 y[i] = &x // ERROR "&x escapes to heap"
536         }
537         return y
538 }
539
540 // issue 2145
541 func foo73() {
542         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
543         for _, v := range s {
544                 vv := v
545                 // actually just escapes its scope
546                 defer func() { // ERROR "func literal escapes to heap"
547                         println(vv)
548                 }()
549         }
550 }
551
552 func foo731() {
553         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
554         for _, v := range s {
555                 vv := v // ERROR "moved to heap: vv"
556                 // actually just escapes its scope
557                 defer func() { // ERROR "func literal escapes to heap"
558                         vv = 42 // ERROR "&vv escapes to heap"
559                         println(vv)
560                 }()
561         }
562 }
563
564 func foo74() {
565         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
566         for _, v := range s {
567                 vv := v
568                 // actually just escapes its scope
569                 fn := func() { // ERROR "func literal escapes to heap"
570                         println(vv)
571                 }
572                 defer fn()
573         }
574 }
575
576 func foo74a() {
577         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
578         for _, v := range s {
579                 vv := v // ERROR "moved to heap: vv"
580                 // actually just escapes its scope
581                 fn := func() { // ERROR "func literal escapes to heap"
582                         vv += 1 // ERROR "&vv escapes to heap"
583                         println(vv)
584                 }
585                 defer fn()
586         }
587 }
588
589 // issue 3975
590 func foo74b() {
591         var array [3]func()
592         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
593         for i, v := range s {
594                 vv := v
595                 // actually just escapes its scope
596                 array[i] = func() { // ERROR "func literal escapes to heap"
597                         println(vv)
598                 }
599         }
600 }
601
602 func foo74c() {
603         var array [3]func()
604         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
605         for i, v := range s {
606                 vv := v // ERROR "moved to heap: vv"
607                 // actually just escapes its scope
608                 array[i] = func() { // ERROR "func literal escapes to heap"
609                         println(&vv) // ERROR "&vv escapes to heap" "&vv does not escape"
610                 }
611         }
612 }
613
614 func myprint(y *int, x ...interface{}) *int { // ERROR "x does not escape" "leaking param: y to result ~r2"
615         return y
616 }
617
618 func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "y does not escape" "leaking param: x to result ~r2"
619         return &x[0] // ERROR "&x.0. escapes to heap"
620 }
621
622 func foo75(z *int) { // ERROR "z does not escape"
623         myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" "1 escapes to heap" "2 escapes to heap" "3 escapes to heap"
624 }
625
626 func foo75a(z *int) { // ERROR "z does not escape"
627         myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" "1 escapes to heap" "2 escapes to heap" "3 escapes to heap"
628 }
629
630 func foo75esc(z *int) { // ERROR "leaking param: z"
631         gxx = myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" "1 escapes to heap" "2 escapes to heap" "3 escapes to heap"
632 }
633
634 func foo75aesc(z *int) { // ERROR "z does not escape"
635         var ppi **interface{}       // assignments to pointer dereferences lose track
636         *ppi = myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" "1 escapes to heap" "2 escapes to heap" "3 escapes to heap"
637 }
638
639 func foo75aesc1(z *int) { // ERROR "z does not escape"
640         sink = myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" "myprint1\(z, 1, 2, 3\) escapes to heap" "1 escapes to heap" "2 escapes to heap" "3 escapes to heap"
641 }
642
643 // BAD: z does not escape here
644 func foo76(z *int) { // ERROR "leaking param: z"
645         myprint(nil, z) // ERROR "[.][.][.] argument does not escape" "z escapes to heap"
646 }
647
648 // BAD: z does not escape here
649 func foo76a(z *int) { // ERROR "leaking param: z"
650         myprint1(nil, z) // ERROR "[.][.][.] argument does not escape" "z escapes to heap"
651 }
652
653 func foo76b() {
654         myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" "1 escapes to heap" "2 escapes to heap" "3 escapes to heap"
655 }
656
657 func foo76c() {
658         myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" "1 escapes to heap" "2 escapes to heap" "3 escapes to heap"
659 }
660
661 func foo76d() {
662         defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" "1 escapes to heap" "2 escapes to heap" "3 escapes to heap"
663 }
664
665 func foo76e() {
666         defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape" "1 escapes to heap" "2 escapes to heap" "3 escapes to heap"
667 }
668
669 func foo76f() {
670         for {
671                 // TODO: This one really only escapes its scope, but we don't distinguish yet.
672                 defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" "1 escapes to heap" "2 escapes to heap" "3 escapes to heap"
673         }
674 }
675
676 func foo76g() {
677         for {
678                 defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap" "1 escapes to heap" "2 escapes to heap" "3 escapes to heap"
679         }
680 }
681
682 func foo77(z []interface{}) { // ERROR "z does not escape"
683         myprint(nil, z...) // z does not escape
684 }
685
686 func foo77a(z []interface{}) { // ERROR "z does not escape"
687         myprint1(nil, z...)
688 }
689
690 func foo77b(z []interface{}) { // ERROR "leaking param: z"
691         var ppi **interface{}
692         *ppi = myprint1(nil, z...)
693 }
694
695 func foo77c(z []interface{}) { // ERROR "leaking param: z"
696         sink = myprint1(nil, z...) // ERROR "myprint1\(nil, z\.\.\.\) escapes to heap"
697 }
698
699 func dotdotdot() {
700         // BAD: i should not escape here
701         i := 0           // ERROR "moved to heap: i"
702         myprint(nil, &i) // ERROR "&i escapes to heap" "\.\.\. argument does not escape"
703
704         // BAD: j should not escape here
705         j := 0            // ERROR "moved to heap: j"
706         myprint1(nil, &j) // ERROR "&j escapes to heap" "\.\.\. argument does not escape"
707 }
708
709 func foo78(z int) *int { // ERROR "moved to heap: z"
710         return &z // ERROR "&z escapes to heap"
711 }
712
713 func foo78a(z int) *int { // ERROR "moved to heap: z"
714         y := &z   // ERROR "&z escapes to heap"
715         x := &y   // ERROR "&y does not escape"
716         return *x // really return y
717 }
718
719 func foo79() *int {
720         return new(int) // ERROR "new[(]int[)] escapes to heap"
721 }
722
723 func foo80() *int {
724         var z *int
725         for {
726                 // Really just escapes its scope but we don't distinguish
727                 z = new(int) // ERROR "new[(]int[)] escapes to heap"
728         }
729         _ = z
730         return nil
731 }
732
733 func foo81() *int {
734         for {
735                 z := new(int) // ERROR "new[(]int[)] does not escape"
736                 _ = z
737         }
738         return nil
739 }
740
741 func tee(p *int) (x, y *int) { return p, p } // ERROR "leaking param"
742
743 func noop(x, y *int) {} // ERROR "does not escape"
744
745 func foo82() {
746         var x, y, z int  // ERROR "moved to heap"
747         go noop(tee(&z)) // ERROR "&z escapes to heap"
748         go noop(&x, &y)  // ERROR "escapes to heap"
749         for {
750                 var u, v, w int     // ERROR "moved to heap"
751                 defer noop(tee(&u)) // ERROR "&u escapes to heap"
752                 defer noop(&v, &w)  // ERROR "escapes to heap"
753         }
754 }
755
756 type Fooer interface {
757         Foo()
758 }
759
760 type LimitedFooer struct {
761         Fooer
762         N int64
763 }
764
765 func LimitFooer(r Fooer, n int64) Fooer { // ERROR "leaking param: r"
766         return &LimitedFooer{r, n} // ERROR "&LimitedFooer literal escapes to heap"
767 }
768
769 func foo90(x *int) map[*int]*int { // ERROR "leaking param: x"
770         return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int literal escapes to heap"
771 }
772
773 func foo91(x *int) map[*int]*int { // ERROR "leaking param: x"
774         return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int literal escapes to heap"
775 }
776
777 func foo92(x *int) [2]*int { // ERROR "leaking param: x"
778         return [2]*int{x, nil}
779 }
780
781 // does not leak c
782 func foo93(c chan *int) *int { // ERROR "c does not escape"
783         for v := range c {
784                 return v
785         }
786         return nil
787 }
788
789 // does not leak m
790 func foo94(m map[*int]*int, b bool) *int { // ERROR "m does not escape"
791         for k, v := range m {
792                 if b {
793                         return k
794                 }
795                 return v
796         }
797         return nil
798 }
799
800 // does leak x
801 func foo95(m map[*int]*int, x *int) { // ERROR "m does not escape" "leaking param: x"
802         m[x] = x
803 }
804
805 // does not leak m
806 func foo96(m []*int) *int { // ERROR "m does not escape"
807         return m[0]
808 }
809
810 // does leak m
811 func foo97(m [1]*int) *int { // ERROR "leaking param: m"
812         return m[0]
813 }
814
815 // does not leak m
816 func foo98(m map[int]*int) *int { // ERROR "m does not escape"
817         return m[0]
818 }
819
820 // does leak m
821 func foo99(m *[1]*int) []*int { // ERROR "leaking param: m"
822         return m[:]
823 }
824
825 // does not leak m
826 func foo100(m []*int) *int { // ERROR "m does not escape"
827         for _, v := range m {
828                 return v
829         }
830         return nil
831 }
832
833 // does leak m
834 func foo101(m [1]*int) *int { // ERROR "leaking param: m"
835         for _, v := range m {
836                 return v
837         }
838         return nil
839 }
840
841 // does not leak m
842 func foo101a(m [1]*int) *int { // ERROR "m does not escape"
843         for i := range m { // ERROR "moved to heap: i"
844                 return &i // ERROR "&i escapes to heap"
845         }
846         return nil
847 }
848
849 // does leak x
850 func foo102(m []*int, x *int) { // ERROR "m does not escape" "leaking param: x"
851         m[0] = x
852 }
853
854 // does not leak x
855 func foo103(m [1]*int, x *int) { // ERROR "m does not escape" "x does not escape"
856         m[0] = x
857 }
858
859 var y []*int
860
861 // does not leak x
862 func foo104(x []*int) { // ERROR "x does not escape"
863         copy(y, x)
864 }
865
866 // does not leak x
867 func foo105(x []*int) { // ERROR "x does not escape"
868         _ = append(y, x...)
869 }
870
871 // does leak x
872 func foo106(x *int) { // ERROR "leaking param: x"
873         _ = append(y, x)
874 }
875
876 func foo107(x *int) map[*int]*int { // ERROR "leaking param: x"
877         return map[*int]*int{x: nil} // ERROR "map.* literal escapes to heap"
878 }
879
880 func foo108(x *int) map[*int]*int { // ERROR "leaking param: x"
881         return map[*int]*int{nil: x} // ERROR "map.* literal escapes to heap"
882 }
883
884 func foo109(x *int) *int { // ERROR "leaking param: x"
885         m := map[*int]*int{x: nil} // ERROR "map.* literal does not escape"
886         for k, _ := range m {
887                 return k
888         }
889         return nil
890 }
891
892 func foo110(x *int) *int { // ERROR "leaking param: x"
893         m := map[*int]*int{nil: x} // ERROR "map.* literal does not escape"
894         return m[nil]
895 }
896
897 func foo111(x *int) *int { // ERROR "leaking param: x"
898         m := []*int{x} // ERROR "\[\]\*int literal does not escape"
899         return m[0]
900 }
901
902 func foo112(x *int) *int { // ERROR "leaking param: x"
903         m := [1]*int{x}
904         return m[0]
905 }
906
907 func foo113(x *int) *int { // ERROR "leaking param: x"
908         m := Bar{ii: x}
909         return m.ii
910 }
911
912 func foo114(x *int) *int { // ERROR "leaking param: x"
913         m := &Bar{ii: x} // ERROR "&Bar literal does not escape"
914         return m.ii
915 }
916
917 func foo115(x *int) *int { // ERROR "leaking param: x"
918         return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1))
919 }
920
921 func foo116(b bool) *int {
922         if b {
923                 x := 1    // ERROR "moved to heap: x"
924                 return &x // ERROR "&x escapes to heap"
925         } else {
926                 y := 1    // ERROR "moved to heap: y"
927                 return &y // ERROR "&y escapes to heap"
928         }
929         return nil
930 }
931
932 func foo117(unknown func(interface{})) { // ERROR "unknown does not escape"
933         x := 1      // ERROR "moved to heap: x"
934         unknown(&x) // ERROR "&x escapes to heap"
935 }
936
937 func foo118(unknown func(*int)) { // ERROR "unknown does not escape"
938         x := 1      // ERROR "moved to heap: x"
939         unknown(&x) // ERROR "&x escapes to heap"
940 }
941
942 func external(*int)
943
944 func foo119(x *int) { // ERROR "leaking param: x"
945         external(x)
946 }
947
948 func foo120() {
949         // formerly exponential time analysis
950 L1:
951 L2:
952 L3:
953 L4:
954 L5:
955 L6:
956 L7:
957 L8:
958 L9:
959 L10:
960 L11:
961 L12:
962 L13:
963 L14:
964 L15:
965 L16:
966 L17:
967 L18:
968 L19:
969 L20:
970 L21:
971 L22:
972 L23:
973 L24:
974 L25:
975 L26:
976 L27:
977 L28:
978 L29:
979 L30:
980 L31:
981 L32:
982 L33:
983 L34:
984 L35:
985 L36:
986 L37:
987 L38:
988 L39:
989 L40:
990 L41:
991 L42:
992 L43:
993 L44:
994 L45:
995 L46:
996 L47:
997 L48:
998 L49:
999 L50:
1000 L51:
1001 L52:
1002 L53:
1003 L54:
1004 L55:
1005 L56:
1006 L57:
1007 L58:
1008 L59:
1009 L60:
1010 L61:
1011 L62:
1012 L63:
1013 L64:
1014 L65:
1015 L66:
1016 L67:
1017 L68:
1018 L69:
1019 L70:
1020 L71:
1021 L72:
1022 L73:
1023 L74:
1024 L75:
1025 L76:
1026 L77:
1027 L78:
1028 L79:
1029 L80:
1030 L81:
1031 L82:
1032 L83:
1033 L84:
1034 L85:
1035 L86:
1036 L87:
1037 L88:
1038 L89:
1039 L90:
1040 L91:
1041 L92:
1042 L93:
1043 L94:
1044 L95:
1045 L96:
1046 L97:
1047 L98:
1048 L99:
1049 L100:
1050         // use the labels to silence compiler errors
1051         goto L1
1052         goto L2
1053         goto L3
1054         goto L4
1055         goto L5
1056         goto L6
1057         goto L7
1058         goto L8
1059         goto L9
1060         goto L10
1061         goto L11
1062         goto L12
1063         goto L13
1064         goto L14
1065         goto L15
1066         goto L16
1067         goto L17
1068         goto L18
1069         goto L19
1070         goto L20
1071         goto L21
1072         goto L22
1073         goto L23
1074         goto L24
1075         goto L25
1076         goto L26
1077         goto L27
1078         goto L28
1079         goto L29
1080         goto L30
1081         goto L31
1082         goto L32
1083         goto L33
1084         goto L34
1085         goto L35
1086         goto L36
1087         goto L37
1088         goto L38
1089         goto L39
1090         goto L40
1091         goto L41
1092         goto L42
1093         goto L43
1094         goto L44
1095         goto L45
1096         goto L46
1097         goto L47
1098         goto L48
1099         goto L49
1100         goto L50
1101         goto L51
1102         goto L52
1103         goto L53
1104         goto L54
1105         goto L55
1106         goto L56
1107         goto L57
1108         goto L58
1109         goto L59
1110         goto L60
1111         goto L61
1112         goto L62
1113         goto L63
1114         goto L64
1115         goto L65
1116         goto L66
1117         goto L67
1118         goto L68
1119         goto L69
1120         goto L70
1121         goto L71
1122         goto L72
1123         goto L73
1124         goto L74
1125         goto L75
1126         goto L76
1127         goto L77
1128         goto L78
1129         goto L79
1130         goto L80
1131         goto L81
1132         goto L82
1133         goto L83
1134         goto L84
1135         goto L85
1136         goto L86
1137         goto L87
1138         goto L88
1139         goto L89
1140         goto L90
1141         goto L91
1142         goto L92
1143         goto L93
1144         goto L94
1145         goto L95
1146         goto L96
1147         goto L97
1148         goto L98
1149         goto L99
1150         goto L100
1151 }
1152
1153 func foo121() {
1154         for i := 0; i < 10; i++ {
1155                 defer myprint(nil, i) // ERROR "[.][.][.] argument escapes to heap" "i escapes to heap"
1156                 go myprint(nil, i)    // ERROR "[.][.][.] argument escapes to heap" "i escapes to heap"
1157         }
1158 }
1159
1160 // same as foo121 but check across import
1161 func foo121b() {
1162         for i := 0; i < 10; i++ {
1163                 defer fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes to heap" "i escapes to heap"
1164                 go fmt.Printf("%d", i)    // ERROR "[.][.][.] argument escapes to heap" "i escapes to heap"
1165         }
1166 }
1167
1168 // a harmless forward jump
1169 func foo122() {
1170         var i *int
1171
1172         goto L1
1173 L1:
1174         i = new(int) // ERROR "new.int. does not escape"
1175         _ = i
1176 }
1177
1178 // a backward jump, increases loopdepth
1179 func foo123() {
1180         var i *int
1181
1182 L1:
1183         i = new(int) // ERROR "new.int. escapes to heap"
1184
1185         goto L1
1186         _ = i
1187 }
1188
1189 func foo124(x **int) { // ERROR "x does not escape"
1190         var i int // ERROR "moved to heap: i"
1191         p := &i   // ERROR "&i escapes"
1192         func() {  // ERROR "func literal does not escape"
1193                 *x = p // ERROR "leaking closure reference p"
1194         }()
1195 }
1196
1197 func foo125(ch chan *int) { // ERROR "does not escape"
1198         var i int // ERROR "moved to heap"
1199         p := &i   // ERROR "&i escapes to heap"
1200         func() {  // ERROR "func literal does not escape"
1201                 ch <- p // ERROR "leaking closure reference p"
1202         }()
1203 }
1204
1205 func foo126() {
1206         var px *int // loopdepth 0
1207         for {
1208                 // loopdepth 1
1209                 var i int // ERROR "moved to heap"
1210                 func() {  // ERROR "func literal does not escape"
1211                         px = &i // ERROR "&i escapes"
1212                 }()
1213         }
1214         _ = px
1215 }
1216
1217 var px *int
1218
1219 func foo127() {
1220         var i int // ERROR "moved to heap: i"
1221         p := &i   // ERROR "&i escapes to heap"
1222         q := p
1223         px = q
1224 }
1225
1226 func foo128() {
1227         var i int
1228         p := &i // ERROR "&i does not escape"
1229         q := p
1230         _ = q
1231 }
1232
1233 func foo129() {
1234         var i int // ERROR "moved to heap: i"
1235         p := &i   // ERROR "&i escapes to heap"
1236         func() {  // ERROR "func literal does not escape"
1237                 q := p   // ERROR "leaking closure reference p"
1238                 func() { // ERROR "func literal does not escape"
1239                         r := q // ERROR "leaking closure reference q"
1240                         px = r
1241                 }()
1242         }()
1243 }
1244
1245 func foo130() {
1246         for {
1247                 var i int // ERROR "moved to heap"
1248                 func() {  // ERROR "func literal does not escape"
1249                         px = &i // ERROR "&i escapes" "leaking closure reference i"
1250                 }()
1251         }
1252 }
1253
1254 func foo131() {
1255         var i int // ERROR "moved to heap"
1256         func() {  // ERROR "func literal does not escape"
1257                 px = &i // ERROR "&i escapes" "leaking closure reference i"
1258         }()
1259 }
1260
1261 func foo132() {
1262         var i int   // ERROR "moved to heap"
1263         go func() { // ERROR "func literal escapes to heap"
1264                 px = &i // ERROR "&i escapes" "leaking closure reference i"
1265         }()
1266 }
1267
1268 func foo133() {
1269         var i int      // ERROR "moved to heap"
1270         defer func() { // ERROR "func literal does not escape"
1271                 px = &i // ERROR "&i escapes" "leaking closure reference i"
1272         }()
1273 }
1274
1275 func foo134() {
1276         var i int
1277         p := &i  // ERROR "&i does not escape"
1278         func() { // ERROR "func literal does not escape"
1279                 q := p
1280                 func() { // ERROR "func literal does not escape"
1281                         r := q
1282                         _ = r
1283                 }()
1284         }()
1285 }
1286
1287 func foo135() {
1288         var i int   // ERROR "moved to heap: i"
1289         p := &i     // ERROR "&i escapes to heap"
1290         go func() { // ERROR "func literal escapes to heap"
1291                 q := p
1292                 func() { // ERROR "func literal does not escape"
1293                         r := q
1294                         _ = r
1295                 }()
1296         }()
1297 }
1298
1299 func foo136() {
1300         var i int   // ERROR "moved to heap: i"
1301         p := &i     // ERROR "&i escapes to heap"
1302         go func() { // ERROR "func literal escapes to heap"
1303                 q := p   // ERROR "leaking closure reference p"
1304                 func() { // ERROR "func literal does not escape"
1305                         r := q // ERROR "leaking closure reference q"
1306                         px = r
1307                 }()
1308         }()
1309 }
1310
1311 func foo137() {
1312         var i int // ERROR "moved to heap: i"
1313         p := &i   // ERROR "&i escapes to heap"
1314         func() {  // ERROR "func literal does not escape"
1315                 q := p      // ERROR "leaking closure reference p"
1316                 go func() { // ERROR "func literal escapes to heap"
1317                         r := q
1318                         _ = r
1319                 }()
1320         }()
1321 }
1322
1323 func foo138() *byte {
1324         type T struct {
1325                 x [1]byte
1326         }
1327         t := new(T)    // ERROR "new.T. escapes to heap"
1328         return &t.x[0] // ERROR "&t.x.0. escapes to heap"
1329 }
1330
1331 func foo139() *byte {
1332         type T struct {
1333                 x struct {
1334                         y byte
1335                 }
1336         }
1337         t := new(T)   // ERROR "new.T. escapes to heap"
1338         return &t.x.y // ERROR "&t.x.y escapes to heap"
1339 }
1340
1341 // issue 4751
1342 func foo140() interface{} {
1343         type T struct {
1344                 X string
1345         }
1346         type U struct {
1347                 X string
1348                 T *T
1349         }
1350         t := &T{} // ERROR "&T literal escapes to heap"
1351         return U{ // ERROR "U literal escapes to heap"
1352                 X: t.X,
1353                 T: t,
1354         }
1355 }
1356
1357 //go:noescape
1358
1359 func F1([]byte)
1360
1361 func F2([]byte)
1362
1363 //go:noescape
1364
1365 func F3(x []byte) // ERROR "F3 x does not escape"
1366
1367 func F4(x []byte)
1368
1369 func G() {
1370         var buf1 [10]byte
1371         F1(buf1[:]) // ERROR "buf1 does not escape"
1372
1373         var buf2 [10]byte // ERROR "moved to heap: buf2"
1374         F2(buf2[:])       // ERROR "buf2 escapes to heap"
1375
1376         var buf3 [10]byte
1377         F3(buf3[:]) // ERROR "buf3 does not escape"
1378
1379         var buf4 [10]byte // ERROR "moved to heap: buf4"
1380         F4(buf4[:])       // ERROR "buf4 escapes to heap"
1381 }
1382
1383 type Tm struct {
1384         x int
1385 }
1386
1387 func (t *Tm) M() { // ERROR "t does not escape"
1388 }
1389
1390 func foo141() {
1391         var f func()
1392
1393         t := new(Tm) // ERROR "escapes to heap"
1394         f = t.M      // ERROR "t.M does not escape"
1395         _ = f
1396 }
1397
1398 var gf func()
1399
1400 func foo142() {
1401         t := new(Tm) // ERROR "escapes to heap"
1402         gf = t.M     // ERROR "t.M escapes to heap"
1403 }
1404
1405 // issue 3888.
1406 func foo143() {
1407         for i := 0; i < 1000; i++ {
1408                 func() { // ERROR "func literal does not escape"
1409                         for i := 0; i < 1; i++ {
1410                                 var t Tm
1411                                 t.M() // ERROR "t does not escape"
1412                         }
1413                 }()
1414         }
1415 }
1416
1417 // issue 5773
1418 // Check that annotations take effect regardless of whether they
1419 // are before or after the use in the source code.
1420
1421 //go:noescape
1422
1423 func foo144a(*int)
1424
1425 func foo144() {
1426         var x int
1427         foo144a(&x) // ERROR "&x does not escape"
1428         var y int
1429         foo144b(&y) // ERROR "&y does not escape"
1430 }
1431
1432 //go:noescape
1433
1434 func foo144b(*int)
1435
1436 // issue 7313: for loop init should not be treated as "in loop"
1437
1438 type List struct {
1439         Next *List
1440 }
1441
1442 func foo145(l List) { // ERROR "l does not escape"
1443         var p *List
1444         for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1445         }
1446 }
1447
1448 func foo146(l List) { // ERROR "l does not escape"
1449         var p *List
1450         p = &l // ERROR "&l does not escape"
1451         for ; p.Next != nil; p = p.Next {
1452         }
1453 }
1454
1455 func foo147(l List) { // ERROR "l does not escape"
1456         var p *List
1457         p = &l // ERROR "&l does not escape"
1458         for p.Next != nil {
1459                 p = p.Next
1460         }
1461 }
1462
1463 func foo148(l List) { // ERROR " l does not escape"
1464         for p := &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1465         }
1466 }
1467
1468 // related: address of variable should have depth of variable, not of loop
1469
1470 func foo149(l List) { // ERROR " l does not escape"
1471         var p *List
1472         for {
1473                 for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1474                 }
1475         }
1476 }
1477
1478 // issue 7934: missed ... if element type had no pointers
1479
1480 var save150 []byte
1481
1482 func foo150(x ...byte) { // ERROR "leaking param: x"
1483         save150 = x
1484 }
1485
1486 func bar150() {
1487         foo150(1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
1488 }
1489
1490 // issue 7931: bad handling of slice of array
1491
1492 var save151 *int
1493
1494 func foo151(x *int) { // ERROR "leaking param: x"
1495         save151 = x
1496 }
1497
1498 func bar151() {
1499         var a [64]int // ERROR "moved to heap: a"
1500         a[4] = 101
1501         foo151(&(&a)[4:8][0]) // ERROR "&\(&a\)\[4:8\]\[0\] escapes to heap" "&a escapes to heap"
1502 }
1503
1504 func bar151b() {
1505         var a [10]int      // ERROR "moved to heap: a"
1506         b := a[:]          // ERROR "a escapes to heap"
1507         foo151(&b[4:8][0]) // ERROR "&b\[4:8\]\[0\] escapes to heap"
1508 }
1509
1510 func bar151c() {
1511         var a [64]int // ERROR "moved to heap: a"
1512         a[4] = 101
1513         foo151(&(&a)[4:8:8][0]) // ERROR "&\(&a\)\[4:8:8\]\[0\] escapes to heap" "&a escapes to heap"
1514 }
1515
1516 func bar151d() {
1517         var a [10]int        // ERROR "moved to heap: a"
1518         b := a[:]            // ERROR "a escapes to heap"
1519         foo151(&b[4:8:8][0]) // ERROR "&b\[4:8:8\]\[0\] escapes to heap"
1520 }
1521
1522 // issue 8120
1523
1524 type U struct {
1525         s *string
1526 }
1527
1528 func (u *U) String() *string { // ERROR "leaking param u content to result ~r0"
1529         return u.s
1530 }
1531
1532 type V struct {
1533         s *string
1534 }
1535
1536 func NewV(u U) *V { // ERROR "leaking param: u"
1537         return &V{u.String()} // ERROR "&V literal escapes to heap" "u does not escape"
1538 }
1539
1540 func foo152() {
1541         a := "a"   // ERROR "moved to heap: a"
1542         u := U{&a} // ERROR "&a escapes to heap"
1543         v := NewV(u)
1544         println(v)
1545 }
1546
1547 // issue 8176 - &x in type switch body not marked as escaping
1548
1549 func foo153(v interface{}) *int { // ERROR "leaking param: v"
1550         switch x := v.(type) {
1551         case int: // ERROR "moved to heap: x"
1552                 return &x // ERROR "&x escapes to heap"
1553         }
1554         panic(0)
1555 }
1556
1557 // issue 8185 - &result escaping into result
1558
1559 func f() (x int, y *int) { // ERROR "moved to heap: x"
1560         y = &x // ERROR "&x escapes to heap"
1561         return
1562 }
1563
1564 func g() (x interface{}) { // ERROR "moved to heap: x"
1565         x = &x // ERROR "&x escapes to heap"
1566         return
1567 }
1568
1569 var sink interface{}
1570
1571 type Lit struct {
1572         p *int
1573 }
1574
1575 func ptrlitNoescape() {
1576         // Both literal and element do not escape.
1577         i := 0
1578         x := &Lit{&i} // ERROR "&Lit literal does not escape" "&i does not escape"
1579         _ = x
1580 }
1581
1582 func ptrlitNoEscape2() {
1583         // Literal does not escape, but element does.
1584         i := 0        // ERROR "moved to heap: i"
1585         x := &Lit{&i} // ERROR "&Lit literal does not escape" "&i escapes to heap"
1586         sink = *x     // ERROR "\*x escapes to heap"
1587 }
1588
1589 func ptrlitEscape() {
1590         // Both literal and element escape.
1591         i := 0        // ERROR "moved to heap: i"
1592         x := &Lit{&i} // ERROR "&Lit literal escapes to heap" "&i escapes to heap"
1593         sink = x      // ERROR "x escapes to heap"
1594 }
1595
1596 // self-assignments
1597
1598 type Buffer struct {
1599         arr  [64]byte
1600         buf1 []byte
1601         buf2 []byte
1602         str1 string
1603         str2 string
1604 }
1605
1606 func (b *Buffer) foo() { // ERROR "b does not escape"
1607         b.buf1 = b.buf1[1:2]   // ERROR "ignoring self-assignment to b.buf1"
1608         b.buf1 = b.buf1[1:2:3] // ERROR "ignoring self-assignment to b.buf1"
1609         b.buf1 = b.buf2[1:2]   // ERROR "ignoring self-assignment to b.buf1"
1610         b.buf1 = b.buf2[1:2:3] // ERROR "ignoring self-assignment to b.buf1"
1611 }
1612
1613 func (b *Buffer) bar() { // ERROR "leaking param: b"
1614         b.buf1 = b.arr[1:2] // ERROR "b.arr escapes to heap"
1615 }
1616
1617 func (b *Buffer) baz() { // ERROR "b does not escape"
1618         b.str1 = b.str1[1:2] // ERROR "ignoring self-assignment to b.str1"
1619         b.str1 = b.str2[1:2] // ERROR "ignoring self-assignment to b.str1"
1620 }
1621
1622 func (b *Buffer) bat() { // ERROR "leaking param: b"
1623         o := new(Buffer) // ERROR "new\(Buffer\) escapes to heap"
1624         o.buf1 = b.buf1[1:2]
1625         sink = o // ERROR "o escapes to heap"
1626 }
1627
1628 func quux(sp *string, bp *[]byte) { // ERROR "sp does not escape" "bp does not escape"
1629         *sp = (*sp)[1:2] // ERROR "quux ignoring self-assignment to \*sp"
1630         *bp = (*bp)[1:2] // ERROR "quux ignoring self-assignment to \*bp"
1631 }
1632
1633 type StructWithString struct {
1634         p *int
1635         s string
1636 }
1637
1638 // This is escape analysis false negative.
1639 // We assign the pointer to x.p but leak x.s. Escape analysis coarsens flows
1640 // to just x, and thus &i looks escaping.
1641 func fieldFlowTracking() {
1642         var x StructWithString
1643         i := 0     // ERROR "moved to heap: i"
1644         x.p = &i   // ERROR "&i escapes to heap"
1645         sink = x.s // ERROR "x.s escapes to heap"
1646 }
1647
1648 // String operations.
1649
1650 func slicebytetostring0() {
1651         b := make([]byte, 20) // ERROR "does not escape"
1652         s := string(b)        // ERROR "string\(b\) does not escape"
1653         _ = s
1654 }
1655
1656 func slicebytetostring1() {
1657         b := make([]byte, 20) // ERROR "does not escape"
1658         s := string(b)        // ERROR "string\(b\) does not escape"
1659         s1 := s[0:1]
1660         _ = s1
1661 }
1662
1663 func slicebytetostring2() {
1664         b := make([]byte, 20) // ERROR "does not escape"
1665         s := string(b)        // ERROR "string\(b\) escapes to heap"
1666         s1 := s[0:1]          // ERROR "moved to heap: s1"
1667         sink = &s1            // ERROR "&s1 escapes to heap"
1668 }
1669
1670 func slicebytetostring3() {
1671         b := make([]byte, 20) // ERROR "does not escape"
1672         s := string(b)        // ERROR "string\(b\) escapes to heap"
1673         s1 := s[0:1]
1674         sink = s1 // ERROR "s1 escapes to heap"
1675 }
1676
1677 func addstr0() {
1678         s0 := "a"
1679         s1 := "b"
1680         s := s0 + s1 // ERROR "s0 \+ s1 does not escape"
1681         _ = s
1682 }
1683
1684 func addstr1() {
1685         s0 := "a"
1686         s1 := "b"
1687         s := "c"
1688         s += s0 + s1 // ERROR "s0 \+ s1 does not escape"
1689         _ = s
1690 }
1691
1692 func addstr2() {
1693         b := make([]byte, 20) // ERROR "does not escape"
1694         s0 := "a"
1695         s := string(b) + s0 // ERROR "string\(b\) does not escape" "string\(b\) \+ s0 does not escape"
1696         _ = s
1697 }
1698
1699 func addstr3() {
1700         s0 := "a"
1701         s1 := "b"
1702         s := s0 + s1 // ERROR "s0 \+ s1 escapes to heap"
1703         s2 := s[0:1]
1704         sink = s2 // ERROR "s2 escapes to heap"
1705 }
1706
1707 func intstring0() bool {
1708         // string does not escape
1709         x := '0'
1710         s := string(x) // ERROR "string\(x\) does not escape"
1711         return s == "0"
1712 }
1713
1714 func intstring1() string {
1715         // string does not escape, but the buffer does
1716         x := '0'
1717         s := string(x) // ERROR "string\(x\) escapes to heap"
1718         return s
1719 }
1720
1721 func intstring2() {
1722         // string escapes to heap
1723         x := '0'
1724         s := string(x) // ERROR "string\(x\) escapes to heap" "moved to heap: s"
1725         sink = &s      // ERROR "&s escapes to heap"
1726 }
1727
1728 func stringtoslicebyte0() {
1729         s := "foo"
1730         x := []byte(s) // ERROR "\(\[\]byte\)\(s\) does not escape"
1731         _ = x
1732 }
1733
1734 func stringtoslicebyte1() []byte {
1735         s := "foo"
1736         return []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap"
1737 }
1738
1739 func stringtoslicebyte2() {
1740         s := "foo"
1741         sink = []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap"
1742 }
1743
1744 func stringtoslicerune0() {
1745         s := "foo"
1746         x := []rune(s) // ERROR "\(\[\]rune\)\(s\) does not escape"
1747         _ = x
1748 }
1749
1750 func stringtoslicerune1() []rune {
1751         s := "foo"
1752         return []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap"
1753 }
1754
1755 func stringtoslicerune2() {
1756         s := "foo"
1757         sink = []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap"
1758 }
1759
1760 func slicerunetostring0() {
1761         r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape"
1762         s := string(r)       // ERROR "string\(r\) does not escape"
1763         _ = s
1764 }
1765
1766 func slicerunetostring1() string {
1767         r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape"
1768         return string(r)     // ERROR "string\(r\) escapes to heap"
1769 }
1770
1771 func slicerunetostring2() {
1772         r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape"
1773         sink = string(r)     // ERROR "string\(r\) escapes to heap"
1774 }
1775
1776 func makemap0() {
1777         m := make(map[int]int) // ERROR "make\(map\[int\]int\) does not escape"
1778         m[0] = 0
1779         m[1]++
1780         delete(m, 1)
1781         sink = m[0] // ERROR "m\[0\] escapes to heap"
1782 }
1783
1784 func makemap1() map[int]int {
1785         return make(map[int]int) // ERROR "make\(map\[int\]int\) escapes to heap"
1786 }
1787
1788 func makemap2() {
1789         m := make(map[int]int) // ERROR "make\(map\[int\]int\) escapes to heap"
1790         sink = m               // ERROR "m escapes to heap"
1791 }
1792
1793 func nonescapingEface(m map[interface{}]bool) bool { // ERROR "m does not escape"
1794         return m["foo"] // ERROR `"foo" does not escape`
1795 }
1796
1797 func nonescapingIface(m map[M]bool) bool { // ERROR "m does not escape"
1798         return m[MV(0)] // ERROR "MV\(0\) does not escape"
1799 }
1800
1801 func issue10353() {
1802         x := new(int) // ERROR "new\(int\) escapes to heap"
1803         issue10353a(x)()
1804 }
1805
1806 func issue10353a(x *int) func() { // ERROR "leaking param: x"
1807         return func() { // ERROR "func literal escapes to heap"
1808                 println(*x)
1809         }
1810 }
1811
1812 func issue10353b() {
1813         var f func()
1814         for {
1815                 x := new(int) // ERROR "new\(int\) escapes to heap"
1816                 f = func() { // ERROR "func literal escapes to heap"
1817                         println(*x)
1818                 }
1819         }
1820         _ = f
1821 }