1// errorcheck -0 -d=nil 2 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. 6 7// Test that nil checks are removed. 8// Optimization is enabled. 9 10package p 11 12type Struct struct { 13 X int 14 Y float64 15} 16 17type BigStruct struct { 18 X int 19 Y float64 20 A [1<<20]int 21 Z string 22} 23 24type Empty struct { 25} 26 27type Empty1 struct { 28 Empty 29} 30 31var ( 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 42func f1() { 43 _ = *intp // ERROR "generated nil check" 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. 48 _ = *arrayp // ERROR "generated nil check" 49 50 // 0-byte indirect doesn't suffice 51 _ = *array0p // ERROR "generated nil check" 52 _ = *array0p // ERROR "removed repeated nil check" 386 53 54 _ = *intp // ERROR "removed repeated nil check" 55 _ = *arrayp // ERROR "removed repeated nil check" 56 _ = *structp // ERROR "generated nil check" 57 _ = *emptyp // ERROR "generated nil check" 58 _ = *arrayp // ERROR "removed repeated nil check" 59} 60 61func f2() { 62 var ( 63 intp *int 64 arrayp *[10]int 65 array0p *[0]int 66 bigarrayp *[1<<20]int 67 structp *Struct 68 bigstructp *BigStruct 69 emptyp *Empty 70 empty1p *Empty1 71 ) 72 73 _ = *intp // ERROR "generated nil check" 74 _ = *arrayp // ERROR "generated nil check" 75 _ = *array0p // ERROR "generated nil check" 76 _ = *array0p // ERROR "removed repeated nil check" 77 _ = *intp // ERROR "removed repeated nil check" 78 _ = *arrayp // ERROR "removed repeated nil check" 79 _ = *structp // ERROR "generated nil check" 80 _ = *emptyp // ERROR "generated nil check" 81 _ = *arrayp // ERROR "removed repeated nil check" 82 _ = *bigarrayp // ERROR "generated nil check" ARM removed nil check before indirect!! 83 _ = *bigstructp // ERROR "generated nil check" 84 _ = *empty1p // ERROR "generated nil check" 85} 86 87func fx10k() *[10000]int 88var b bool 89 90 91func f3(x *[10000]int) { 92 // Using a huge type and huge offsets so the compiler 93 // does not expect the memory hardware to fault. 94 _ = x[9999] // ERROR "generated nil check" 95 96 for { 97 if x[9999] != 0 { // ERROR "generated nil check" 98 break 99 } 100 } 101 102 x = fx10k() 103 _ = x[9999] // ERROR "generated nil check" 104 if b { 105 _ = x[9999] // ERROR "removed repeated nil check" 106 } else { 107 _ = x[9999] // ERROR "removed repeated nil check" 108 } 109 _ = x[9999] // ERROR "generated nil check" 110 111 x = fx10k() 112 if b { 113 _ = x[9999] // ERROR "generated nil check" 114 } else { 115 _ = x[9999] // ERROR "generated nil check" 116 } 117 _ = x[9999] // ERROR "generated nil check" 118 119 fx10k() 120 // This one is a bit redundant, if we figured out that 121 // x wasn't going to change across the function call. 122 // But it's a little complex to do and in practice doesn't 123 // matter enough. 124 _ = x[9999] // ERROR "generated nil check" 125} 126 127func f3a() { 128 x := fx10k() 129 y := fx10k() 130 z := fx10k() 131 _ = &x[9] // ERROR "generated nil check" 132 y = z 133 _ = &x[9] // ERROR "removed repeated nil check" 134 x = y 135 _ = &x[9] // ERROR "generated nil check" 136} 137 138func f3b() { 139 x := fx10k() 140 y := fx10k() 141 _ = &x[9] // ERROR "generated nil check" 142 y = x 143 _ = &x[9] // ERROR "removed repeated nil check" 144 x = y 145 _ = &x[9] // ERROR "removed repeated nil check" 146} 147 148func fx10() *[10]int 149 150func f4(x *[10]int) { 151 // Most of these have no checks because a real memory reference follows, 152 // and the offset is small enough that if x is nil, the address will still be 153 // in the first unmapped page of memory. 154 155 _ = x[9] // ERROR "removed nil check before indirect" 156 157 for { 158 if x[9] != 0 { // ERROR "removed nil check before indirect" 159 break 160 } 161 } 162 163 x = fx10() 164 _ = x[9] // ERROR "removed nil check before indirect" 165 if b { 166 _ = x[9] // ERROR "removed nil check before indirect" 167 } else { 168 _ = x[9] // ERROR "removed nil check before indirect" 169 } 170 _ = x[9] // ERROR "removed nil check before indirect" 171 172 x = fx10() 173 if b { 174 _ = x[9] // ERROR "removed nil check before indirect" 175 } else { 176 _ = &x[9] // ERROR "generated nil check" 177 } 178 _ = x[9] // ERROR "removed nil check before indirect" 179 180 fx10() 181 _ = x[9] // ERROR "removed nil check before indirect" 182 183 x = fx10() 184 y := fx10() 185 _ = &x[9] // ERROR "generated nil check" 186 y = x 187 _ = &x[9] // ERROR "removed repeated nil check" 188 x = y 189 _ = &x[9] // ERROR "removed repeated nil check" 190} 191 192