You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: test/stmt/if_while_var.swift
+44-2Lines changed: 44 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
// RUN: %target-typecheck-verify-swift
2
2
3
-
structNonOptionalStruct{}
3
+
structNonOptionalStruct{letproperty:Any}
4
4
enumNonOptionalEnum{case foo }
5
5
6
6
func foo()->Int?{return.none }
@@ -21,11 +21,30 @@ if var x = foo() {
21
21
modify(&x)
22
22
}
23
23
24
+
iflet x { // expected-error{{cannot find 'x' in scope}}
25
+
use(x)
26
+
}
27
+
28
+
ifvar x { // expected-error{{cannot find 'x' in scope}}
29
+
use(x)
30
+
}
31
+
24
32
use(x) // expected-error{{cannot find 'x' in scope}}
25
33
34
+
letnonOptional=nonOptionalStruct()
35
+
26
36
iflet x =nonOptionalStruct(){ _ = x} // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
27
37
iflet x =nonOptionalEnum(){ _ = x} // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalEnum'}}
28
38
39
+
iflet nonOptional { _ = nonOptional } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
40
+
ifvar nonOptional { nonOptional =nonOptionalStruct(); _ = nonOptional } // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
41
+
42
+
guardlet nonOptional else{ _ = nonOptional; fatalError()} // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
43
+
guardvar nonOptional else{ _ = nonOptional; fatalError()} // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
44
+
45
+
ifletnonOptional.property {} // expected-error{{variable binding in a condition requires an initializer}} expected-error{{pattern matching in a condition requires the 'case' keyword}}
46
+
ifvarnonOptional.property {} // expected-error{{variable binding in a condition requires an initializer}} expected-error{{pattern matching in a condition requires the 'case' keyword}}
47
+
29
48
guardlet _ =nonOptionalStruct()else{fatalError()} // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalStruct'}}
30
49
guardlet _ =nonOptionalEnum()else{fatalError()} // expected-error{{initializer for conditional binding must have Optional type, not 'NonOptionalEnum'}}
31
50
@@ -59,6 +78,16 @@ if let x = foo() {
59
78
60
79
var opt: Int?=.none
61
80
81
+
iflet opt {
82
+
use(opt)
83
+
opt =10 // expected-error{{cannot assign to value: 'opt' is a 'let' constant}}
84
+
}
85
+
86
+
ifvar opt {
87
+
use(opt)
88
+
opt =10
89
+
}
90
+
62
91
iflet x =opt{} // expected-warning {{value 'x' was defined but never used; consider replacing with boolean test}} {{4-12=}} {{15-15= != nil}}
63
92
if var x =opt{} // expected-warning {{value 'x' was defined but never used; consider replacing with boolean test}} {{4-12=}} {{15-15= != nil}}
0 commit comments