1 // errorcheck -0 -d=nil
3 // Copyright 2013 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 // Test that nil checks are removed.
8 // Optimization is enabled.
17 type BigStruct struct {
35 bigarrayp *[1 << 26]int
43 _ = *intp // ERROR "generated nil check"
45 // This one should be removed but the block copy needs
46 // to be turned into its own pseudo-op in order to see
48 _ = *arrayp // ERROR "generated nil check"
50 // 0-byte indirect doesn't suffice.
51 // we don't registerize globals, so there are no removed.* nil checks.
52 _ = *array0p // ERROR "generated nil check"
53 _ = *array0p // ERROR "removed nil check"
55 _ = *intp // ERROR "removed nil check"
56 _ = *arrayp // ERROR "removed nil check"
57 _ = *structp // ERROR "generated nil check"
58 _ = *emptyp // ERROR "generated nil check"
59 _ = *arrayp // ERROR "removed nil check"
67 bigarrayp *[1 << 20]int
74 _ = *intp // ERROR "generated nil check"
75 _ = *arrayp // ERROR "generated nil check"
76 _ = *array0p // ERROR "generated nil check"
77 _ = *array0p // ERROR "removed.* nil check"
78 _ = *intp // ERROR "removed.* nil check"
79 _ = *arrayp // ERROR "removed.* nil check"
80 _ = *structp // ERROR "generated nil check"
81 _ = *emptyp // ERROR "generated nil check"
82 _ = *arrayp // ERROR "removed.* nil check"
83 _ = *bigarrayp // ERROR "generated nil check" ARM removed nil check before indirect!!
84 _ = *bigstructp // ERROR "generated nil check"
85 _ = *empty1p // ERROR "generated nil check"
88 func fx10k() *[10000]int
92 func f3(x *[10000]int) {
93 // Using a huge type and huge offsets so the compiler
94 // does not expect the memory hardware to fault.
95 _ = x[9999] // ERROR "generated nil check"
98 if x[9999] != 0 { // ERROR "removed nil check"
104 _ = x[9999] // ERROR "generated nil check"
106 _ = x[9999] // ERROR "removed.* nil check"
108 _ = x[9999] // ERROR "removed.* nil check"
110 _ = x[9999] // ERROR "removed nil check"
114 _ = x[9999] // ERROR "generated nil check"
116 _ = x[9999] // ERROR "generated nil check"
118 _ = x[9999] // ERROR "generated nil check"
121 // This one is a bit redundant, if we figured out that
122 // x wasn't going to change across the function call.
123 // But it's a little complex to do and in practice doesn't
125 _ = x[9999] // ERROR "removed nil check"
132 _ = &x[9] // ERROR "generated nil check"
134 _ = &x[9] // ERROR "removed.* nil check"
136 _ = &x[9] // ERROR "generated nil check"
142 _ = &x[9] // ERROR "generated nil check"
144 _ = &x[9] // ERROR "removed.* nil check"
146 _ = &x[9] // ERROR "removed.* nil check"
151 func f4(x *[10]int) {
152 // Most of these have no checks because a real memory reference follows,
153 // and the offset is small enough that if x is nil, the address will still be
154 // in the first unmapped page of memory.
156 _ = x[9] // ERROR "generated nil check" // bug: would like to remove this check (but nilcheck and load are in different blocks)
159 if x[9] != 0 { // ERROR "removed nil check"
165 _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect
167 _ = x[9] // ERROR "removed nil check"
169 _ = x[9] // ERROR "removed nil check"
171 _ = x[9] // ERROR "removed nil check"
175 _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect
177 _ = &x[9] // ERROR "generated nil check"
179 _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect
182 _ = x[9] // ERROR "removed nil check"
186 _ = &x[9] // ERROR "generated nil check"
188 _ = &x[9] // ERROR "removed[a-z ]* nil check"
190 _ = &x[9] // ERROR "removed[a-z ]* nil check"
193 func f5(p *float32, q *float64, r *float32, s *float64) float64 {
194 x := float64(*p) // ERROR "removed nil check"
195 y := *q // ERROR "removed nil check"
196 *r = 7 // ERROR "removed nil check"
197 *s = 9 // ERROR "removed nil check"
204 x := *p // ERROR "removed nil check"
205 *q = x // ERROR "removed nil check"
208 func m1(m map[int][80]byte) byte {
209 v := m[3] // ERROR "removed nil check"
212 func m2(m map[int][800]byte) byte {
213 v := m[3] // ERROR "removed nil check"
216 func m3(m map[int][80]byte) (byte, bool) {
217 v, ok := m[3] // ERROR "removed nil check"
220 func m4(m map[int][800]byte) (byte, bool) {
221 v, ok := m[3] // ERROR "removed nil check"
226 return p[5] // ERROR "removed nil check"
229 // make sure not to do nil check for access of PAUTOHEAP
231 func (p *Struct) m() {}
234 func() { x.m() }() // ERROR "removed nil check"
245 func f(t *TT) *byte {
247 s := &t.SS // ERROR "removed nil check"
248 return &s.x // ERROR "generated nil check"