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"
22 s = append(s, &i) // ERROR "&i escapes to heap"
28 i := 0 // ERROR "moved to heap: i"
29 s = append(s, &i) // ERROR "&i escapes to heap"
33 func slice2() []*int {
35 i := 0 // ERROR "moved to heap: i"
36 s = append(s, &i) // ERROR "&i escapes to heap"
42 i := 0 // ERROR "moved to heap: i"
43 s = append(s, &i) // ERROR "&i escapes to heap"
50 func slice4(s []*int) { // ERROR "s does not escape"
51 i := 0 // ERROR "moved to heap: i"
52 s[0] = &i // ERROR "&i escapes to heap"
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"
60 s[0] = &i // ERROR "&i escapes to heap"
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"
67 s[0] = &i // ERROR "&i escapes to heap"
72 s := make([]*int, 10) // ERROR "make\(\[\]\*int, 10\) does not escape"
73 i := 0 // ERROR "moved to heap: i"
74 s[0] = &i // ERROR "&i escapes to heap"
80 s := []*int{&i} // ERROR "&i does not escape" "literal does not escape"
85 i := 0 // ERROR "moved to heap: i"
86 s := []*int{&i} // ERROR "&i escapes to heap" "literal does not escape"
90 func slice10() []*int {
91 i := 0 // ERROR "moved to heap: i"
92 s := []*int{&i} // ERROR "&i escapes to heap" "literal escapes to heap"
96 func envForDir(dir string) []string { // ERROR "dir does not escape"
98 return mergeEnvLists([]string{"PWD=" + dir}, env) // ERROR ".PWD=. \+ dir escapes to heap" "\[\]string literal does not escape"
101 func mergeEnvLists(in, out []string) []string { // ERROR "leaking param content: in" "leaking param content: out" "leaking param: out to result ~r2 level=0"
103 for _, inkv := range in {
104 k := strings.SplitAfterN(inkv, "=", 2)[0]
105 for i, outkv := range out {
106 if strings.HasPrefix(outkv, k) {
111 out = append(out, inkv)
121 var v4InV6Prefix = []byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
123 func IPv4(a, b, c, d byte) IP {
124 p := make(IP, IPv6len) // ERROR "make\(IP, IPv6len\) escapes to heap"
125 copy(p, v4InV6Prefix)
137 Zone string // IPv6 scoped addressing zone
140 type resolveIPAddrTest struct {
147 var resolveIPAddrTests = []resolveIPAddrTest{
148 {"ip", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
149 {"ip4", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
150 {"ip4:icmp", "127.0.0.1", &IPAddr{IP: IPv4(127, 0, 0, 1)}, nil},
153 func setupTestData() {
154 resolveIPAddrTests = append(resolveIPAddrTests,
155 []resolveIPAddrTest{ // ERROR "\[\]resolveIPAddrTest literal does not escape"
158 &IPAddr{IP: IPv4(127, 0, 0, 1)}, // ERROR "&IPAddr literal escapes to heap"
162 &IPAddr{IP: IPv4(127, 0, 0, 1)}, // ERROR "&IPAddr literal escapes to heap"