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 when assigning to indirections.
13 type ConstPtr struct {
19 type ConstPtr2 struct {
25 i := 0 // ERROR "moved to heap: i"
26 x := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
27 // BAD: i should not escape here
28 x.p = &i // ERROR "&i escapes to heap"
32 func constptr01() *ConstPtr {
33 i := 0 // ERROR "moved to heap: i"
34 x := &ConstPtr{} // ERROR "&ConstPtr literal escapes to heap"
35 x.p = &i // ERROR "&i escapes to heap"
39 func constptr02() ConstPtr {
40 i := 0 // ERROR "moved to heap: i"
41 x := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
42 x.p = &i // ERROR "&i escapes to heap"
46 func constptr03() **ConstPtr {
47 i := 0 // ERROR "moved to heap: i"
48 x := &ConstPtr{} // ERROR "&ConstPtr literal escapes to heap" "moved to heap: x"
49 x.p = &i // ERROR "&i escapes to heap"
50 return &x // ERROR "&x escapes to heap"
54 i := 0 // ERROR "moved to heap: i"
55 x := &ConstPtr{} // ERROR "&ConstPtr literal escapes to heap"
56 x.p = &i // ERROR "&i escapes to heap"
57 sink = x // ERROR "x escapes to heap"
61 i := 0 // ERROR "moved to heap: i"
62 x := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
63 x.p = &i // ERROR "&i escapes to heap"
64 sink = *x // ERROR "\*x escapes to heap"
67 func constptr4() *ConstPtr {
68 p := new(ConstPtr) // ERROR "new\(ConstPtr\) escapes to heap"
69 *p = *&ConstPtr{} // ERROR "&ConstPtr literal does not escape"
73 func constptr5() *ConstPtr {
74 p := new(ConstPtr) // ERROR "new\(ConstPtr\) escapes to heap"
75 p1 := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
80 // BAD: p should not escape here
81 func constptr6(p *ConstPtr) { // ERROR "leaking param content: p"
82 p1 := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
87 func constptr7() **ConstPtr {
88 p := new(ConstPtr) // ERROR "new\(ConstPtr\) escapes to heap" "moved to heap: p"
90 p1 := &tmp // ERROR "&tmp does not escape"
92 return &p // ERROR "&p escapes to heap"
95 func constptr8() *ConstPtr {
96 p := new(ConstPtr) // ERROR "new\(ConstPtr\) escapes to heap"
98 p.c = *&tmp // ERROR "&tmp does not escape"
102 func constptr9() ConstPtr {
103 p := new(ConstPtr) // ERROR "new\(ConstPtr\) does not escape"
105 i := 0 // ERROR "moved to heap: i"
106 p1.p = &i // ERROR "&i escapes to heap"
111 func constptr10() ConstPtr {
112 x := &ConstPtr{} // ERROR "moved to heap: x" "&ConstPtr literal escapes to heap"
113 i := 0 // ERROR "moved to heap: i"
115 p = &ConstPtr{p: &i, x: &x} // ERROR "&i escapes to heap" "&x escapes to heap" "&ConstPtr literal does not escape"
117 pp = &p // ERROR "&p does not escape"
121 func constptr11() *ConstPtr {
122 i := 0 // ERROR "moved to heap: i"
123 p := new(ConstPtr) // ERROR "new\(ConstPtr\) escapes to heap"
124 p1 := &ConstPtr{} // ERROR "&ConstPtr literal does not escape"
125 p1.p = &i // ERROR "&i escapes to heap"
130 func foo(p **int) { // ERROR "foo p does not escape"
131 i := 0 // ERROR "moved to heap: i"
133 *y = &i // ERROR "&i escapes to heap"
136 func foo1(p *int) { // ERROR "p does not escape"
137 i := 0 // ERROR "moved to heap: i"
138 y := &p // ERROR "&p does not escape"
139 *y = &i // ERROR "&i escapes to heap"
146 x := new(int) // ERROR "moved to heap: x" "new\(int\) escapes to heap"
147 sink = &x // ERROR "&x escapes to heap"
149 z.f = &x // ERROR "&x does not escape"
151 i := 0 // ERROR "moved to heap: i"
152 *p = &i // ERROR "&i escapes to heap"
158 var x byte // ERROR "moved to heap: x"
159 global = &*&x // ERROR "&\(\*\(&x\)\) escapes to heap" "&x escapes to heap"