1 // errorcheck -0 -d=nil
3 //go:build !wasm && !aix
5 // Copyright 2013 The Go Authors. All rights reserved.
6 // Use of this source code is governed by a BSD-style
7 // license that can be found in the LICENSE file.
9 // Test that nil checks are removed.
10 // Optimization is enabled.
19 type BigStruct struct {
37 bigarrayp *[1 << 26]int
45 _ = *intp // ERROR "generated nil check"
47 // This one should be removed but the block copy needs
48 // to be turned into its own pseudo-op in order to see
50 _ = *arrayp // ERROR "generated nil check"
52 // 0-byte indirect doesn't suffice.
53 // we don't registerize globals, so there are no removed.* nil checks.
54 _ = *array0p // ERROR "generated nil check"
55 _ = *array0p // ERROR "removed nil check"
57 _ = *intp // ERROR "removed nil check"
58 _ = *arrayp // ERROR "removed nil check"
59 _ = *structp // ERROR "generated nil check"
60 _ = *emptyp // ERROR "generated nil check"
61 _ = *arrayp // ERROR "removed nil check"
69 bigarrayp *[1 << 20]int
76 _ = *intp // ERROR "generated nil check"
77 _ = *arrayp // ERROR "generated nil check"
78 _ = *array0p // ERROR "generated nil check"
79 _ = *array0p // ERROR "removed.* nil check"
80 _ = *intp // ERROR "removed.* nil check"
81 _ = *arrayp // ERROR "removed.* nil check"
82 _ = *structp // ERROR "generated nil check"
83 _ = *emptyp // ERROR "generated nil check"
84 _ = *arrayp // ERROR "removed.* nil check"
85 _ = *bigarrayp // ERROR "generated nil check" ARM removed nil check before indirect!!
86 _ = *bigstructp // ERROR "generated nil check"
87 _ = *empty1p // ERROR "generated nil check"
90 func fx10k() *[10000]int
94 func f3(x *[10000]int) {
95 // Using a huge type and huge offsets so the compiler
96 // does not expect the memory hardware to fault.
97 _ = x[9999] // ERROR "generated nil check"
100 if x[9999] != 0 { // ERROR "removed nil check"
106 _ = x[9999] // ERROR "generated nil check"
108 _ = x[9999] // ERROR "removed.* nil check"
110 _ = x[9999] // ERROR "removed.* nil check"
112 _ = x[9999] // ERROR "removed nil check"
116 _ = x[9999] // ERROR "generated nil check"
118 _ = x[9999] // ERROR "generated nil check"
120 _ = x[9999] // ERROR "generated nil check"
123 // This one is a bit redundant, if we figured out that
124 // x wasn't going to change across the function call.
125 // But it's a little complex to do and in practice doesn't
127 _ = x[9999] // ERROR "removed nil check"
134 _ = &x[9] // ERROR "generated nil check"
136 _ = &x[9] // ERROR "removed.* nil check"
138 _ = &x[9] // ERROR "generated nil check"
144 _ = &x[9] // ERROR "generated nil check"
146 _ = &x[9] // ERROR "removed.* nil check"
148 _ = &x[9] // ERROR "removed.* nil check"
153 func f4(x *[10]int) {
154 // Most of these have no checks because a real memory reference follows,
155 // and the offset is small enough that if x is nil, the address will still be
156 // in the first unmapped page of memory.
158 _ = x[9] // ERROR "generated nil check" // bug: would like to remove this check (but nilcheck and load are in different blocks)
161 if x[9] != 0 { // ERROR "removed nil check"
167 _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect
169 _ = x[9] // ERROR "removed nil check"
171 _ = x[9] // ERROR "removed nil check"
173 _ = x[9] // ERROR "removed nil check"
177 _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect
179 _ = &x[9] // ERROR "generated nil check"
181 _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect
184 _ = x[9] // ERROR "removed nil check"
188 _ = &x[9] // ERROR "generated nil check"
190 _ = &x[9] // ERROR "removed[a-z ]* nil check"
192 _ = &x[9] // ERROR "removed[a-z ]* nil check"
195 func m1(m map[int][80]byte) byte {
196 v := m[3] // ERROR "removed nil check"
199 func m2(m map[int][800]byte) byte {
200 v := m[3] // ERROR "removed nil check"
203 func m3(m map[int][80]byte) (byte, bool) {
204 v, ok := m[3] // ERROR "removed nil check"
207 func m4(m map[int][800]byte) (byte, bool) {
208 v, ok := m[3] // ERROR "removed nil check"
213 return p[5] // ERROR "removed nil check"
224 func f(t *TT) *byte {
226 s := &t.SS // ERROR "generated nil check"
227 return &s.x // ERROR "removed nil check"
230 // make sure not to do nil check for newobject
231 func f7() (*Struct, float64) {
233 p := &t.Y // ERROR "removed nil check"
234 return t, *p // ERROR "removed nil check"
239 x[0] = 1 // ERROR "removed nil check"
240 y := x[:] // ERROR "removed nil check"
245 func f10(p **int) int {
246 return * // ERROR "removed nil check"
248 *p // ERROR "removed nil check"
253 _ = *p // ERROR "generated nil check"
255 _ = *q // ERROR "removed nil check"