Update to copy of current master Go testsuite.
[official-gcc.git] / gcc / testsuite / go.test / test / nilcheck.go
blobfe05d05c9252904a25be7c8686cb8f514d36980f
1 // errorcheck -0 -N -d=nil
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.
7 // Test that nil checks are inserted.
8 // Optimization is disabled, so redundant checks are not removed.
10 package p
12 type Struct struct {
13 X int
14 Y float64
17 type BigStruct struct {
18 X int
19 Y float64
20 A [1<<20]int
21 Z string
24 type Empty struct {
27 type Empty1 struct {
28 Empty
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
42 func f1() {
43 _ = *intp // ERROR "nil check"
44 _ = *arrayp // ERROR "nil check"
45 _ = *array0p // ERROR "nil check"
46 _ = *array0p // ERROR "nil check"
47 _ = *intp // ERROR "nil check"
48 _ = *arrayp // ERROR "nil check"
49 _ = *structp // ERROR "nil check"
50 _ = *emptyp // ERROR "nil check"
51 _ = *arrayp // ERROR "nil check"
54 func f2() {
55 var (
56 intp *int
57 arrayp *[10]int
58 array0p *[0]int
59 bigarrayp *[1<<20]int
60 structp *Struct
61 bigstructp *BigStruct
62 emptyp *Empty
63 empty1p *Empty1
66 _ = *intp // ERROR "nil check"
67 _ = *arrayp // ERROR "nil check"
68 _ = *array0p // ERROR "nil check"
69 _ = *array0p // ERROR "nil check"
70 _ = *intp // ERROR "nil check"
71 _ = *arrayp // ERROR "nil check"
72 _ = *structp // ERROR "nil check"
73 _ = *emptyp // ERROR "nil check"
74 _ = *arrayp // ERROR "nil check"
75 _ = *bigarrayp // ERROR "nil check"
76 _ = *bigstructp // ERROR "nil check"
77 _ = *empty1p // ERROR "nil check"
80 func fx10k() *[10000]int
81 var b bool
84 func f3(x *[10000]int) {
85 // Using a huge type and huge offsets so the compiler
86 // does not expect the memory hardware to fault.
87 _ = x[9999] // ERROR "nil check"
89 for {
90 if x[9999] != 0 { // ERROR "nil check"
91 break
95 x = fx10k()
96 _ = x[9999] // ERROR "nil check"
97 if b {
98 _ = x[9999] // ERROR "nil check"
99 } else {
100 _ = x[9999] // ERROR "nil check"
102 _ = x[9999] // ERROR "nil check"
104 x = fx10k()
105 if b {
106 _ = x[9999] // ERROR "nil check"
107 } else {
108 _ = x[9999] // ERROR "nil check"
110 _ = x[9999] // ERROR "nil check"
112 fx10k()
113 // This one is a bit redundant, if we figured out that
114 // x wasn't going to change across the function call.
115 // But it's a little complex to do and in practice doesn't
116 // matter enough.
117 _ = x[9999] // ERROR "nil check"
120 func f3a() {
121 x := fx10k()
122 y := fx10k()
123 z := fx10k()
124 _ = &x[9] // ERROR "nil check"
125 y = z
126 _ = &x[9] // ERROR "nil check"
127 x = y
128 _ = &x[9] // ERROR "nil check"
131 func f3b() {
132 x := fx10k()
133 y := fx10k()
134 _ = &x[9] // ERROR "nil check"
135 y = x
136 _ = &x[9] // ERROR "nil check"
137 x = y
138 _ = &x[9] // ERROR "nil check"
141 func fx10() *[10]int
143 func f4(x *[10]int) {
144 // Most of these have no checks because a real memory reference follows,
145 // and the offset is small enough that if x is nil, the address will still be
146 // in the first unmapped page of memory.
148 _ = x[9] // ERROR "nil check"
150 for {
151 if x[9] != 0 { // ERROR "nil check"
152 break
156 x = fx10()
157 _ = x[9] // ERROR "nil check"
158 if b {
159 _ = x[9] // ERROR "nil check"
160 } else {
161 _ = x[9] // ERROR "nil check"
163 _ = x[9] // ERROR "nil check"
165 x = fx10()
166 if b {
167 _ = x[9] // ERROR "nil check"
168 } else {
169 _ = &x[9] // ERROR "nil check"
171 _ = x[9] // ERROR "nil check"
173 fx10()
174 _ = x[9] // ERROR "nil check"
176 x = fx10()
177 y := fx10()
178 _ = &x[9] // ERROR "nil check"
179 y = x
180 _ = &x[9] // ERROR "nil check"
181 x = y
182 _ = &x[9] // ERROR "nil check"