summaryrefslogtreecommitdiffstats
path: root/test/escape_field.go
blob: 95d0784d9167cc9cf358dda69914ef9d0a319af0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
// errorcheck -0 -m -l

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Test escape analysis with respect to field assignments.

package escape

var sink interface{}

type X struct {
	p1 *int
	p2 *int
	a  [2]*int
}

type Y struct {
	x X
}

func field0() {
	i := 0 // ERROR "moved to heap: i$"
	var x X
	x.p1 = &i
	sink = x.p1
}

func field1() {
	i := 0 // ERROR "moved to heap: i$"
	var x X
	// BAD: &i should not escape
	x.p1 = &i
	sink = x.p2
}

func field3() {
	i := 0 // ERROR "moved to heap: i$"
	var x X
	x.p1 = &i
	sink = x // ERROR "x escapes to heap"
}

func field4() {
	i := 0 // ERROR "moved to heap: i$"
	var y Y
	y.x.p1 = &i
	x := y.x
	sink = x // ERROR "x escapes to heap"
}

func field5() {
	i := 0 // ERROR "moved to heap: i$"
	var x X
	// BAD: &i should not escape here
	x.a[0] = &i
	sink = x.a[1]
}

// BAD: we are not leaking param x, only x.p2
func field6(x *X) { // ERROR "leaking param content: x$"
	sink = x.p2
}

func field6a() {
	i := 0 // ERROR "moved to heap: i$"
	var x X
	// BAD: &i should not escape
	x.p1 = &i
	field6(&x)
}

func field7() {
	i := 0
	var y Y
	y.x.p1 = &i
	x := y.x
	var y1 Y
	y1.x = x
	_ = y1.x.p1
}

func field8() {
	i := 0 // ERROR "moved to heap: i$"
	var y Y
	y.x.p1 = &i
	x := y.x
	var y1 Y
	y1.x = x
	sink = y1.x.p1
}

func field9() {
	i := 0 // ERROR "moved to heap: i$"
	var y Y
	y.x.p1 = &i
	x := y.x
	var y1 Y
	y1.x = x
	sink = y1.x // ERROR "y1\.x escapes to heap"
}

func field10() {
	i := 0 // ERROR "moved to heap: i$"
	var y Y
	// BAD: &i should not escape
	y.x.p1 = &i
	x := y.x
	var y1 Y
	y1.x = x
	sink = y1.x.p2
}

func field11() {
	i := 0 // ERROR "moved to heap: i$"
	x := X{p1: &i}
	sink = x.p1
}

func field12() {
	i := 0 // ERROR "moved to heap: i$"
	// BAD: &i should not escape
	x := X{p1: &i}
	sink = x.p2
}

func field13() {
	i := 0          // ERROR "moved to heap: i$"
	x := &X{p1: &i} // ERROR "&X{...} does not escape$"
	sink = x.p1
}

func field14() {
	i := 0 // ERROR "moved to heap: i$"
	// BAD: &i should not escape
	x := &X{p1: &i} // ERROR "&X{...} does not escape$"
	sink = x.p2
}

func field15() {
	i := 0          // ERROR "moved to heap: i$"
	x := &X{p1: &i} // ERROR "&X{...} escapes to heap$"
	sink = x
}

func field16() {
	i := 0 // ERROR "moved to heap: i$"
	var x X
	// BAD: &i should not escape
	x.p1 = &i
	var iface interface{} = x // ERROR "x does not escape"
	x1 := iface.(X)
	sink = x1.p2
}

func field17() {
	i := 0 // ERROR "moved to heap: i$"
	var x X
	x.p1 = &i
	var iface interface{} = x // ERROR "x does not escape"
	x1 := iface.(X)
	sink = x1.p1
}

func field18() {
	i := 0 // ERROR "moved to heap: i$"
	var x X
	// BAD: &i should not escape
	x.p1 = &i
	var iface interface{} = x // ERROR "x does not escape"
	y, _ := iface.(Y)         // Put X, but extracted Y. The cast will fail, so y is zero initialized.
	sink = y                  // ERROR "y escapes to heap"
}