Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 1 | // errorcheck -0 -d=nil |
Shenghou Ma | 63d72f6 | 2015-04-04 01:24:36 -0400 | [diff] [blame] | 2 | // Fails on ppc64x because of incomplete optimization. |
| 3 | // See issues 9058. |
Michael Munday | dae98d5 | 2016-03-20 19:35:59 -0400 | [diff] [blame] | 4 | // Same reason for mips64x and s390x. |
| 5 | // +build !ppc64,!ppc64le,!mips64,!mips64le,!amd64,!s390x |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 6 | |
| 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. |
| 10 | |
| 11 | // Test that nil checks are removed. |
| 12 | // Optimization is enabled. |
| 13 | |
| 14 | package p |
| 15 | |
| 16 | type Struct struct { |
| 17 | X int |
| 18 | Y float64 |
| 19 | } |
| 20 | |
| 21 | type BigStruct struct { |
| 22 | X int |
| 23 | Y float64 |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 24 | A [1 << 20]int |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 25 | Z string |
| 26 | } |
| 27 | |
| 28 | type Empty struct { |
| 29 | } |
| 30 | |
| 31 | type Empty1 struct { |
| 32 | Empty |
| 33 | } |
| 34 | |
| 35 | var ( |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 36 | intp *int |
| 37 | arrayp *[10]int |
| 38 | array0p *[0]int |
| 39 | bigarrayp *[1 << 26]int |
| 40 | structp *Struct |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 41 | bigstructp *BigStruct |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 42 | emptyp *Empty |
| 43 | empty1p *Empty1 |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 44 | ) |
| 45 | |
| 46 | func f1() { |
| 47 | _ = *intp // ERROR "generated nil check" |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 48 | |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 49 | // This one should be removed but the block copy needs |
| 50 | // to be turned into its own pseudo-op in order to see |
| 51 | // the indirect. |
| 52 | _ = *arrayp // ERROR "generated nil check" |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 53 | |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 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" |
| 58 | |
| 59 | _ = *intp // ERROR "generated nil check" |
| 60 | _ = *arrayp // ERROR "generated nil check" |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 61 | _ = *structp // ERROR "generated nil check" |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 62 | _ = *emptyp // ERROR "generated nil check" |
| 63 | _ = *arrayp // ERROR "generated nil check" |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | func f2() { |
| 67 | var ( |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 68 | intp *int |
| 69 | arrayp *[10]int |
| 70 | array0p *[0]int |
| 71 | bigarrayp *[1 << 20]int |
| 72 | structp *Struct |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 73 | bigstructp *BigStruct |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 74 | emptyp *Empty |
| 75 | empty1p *Empty1 |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 76 | ) |
| 77 | |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 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!! |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 88 | _ = *bigstructp // ERROR "generated nil check" |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 89 | _ = *empty1p // ERROR "generated nil check" |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | func fx10k() *[10000]int |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 93 | |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 94 | var b bool |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 95 | |
| 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" |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 100 | |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 101 | for { |
| 102 | if x[9999] != 0 { // ERROR "generated nil check" |
| 103 | break |
| 104 | } |
| 105 | } |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 106 | |
| 107 | x = fx10k() |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 108 | _ = x[9999] // ERROR "generated nil check" |
| 109 | if b { |
| 110 | _ = x[9999] // ERROR "removed repeated nil check" |
| 111 | } else { |
| 112 | _ = x[9999] // ERROR "removed repeated nil check" |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 113 | } |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 114 | _ = x[9999] // ERROR "generated nil check" |
| 115 | |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 116 | x = fx10k() |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 117 | if b { |
| 118 | _ = x[9999] // ERROR "generated nil check" |
| 119 | } else { |
| 120 | _ = x[9999] // ERROR "generated nil check" |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 121 | } |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 122 | _ = x[9999] // ERROR "generated nil check" |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 123 | |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 124 | fx10k() |
| 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 |
| 128 | // matter enough. |
| 129 | _ = x[9999] // ERROR "generated nil check" |
| 130 | } |
| 131 | |
| 132 | func f3a() { |
| 133 | x := fx10k() |
| 134 | y := fx10k() |
| 135 | z := fx10k() |
| 136 | _ = &x[9] // ERROR "generated nil check" |
| 137 | y = z |
| 138 | _ = &x[9] // ERROR "removed repeated nil check" |
| 139 | x = y |
| 140 | _ = &x[9] // ERROR "generated nil check" |
| 141 | } |
| 142 | |
| 143 | func f3b() { |
| 144 | x := fx10k() |
| 145 | y := fx10k() |
| 146 | _ = &x[9] // ERROR "generated nil check" |
| 147 | y = x |
| 148 | _ = &x[9] // ERROR "removed repeated nil check" |
| 149 | x = y |
| 150 | _ = &x[9] // ERROR "removed repeated nil check" |
| 151 | } |
| 152 | |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 153 | func fx10() *[10]int |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 154 | |
| 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. |
| 159 | |
| 160 | _ = x[9] // ERROR "removed nil check before indirect" |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 161 | |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 162 | for { |
| 163 | if x[9] != 0 { // ERROR "removed nil check before indirect" |
| 164 | break |
| 165 | } |
| 166 | } |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 167 | |
| 168 | x = fx10() |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 169 | _ = x[9] // ERROR "removed nil check before indirect" |
| 170 | if b { |
| 171 | _ = x[9] // ERROR "removed nil check before indirect" |
| 172 | } else { |
| 173 | _ = x[9] // ERROR "removed nil check before indirect" |
| 174 | } |
| 175 | _ = x[9] // ERROR "removed nil check before indirect" |
| 176 | |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 177 | x = fx10() |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 178 | if b { |
| 179 | _ = x[9] // ERROR "removed nil check before indirect" |
| 180 | } else { |
| 181 | _ = &x[9] // ERROR "generated nil check" |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 182 | } |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 183 | _ = x[9] // ERROR "removed nil check before indirect" |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 184 | |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 185 | fx10() |
| 186 | _ = x[9] // ERROR "removed nil check before indirect" |
Russ Cox | f5184d3 | 2014-05-15 15:34:53 -0400 | [diff] [blame] | 187 | |
Russ Cox | aa0439b | 2013-09-17 16:54:22 -0400 | [diff] [blame] | 188 | x = fx10() |
| 189 | y := fx10() |
| 190 | _ = &x[9] // ERROR "generated nil check" |
| 191 | y = x |
| 192 | _ = &x[9] // ERROR "removed repeated nil check" |
| 193 | x = y |
| 194 | _ = &x[9] // ERROR "removed repeated nil check" |
| 195 | } |