David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 1 | // errorcheck -0 -d=nil |
| 2 | // Fails on ppc64x because of incomplete optimization. |
| 3 | // See issues 9058. |
| 4 | // +build !ppc64,!ppc64le,amd64 |
| 5 | |
| 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. |
| 9 | |
| 10 | // Test that nil checks are removed. |
| 11 | // Optimization is enabled. |
| 12 | |
| 13 | package p |
| 14 | |
| 15 | type Struct struct { |
| 16 | X int |
| 17 | Y float64 |
| 18 | } |
| 19 | |
| 20 | type BigStruct struct { |
| 21 | X int |
| 22 | Y float64 |
| 23 | A [1 << 20]int |
| 24 | Z string |
| 25 | } |
| 26 | |
| 27 | type Empty struct { |
| 28 | } |
| 29 | |
| 30 | type Empty1 struct { |
| 31 | Empty |
| 32 | } |
| 33 | |
| 34 | var ( |
| 35 | intp *int |
| 36 | arrayp *[10]int |
| 37 | array0p *[0]int |
| 38 | bigarrayp *[1 << 26]int |
| 39 | structp *Struct |
| 40 | bigstructp *BigStruct |
| 41 | emptyp *Empty |
| 42 | empty1p *Empty1 |
| 43 | ) |
| 44 | |
| 45 | func f1() { |
| 46 | _ = *intp // ERROR "generated nil check" |
| 47 | |
| 48 | // This one should be removed but the block copy needs |
| 49 | // to be turned into its own pseudo-op in order to see |
| 50 | // the indirect. |
| 51 | _ = *arrayp // ERROR "generated nil check" |
| 52 | |
| 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" |
| 57 | |
| 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" |
| 63 | } |
| 64 | |
| 65 | func f2() { |
| 66 | var ( |
| 67 | intp *int |
| 68 | arrayp *[10]int |
| 69 | array0p *[0]int |
| 70 | bigarrayp *[1 << 20]int |
| 71 | structp *Struct |
| 72 | bigstructp *BigStruct |
| 73 | emptyp *Empty |
| 74 | empty1p *Empty1 |
| 75 | ) |
| 76 | |
| 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" |
| 89 | } |
| 90 | |
| 91 | func fx10k() *[10000]int |
| 92 | |
| 93 | var b bool |
| 94 | |
| 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" |
| 99 | |
| 100 | for { |
| 101 | if x[9999] != 0 { // ERROR "removed nil check" |
| 102 | break |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | x = fx10k() |
| 107 | _ = x[9999] // ERROR "generated nil check" |
| 108 | if b { |
| 109 | _ = x[9999] // ERROR "removed.* nil check" |
| 110 | } else { |
| 111 | _ = x[9999] // ERROR "removed.* nil check" |
| 112 | } |
| 113 | _ = x[9999] // ERROR "removed nil check" |
| 114 | |
| 115 | x = fx10k() |
| 116 | if b { |
| 117 | _ = x[9999] // ERROR "generated nil check" |
| 118 | } else { |
| 119 | _ = x[9999] // ERROR "generated nil check" |
| 120 | } |
| 121 | _ = x[9999] // ERROR "generated nil check" |
| 122 | |
| 123 | fx10k() |
| 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 |
| 127 | // matter enough. |
| 128 | _ = x[9999] // ERROR "removed nil check" |
| 129 | } |
| 130 | |
| 131 | func f3a() { |
| 132 | x := fx10k() |
| 133 | y := fx10k() |
| 134 | z := fx10k() |
| 135 | _ = &x[9] // ERROR "generated nil check" |
| 136 | y = z |
| 137 | _ = &x[9] // ERROR "removed.* nil check" |
| 138 | x = y |
| 139 | _ = &x[9] // ERROR "generated nil check" |
| 140 | } |
| 141 | |
| 142 | func f3b() { |
| 143 | x := fx10k() |
| 144 | y := fx10k() |
| 145 | _ = &x[9] // ERROR "generated nil check" |
| 146 | y = x |
| 147 | _ = &x[9] // ERROR "removed.* nil check" |
| 148 | x = y |
| 149 | _ = &x[9] // ERROR "removed.* nil check" |
| 150 | } |
| 151 | |
| 152 | func fx10() *[10]int |
| 153 | |
| 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. |
| 158 | |
Keith Randall | 3c26c0d | 2016-01-21 13:27:01 -0800 | [diff] [blame] | 159 | _ = x[9] // ERROR "removed nil check" |
David Chase | 729abfa | 2015-10-26 17:34:06 -0400 | [diff] [blame] | 160 | |
| 161 | for { |
| 162 | if x[9] != 0 { // ERROR "removed nil check" |
| 163 | break |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | x = fx10() |
| 168 | _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect |
| 169 | if b { |
| 170 | _ = x[9] // ERROR "removed nil check" |
| 171 | } else { |
| 172 | _ = x[9] // ERROR "removed nil check" |
| 173 | } |
| 174 | _ = x[9] // ERROR "removed nil check" |
| 175 | |
| 176 | x = fx10() |
| 177 | if b { |
| 178 | _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect |
| 179 | } else { |
| 180 | _ = &x[9] // ERROR "generated nil check" |
| 181 | } |
| 182 | _ = x[9] // ERROR "generated nil check" // bug would like to remove before indirect |
| 183 | |
| 184 | fx10() |
| 185 | _ = x[9] // ERROR "removed nil check" |
| 186 | |
| 187 | x = fx10() |
| 188 | y := fx10() |
| 189 | _ = &x[9] // ERROR "generated nil check" |
| 190 | y = x |
| 191 | _ = &x[9] // ERROR "removed[a-z ]* nil check" |
| 192 | x = y |
| 193 | _ = &x[9] // ERROR "removed[a-z ]* nil check" |
| 194 | } |
Keith Randall | 4a346e7 | 2016-02-25 13:45:22 -0800 | [diff] [blame^] | 195 | |
| 196 | func f5(p *float32, q *float64, r *float32, s *float64) float64 { |
| 197 | x := float64(*p) // ERROR "removed nil check" |
| 198 | y := *q // ERROR "removed nil check" |
| 199 | *r = 7 // ERROR "removed nil check" |
| 200 | *s = 9 // ERROR "removed nil check" |
| 201 | return x + y |
| 202 | } |
| 203 | |
| 204 | type T [29]byte |
| 205 | |
| 206 | func f6(p, q *T) { |
| 207 | x := *p // ERROR "removed nil check" |
| 208 | *q = x // ERROR "removed nil check" |
| 209 | } |