]> Cypherpunks.ru repositories - gostls13.git/blob - test/nilptr3.go
cmd/compile/internal/noder: stop preserving original const strings
[gostls13.git] / test / nilptr3.go
1 // errorcheck -0 -d=nil
2
3 // +build !wasm
4 // +build !aix
5
6 // Copyright 2013 The Go Authors. All rights reserved.
7 // Use of this source code is governed by a BSD-style
8 // license that can be found in the LICENSE file.
9
10 // Test that nil checks are removed.
11 // Optimization is enabled.
12
13 package p
14
15 type Struct struct {
16         X int
17         Y float64
18 }
19
20 type BigStruct struct {
21         X int
22         Y float64
23         A [1 << 20]int
24         Z string
25 }
26
27 type Empty struct {
28 }
29
30 type Empty1 struct {
31         Empty
32 }
33
34 var (
35         intp       *int
36         arrayp     *[10]int
37         array0p    *[0]int
38         bigarrayp  *[1 << 26]int
39         structp    *Struct
40         bigstructp *BigStruct
41         emptyp     *Empty
42         empty1p    *Empty1
43 )
44
45 func f1() {
46         _ = *intp // ERROR "generated nil check"
47
48         // This one should be removed but the block copy needs
49         // to be turned into its own pseudo-op in order to see
50         // the indirect.
51         _ = *arrayp // ERROR "generated nil check"
52
53         // 0-byte indirect doesn't suffice.
54         // we don't registerize globals, so there are no removed.* nil checks.
55         _ = *array0p // ERROR "generated nil check"
56         _ = *array0p // ERROR "removed nil check"
57
58         _ = *intp    // ERROR "removed nil check"
59         _ = *arrayp  // ERROR "removed nil check"
60         _ = *structp // ERROR "generated nil check"
61         _ = *emptyp  // ERROR "generated nil check"
62         _ = *arrayp  // ERROR "removed nil check"
63 }
64
65 func f2() {
66         var (
67                 intp       *int
68                 arrayp     *[10]int
69                 array0p    *[0]int
70                 bigarrayp  *[1 << 20]int
71                 structp    *Struct
72                 bigstructp *BigStruct
73                 emptyp     *Empty
74                 empty1p    *Empty1
75         )
76
77         _ = *intp       // ERROR "generated nil check"
78         _ = *arrayp     // ERROR "generated nil check"
79         _ = *array0p    // ERROR "generated nil check"
80         _ = *array0p    // ERROR "removed.* nil check"
81         _ = *intp       // ERROR "removed.* nil check"
82         _ = *arrayp     // ERROR "removed.* nil check"
83         _ = *structp    // ERROR "generated nil check"
84         _ = *emptyp     // ERROR "generated nil check"
85         _ = *arrayp     // ERROR "removed.* nil check"
86         _ = *bigarrayp  // ERROR "generated nil check" ARM removed nil check before indirect!!
87         _ = *bigstructp // ERROR "generated nil check"
88         _ = *empty1p    // ERROR "generated nil check"
89 }
90
91 func fx10k() *[10000]int
92
93 var b bool
94
95 func f3(x *[10000]int) {
96         // Using a huge type and huge offsets so the compiler
97         // does not expect the memory hardware to fault.
98         _ = x[9999] // ERROR "generated nil check"
99
100         for {
101                 if x[9999] != 0 { // ERROR "removed nil check"
102                         break
103                 }
104         }
105
106         x = fx10k()
107         _ = x[9999] // ERROR "generated nil check"
108         if b {
109                 _ = x[9999] // ERROR "removed.* nil check"
110         } else {
111                 _ = x[9999] // ERROR "removed.* nil check"
112         }
113         _ = x[9999] // ERROR "removed nil check"
114
115         x = fx10k()
116         if b {
117                 _ = x[9999] // ERROR "generated nil check"
118         } else {
119                 _ = x[9999] // ERROR "generated nil check"
120         }
121         _ = x[9999] // ERROR "generated nil check"
122
123         fx10k()
124         // This one is a bit redundant, if we figured out that
125         // x wasn't going to change across the function call.
126         // But it's a little complex to do and in practice doesn't
127         // matter enough.
128         _ = x[9999] // ERROR "removed nil check"
129 }
130
131 func f3a() {
132         x := fx10k()
133         y := fx10k()
134         z := fx10k()
135         _ = &x[9] // ERROR "generated nil check"
136         y = z
137         _ = &x[9] // ERROR "removed.* nil check"
138         x = y
139         _ = &x[9] // ERROR "generated nil check"
140 }
141
142 func f3b() {
143         x := fx10k()
144         y := fx10k()
145         _ = &x[9] // ERROR "generated nil check"
146         y = x
147         _ = &x[9] // ERROR "removed.* nil check"
148         x = y
149         _ = &x[9] // ERROR "removed.* nil check"
150 }
151
152 func fx10() *[10]int
153
154 func f4(x *[10]int) {
155         // Most of these have no checks because a real memory reference follows,
156         // and the offset is small enough that if x is nil, the address will still be
157         // in the first unmapped page of memory.
158
159         _ = x[9] // ERROR "generated nil check" // bug: would like to remove this check (but nilcheck and load are in different blocks)
160
161         for {
162                 if x[9] != 0 { // ERROR "removed nil check"
163                         break
164                 }
165         }
166
167         x = fx10()
168         _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect
169         if b {
170                 _ = x[9] // ERROR "removed nil check"
171         } else {
172                 _ = x[9] // ERROR "removed nil check"
173         }
174         _ = x[9] // ERROR "removed nil check"
175
176         x = fx10()
177         if b {
178                 _ = x[9] // ERROR "generated nil check"  // bug would like to remove before indirect
179         } else {
180                 _ = &x[9] // ERROR "generated nil check"
181         }
182         _ = x[9] // ERROR "generated nil check"  // bug would like to remove before indirect
183
184         fx10()
185         _ = x[9] // ERROR "removed nil check"
186
187         x = fx10()
188         y := fx10()
189         _ = &x[9] // ERROR "generated nil check"
190         y = x
191         _ = &x[9] // ERROR "removed[a-z ]* nil check"
192         x = y
193         _ = &x[9] // ERROR "removed[a-z ]* nil check"
194 }
195
196 func m1(m map[int][80]byte) byte {
197         v := m[3] // ERROR "removed nil check"
198         return v[5]
199 }
200 func m2(m map[int][800]byte) byte {
201         v := m[3] // ERROR "removed nil check"
202         return v[5]
203 }
204 func m3(m map[int][80]byte) (byte, bool) {
205         v, ok := m[3] // ERROR "removed nil check"
206         return v[5], ok
207 }
208 func m4(m map[int][800]byte) (byte, bool) {
209         v, ok := m[3] // ERROR "removed nil check"
210         return v[5], ok
211 }
212 func p1() byte {
213         p := new([100]byte)
214         return p[5] // ERROR "removed nil check"
215 }
216
217 type SS struct {
218         x byte
219 }
220
221 type TT struct {
222         SS
223 }
224
225 func f(t *TT) *byte {
226         // See issue 17242.
227         s := &t.SS  // ERROR "generated nil check"
228         return &s.x // ERROR "removed nil check"
229 }
230
231 // make sure not to do nil check for newobject
232 func f7() (*Struct, float64) {
233         t := new(Struct)
234         p := &t.Y    // ERROR "removed nil check"
235         return t, *p // ERROR "removed nil check"
236 }
237
238 func f9() []int {
239         x := new([1]int)
240         x[0] = 1  // ERROR "removed nil check"
241         y := x[:] // ERROR "removed nil check"
242         return y
243 }
244
245 // See issue 42673.
246 func f10(p **int) int {
247         return * // ERROR "removed nil check"
248         /* */
249         *p // ERROR "removed nil check"
250 }
251
252 func f11(x []byte) {
253         p := (*[0]byte)(x)
254         _ = *p // ERROR "generated nil check"
255         q := (*[4]byte)(x)
256         _ = *q // ERROR "removed nil check"
257 }