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 slices.
20 // BAD: i should not escape
21 i := 0 // ERROR "moved to heap: i"
28 i := 0 // ERROR "moved to heap: i"
33 func slice2() []*int {
35 i := 0 // ERROR "moved to heap: i"
42 i := 0 // ERROR "moved to heap: i"
50 func slice4(s []*int) { // ERROR "s does not escape"
51 i := 0 // ERROR "moved to heap: i"
55 func slice5(s []*int) { // ERROR "s does not escape"
57 s = make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
59 i := 0 // ERROR "moved to heap: i"
64 s := make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
65 // BAD: i should not escape
66 i := 0 // ERROR "moved to heap: i"
72 s := make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
73 i := 0 // ERROR "moved to heap: i"
80 s := []*int{&i} // ERROR "\[\]\*int{...} does not escape"
85 i := 0 // ERROR "moved to heap: i"
86 s := []*int{&i} // ERROR "\[\]\*int{...} does not escape"
90 func slice10() []*int {
91 i := 0 // ERROR "moved to heap: i"
92 s := []*int{&i} // ERROR "\[\]\*int{...} escapes to heap"
98 s := make([]int, 2, 3) // ERROR "make\(\[\]int, 2, 3\) does not escape"
99 s = make([]int, i, 3) // ERROR "make\(\[\]int, i, 3\) does not escape"
100 s = make([]int, i, 1) // ERROR "make\(\[\]int, i, 1\) does not escape"
104 func slice12(x []int) *[1]int { // ERROR "leaking param: x to result ~r0 level=0$"
108 func slice13(x []*int) [1]*int { // ERROR "leaking param: x to result ~r0 level=1$"
112 func envForDir(dir string) []string { // ERROR "dir does not escape"
114 return mergeEnvLists([]string{"PWD=" + dir}, env) // ERROR ".PWD=. \+ dir escapes to heap" "\[\]string{...} does not escape"
117 func mergeEnvLists(in, out []string) []string { // ERROR "leaking param content: in" "leaking param content: out" "leaking param: out to result ~r0 level=0"
119 for _, inkv := range in {
120 k := strings.SplitAfterN(inkv, "=", 2)[0]
121 for i, outkv := range out {
122 if strings.HasPrefix(outkv, k) {
127 out = append(out, inkv)
137 var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
139 func IPv4(a, b, c, d byte) IP {
140 p := make(IP, IPv6len) // ERROR "make\(IP, 16\) escapes to heap"
141 copy(p, v4InV6Prefix)
153 Zone string // IPv6 scoped addressing zone
156 type resolveIPAddrTest struct {
163 var resolveIPAddrTests = []resolveIPAddrTest{
164 {"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
165 {"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
166 {"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
169 func setupTestData() {
170 resolveIPAddrTests = append(resolveIPAddrTests,
171 []resolveIPAddrTest{ // ERROR "\[\]resolveIPAddrTest{...} does not escape"
174 &IPAddr{IP: IPv4(127, 0, 0, 1)}, // ERROR "&IPAddr{...} escapes to heap"
178 &IPAddr{IP: IPv4(127, 0, 0, 1)}, // ERROR "&IPAddr{...} escapes to heap"