David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 1 | // errorcheck -0 -d=nil |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 2 | |
Emmanuel Odeke | 53fd522 | 2016-04-10 14:32:26 -0700 | [diff] [blame] | 3 | // Copyright 2013 The Go Authors. All rights reserved. |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 4 | // Use of this source code is governed by a BSD-style |
| 5 | // license that can be found in the LICENSE file. |
| 6 | |
| 7 | // Test that nil checks are removed. |
| 8 | // Optimization is enabled. |
| 9 | |
| 10 | package p |
| 11 | |
| 12 | type Struct struct { |
| 13 | X int |
| 14 | Y float64 |
| 15 | } |
| 16 | |
| 17 | type BigStruct struct { |
| 18 | X int |
| 19 | Y float64 |
| 20 | A [1 << 20]int |
| 21 | Z string |
| 22 | } |
| 23 | |
| 24 | type Empty struct { |
| 25 | } |
| 26 | |
| 27 | type Empty1 struct { |
| 28 | Empty |
| 29 | } |
| 30 | |
| 31 | var ( |
| 32 | intp *int |
| 33 | arrayp *[10]int |
| 34 | array0p *[0]int |
| 35 | bigarrayp *[1 << 26]int |
| 36 | structp *Struct |
| 37 | bigstructp *BigStruct |
| 38 | emptyp *Empty |
| 39 | empty1p *Empty1 |
| 40 | ) |
| 41 | |
| 42 | func f1() { |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 43 | _ = *intp // ERROR "generated nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 44 | |
| 45 | // This one should be removed but the block copy needs |
| 46 | // to be turned into its own pseudo-op in order to see |
| 47 | // the indirect. |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 48 | _ = *arrayp // ERROR "generated nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 49 | |
| 50 | // 0-byte indirect doesn't suffice. |
| 51 | // we don't registerize globals, so there are no removed.* nil checks. |
Keith Randall | 256a605 | 2017-01-20 09:54:10 -0800 | [diff] [blame] | 52 | _ = *array0p // ERROR "generated nil check" |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 53 | _ = *array0p // ERROR "removed nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 54 | |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 55 | _ = *intp // ERROR "removed nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 56 | _ = *arrayp // ERROR "removed nil check" |
| 57 | _ = *structp // ERROR "generated nil check" |
| 58 | _ = *emptyp // ERROR "generated nil check" |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 59 | _ = *arrayp // ERROR "removed nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | func f2() { |
| 63 | var ( |
| 64 | intp *int |
| 65 | arrayp *[10]int |
| 66 | array0p *[0]int |
| 67 | bigarrayp *[1 << 20]int |
| 68 | structp *Struct |
| 69 | bigstructp *BigStruct |
| 70 | emptyp *Empty |
| 71 | empty1p *Empty1 |
| 72 | ) |
| 73 | |
Keith Randall | 256a605 | 2017-01-20 09:54:10 -0800 | [diff] [blame] | 74 | _ = *intp // ERROR "generated nil check" |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 75 | _ = *arrayp // ERROR "generated nil check" |
| 76 | _ = *array0p // ERROR "generated nil check" |
| 77 | _ = *array0p // ERROR "removed.* nil check" |
| 78 | _ = *intp // ERROR "removed.* nil check" |
Keith Randall | 256a605 | 2017-01-20 09:54:10 -0800 | [diff] [blame] | 79 | _ = *arrayp // ERROR "removed.* nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 80 | _ = *structp // ERROR "generated nil check" |
| 81 | _ = *emptyp // ERROR "generated nil check" |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 82 | _ = *arrayp // ERROR "removed.* nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 83 | _ = *bigarrayp // ERROR "generated nil check" ARM removed nil check before indirect!! |
| 84 | _ = *bigstructp // ERROR "generated nil check" |
| 85 | _ = *empty1p // ERROR "generated nil check" |
| 86 | } |
| 87 | |
| 88 | func fx10k() *[10000]int |
| 89 | |
| 90 | var b bool |
| 91 | |
| 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" |
| 96 | |
| 97 | for { |
| 98 | if x[9999] != 0 { // ERROR "removed nil check" |
| 99 | break |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | x = fx10k() |
| 104 | _ = x[9999] // ERROR "generated nil check" |
| 105 | if b { |
| 106 | _ = x[9999] // ERROR "removed.* nil check" |
| 107 | } else { |
| 108 | _ = x[9999] // ERROR "removed.* nil check" |
| 109 | } |
| 110 | _ = x[9999] // ERROR "removed nil check" |
| 111 | |
| 112 | x = fx10k() |
| 113 | if b { |
| 114 | _ = x[9999] // ERROR "generated nil check" |
| 115 | } else { |
| 116 | _ = x[9999] // ERROR "generated nil check" |
| 117 | } |
| 118 | _ = x[9999] // ERROR "generated nil check" |
| 119 | |
| 120 | fx10k() |
| 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 |
| 124 | // matter enough. |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 125 | _ = x[9999] // ERROR "removed nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | func f3a() { |
| 129 | x := fx10k() |
| 130 | y := fx10k() |
| 131 | z := fx10k() |
Keith Randall | 256a605 | 2017-01-20 09:54:10 -0800 | [diff] [blame] | 132 | _ = &x[9] // ERROR "generated nil check" |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 133 | y = z |
| 134 | _ = &x[9] // ERROR "removed.* nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 135 | x = y |
| 136 | _ = &x[9] // ERROR "generated nil check" |
| 137 | } |
| 138 | |
| 139 | func f3b() { |
| 140 | x := fx10k() |
| 141 | y := fx10k() |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 142 | _ = &x[9] // ERROR "generated nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 143 | y = x |
| 144 | _ = &x[9] // ERROR "removed.* nil check" |
| 145 | x = y |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 146 | _ = &x[9] // ERROR "removed.* nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 147 | } |
| 148 | |
| 149 | func fx10() *[10]int |
| 150 | |
| 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. |
| 155 | |
Keith Randall | 3134ab3 | 2016-09-13 17:01:01 -0700 | [diff] [blame] | 156 | _ = x[9] // ERROR "generated nil check" // bug: would like to remove this check (but nilcheck and load are in different blocks) |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 157 | |
| 158 | for { |
| 159 | if x[9] != 0 { // ERROR "removed nil check" |
| 160 | break |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | x = fx10() |
| 165 | _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect |
| 166 | if b { |
| 167 | _ = x[9] // ERROR "removed nil check" |
| 168 | } else { |
| 169 | _ = x[9] // ERROR "removed nil check" |
| 170 | } |
| 171 | _ = x[9] // ERROR "removed nil check" |
| 172 | |
| 173 | x = fx10() |
| 174 | if b { |
| 175 | _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect |
| 176 | } else { |
| 177 | _ = &x[9] // ERROR "generated nil check" |
| 178 | } |
| 179 | _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect |
| 180 | |
| 181 | fx10() |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 182 | _ = x[9] // ERROR "removed nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 183 | |
| 184 | x = fx10() |
| 185 | y := fx10() |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 186 | _ = &x[9] // ERROR "generated nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 187 | y = x |
| 188 | _ = &x[9] // ERROR "removed[a-z ]* nil check" |
| 189 | x = y |
Cherry Zhang | 98061fa | 2017-01-20 10:38:05 -0500 | [diff] [blame] | 190 | _ = &x[9] // ERROR "removed[a-z ]* nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 191 | } |
Keith Randall | 4a346e7 | 2016-02-25 13:45:22 -0800 | [diff] [blame] | 192 | |
| 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" |
| 198 | return x + y |
| 199 | } |
| 200 | |
| 201 | type T [29]byte |
| 202 | |
| 203 | func f6(p, q *T) { |
| 204 | x := *p // ERROR "removed nil check" |
| 205 | *q = x // ERROR "removed nil check" |
| 206 | } |
Keith Randall | 3c1a4c1 | 2016-04-19 21:06:53 -0700 | [diff] [blame] | 207 | |
| 208 | func m1(m map[int][80]byte) byte { |
| 209 | v := m[3] // ERROR "removed nil check" |
| 210 | return v[5] |
| 211 | } |
| 212 | func m2(m map[int][800]byte) byte { |
| 213 | v := m[3] // ERROR "removed nil check" |
| 214 | return v[5] |
| 215 | } |
| 216 | func m3(m map[int][80]byte) (byte, bool) { |
| 217 | v, ok := m[3] // ERROR "removed nil check" |
| 218 | return v[5], ok |
| 219 | } |
| 220 | func m4(m map[int][800]byte) (byte, bool) { |
| 221 | v, ok := m[3] // ERROR "removed nil check" |
| 222 | return v[5], ok |
| 223 | } |
| 224 | func p1() byte { |
| 225 | p := new([100]byte) |
| 226 | return p[5] // ERROR "removed nil check" |
| 227 | } |
Cherry Zhang | 04e76f2 | 2016-08-17 10:32:39 -0400 | [diff] [blame] | 228 | |
| 229 | // make sure not to do nil check for access of PAUTOHEAP |
| 230 | //go:noinline |
| 231 | func (p *Struct) m() {} |
| 232 | func c1() { |
| 233 | var x Struct |
| 234 | func() { x.m() }() // ERROR "removed nil check" |
| 235 | } |
Keith Randall | 9893818 | 2016-09-27 14:39:27 -0700 | [diff] [blame] | 236 | |
| 237 | type SS struct { |
| 238 | x byte |
| 239 | } |
| 240 | |
| 241 | type TT struct { |
| 242 | SS |
| 243 | } |
| 244 | |
| 245 | func f(t *TT) *byte { |
| 246 | // See issue 17242. |
| 247 | s := &t.SS // ERROR "removed nil check" |
| 248 | return &s.x // ERROR "generated nil check" |
| 249 | } |
Cherry Zhang | 4d07d3e | 2016-09-28 10:20:24 -0400 | [diff] [blame] | 250 | |
| 251 | // make sure not to do nil check for newobject |
| 252 | func f7() (*Struct, float64) { |
| 253 | t := new(Struct) |
| 254 | p := &t.Y // ERROR "removed nil check" |
| 255 | return t, *p // ERROR "removed nil check" |
| 256 | } |
Cherry Zhang | fddc004 | 2016-11-21 11:31:39 -0500 | [diff] [blame] | 257 | |
| 258 | // make sure to remove nil check for memory move (issue #18003) |
| 259 | func f8(t *[8]int) [8]int { |
| 260 | return *t // ERROR "removed nil check" |
| 261 | } |
Keith Randall | f1517ec | 2017-08-24 15:23:27 -0700 | [diff] [blame] | 262 | |
| 263 | func f9() []int { |
| 264 | x := new([1]int) |
| 265 | x[0] = 1 // ERROR "removed nil check" |
| 266 | y := x[:] // ERROR "removed nil check" |
| 267 | return y |
| 268 | } |