1 // errorcheck -0 -d=nil
2 // Fails on ppc64x because of incomplete optimization.
4 // Same reason for mips64x and s390x.
5 // +build !ppc64,!ppc64le,!mips64,!mips64le,!amd64,!s390x,!arm,!amd64p32,!386,!arm64
7 // Copyright 2013 The Go Authors. All rights reserved.
8 // Use of this source code is governed by a BSD-style
9 // license that can be found in the LICENSE file.
11 // Test that nil checks are removed.
12 // Optimization is enabled.
21 type BigStruct struct {
39 bigarrayp *[1 << 26]int
47 _ = *intp // ERROR "generated nil check"
49 // This one should be removed but the block copy needs
50 // to be turned into its own pseudo-op in order to see
52 _ = *arrayp // ERROR "generated nil check"
54 // 0-byte indirect doesn't suffice.
55 // we don't registerize globals, so there are no removed repeated nil checks.
56 _ = *array0p // ERROR "generated nil check"
57 _ = *array0p // ERROR "generated nil check"
59 _ = *intp // ERROR "generated nil check"
60 _ = *arrayp // ERROR "generated nil check"
61 _ = *structp // ERROR "generated nil check"
62 _ = *emptyp // ERROR "generated nil check"
63 _ = *arrayp // ERROR "generated nil check"
71 bigarrayp *[1 << 20]int
78 _ = *intp // ERROR "generated nil check"
79 _ = *arrayp // ERROR "generated nil check"
80 _ = *array0p // ERROR "generated nil check"
81 _ = *array0p // ERROR "removed repeated nil check"
82 _ = *intp // ERROR "removed repeated nil check"
83 _ = *arrayp // ERROR "removed repeated nil check"
84 _ = *structp // ERROR "generated nil check"
85 _ = *emptyp // ERROR "generated nil check"
86 _ = *arrayp // ERROR "removed repeated nil check"
87 _ = *bigarrayp // ERROR "generated nil check" ARM removed nil check before indirect!!
88 _ = *bigstructp // ERROR "generated nil check"
89 _ = *empty1p // ERROR "generated nil check"
92 func fx10k() *[10000]int
96 func f3(x *[10000]int) {
97 // Using a huge type and huge offsets so the compiler
98 // does not expect the memory hardware to fault.
99 _ = x[9999] // ERROR "generated nil check"
102 if x[9999] != 0 { // ERROR "generated nil check"
108 _ = x[9999] // ERROR "generated nil check"
110 _ = x[9999] // ERROR "removed repeated nil check"
112 _ = x[9999] // ERROR "removed repeated nil check"
114 _ = x[9999] // ERROR "generated nil check"
118 _ = x[9999] // ERROR "generated nil check"
120 _ = x[9999] // ERROR "generated nil check"
122 _ = x[9999] // ERROR "generated nil check"
125 // This one is a bit redundant, if we figured out that
126 // x wasn't going to change across the function call.
127 // But it's a little complex to do and in practice doesn't
129 _ = x[9999] // ERROR "generated nil check"
136 _ = &x[9] // ERROR "generated nil check"
138 _ = &x[9] // ERROR "removed repeated nil check"
140 _ = &x[9] // ERROR "generated nil check"
146 _ = &x[9] // ERROR "generated nil check"
148 _ = &x[9] // ERROR "removed repeated nil check"
150 _ = &x[9] // ERROR "removed repeated nil check"
155 func f4(x *[10]int) {
156 // Most of these have no checks because a real memory reference follows,
157 // and the offset is small enough that if x is nil, the address will still be
158 // in the first unmapped page of memory.
160 _ = x[9] // ERROR "removed nil check before indirect"
163 if x[9] != 0 { // ERROR "removed nil check before indirect"
169 _ = x[9] // ERROR "removed nil check before indirect"
171 _ = x[9] // ERROR "removed nil check before indirect"
173 _ = x[9] // ERROR "removed nil check before indirect"
175 _ = x[9] // ERROR "removed nil check before indirect"
179 _ = x[9] // ERROR "removed nil check before indirect"
181 _ = &x[9] // ERROR "generated nil check"
183 _ = x[9] // ERROR "removed nil check before indirect"
186 _ = x[9] // ERROR "removed nil check before indirect"
190 _ = &x[9] // ERROR "generated nil check"
192 _ = &x[9] // ERROR "removed repeated nil check"
194 _ = &x[9] // ERROR "removed repeated nil check"
197 func m1(m map[int][80]byte) byte {
198 v := m[3] // ERROR "removed nil check"
201 func m2(m map[int][800]byte) byte {
202 v := m[3] // ERROR "removed nil check"
205 func m3(m map[int][80]byte) (byte, bool) {
206 v, ok := m[3] // ERROR "removed nil check"
209 func m4(m map[int][800]byte) (byte, bool) {
210 v, ok := m[3] // ERROR "removed nil check"
215 return p[5] // ERROR "removed nil check"
218 // make sure not to do nil check for access of PAUTOHEAP
220 func (p *Struct) m() {}
223 func() { x.m() }() // ERROR "removed nil check"