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
+56-4Lines changed: 56 additions & 4 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,23 +21,52 @@ 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{{unwrap condition requires a valid identifier}} expected-error{{pattern matching in a condition requires the 'case' keyword}}
46
+
ifvarnonOptional.property {} // expected-error{{unwrap condition requires a valid identifier}} 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
51
+
letoptional:String?=nil
52
+
if case let optional?{ _ = optional } // expected-error{{variable binding in a condition requires an initializer}}
53
+
if case let.some(optional){ _ = optional } // expected-error{{variable binding in a condition requires an initializer}}
54
+
if case .some(let optional){ _ = optional } // expected-error{{variable binding in a condition requires an initializer}}
55
+
32
56
if case let x?=nonOptionalStruct(){ _ = x } // expected-error{{'?' pattern cannot match values of type 'NonOptionalStruct'}}
33
57
if case let x?=nonOptionalEnum(){ _ = x } // expected-error{{'?' pattern cannot match values of type 'NonOptionalEnum'}}
34
58
59
+
iflet x { _ = x } // expected-error{{cannot find 'x' in scope}}
60
+
61
+
iflet optional:String{ _ = optional }
62
+
iflet optional:Int{ _ = optional } // expected-error{{cannot convert value of type 'String?' to specified type 'Int?'}}
63
+
35
64
classB{} // expected-note * {{did you mean 'B'?}}
36
65
classD:B{}// expected-note * {{did you mean 'D'?}}
37
66
38
67
// TODO poor recovery in these cases
39
-
iflet{} // expected-error {{expected '{' after 'if' condition}} expected-error {{pattern matching in a condition requires the 'case' keyword}}
40
-
iflet x ={} // expected-error{{'{' after 'if'}} expected-error {{variable binding in a condition requires an initializer}} expected-error{{initializer for conditional binding must have Optional type, not '() -> ()'}}
68
+
iflet{} // expected-error {{expected '{' after 'if' condition}} expected-error {{pattern matching in a condition requires the 'case' keyword}} expected-error {{unwrap condition requires a valid identifier}}
69
+
iflet x ={} // expected-error{{'{' after 'if'}} expected-error{{initializer for conditional binding must have Optional type, not '() -> ()'}}
41
70
// expected-warning@-1{{value 'x' was defined but never used}}
42
71
43
72
if let x =foo(){
@@ -59,6 +88,16 @@ if let x = foo() {
59
88
60
89
var opt: Int?=.none
61
90
91
+
iflet opt {
92
+
use(opt)
93
+
opt =10 // expected-error{{cannot assign to value: 'opt' is a 'let' constant}}
94
+
}
95
+
96
+
ifvar opt {
97
+
use(opt)
98
+
opt =10
99
+
}
100
+
62
101
iflet x =opt{} // expected-warning {{value 'x' was defined but never used; consider replacing with boolean test}} {{4-12=}} {{15-15= != nil}}
63
102
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