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/expr/cast/as_coerce.swift
+180-1Lines changed: 180 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -81,7 +81,7 @@ c3 as C4 // expected-error {{'C3' is not convertible to 'C4'; did you mean to us
81
81
// <rdar://problem/19495142> Various incorrect diagnostics for explicit type conversions
82
82
1asDoubleasFloat // expected-error{{cannot convert value of type 'Double' to type 'Float' in coercion}}
83
83
1asIntasString // expected-error{{cannot convert value of type 'Int' to type 'String' in coercion}}
84
-
Double(1)asDoubleasString // expected-error{{cannot convert value of type 'Double' to type 'String' in coercion}}
84
+
Double(1)asDoubleasString // expected-error{{cannot convert value of type 'Double' to type 'String' in coercion}} expected-warning {{redundant cast to 'Double' has no effect}} {{11-21=}}
85
85
["awd"]as[Int] // expected-error{{cannot convert value of type 'String' to expected element type 'Int'}}
86
86
([1,2,1.0],1)as([String],Int)
87
87
// expected-error@-1 2 {{cannot convert value of type 'Int' to expected element type 'String'}}
@@ -135,3 +135,182 @@ _ = sr6022 as! AnyObject // expected-warning {{forced cast from '() -> Any' to '
135
135
_ = sr6022 as?AnyObject // expected-warning {{conditional cast from '() -> Any' to 'AnyObject' always succeeds}}
136
136
_ = sr6022_1 as!Any // expected-warning {{forced cast from '() -> ()' to 'Any' always succeeds; did you mean to use 'as'?}}
137
137
_ = sr6022_1 as?Any // expected-warning {{conditional cast from '() -> ()' to 'Any' always succeeds}}
138
+
139
+
// SR-11295
140
+
letsr11295a="Hello"
141
+
_ = sr11295a asString // expected-warning {{redundant cast to 'String' has no effect}} {{14-24=}}
142
+
143
+
letsr11295b=1
144
+
_ = sr11295b asInt // expected-warning {{redundant cast to 'Int' has no effect}} {{14-21=}}
145
+
146
+
typealiasType=String
147
+
148
+
letsr11295c:Type="Hello Typealias"
149
+
_ = sr11295c asString // expected-warning {{redundant cast to 'String' has no effect}} {{14-24=}}
150
+
151
+
letsr11295d="Hello Typealias"
152
+
_ = sr11295d asType // expected-warning {{redundant cast to 'Type' (aka 'String') has no effect}} {{14-22=}}
153
+
154
+
_ ="Hello"asString // Ok
155
+
_ =1asInt64 // Ok
156
+
_ =[]asSet<Int> // Ok
157
+
158
+
classSR11295A{}
159
+
classSR11295B:SR11295A{}
160
+
161
+
varsr11295ap=SR11295A()
162
+
varsr11295bc=SR11295B()
163
+
164
+
_ = sr11295bc asSR11295A // Ok
165
+
166
+
_ =1asDoubleasDouble // expected-warning {{redundant cast to 'Double' has no effect}} {{17-27=}}
167
+
_ =Double(1)asDouble // expected-warning {{redundant cast to 'Double' has no effect}} {{15-25=}}
168
+
_ =Int(1)asInt // expected-warning {{redundant cast to 'Int' has no effect}} {{12-19=}}
169
+
170
+
typealiasDouble1=Double
171
+
typealiasDouble2=Double
172
+
173
+
letsr11295ta1:Double1=1.0
174
+
_ = sr11295ta1 asDouble2 // expected-warning {{redundant cast from 'Double1' (aka 'Double') to 'Double2' (aka 'Double') has no effect}} {{16-27=}}
175
+
_ = sr11295ta1 asDouble1 // expected-warning {{redundant cast to 'Double1' (aka 'Double') has no effect}} {{16-27=}}
176
+
177
+
func ff11295_0<T>(_ t:T)->T{return t }
178
+
func ff11295_1(_ i:Int)->Int{return i }
179
+
180
+
// Function call expressions
181
+
_ =ff11295_0(1asInt)asInt // expected-warning {{redundant cast to 'Int' has no effect}} {{25-32=}}
182
+
_ =ff11295_1(1)asInt // expected-warning {{redundant cast to 'Int' has no effect}} {{18-25=}}
183
+
184
+
func ff11295_3(i:Int){
185
+
leta:[Int]=[]
186
+
_ = a.count -((i +1)asInt) // Ok
187
+
}
188
+
189
+
structSR11295C{
190
+
vari:Int
191
+
}
192
+
structSR11295D<T>{
193
+
vart:T
194
+
195
+
func f()->T{return t }
196
+
}
197
+
enumSR11295_E:Int{
198
+
case a
199
+
}
200
+
201
+
// Coerce members
202
+
_ =SR11295C(i:1).i asInt // expected-warning {{redundant cast to 'Int' has no effect}} {{22-29=}}
203
+
_ =SR11295D<Int>(t:1).t asInt // expected-warning {{redundant cast to 'Int' has no effect}} {{27-34=}}
204
+
_ =SR11295D(t:1asInt).t asInt // expected-warning {{redundant cast to 'Int' has no effect}} {{29-36=}}
205
+
_ =SR11295D(t:1).t asInt // Ok
206
+
_ =SR11295D(t:1).t asUInt // Ok
207
+
_ =SR11295_E.a.rawValue asInt // expected-warning {{redundant cast to 'Int' has no effect}} {{26-33=}}
208
+
_ =SR11295D(t:1asInt).f()asInt // expected-warning {{redundant cast to 'Int' has no effect}} {{31-38=}}
// We don't need coercion here because we get the right selector
56
56
// from the static method.
57
57
_ ="staticOrNonStatic:"asSelector // expected-warning{{use of string literal for Objective-C selectors is deprecated; use '#selector' instead}}{{7-39=#selector(Bar.staticOrNonStatic(_:))}}
58
-
59
58
// We need coercion here because we asked for a selector from an
60
59
// instance method with the same name as (but a different selector
Copy file name to clipboardExpand all lines: test/type/subclass_composition.swift
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -126,8 +126,8 @@ func basicSubtyping(
126
126
let _:Derived&AnyObject= derived
127
127
128
128
let _ = base asBase<Int>&P1
129
-
let _ = base asBase<Int>&AnyObject
130
-
let _ = derived asDerived&AnyObject
129
+
let _ = base asBase<Int>&AnyObject // expected-warning {{redundant cast to 'Base<Int>' has no effect}} {{16-41=}}
130
+
let _ = derived asDerived&AnyObject // expected-warning {{redundant cast to 'Derived' has no effect}} {{19-42=}}
131
131
132
132
let _ = base as?Base<Int>&P1 // expected-warning {{always succeeds}}
133
133
let _ = base as?Base<Int>&AnyObject // expected-warning {{always succeeds}}
@@ -170,7 +170,7 @@ func basicSubtyping(
170
170
let _ = baseAndP1 asBase<Int>
171
171
let _ = derivedAndP3 asBase<Int>
172
172
let _ = derivedAndP2 asDerived
173
-
let _ = derivedAndAnyObject asDerived
173
+
let _ = derivedAndAnyObject asDerived // expected-warning {{redundant cast to 'Derived' has no effect}} {{31-42=}}
174
174
175
175
let _ = baseAndP1 as?Base<Int> // expected-warning {{always succeeds}}
176
176
let _ = derivedAndP3 as?Base<Int> // expected-warning {{always succeeds}}
@@ -343,8 +343,8 @@ func metatypeSubtyping(
343
343
344
344
let _ = baseIntAndP2 asBase<Int>.Type
345
345
let _ = baseIntAndP2AndAnyObject asBase<Int>.Type
346
-
let _ = derivedAndAnyObject asDerived.Type
347
-
let _ = baseIntAndP2AndAnyObject asBaseAndP2<Int>.Type
346
+
let _ = derivedAndAnyObject asDerived.Type // expected-warning {{redundant cast to 'Derived.Type' has no effect}} {{31-47=}}
347
+
let _ = baseIntAndP2AndAnyObject asBaseAndP2<Int>.Type // expected-warning {{redundant cast to 'BaseAndP2<Int>.Type' (aka '(Base<Int> & P2).Type') has no effect}} {{36-59=}}
348
348
349
349
let _ = baseIntAndP2 as?Base<Int>.Type // expected-warning {{always succeeds}}
350
350
let _ = baseIntAndP2AndAnyObject as?Base<Int>.Type // expected-warning {{always succeeds}}
0 commit comments