]> Cypherpunks.ru repositories - gostls13.git/blob - test/escape2n.go
cmd/gc: allocate non-escaping maps on stack
[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)
479 }
480
481 func foo68() {
482         var mv MV
483         foo64(mv) // escapes but it's an int so irrelevant
484 }
485
486 func foo69(m M) { // ERROR "leaking param: m"
487         foo64(m)
488 }
489
490 func foo70(mv1 *MV, m M) { // ERROR "leaking param: mv1" "leaking param: m"
491         m = mv1
492         foo64(m)
493 }
494
495 func foo71(x *int) []*int { // ERROR "leaking param: x"
496         var y []*int
497         y = append(y, x)
498         return y
499 }
500
501 func foo71a(x int) []*int { // ERROR "moved to heap: x"
502         var y []*int
503         y = append(y, &x) // ERROR "&x escapes to heap"
504         return y
505 }
506
507 func foo72() {
508         var x int
509         var y [1]*int
510         y[0] = &x // ERROR "&x does not escape"
511 }
512
513 func foo72aa() [10]*int {
514         var x int // ERROR "moved to heap: x"
515         var y [10]*int
516         y[0] = &x // ERROR "&x escapes to heap"
517         return y
518 }
519
520 func foo72a() {
521         var y [10]*int
522         for i := 0; i < 10; i++ {
523                 // escapes its scope
524                 x := i    // ERROR "moved to heap: x"
525                 y[i] = &x // ERROR "&x escapes to heap"
526         }
527         return
528 }
529
530 func foo72b() [10]*int {
531         var y [10]*int
532         for i := 0; i < 10; i++ {
533                 x := i    // ERROR "moved to heap: x"
534                 y[i] = &x // ERROR "&x escapes to heap"
535         }
536         return y
537 }
538
539 // issue 2145
540 func foo73() {
541         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
542         for _, v := range s {
543                 vv := v
544                 // actually just escapes its scope
545                 defer func() { // ERROR "func literal escapes to heap"
546                         println(vv)
547                 }()
548         }
549 }
550
551 func foo731() {
552         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
553         for _, v := range s {
554                 vv := v // ERROR "moved to heap: vv"
555                 // actually just escapes its scope
556                 defer func() { // ERROR "func literal escapes to heap"
557                         vv = 42 // ERROR "&vv escapes to heap"
558                         println(vv)
559                 }()
560         }
561 }
562
563 func foo74() {
564         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
565         for _, v := range s {
566                 vv := v
567                 // actually just escapes its scope
568                 fn := func() { // ERROR "func literal escapes to heap"
569                         println(vv)
570                 }
571                 defer fn()
572         }
573 }
574
575 func foo74a() {
576         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
577         for _, v := range s {
578                 vv := v // ERROR "moved to heap: vv"
579                 // actually just escapes its scope
580                 fn := func() { // ERROR "func literal escapes to heap"
581                         vv += 1 // ERROR "&vv escapes to heap"
582                         println(vv)
583                 }
584                 defer fn()
585         }
586 }
587
588 // issue 3975
589 func foo74b() {
590         var array [3]func()
591         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
592         for i, v := range s {
593                 vv := v
594                 // actually just escapes its scope
595                 array[i] = func() { // ERROR "func literal escapes to heap"
596                         println(vv)
597                 }
598         }
599 }
600
601 func foo74c() {
602         var array [3]func()
603         s := []int{3, 2, 1} // ERROR "\[\]int literal does not escape"
604         for i, v := range s {
605                 vv := v // ERROR "moved to heap: vv"
606                 // actually just escapes its scope
607                 array[i] = func() { // ERROR "func literal escapes to heap"
608                         println(&vv) // ERROR "&vv escapes to heap" "&vv does not escape"
609                 }
610         }
611 }
612
613 func myprint(y *int, x ...interface{}) *int { // ERROR "x does not escape" "leaking param: y"
614         return y
615 }
616
617 func myprint1(y *int, x ...interface{}) *interface{} { // ERROR "y does not escape" "leaking param: x"
618         return &x[0] // ERROR "&x.0. escapes to heap"
619 }
620
621 func foo75(z *int) { // ERROR "z does not escape"
622         myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
623 }
624
625 func foo75a(z *int) { // ERROR "z does not escape"
626         myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
627 }
628
629 func foo75esc(z *int) { // ERROR "leaking param: z"
630         gxx = myprint(z, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
631 }
632
633 func foo75aesc(z *int) { // ERROR "z does not escape"
634         var ppi **interface{}       // assignments to pointer dereferences lose track
635         *ppi = myprint1(z, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
636 }
637
638 func foo76(z *int) { // ERROR "leaking param: z"
639         myprint(nil, z) // ERROR "[.][.][.] argument does not escape"
640 }
641
642 func foo76a(z *int) { // ERROR "leaking param: z"
643         myprint1(nil, z) // ERROR "[.][.][.] argument does not escape"
644 }
645
646 func foo76b() {
647         myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
648 }
649
650 func foo76c() {
651         myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
652 }
653
654 func foo76d() {
655         defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
656 }
657
658 func foo76e() {
659         defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument does not escape"
660 }
661
662 func foo76f() {
663         for {
664                 // TODO: This one really only escapes its scope, but we don't distinguish yet.
665                 defer myprint(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
666         }
667 }
668
669 func foo76g() {
670         for {
671                 defer myprint1(nil, 1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
672         }
673 }
674
675 func foo77(z []interface{}) { // ERROR "z does not escape"
676         myprint(nil, z...) // z does not escape
677 }
678
679 func foo77a(z []interface{}) { // ERROR "z does not escape"
680         myprint1(nil, z...)
681 }
682
683 func foo77b(z []interface{}) { // ERROR "leaking param: z"
684         var ppi **interface{}
685         *ppi = myprint1(nil, z...)
686 }
687
688 func foo78(z int) *int { // ERROR "moved to heap: z"
689         return &z // ERROR "&z escapes to heap"
690 }
691
692 func foo78a(z int) *int { // ERROR "moved to heap: z"
693         y := &z   // ERROR "&z escapes to heap"
694         x := &y   // ERROR "&y does not escape"
695         return *x // really return y
696 }
697
698 func foo79() *int {
699         return new(int) // ERROR "new[(]int[)] escapes to heap"
700 }
701
702 func foo80() *int {
703         var z *int
704         for {
705                 // Really just escapes its scope but we don't distinguish
706                 z = new(int) // ERROR "new[(]int[)] escapes to heap"
707         }
708         _ = z
709         return nil
710 }
711
712 func foo81() *int {
713         for {
714                 z := new(int) // ERROR "new[(]int[)] does not escape"
715                 _ = z
716         }
717         return nil
718 }
719
720 func tee(p *int) (x, y *int) { return p, p } // ERROR "leaking param"
721
722 func noop(x, y *int) {} // ERROR "does not escape"
723
724 func foo82() {
725         var x, y, z int  // ERROR "moved to heap"
726         go noop(tee(&z)) // ERROR "&z escapes to heap"
727         go noop(&x, &y)  // ERROR "escapes to heap"
728         for {
729                 var u, v, w int     // ERROR "moved to heap"
730                 defer noop(tee(&u)) // ERROR "&u escapes to heap"
731                 defer noop(&v, &w)  // ERROR "escapes to heap"
732         }
733 }
734
735 type Fooer interface {
736         Foo()
737 }
738
739 type LimitedFooer struct {
740         Fooer
741         N int64
742 }
743
744 func LimitFooer(r Fooer, n int64) Fooer { // ERROR "leaking param: r"
745         return &LimitedFooer{r, n} // ERROR "&LimitedFooer literal escapes to heap"
746 }
747
748 func foo90(x *int) map[*int]*int { // ERROR "leaking param: x"
749         return map[*int]*int{nil: x} // ERROR "map\[\*int\]\*int literal escapes to heap"
750 }
751
752 func foo91(x *int) map[*int]*int { // ERROR "leaking param: x"
753         return map[*int]*int{x: nil} // ERROR "map\[\*int\]\*int literal escapes to heap"
754 }
755
756 func foo92(x *int) [2]*int { // ERROR "leaking param: x"
757         return [2]*int{x, nil}
758 }
759
760 // does not leak c
761 func foo93(c chan *int) *int { // ERROR "c does not escape"
762         for v := range c {
763                 return v
764         }
765         return nil
766 }
767
768 // does not leak m
769 func foo94(m map[*int]*int, b bool) *int { // ERROR "m does not escape"
770         for k, v := range m {
771                 if b {
772                         return k
773                 }
774                 return v
775         }
776         return nil
777 }
778
779 // does leak x
780 func foo95(m map[*int]*int, x *int) { // ERROR "m does not escape" "leaking param: x"
781         m[x] = x
782 }
783
784 // does not leak m
785 func foo96(m []*int) *int { // ERROR "m does not escape"
786         return m[0]
787 }
788
789 // does leak m
790 func foo97(m [1]*int) *int { // ERROR "leaking param: m"
791         return m[0]
792 }
793
794 // does not leak m
795 func foo98(m map[int]*int) *int { // ERROR "m does not escape"
796         return m[0]
797 }
798
799 // does leak m
800 func foo99(m *[1]*int) []*int { // ERROR "leaking param: m"
801         return m[:]
802 }
803
804 // does not leak m
805 func foo100(m []*int) *int { // ERROR "m does not escape"
806         for _, v := range m {
807                 return v
808         }
809         return nil
810 }
811
812 // does leak m
813 func foo101(m [1]*int) *int { // ERROR "leaking param: m"
814         for _, v := range m {
815                 return v
816         }
817         return nil
818 }
819
820 // does not leak m
821 func foo101a(m [1]*int) *int { // ERROR "m does not escape"
822         for i := range m { // ERROR "moved to heap: i"
823                 return &i // ERROR "&i escapes to heap"
824         }
825         return nil
826 }
827
828 // does leak x
829 func foo102(m []*int, x *int) { // ERROR "m does not escape" "leaking param: x"
830         m[0] = x
831 }
832
833 // does not leak x
834 func foo103(m [1]*int, x *int) { // ERROR "m does not escape" "x does not escape"
835         m[0] = x
836 }
837
838 var y []*int
839
840 // does not leak x
841 func foo104(x []*int) { // ERROR "x does not escape"
842         copy(y, x)
843 }
844
845 // does not leak x
846 func foo105(x []*int) { // ERROR "x does not escape"
847         _ = append(y, x...)
848 }
849
850 // does leak x
851 func foo106(x *int) { // ERROR "leaking param: x"
852         _ = append(y, x)
853 }
854
855 func foo107(x *int) map[*int]*int { // ERROR "leaking param: x"
856         return map[*int]*int{x: nil} // ERROR "map.* literal escapes to heap"
857 }
858
859 func foo108(x *int) map[*int]*int { // ERROR "leaking param: x"
860         return map[*int]*int{nil: x} // ERROR "map.* literal escapes to heap"
861 }
862
863 func foo109(x *int) *int { // ERROR "leaking param: x"
864         m := map[*int]*int{x: nil} // ERROR "map.* literal does not escape"
865         for k, _ := range m {
866                 return k
867         }
868         return nil
869 }
870
871 func foo110(x *int) *int { // ERROR "leaking param: x"
872         m := map[*int]*int{nil: x} // ERROR "map.* literal does not escape"
873         return m[nil]
874 }
875
876 func foo111(x *int) *int { // ERROR "leaking param: x"
877         m := []*int{x} // ERROR "\[\]\*int literal does not escape"
878         return m[0]
879 }
880
881 func foo112(x *int) *int { // ERROR "leaking param: x"
882         m := [1]*int{x}
883         return m[0]
884 }
885
886 func foo113(x *int) *int { // ERROR "leaking param: x"
887         m := Bar{ii: x}
888         return m.ii
889 }
890
891 func foo114(x *int) *int { // ERROR "leaking param: x"
892         m := &Bar{ii: x} // ERROR "&Bar literal does not escape"
893         return m.ii
894 }
895
896 func foo115(x *int) *int { // ERROR "leaking param: x"
897         return (*int)(unsafe.Pointer(uintptr(unsafe.Pointer(x)) + 1))
898 }
899
900 func foo116(b bool) *int {
901         if b {
902                 x := 1    // ERROR "moved to heap: x"
903                 return &x // ERROR "&x escapes to heap"
904         } else {
905                 y := 1    // ERROR "moved to heap: y"
906                 return &y // ERROR "&y escapes to heap"
907         }
908         return nil
909 }
910
911 func foo117(unknown func(interface{})) { // ERROR "unknown does not escape"
912         x := 1      // ERROR "moved to heap: x"
913         unknown(&x) // ERROR "&x escapes to heap"
914 }
915
916 func foo118(unknown func(*int)) { // ERROR "unknown does not escape"
917         x := 1      // ERROR "moved to heap: x"
918         unknown(&x) // ERROR "&x escapes to heap"
919 }
920
921 func external(*int)
922
923 func foo119(x *int) { // ERROR "leaking param: x"
924         external(x)
925 }
926
927 func foo120() {
928         // formerly exponential time analysis
929 L1:
930 L2:
931 L3:
932 L4:
933 L5:
934 L6:
935 L7:
936 L8:
937 L9:
938 L10:
939 L11:
940 L12:
941 L13:
942 L14:
943 L15:
944 L16:
945 L17:
946 L18:
947 L19:
948 L20:
949 L21:
950 L22:
951 L23:
952 L24:
953 L25:
954 L26:
955 L27:
956 L28:
957 L29:
958 L30:
959 L31:
960 L32:
961 L33:
962 L34:
963 L35:
964 L36:
965 L37:
966 L38:
967 L39:
968 L40:
969 L41:
970 L42:
971 L43:
972 L44:
973 L45:
974 L46:
975 L47:
976 L48:
977 L49:
978 L50:
979 L51:
980 L52:
981 L53:
982 L54:
983 L55:
984 L56:
985 L57:
986 L58:
987 L59:
988 L60:
989 L61:
990 L62:
991 L63:
992 L64:
993 L65:
994 L66:
995 L67:
996 L68:
997 L69:
998 L70:
999 L71:
1000 L72:
1001 L73:
1002 L74:
1003 L75:
1004 L76:
1005 L77:
1006 L78:
1007 L79:
1008 L80:
1009 L81:
1010 L82:
1011 L83:
1012 L84:
1013 L85:
1014 L86:
1015 L87:
1016 L88:
1017 L89:
1018 L90:
1019 L91:
1020 L92:
1021 L93:
1022 L94:
1023 L95:
1024 L96:
1025 L97:
1026 L98:
1027 L99:
1028 L100:
1029         // use the labels to silence compiler errors
1030         goto L1
1031         goto L2
1032         goto L3
1033         goto L4
1034         goto L5
1035         goto L6
1036         goto L7
1037         goto L8
1038         goto L9
1039         goto L10
1040         goto L11
1041         goto L12
1042         goto L13
1043         goto L14
1044         goto L15
1045         goto L16
1046         goto L17
1047         goto L18
1048         goto L19
1049         goto L20
1050         goto L21
1051         goto L22
1052         goto L23
1053         goto L24
1054         goto L25
1055         goto L26
1056         goto L27
1057         goto L28
1058         goto L29
1059         goto L30
1060         goto L31
1061         goto L32
1062         goto L33
1063         goto L34
1064         goto L35
1065         goto L36
1066         goto L37
1067         goto L38
1068         goto L39
1069         goto L40
1070         goto L41
1071         goto L42
1072         goto L43
1073         goto L44
1074         goto L45
1075         goto L46
1076         goto L47
1077         goto L48
1078         goto L49
1079         goto L50
1080         goto L51
1081         goto L52
1082         goto L53
1083         goto L54
1084         goto L55
1085         goto L56
1086         goto L57
1087         goto L58
1088         goto L59
1089         goto L60
1090         goto L61
1091         goto L62
1092         goto L63
1093         goto L64
1094         goto L65
1095         goto L66
1096         goto L67
1097         goto L68
1098         goto L69
1099         goto L70
1100         goto L71
1101         goto L72
1102         goto L73
1103         goto L74
1104         goto L75
1105         goto L76
1106         goto L77
1107         goto L78
1108         goto L79
1109         goto L80
1110         goto L81
1111         goto L82
1112         goto L83
1113         goto L84
1114         goto L85
1115         goto L86
1116         goto L87
1117         goto L88
1118         goto L89
1119         goto L90
1120         goto L91
1121         goto L92
1122         goto L93
1123         goto L94
1124         goto L95
1125         goto L96
1126         goto L97
1127         goto L98
1128         goto L99
1129         goto L100
1130 }
1131
1132 func foo121() {
1133         for i := 0; i < 10; i++ {
1134                 defer myprint(nil, i) // ERROR "[.][.][.] argument escapes to heap"
1135                 go myprint(nil, i)    // ERROR "[.][.][.] argument escapes to heap"
1136         }
1137 }
1138
1139 // same as foo121 but check across import
1140 func foo121b() {
1141         for i := 0; i < 10; i++ {
1142                 defer fmt.Printf("%d", i) // ERROR "[.][.][.] argument escapes to heap"
1143                 go fmt.Printf("%d", i)    // ERROR "[.][.][.] argument escapes to heap"
1144         }
1145 }
1146
1147 // a harmless forward jump
1148 func foo122() {
1149         var i *int
1150
1151         goto L1
1152 L1:
1153         i = new(int) // ERROR "new.int. does not escape"
1154         _ = i
1155 }
1156
1157 // a backward jump, increases loopdepth
1158 func foo123() {
1159         var i *int
1160
1161 L1:
1162         i = new(int) // ERROR "new.int. escapes to heap"
1163
1164         goto L1
1165         _ = i
1166 }
1167
1168 func foo124(x **int) { // ERROR "x does not escape"
1169         var i int // ERROR "moved to heap: i"
1170         p := &i   // ERROR "&i escapes"
1171         func() {  // ERROR "func literal does not escape"
1172                 *x = p // ERROR "leaking closure reference p"
1173         }()
1174 }
1175
1176 func foo125(ch chan *int) { // ERROR "does not escape"
1177         var i int // ERROR "moved to heap"
1178         p := &i   // ERROR "&i escapes to heap"
1179         func() {  // ERROR "func literal does not escape"
1180                 ch <- p // ERROR "leaking closure reference p"
1181         }()
1182 }
1183
1184 func foo126() {
1185         var px *int // loopdepth 0
1186         for {
1187                 // loopdepth 1
1188                 var i int // ERROR "moved to heap"
1189                 func() {  // ERROR "func literal does not escape"
1190                         px = &i // ERROR "&i escapes"
1191                 }()
1192         }
1193         _ = px
1194 }
1195
1196 var px *int
1197
1198 func foo127() {
1199         var i int // ERROR "moved to heap: i"
1200         p := &i   // ERROR "&i escapes to heap"
1201         q := p
1202         px = q
1203 }
1204
1205 func foo128() {
1206         var i int
1207         p := &i // ERROR "&i does not escape"
1208         q := p
1209         _ = q
1210 }
1211
1212 func foo129() {
1213         var i int // ERROR "moved to heap: i"
1214         p := &i   // ERROR "&i escapes to heap"
1215         func() {  // ERROR "func literal does not escape"
1216                 q := p   // ERROR "leaking closure reference p"
1217                 func() { // ERROR "func literal does not escape"
1218                         r := q // ERROR "leaking closure reference q"
1219                         px = r
1220                 }()
1221         }()
1222 }
1223
1224 func foo130() {
1225         for {
1226                 var i int // ERROR "moved to heap"
1227                 func() {  // ERROR "func literal does not escape"
1228                         px = &i // ERROR "&i escapes" "leaking closure reference i"
1229                 }()
1230         }
1231 }
1232
1233 func foo131() {
1234         var i int // ERROR "moved to heap"
1235         func() {  // ERROR "func literal does not escape"
1236                 px = &i // ERROR "&i escapes" "leaking closure reference i"
1237         }()
1238 }
1239
1240 func foo132() {
1241         var i int   // ERROR "moved to heap"
1242         go func() { // ERROR "func literal escapes to heap"
1243                 px = &i // ERROR "&i escapes" "leaking closure reference i"
1244         }()
1245 }
1246
1247 func foo133() {
1248         var i int      // ERROR "moved to heap"
1249         defer func() { // ERROR "func literal does not escape"
1250                 px = &i // ERROR "&i escapes" "leaking closure reference i"
1251         }()
1252 }
1253
1254 func foo134() {
1255         var i int
1256         p := &i  // ERROR "&i does not escape"
1257         func() { // ERROR "func literal does not escape"
1258                 q := p
1259                 func() { // ERROR "func literal does not escape"
1260                         r := q
1261                         _ = r
1262                 }()
1263         }()
1264 }
1265
1266 func foo135() {
1267         var i int   // ERROR "moved to heap: i"
1268         p := &i     // ERROR "&i escapes to heap"
1269         go func() { // ERROR "func literal escapes to heap"
1270                 q := p
1271                 func() { // ERROR "func literal does not escape"
1272                         r := q
1273                         _ = r
1274                 }()
1275         }()
1276 }
1277
1278 func foo136() {
1279         var i int   // ERROR "moved to heap: i"
1280         p := &i     // ERROR "&i escapes to heap"
1281         go func() { // ERROR "func literal escapes to heap"
1282                 q := p   // ERROR "leaking closure reference p"
1283                 func() { // ERROR "func literal does not escape"
1284                         r := q // ERROR "leaking closure reference q"
1285                         px = r
1286                 }()
1287         }()
1288 }
1289
1290 func foo137() {
1291         var i int // ERROR "moved to heap: i"
1292         p := &i   // ERROR "&i escapes to heap"
1293         func() {  // ERROR "func literal does not escape"
1294                 q := p      // ERROR "leaking closure reference p"
1295                 go func() { // ERROR "func literal escapes to heap"
1296                         r := q
1297                         _ = r
1298                 }()
1299         }()
1300 }
1301
1302 func foo138() *byte {
1303         type T struct {
1304                 x [1]byte
1305         }
1306         t := new(T)    // ERROR "new.T. escapes to heap"
1307         return &t.x[0] // ERROR "&t.x.0. escapes to heap"
1308 }
1309
1310 func foo139() *byte {
1311         type T struct {
1312                 x struct {
1313                         y byte
1314                 }
1315         }
1316         t := new(T)   // ERROR "new.T. escapes to heap"
1317         return &t.x.y // ERROR "&t.x.y escapes to heap"
1318 }
1319
1320 // issue 4751
1321 func foo140() interface{} {
1322         type T struct {
1323                 X string
1324         }
1325         type U struct {
1326                 X string
1327                 T *T
1328         }
1329         t := &T{} // ERROR "&T literal escapes to heap"
1330         return U{
1331                 X: t.X,
1332                 T: t,
1333         }
1334 }
1335
1336 //go:noescape
1337
1338 func F1([]byte)
1339
1340 func F2([]byte)
1341
1342 //go:noescape
1343
1344 func F3(x []byte) // ERROR "F3 x does not escape"
1345
1346 func F4(x []byte)
1347
1348 func G() {
1349         var buf1 [10]byte
1350         F1(buf1[:]) // ERROR "buf1 does not escape"
1351
1352         var buf2 [10]byte // ERROR "moved to heap: buf2"
1353         F2(buf2[:])       // ERROR "buf2 escapes to heap"
1354
1355         var buf3 [10]byte
1356         F3(buf3[:]) // ERROR "buf3 does not escape"
1357
1358         var buf4 [10]byte // ERROR "moved to heap: buf4"
1359         F4(buf4[:])       // ERROR "buf4 escapes to heap"
1360 }
1361
1362 type Tm struct {
1363         x int
1364 }
1365
1366 func (t *Tm) M() { // ERROR "t does not escape"
1367 }
1368
1369 func foo141() {
1370         var f func()
1371
1372         t := new(Tm) // ERROR "escapes to heap"
1373         f = t.M      // ERROR "t.M does not escape"
1374         _ = f
1375 }
1376
1377 var gf func()
1378
1379 func foo142() {
1380         t := new(Tm) // ERROR "escapes to heap"
1381         gf = t.M     // ERROR "t.M escapes to heap"
1382 }
1383
1384 // issue 3888.
1385 func foo143() {
1386         for i := 0; i < 1000; i++ {
1387                 func() { // ERROR "func literal does not escape"
1388                         for i := 0; i < 1; i++ {
1389                                 var t Tm
1390                                 t.M() // ERROR "t does not escape"
1391                         }
1392                 }()
1393         }
1394 }
1395
1396 // issue 5773
1397 // Check that annotations take effect regardless of whether they
1398 // are before or after the use in the source code.
1399
1400 //go:noescape
1401
1402 func foo144a(*int)
1403
1404 func foo144() {
1405         var x int
1406         foo144a(&x) // ERROR "&x does not escape"
1407         var y int
1408         foo144b(&y) // ERROR "&y does not escape"
1409 }
1410
1411 //go:noescape
1412
1413 func foo144b(*int)
1414
1415 // issue 7313: for loop init should not be treated as "in loop"
1416
1417 type List struct {
1418         Next *List
1419 }
1420
1421 func foo145(l List) { // ERROR "l does not escape"
1422         var p *List
1423         for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1424         }
1425 }
1426
1427 func foo146(l List) { // ERROR "l does not escape"
1428         var p *List
1429         p = &l // ERROR "&l does not escape"
1430         for ; p.Next != nil; p = p.Next {
1431         }
1432 }
1433
1434 func foo147(l List) { // ERROR "l does not escape"
1435         var p *List
1436         p = &l // ERROR "&l does not escape"
1437         for p.Next != nil {
1438                 p = p.Next
1439         }
1440 }
1441
1442 func foo148(l List) { // ERROR " l does not escape"
1443         for p := &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1444         }
1445 }
1446
1447 // related: address of variable should have depth of variable, not of loop
1448
1449 func foo149(l List) { // ERROR " l does not escape"
1450         var p *List
1451         for {
1452                 for p = &l; p.Next != nil; p = p.Next { // ERROR "&l does not escape"
1453                 }
1454         }
1455 }
1456
1457 // issue 7934: missed ... if element type had no pointers
1458
1459 var save150 []byte
1460
1461 func foo150(x ...byte) { // ERROR "leaking param: x"
1462         save150 = x
1463 }
1464
1465 func bar150() {
1466         foo150(1, 2, 3) // ERROR "[.][.][.] argument escapes to heap"
1467 }
1468
1469 // issue 7931: bad handling of slice of array
1470
1471 var save151 *int
1472
1473 func foo151(x *int) { // ERROR "leaking param: x"
1474         save151 = x
1475 }
1476
1477 func bar151() {
1478         var a [64]int // ERROR "moved to heap: a"
1479         a[4] = 101
1480         foo151(&(&a)[4:8][0]) // ERROR "&\(&a\)\[4:8\]\[0\] escapes to heap" "&a escapes to heap"
1481 }
1482
1483 func bar151b() {
1484         var a [10]int      // ERROR "moved to heap: a"
1485         b := a[:]          // ERROR "a escapes to heap"
1486         foo151(&b[4:8][0]) // ERROR "&b\[4:8\]\[0\] escapes to heap"
1487 }
1488
1489 func bar151c() {
1490         var a [64]int // ERROR "moved to heap: a"
1491         a[4] = 101
1492         foo151(&(&a)[4:8:8][0]) // ERROR "&\(&a\)\[4:8:8\]\[0\] escapes to heap" "&a escapes to heap"
1493 }
1494
1495 func bar151d() {
1496         var a [10]int        // ERROR "moved to heap: a"
1497         b := a[:]            // ERROR "a escapes to heap"
1498         foo151(&b[4:8:8][0]) // ERROR "&b\[4:8:8\]\[0\] escapes to heap"
1499 }
1500
1501 // issue 8120
1502
1503 type U struct {
1504         s *string
1505 }
1506
1507 func (u *U) String() *string { // ERROR "leaking param u content to result ~r0"
1508         return u.s
1509 }
1510
1511 type V struct {
1512         s *string
1513 }
1514
1515 func NewV(u U) *V { // ERROR "leaking param: u"
1516         return &V{u.String()} // ERROR "&V literal escapes to heap" "u does not escape"
1517 }
1518
1519 func foo152() {
1520         a := "a"   // ERROR "moved to heap: a"
1521         u := U{&a} // ERROR "&a escapes to heap"
1522         v := NewV(u)
1523         println(v)
1524 }
1525
1526 // issue 8176 - &x in type switch body not marked as escaping
1527
1528 func foo153(v interface{}) *int { // ERROR "leaking param: v"
1529         switch x := v.(type) {
1530         case int: // ERROR "moved to heap: x"
1531                 return &x // ERROR "&x escapes to heap"
1532         }
1533         panic(0)
1534 }
1535
1536 // issue 8185 - &result escaping into result
1537
1538 func f() (x int, y *int) { // ERROR "moved to heap: x"
1539         y = &x // ERROR "&x escapes to heap"
1540         return
1541 }
1542
1543 func g() (x interface{}) { // ERROR "moved to heap: x"
1544         x = &x // ERROR "&x escapes to heap"
1545         return
1546 }
1547
1548 var sink interface{}
1549
1550 type Lit struct {
1551         p *int
1552 }
1553
1554 func ptrlitNoescape() {
1555         // Both literal and element do not escape.
1556         i := 0
1557         x := &Lit{&i} // ERROR "&Lit literal does not escape" "&i does not escape"
1558         _ = x
1559 }
1560
1561 func ptrlitNoEscape2() {
1562         // Literal does not escape, but element does.
1563         i := 0        // ERROR "moved to heap: i"
1564         x := &Lit{&i} // ERROR "&Lit literal does not escape" "&i escapes to heap"
1565         sink = *x
1566 }
1567
1568 func ptrlitEscape() {
1569         // Both literal and element escape.
1570         i := 0        // ERROR "moved to heap: i"
1571         x := &Lit{&i} // ERROR "&Lit literal escapes to heap" "&i escapes to heap"
1572         sink = x
1573 }
1574
1575 // self-assignments
1576
1577 type Buffer struct {
1578         arr  [64]byte
1579         buf1 []byte
1580         buf2 []byte
1581         str1 string
1582         str2 string
1583 }
1584
1585 func (b *Buffer) foo() { // ERROR "b does not escape"
1586         b.buf1 = b.buf1[1:2]   // ERROR "ignoring self-assignment to b.buf1"
1587         b.buf1 = b.buf1[1:2:3] // ERROR "ignoring self-assignment to b.buf1"
1588         b.buf1 = b.buf2[1:2]   // ERROR "ignoring self-assignment to b.buf1"
1589         b.buf1 = b.buf2[1:2:3] // ERROR "ignoring self-assignment to b.buf1"
1590 }
1591
1592 func (b *Buffer) bar() { // ERROR "leaking param: b"
1593         b.buf1 = b.arr[1:2] // ERROR "b.arr escapes to heap"
1594 }
1595
1596 func (b *Buffer) baz() { // ERROR "b does not escape"
1597         b.str1 = b.str1[1:2] // ERROR "ignoring self-assignment to b.str1"
1598         b.str1 = b.str2[1:2] // ERROR "ignoring self-assignment to b.str1"
1599 }
1600
1601 func (b *Buffer) bat() { // ERROR "leaking param: b"
1602         o := new(Buffer) // ERROR "new\(Buffer\) escapes to heap"
1603         o.buf1 = b.buf1[1:2]
1604         sink = o
1605 }
1606
1607 func quux(sp *string, bp *[]byte) { // ERROR "sp does not escape" "bp does not escape"
1608         *sp = (*sp)[1:2] // ERROR "quux ignoring self-assignment to \*sp"
1609         *bp = (*bp)[1:2] // ERROR "quux ignoring self-assignment to \*bp"
1610 }
1611
1612 type StructWithString struct {
1613         p *int
1614         s string
1615 }
1616
1617 // This is escape analysis false negative.
1618 // We assign the pointer to x.p but leak x.s. Escape analysis coarsens flows
1619 // to just x, and thus &i looks escaping.
1620 func fieldFlowTracking() {
1621         var x StructWithString
1622         i := 0   // ERROR "moved to heap: i"
1623         x.p = &i // ERROR "&i escapes to heap"
1624         sink = x.s
1625 }
1626
1627 // String operations.
1628
1629 func slicebytetostring0() {
1630         b := make([]byte, 20) // ERROR "does not escape"
1631         s := string(b)        // ERROR "string\(b\) does not escape"
1632         _ = s
1633 }
1634
1635 func slicebytetostring1() {
1636         b := make([]byte, 20) // ERROR "does not escape"
1637         s := string(b)        // ERROR "string\(b\) does not escape"
1638         s1 := s[0:1]
1639         _ = s1
1640 }
1641
1642 func slicebytetostring2() {
1643         b := make([]byte, 20) // ERROR "does not escape"
1644         s := string(b)        // ERROR "string\(b\) escapes to heap"
1645         s1 := s[0:1]          // ERROR "moved to heap: s1"
1646         sink = &s1            // ERROR "&s1 escapes to heap"
1647 }
1648
1649 func slicebytetostring3() {
1650         b := make([]byte, 20) // ERROR "does not escape"
1651         s := string(b)        // ERROR "string\(b\) escapes to heap"
1652         s1 := s[0:1]
1653         sink = s1
1654 }
1655
1656 func addstr0() {
1657         s0 := "a"
1658         s1 := "b"
1659         s := s0 + s1 // ERROR "s0 \+ s1 does not escape"
1660         _ = s
1661 }
1662
1663 func addstr1() {
1664         s0 := "a"
1665         s1 := "b"
1666         s := "c"
1667         s += s0 + s1 // ERROR "s0 \+ s1 does not escape"
1668         _ = s
1669 }
1670
1671 func addstr2() {
1672         b := make([]byte, 20) // ERROR "does not escape"
1673         s0 := "a"
1674         s := string(b) + s0 // ERROR "string\(b\) does not escape" "string\(b\) \+ s0 does not escape"
1675         _ = s
1676 }
1677
1678 func addstr3() {
1679         s0 := "a"
1680         s1 := "b"
1681         s := s0 + s1 // ERROR "s0 \+ s1 escapes to heap"
1682         s2 := s[0:1]
1683         sink = s2
1684 }
1685
1686 func intstring0() bool {
1687         // string does not escape
1688         x := '0'
1689         s := string(x) // ERROR "string\(x\) does not escape"
1690         return s == "0"
1691 }
1692
1693 func intstring1() string {
1694         // string does not escape, but the buffer does
1695         x := '0'
1696         s := string(x) // ERROR "string\(x\) escapes to heap"
1697         return s
1698 }
1699
1700 func intstring2() {
1701         // string escapes to heap
1702         x := '0'
1703         s := string(x) // ERROR "string\(x\) escapes to heap" "moved to heap: s"
1704         sink = &s      // ERROR "&s escapes to heap"
1705 }
1706
1707 func stringtoslicebyte0() {
1708         s := "foo"
1709         x := []byte(s) // ERROR "\(\[\]byte\)\(s\) does not escape"
1710         _ = x
1711 }
1712
1713 func stringtoslicebyte1() []byte {
1714         s := "foo"
1715         return []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap"
1716 }
1717
1718 func stringtoslicebyte2() {
1719         s := "foo"
1720         sink = []byte(s) // ERROR "\(\[\]byte\)\(s\) escapes to heap"
1721 }
1722
1723 func stringtoslicerune0() {
1724         s := "foo"
1725         x := []rune(s) // ERROR "\(\[\]rune\)\(s\) does not escape"
1726         _ = x
1727 }
1728
1729 func stringtoslicerune1() []rune {
1730         s := "foo"
1731         return []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap"
1732 }
1733
1734 func stringtoslicerune2() {
1735         s := "foo"
1736         sink = []rune(s) // ERROR "\(\[\]rune\)\(s\) escapes to heap"
1737 }
1738
1739 func slicerunetostring0() {
1740         r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape"
1741         s := string(r)       // ERROR "string\(r\) does not escape"
1742         _ = s
1743 }
1744
1745 func slicerunetostring1() string {
1746         r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape"
1747         return string(r)     // ERROR "string\(r\) escapes to heap"
1748 }
1749
1750 func slicerunetostring2() {
1751         r := []rune{1, 2, 3} // ERROR "\[\]rune literal does not escape"
1752         sink = string(r)     // ERROR "string\(r\) escapes to heap"
1753 }
1754
1755 func makemap0() {
1756         m := make(map[int]int) // ERROR "make\(map\[int\]int\, 0\) does not escape"
1757         m[0] = 0
1758         m[1]++
1759         delete(m, 1)
1760         sink = m[0]
1761 }
1762
1763 func makemap1() map[int]int {
1764         return make(map[int]int) // ERROR "make\(map\[int\]int\, 0\) escapes to heap"
1765 }
1766
1767 func makemap2() {
1768         m := make(map[int]int) // ERROR "make\(map\[int\]int\, 0\) escapes to heap"
1769         sink = m
1770 }