-
Notifications
You must be signed in to change notification settings - Fork 10.4k
/
Copy pathif_while_var.swift
308 lines (233 loc) · 11.2 KB
/
if_while_var.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// RUN: %target-typecheck-verify-swift
struct NonOptionalStruct { let property: Any }
enum NonOptionalEnum { case foo }
func foo() -> Int? { return .none }
func nonOptionalStruct() -> NonOptionalStruct { fatalError() }
func nonOptionalEnum() -> NonOptionalEnum { fatalError() }
func use(_ x: Int) {}
func modify(_ x: inout Int) {}
if let x = foo() {
use(x)
modify(&x) // expected-error{{cannot pass immutable value as inout argument: 'x' is a 'let' constant}}
}
use(x) // expected-error{{cannot find 'x' in scope}}
if var x = foo() {
use(x)
modify(&x)
}
if let x { // expected-error{{cannot find 'x' in scope}}
use(x)
}
if var x { // expected-error{{cannot find 'x' in scope}}
use(x)
}
use(x) // expected-error{{cannot find 'x' in scope}}
let nonOptional = nonOptionalStruct()
if let x = nonOptionalStruct() { _ = x} // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
if let x = nonOptionalEnum() { _ = x} // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalEnum'}}
if let nonOptional { _ = nonOptional } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
if var nonOptional { nonOptional = nonOptionalStruct(); _ = nonOptional } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
guard let nonOptional else { _ = nonOptional; fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
guard var nonOptional else { _ = nonOptional; fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
if let nonOptional.property { }
// expected-error@-1 {{unwrap condition requires a valid identifier}}
// expected-error@-2 {{initializer for conditional binding must have Optional type, not 'Any'}}
if var nonOptional.property { }
// expected-error@-1 {{unwrap condition requires a valid identifier}}
// expected-error@-2 {{initializer for conditional binding must have Optional type, not 'Any'}}
guard let _ = nonOptionalStruct() else { fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
guard let _ = nonOptionalEnum() else { fatalError() } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalEnum'}}
let optional: String? = nil
if case let optional? { _ = optional } // expected-error{{variable binding in a condition requires an initializer}}
if case let .some(optional) { _ = optional } // expected-error{{variable binding in a condition requires an initializer}}
if case .some(let optional) { _ = optional } // expected-error{{variable binding in a condition requires an initializer}}
if case let x? = nonOptionalStruct() { _ = x } // expected-error{{'?' pattern cannot match values of type 'NonOptionalStruct'}}
if case let x? = nonOptionalEnum() { _ = x } // expected-error{{'?' pattern cannot match values of type 'NonOptionalEnum'}}
if let x { _ = x } // expected-error{{cannot find 'x' in scope}}
if let optional: String { _ = optional }
if let optional: Int { _ = optional } // expected-error{{cannot convert value of type 'String?' to specified type 'Int?'}}
class B {} // expected-note * {{did you mean 'B'?}}
class D : B {}// expected-note * {{did you mean 'D'?}}
// TODO poor recovery in these cases
if let {}
// expected-error@-1 {{expected '{' after 'if' condition}}
// expected-error@-2 {{unwrap condition requires a valid identifier}}
// expected-error@-3 {{initializer for conditional binding must have Optional type, not '() -> ()'}}
if let x = { } // expected-error{{'{' after 'if'}} expected-error{{initializer for conditional binding must have Optional type, not '() -> ()'}}
// expected-warning@-1{{value 'x' was defined but never used}}
if let x = foo() {
_ = x
} else {
// TODO: more contextual error? "x is only available on the true branch"?
use(x) // expected-error{{cannot find 'x' in scope}}
}
if let x = foo() {
use(x)
} else if let y = foo() { // expected-note {{did you mean 'y'?}}
use(x) // expected-error{{cannot find 'x' in scope}}
use(y)
} else {
use(x) // expected-error{{cannot find 'x' in scope}}
use(y) // expected-error{{cannot find 'y' in scope}}
}
var opt: Int? = .none
if let opt {
use(opt)
opt = 10 // expected-error{{cannot assign to value: 'opt' is a 'let' constant}}
}
if var opt {
use(opt)
opt = 10
}
if let x = opt {} // expected-warning {{value 'x' was defined but never used; consider replacing with boolean test}} {{4-12=}} {{15-15= != nil}}
if var x = opt {} // expected-warning {{value 'x' was defined but never used; consider replacing with boolean test}} {{4-12=}} {{15-15= != nil}}
// <rdar://problem/20800015> Fix error message for invalid if-let
let someInteger = 1
if let y = someInteger { _ = y } // expected-error {{initializer for conditional binding must have Optional type, not 'Int'}}
if case let y? = someInteger { _ = y } // expected-error {{'?' pattern cannot match values of type 'Int'}}
// Test multiple clauses on "if let".
if let x = opt, let y = opt, x != y,
let a = opt, var b = opt { // expected-warning {{immutable value 'a' was never used; consider replacing with '_' or removing it}} expected-warning {{variable 'b' was never used; consider replacing with '_' or removing it}}
}
// Leading boolean conditional.
if 1 != 2, let x = opt,
y = opt, // expected-error {{expected 'let' in conditional}} {{4-4=let }}
x != y,
let a = opt, var b = opt { // expected-warning {{immutable value 'a' was never used; consider replacing with '_' or removing it}} expected-warning {{variable 'b' was never used; consider replacing with '_' or removing it}}
}
// <rdar://problem/20457938> typed pattern is not allowed on if/let condition
if 1 != 2, let x : Int? = opt {} // expected-warning {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
// expected-warning @-1 {{explicitly specified type 'Int?' adds an additional level of optional to the initializer, making the optional check always succeed}} {{20-24=Int}}
if 1 != 2, case let x? : Int? = 42 {} // expected-warning {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
// expected-warning @-1 {{non-optional expression of type 'Int' used in a check for optionals}}
// Test error recovery.
// <rdar://problem/19939746> Improve error recovery for malformed if statements
if 1 != 2, { // expected-error {{cannot convert value of type '() -> ()' to expected condition type 'Bool'}}
} // expected-error {{expected '{' after 'if' condition}}
if 1 != 2, 4 == 57 {}
if 1 != 2, 4 == 57, let x = opt {} // expected-warning {{immutable value 'x' was never used; consider replacing with '_' or removing it}}
// Test that these don't cause the parser to crash.
if true { if a == 0; {} } // expected-error {{cannot find 'a' in scope}} expected-error {{expected '{' after 'if' condition}}
if a == 0, where b == 0 {} // expected-error {{cannot find 'a' in scope}} expected-error {{expected expression in conditional}}
func testIfCase(_ a : Int?) {
if case nil = a, a != nil {}
if case let (b?) = a, b != 42 {}
if let case (b?) = a, b != 42 {} // expected-error {{pattern matching binding is spelled with 'case let', not 'let case'}} {{6-10=}} {{14-14= let}}
if a != nil, let c = a, case nil = a { _ = c}
if let p? = a {_ = p} // expected-error {{pattern matching in a condition implicitly unwraps optionals}} {{11-12=}}
if let .some(x) = a {_ = x} // expected-error {{pattern matching in a condition requires the 'case' keyword}} {{6-6=case }}
if case _ = a {} // expected-warning {{'if' condition is always true}}
while case _ = a {} // expected-warning {{'while' condition is always true}}
}
// <rdar://problem/20883147> Type annotation for 'let' condition still expected to be optional
func testTypeAnnotations() {
if let x: Int = Optional(1) {_ = x}
if let x: Int = .some(1) {_ = x}
if case _ : Int8 = 19 {} // expected-warning {{'if' condition is always true}}
}
func testShadowing(_ a: Int?, b: Int?, c: Int?, d: Int?) {
guard let a = a, let b = a > 0 ? b : nil else { return }
_ = b
if let c = c, let d = c > 0 ? d : nil {
_ = d
}
}
func testShadowingWithShorthand(_ a: Int?, b: Int?, c: Int?, d: Int?) {
guard let a, let b = a > 0 ? b : nil else { return }
_ = b
if let c, let d = c > 0 ? d : nil {
_ = d
}
}
func useInt(_ x: Int) {}
func testWhileScoping(_ a: Int?) {
while let x = a { }
useInt(x) // expected-error{{cannot find 'x' in scope}}
}
func testWhileShorthand(_ b: Int?) {
while let b { _ = b }
}
// Matching a case with a single, labeled associated value.
public enum SomeParseResult<T> {
case error(length: Int)
case repeated(value: String, repetitions: Int)
// expected-note@-1{{'repeated(value:repetitions:)' declared here}}
var _error: Int? {
if case .error(let result) = self { return result }
return nil
}
var _error2: Int? {
if case .error(length: let result) = self { return result }
return nil
}
var _error3: Int? {
if case .error(wrong: let result) = self { return result } // expected-error{{tuple pattern element label 'wrong' must be 'length'}}
return nil
}
var _repeated: (String, Int)? {
if case .repeated(let value, let repetitions) = self {
return (value, repetitions)
}
return nil
}
var _repeated2: (String, Int)? {
if case .repeated(value: let value, let repetitions) = self {
return (value, repetitions)
}
return nil
}
var _repeated3: (String, Int)? {
if case .repeated(let value, repetitions: let repetitions) = self {
return (value, repetitions)
}
return nil
}
var _repeated4: (String, Int)? {
if case .repeated(value: let value, repetitions: let repetitions) = self {
return (value, repetitions)
}
return nil
}
var _repeated5: (String, Int)? {
if case .repeated(value: let value, wrong: let repetitions) = self { // expected-error{{tuple pattern element label 'wrong' must be 'repetitions'}}
return (value, repetitions)
}
return nil
}
}
func matchImplicitTupling(pr: SomeParseResult<Int>) {
if case .repeated(let x) = pr { // expected-warning{{enum case 'repeated' has 2 associated values; matching them as a tuple is deprecated}}
let y: Int = x // expected-error{{cannot convert value of type '(value: String, repetitions: Int)' to specified type 'Int'}}
}
}
// Cope with an ambiguity between a case name and a static member. Prefer the
// case.
enum CaseStaticAmbiguity {
case C(Bool)
var isC: Bool {
if case .C = self { return true }
return false
}
static func C(_: Int) -> CaseStaticAmbiguity { return .C(true) }
}
// Case name/static member ambiguity along with implicit optional unwrapping.
enum HasPayload {
case payload(Int, Bool)
static func payload(_ bool: Bool, int: Int) -> HasPayload {
.payload(int, bool)
}
}
class UsesPayload {
private var eOpt: HasPayload? = nil
deinit {
if case .payload(_, let x) = eOpt {
_ = x
}
}
}
// https://github.com/apple/swift/issues/55698
do {
let a = 1
let b = Int?.none
if let c = b ?? a { _ = c } // expected-error {{initializer for conditional binding must have Optional type, not 'Int'}}
}