blob: e8835ae6ff3f536411831c78b643dd707887e737 [file] [log] [blame]
Dmitry Vyukov097b1e02015-02-19 23:30:05 +03001// errorcheck -0 -m -l
2
Emmanuel Odeke53fd5222016-04-10 14:32:26 -07003// Copyright 2015 The Go Authors. All rights reserved.
Dmitry Vyukov097b1e02015-02-19 23:30:05 +03004// Use of this source code is governed by a BSD-style
5// license that can be found in the LICENSE file.
6
7// Test escape analysis with respect to field assignments.
8
9package escape
10
11var sink interface{}
12
13type X struct {
14 p1 *int
15 p2 *int
16 a [2]*int
17}
18
19type Y struct {
20 x X
21}
22
23func field0() {
24 i := 0 // ERROR "moved to heap: i$"
25 var x X
David Chase7fbb1b32015-03-26 16:36:15 -040026 x.p1 = &i // ERROR "&i escapes to heap$"
Dmitry Vyukovedcc0622015-02-19 16:27:32 +030027 sink = x.p1 // ERROR "x\.p1 escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +030028}
29
30func field1() {
31 i := 0 // ERROR "moved to heap: i$"
32 var x X
33 // BAD: &i should not escape
David Chase7fbb1b32015-03-26 16:36:15 -040034 x.p1 = &i // ERROR "&i escapes to heap$"
Dmitry Vyukovedcc0622015-02-19 16:27:32 +030035 sink = x.p2 // ERROR "x\.p2 escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +030036}
37
38func field3() {
39 i := 0 // ERROR "moved to heap: i$"
40 var x X
41 x.p1 = &i // ERROR "&i escapes to heap$"
David Chase7fbb1b32015-03-26 16:36:15 -040042 sink = x // ERROR "x escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +030043}
44
45func field4() {
46 i := 0 // ERROR "moved to heap: i$"
47 var y Y
48 y.x.p1 = &i // ERROR "&i escapes to heap$"
49 x := y.x
Dmitry Vyukovedcc0622015-02-19 16:27:32 +030050 sink = x // ERROR "x escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +030051}
52
53func field5() {
54 i := 0 // ERROR "moved to heap: i$"
55 var x X
56 // BAD: &i should not escape here
David Chase7fbb1b32015-03-26 16:36:15 -040057 x.a[0] = &i // ERROR "&i escapes to heap$"
Dmitry Vyukovedcc0622015-02-19 16:27:32 +030058 sink = x.a[1] // ERROR "x\.a\[1\] escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +030059}
60
61// BAD: we are not leaking param x, only x.p2
David Chase7fbb1b32015-03-26 16:36:15 -040062func field6(x *X) { // ERROR "leaking param content: x$"
Dmitry Vyukovedcc0622015-02-19 16:27:32 +030063 sink = x.p2 // ERROR "x\.p2 escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +030064}
65
66func field6a() {
David Chase7fbb1b32015-03-26 16:36:15 -040067 i := 0 // ERROR "moved to heap: i$"
68 var x X
Dmitry Vyukov097b1e02015-02-19 23:30:05 +030069 // BAD: &i should not escape
David Chase7fbb1b32015-03-26 16:36:15 -040070 x.p1 = &i // ERROR "&i escapes to heap$"
71 field6(&x) // ERROR "field6a &x does not escape"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +030072}
73
74func field7() {
75 i := 0
76 var y Y
77 y.x.p1 = &i // ERROR "field7 &i does not escape$"
78 x := y.x
79 var y1 Y
80 y1.x = x
81 _ = y1.x.p1
82}
83
84func field8() {
85 i := 0 // ERROR "moved to heap: i$"
86 var y Y
87 y.x.p1 = &i // ERROR "&i escapes to heap$"
88 x := y.x
89 var y1 Y
90 y1.x = x
Dmitry Vyukovedcc0622015-02-19 16:27:32 +030091 sink = y1.x.p1 // ERROR "y1\.x\.p1 escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +030092}
93
94func field9() {
95 i := 0 // ERROR "moved to heap: i$"
96 var y Y
97 y.x.p1 = &i // ERROR "&i escapes to heap$"
98 x := y.x
99 var y1 Y
100 y1.x = x
Dmitry Vyukovedcc0622015-02-19 16:27:32 +0300101 sink = y1.x // ERROR "y1\.x escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300102}
103
104func field10() {
105 i := 0 // ERROR "moved to heap: i$"
106 var y Y
107 // BAD: &i should not escape
108 y.x.p1 = &i // ERROR "&i escapes to heap$"
109 x := y.x
110 var y1 Y
111 y1.x = x
Dmitry Vyukovedcc0622015-02-19 16:27:32 +0300112 sink = y1.x.p2 // ERROR "y1\.x\.p2 escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300113}
114
115func field11() {
116 i := 0 // ERROR "moved to heap: i$"
117 x := X{p1: &i} // ERROR "&i escapes to heap$"
David Chase7fbb1b32015-03-26 16:36:15 -0400118 sink = x.p1 // ERROR "x\.p1 escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300119}
120
121func field12() {
122 i := 0 // ERROR "moved to heap: i$"
123 // BAD: &i should not escape
124 x := X{p1: &i} // ERROR "&i escapes to heap$"
David Chase7fbb1b32015-03-26 16:36:15 -0400125 sink = x.p2 // ERROR "x\.p2 escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300126}
127
128func field13() {
129 i := 0 // ERROR "moved to heap: i$"
130 x := &X{p1: &i} // ERROR "&i escapes to heap$" "field13 &X literal does not escape$"
David Chase7fbb1b32015-03-26 16:36:15 -0400131 sink = x.p1 // ERROR "x\.p1 escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300132}
133
134func field14() {
135 i := 0 // ERROR "moved to heap: i$"
136 // BAD: &i should not escape
137 x := &X{p1: &i} // ERROR "&i escapes to heap$" "field14 &X literal does not escape$"
David Chase7fbb1b32015-03-26 16:36:15 -0400138 sink = x.p2 // ERROR "x\.p2 escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300139}
140
141func field15() {
142 i := 0 // ERROR "moved to heap: i$"
143 x := &X{p1: &i} // ERROR "&X literal escapes to heap$" "&i escapes to heap$"
David Chase7fbb1b32015-03-26 16:36:15 -0400144 sink = x // ERROR "x escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300145}
146
147func field16() {
148 i := 0 // ERROR "moved to heap: i$"
149 var x X
150 // BAD: &i should not escape
David Chase7fbb1b32015-03-26 16:36:15 -0400151 x.p1 = &i // ERROR "&i escapes to heap$"
Dmitry Vyukovedcc0622015-02-19 16:27:32 +0300152 var iface interface{} = x // ERROR "x escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300153 x1 := iface.(X)
Dmitry Vyukovedcc0622015-02-19 16:27:32 +0300154 sink = x1.p2 // ERROR "x1\.p2 escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300155}
156
157func field17() {
158 i := 0 // ERROR "moved to heap: i$"
159 var x X
David Chase7fbb1b32015-03-26 16:36:15 -0400160 x.p1 = &i // ERROR "&i escapes to heap$"
Dmitry Vyukovedcc0622015-02-19 16:27:32 +0300161 var iface interface{} = x // ERROR "x escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300162 x1 := iface.(X)
Dmitry Vyukovedcc0622015-02-19 16:27:32 +0300163 sink = x1.p1 // ERROR "x1\.p1 escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300164}
165
166func field18() {
167 i := 0 // ERROR "moved to heap: i$"
168 var x X
169 // BAD: &i should not escape
David Chase7fbb1b32015-03-26 16:36:15 -0400170 x.p1 = &i // ERROR "&i escapes to heap$"
Dmitry Vyukovedcc0622015-02-19 16:27:32 +0300171 var iface interface{} = x // ERROR "x escapes to heap"
David Chase7fbb1b32015-03-26 16:36:15 -0400172 y, _ := iface.(Y) // Put X, but extracted Y. The cast will fail, so y is zero initialized.
173 sink = y // ERROR "y escapes to heap"
Dmitry Vyukov097b1e02015-02-19 23:30:05 +0300174}