3 // Copyright 2015 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Test escape analysis for arrays and some large things
15 func bar(a, b *string) U { // ERROR "leaking param: a to result ~r0 level=0$" "leaking param: b to result ~r0 level=0$"
19 func foo(x U) U { // ERROR "leaking param: x to result ~r0 level=0$"
23 func bff(a, b *string) U { // ERROR "leaking param: a to result ~r0 level=0$" "leaking param: b to result ~r0 level=0$"
24 return foo(foo(bar(a, b)))
27 func tbff1() *string {
29 b := "dog" // ERROR "moved to heap: b$"
35 // BAD: need fine-grained analysis to track u[0] and u[1] differently.
36 func tbff2() *string {
37 a := "cat" // ERROR "moved to heap: a$"
38 b := "dog" // ERROR "moved to heap: b$"
44 func car(x U) *string { // ERROR "leaking param: x to result ~r0 level=0$"
48 // BAD: need fine-grained analysis to track x[0] and x[1] differently.
49 func fun(x U, y *string) *string { // ERROR "leaking param: x to result ~r0 level=0$" "leaking param: y to result ~r0 level=0$"
54 func fup(x *U, y *string) *string { // ERROR "leaking param: x to result ~r0 level=1$" "leaking param: y$"
55 x[0] = y // leaking y to heap is intended
59 func fum(x *U, y **string) *string { // ERROR "leaking param: x to result ~r0 level=1$" "leaking param content: y$"
64 func fuo(x *U, y *U) *string { // ERROR "leaking param: x to result ~r0 level=1$" "leaking param content: y$"
69 // These two tests verify that:
70 // small array literals are stack allocated;
71 // pointers stored in small array literals do not escape;
72 // large array literals are heap allocated;
73 // pointers stored in large array literals escape.
74 func hugeLeaks1(x **string, y **string) { // ERROR "leaking param content: x" "y does not escape"
77 // 4 x 4,000,000 exceeds MaxStackVarSize, therefore it must be heap allocated if pointers are 4 bytes or larger.
78 b := [4000000]*string{*x} // ERROR "moved to heap: b"
82 func hugeLeaks2(x *string, y *string) { // ERROR "leaking param: x" "y does not escape"
85 // 4 x 4,000,000 exceeds MaxStackVarSize, therefore it must be heap allocated if pointers are 4 bytes or larger.
86 b := [4000000]*string{x} // ERROR "moved to heap: b"
90 // BAD: x need not leak.
91 func doesNew1(x *string, y *string) { // ERROR "leaking param: x" "leaking param: y"
92 a := new([10]*string) // ERROR "new\(\[10\]\*string\) does not escape"
94 b := new([65537]*string) // ERROR "new\(\[65537\]\*string\) escapes to heap"
108 // BAD: x need not leak.
109 func doesNew2(x *string, y *string) { // ERROR "leaking param: x" "leaking param: y"
110 a := new(a10) // ERROR "new\(a10\) does not escape"
112 b := new(a65537) // ERROR "new\(a65537\) escapes to heap"
116 // BAD: x need not leak.
117 func doesMakeSlice(x *string, y *string) { // ERROR "leaking param: x" "leaking param: y"
118 a := make([]*string, 10) // ERROR "make\(\[\]\*string, 10\) does not escape"
120 b := make([]*string, 65537) // ERROR "make\(\[\]\*string, 65537\) escapes to heap"
124 func nonconstArray() {
126 s1 := make([]int, n) // ERROR "make\(\[\]int, n\) escapes to heap"
127 s2 := make([]int, 0, n) // ERROR "make\(\[\]int, 0, n\) escapes to heap"