]> Cypherpunks.ru repositories - gostls13.git/blob - test/nilcheck.go
[dev.ssa] Merge remote-tracking branch 'origin/master' into mergebranch
[gostls13.git] / test / nilcheck.go
1 // +build !amd64
2 // errorcheck -0 -N -d=nil
3
4 // Copyright 2013 The Go Authors.  All rights reserved.
5 // Use of this source code is governed by a BSD-style
6 // license that can be found in the LICENSE file.
7
8 // Test that nil checks are inserted.
9 // Optimization is disabled, so redundant checks are not removed.
10
11 package p
12
13 type Struct struct {
14         X int
15         Y float64
16 }
17
18 type BigStruct struct {
19         X int
20         Y float64
21         A [1 << 20]int
22         Z string
23 }
24
25 type Empty struct {
26 }
27
28 type Empty1 struct {
29         Empty
30 }
31
32 var (
33         intp       *int
34         arrayp     *[10]int
35         array0p    *[0]int
36         bigarrayp  *[1 << 26]int
37         structp    *Struct
38         bigstructp *BigStruct
39         emptyp     *Empty
40         empty1p    *Empty1
41 )
42
43 func f1() {
44         _ = *intp    // ERROR "nil check"
45         _ = *arrayp  // ERROR "nil check"
46         _ = *array0p // ERROR "nil check"
47         _ = *array0p // ERROR "nil check"
48         _ = *intp    // ERROR "nil check"
49         _ = *arrayp  // ERROR "nil check"
50         _ = *structp // ERROR "nil check"
51         _ = *emptyp  // ERROR "nil check"
52         _ = *arrayp  // ERROR "nil check"
53 }
54
55 func f2() {
56         var (
57                 intp       *int
58                 arrayp     *[10]int
59                 array0p    *[0]int
60                 bigarrayp  *[1 << 20]int
61                 structp    *Struct
62                 bigstructp *BigStruct
63                 emptyp     *Empty
64                 empty1p    *Empty1
65         )
66
67         _ = *intp       // ERROR "nil check"
68         _ = *arrayp     // ERROR "nil check"
69         _ = *array0p    // ERROR "nil check"
70         _ = *array0p    // ERROR "nil check"
71         _ = *intp       // ERROR "nil check"
72         _ = *arrayp     // ERROR "nil check"
73         _ = *structp    // ERROR "nil check"
74         _ = *emptyp     // ERROR "nil check"
75         _ = *arrayp     // ERROR "nil check"
76         _ = *bigarrayp  // ERROR "nil check"
77         _ = *bigstructp // ERROR "nil check"
78         _ = *empty1p    // ERROR "nil check"
79 }
80
81 func fx10k() *[10000]int
82
83 var b bool
84
85 func f3(x *[10000]int) {
86         // Using a huge type and huge offsets so the compiler
87         // does not expect the memory hardware to fault.
88         _ = x[9999] // ERROR "nil check"
89
90         for {
91                 if x[9999] != 0 { // ERROR "nil check"
92                         break
93                 }
94         }
95
96         x = fx10k()
97         _ = x[9999] // ERROR "nil check"
98         if b {
99                 _ = x[9999] // ERROR "nil check"
100         } else {
101                 _ = x[9999] // ERROR "nil check"
102         }
103         _ = x[9999] // ERROR "nil check"
104
105         x = fx10k()
106         if b {
107                 _ = x[9999] // ERROR "nil check"
108         } else {
109                 _ = x[9999] // ERROR "nil check"
110         }
111         _ = x[9999] // ERROR "nil check"
112
113         fx10k()
114         // This one is a bit redundant, if we figured out that
115         // x wasn't going to change across the function call.
116         // But it's a little complex to do and in practice doesn't
117         // matter enough.
118         _ = x[9999] // ERROR "nil check"
119 }
120
121 func f3a() {
122         x := fx10k()
123         y := fx10k()
124         z := fx10k()
125         _ = &x[9] // ERROR "nil check"
126         y = z
127         _ = &x[9] // ERROR "nil check"
128         x = y
129         _ = &x[9] // ERROR "nil check"
130 }
131
132 func f3b() {
133         x := fx10k()
134         y := fx10k()
135         _ = &x[9] // ERROR "nil check"
136         y = x
137         _ = &x[9] // ERROR "nil check"
138         x = y
139         _ = &x[9] // ERROR "nil check"
140 }
141
142 func fx10() *[10]int
143
144 func f4(x *[10]int) {
145         // Most of these have no checks because a real memory reference follows,
146         // and the offset is small enough that if x is nil, the address will still be
147         // in the first unmapped page of memory.
148
149         _ = x[9] // ERROR "nil check"
150
151         for {
152                 if x[9] != 0 { // ERROR "nil check"
153                         break
154                 }
155         }
156
157         x = fx10()
158         _ = x[9] // ERROR "nil check"
159         if b {
160                 _ = x[9] // ERROR "nil check"
161         } else {
162                 _ = x[9] // ERROR "nil check"
163         }
164         _ = x[9] // ERROR "nil check"
165
166         x = fx10()
167         if b {
168                 _ = x[9] // ERROR "nil check"
169         } else {
170                 _ = &x[9] // ERROR "nil check"
171         }
172         _ = x[9] // ERROR "nil check"
173
174         fx10()
175         _ = x[9] // ERROR "nil check"
176
177         x = fx10()
178         y := fx10()
179         _ = &x[9] // ERROR "nil check"
180         y = x
181         _ = &x[9] // ERROR "nil check"
182         x = y
183         _ = &x[9] // ERROR "nil check"
184 }
185
186 func f5(m map[string]struct{}) bool {
187         // Existence-only map lookups should not generate a nil check
188         _, ok := m[""]
189         return ok
190 }