1 // errorcheck -0 -d=nil
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.
10 // Test that nil checks are removed.
11 // Optimization is enabled.
20 type BigStruct struct {
38 bigarrayp *[1 << 26]int
46 _ = *intp // ERROR "generated nil check"
48 // This one should be removed but the block copy needs
49 // to be turned into its own pseudo-op in order to see
51 _ = *arrayp // ERROR "generated nil check"
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"
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"
70 bigarrayp *[1 << 20]int
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"
91 func fx10k() *[10000]int
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"
101 if x[9999] != 0 { // ERROR "removed nil check"
107 _ = x[9999] // ERROR "generated nil check"
109 _ = x[9999] // ERROR "removed.* nil check"
111 _ = x[9999] // ERROR "removed.* nil check"
113 _ = x[9999] // ERROR "removed nil check"
117 _ = x[9999] // ERROR "generated nil check"
119 _ = x[9999] // ERROR "generated nil check"
121 _ = x[9999] // ERROR "generated nil check"
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
128 _ = x[9999] // ERROR "removed nil check"
135 _ = &x[9] // ERROR "generated nil check"
137 _ = &x[9] // ERROR "removed.* nil check"
139 _ = &x[9] // ERROR "generated nil check"
145 _ = &x[9] // ERROR "generated nil check"
147 _ = &x[9] // ERROR "removed.* nil check"
149 _ = &x[9] // ERROR "removed.* nil check"
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.
159 _ = x[9] // ERROR "generated nil check" // bug: would like to remove this check (but nilcheck and load are in different blocks)
162 if x[9] != 0 { // ERROR "removed nil check"
168 _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect
170 _ = x[9] // ERROR "removed nil check"
172 _ = x[9] // ERROR "removed nil check"
174 _ = x[9] // ERROR "removed nil check"
178 _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect
180 _ = &x[9] // ERROR "generated nil check"
182 _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect
185 _ = x[9] // ERROR "removed nil check"
189 _ = &x[9] // ERROR "generated nil check"
191 _ = &x[9] // ERROR "removed[a-z ]* nil check"
193 _ = &x[9] // ERROR "removed[a-z ]* nil check"
196 func m1(m map[int][80]byte) byte {
197 v := m[3] // ERROR "removed nil check"
200 func m2(m map[int][800]byte) byte {
201 v := m[3] // ERROR "removed nil check"
204 func m3(m map[int][80]byte) (byte, bool) {
205 v, ok := m[3] // ERROR "removed nil check"
208 func m4(m map[int][800]byte) (byte, bool) {
209 v, ok := m[3] // ERROR "removed nil check"
214 return p[5] // ERROR "removed nil check"
225 func f(t *TT) *byte {
227 s := &t.SS // ERROR "generated nil check"
228 return &s.x // ERROR "removed nil check"
231 // make sure not to do nil check for newobject
232 func f7() (*Struct, float64) {
234 p := &t.Y // ERROR "removed nil check"
235 return t, *p // ERROR "removed nil check"
240 x[0] = 1 // ERROR "removed nil check"
241 y := x[:] // ERROR "removed nil check"
246 func f10(p **int) int {
247 return * // ERROR "removed nil check"
249 *p // ERROR "removed nil check"
254 _ = *p // ERROR "generated nil check"
256 _ = *q // ERROR "removed nil check"