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 "literal does not escape"
85 i := 0 // ERROR "moved to heap: i"
86 s := []*int{&i} // ERROR "literal does not escape"
90 func slice10() []*int {
91 i := 0 // ERROR "moved to heap: i"
92 s := []*int{&i} // ERROR "literal 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 envForDir(dir string) []string { // ERROR "dir does not escape"
106 return mergeEnvLists([]string{"PWD=" + dir}, env) // ERROR ".PWD=. \+ dir escapes to heap" "\[\]string literal does not escape"
109 func mergeEnvLists(in, out []string) []string { // ERROR "leaking param content: in" "leaking param content: out" "leaking param: out to result ~r2 level=0"
111 for _, inkv := range in {
112 k := strings.SplitAfterN(inkv, "=", 2)[0]
113 for i, outkv := range out {
114 if strings.HasPrefix(outkv, k) {
119 out = append(out, inkv)
129 var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
131 func IPv4(a, b, c, d byte) IP {
132 p := make(IP, IPv6len) // ERROR "make\(IP, IPv6len\) escapes to heap"
133 copy(p, v4InV6Prefix)
145 Zone string // IPv6 scoped addressing zone
148 type resolveIPAddrTest struct {
155 var resolveIPAddrTests = []resolveIPAddrTest{
156 {"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
157 {"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
158 {"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
161 func setupTestData() {
162 resolveIPAddrTests = append(resolveIPAddrTests,
163 []resolveIPAddrTest{ // ERROR "\[\]resolveIPAddrTest literal does not escape"
166 &IPAddr{IP: IPv4(127, 0, 0, 1)}, // ERROR "&IPAddr literal escapes to heap"
170 &IPAddr{IP: IPv4(127, 0, 0, 1)}, // ERROR "&IPAddr literal escapes to heap"