@@ -139,11 +139,12 @@ enum PE: P {}
139
139
class PC : P { }
140
140
class PCSub : PC { }
141
141
142
+ // `is` checks
142
143
func nongenericAnyIsP( type: Any . Type ) -> Bool {
143
- return type is P . Type
144
+ return type is P . Protocol
144
145
}
145
146
func nongenericAnyIsPAndAnyObject( type: Any . Type ) -> Bool {
146
- return type is ( P & AnyObject ) . Type
147
+ return type is ( P & AnyObject ) . Protocol
147
148
}
148
149
func nongenericAnyIsPAndPCSub( type: Any . Type ) -> Bool {
149
150
return type is ( P & PCSub ) . Type
@@ -157,27 +158,104 @@ func genericAnyIs<T>(type: Any.Type, to: T.Type, expected: Bool) -> Bool {
157
158
return expected
158
159
}
159
160
}
161
+ // `as?` checks
162
+ func nongenericAnyAsConditionalP( type: Any . Type ) -> Bool {
163
+ return ( type as? P . Protocol) != nil
164
+ }
165
+ func nongenericAnyAsConditionalPAndAnyObject( type: Any . Type ) -> Bool {
166
+ return ( type as? ( P & AnyObject ) . Protocol) != nil
167
+ }
168
+ func nongenericAnyAsConditionalPAndPCSub( type: Any . Type ) -> Bool {
169
+ return ( type as? ( P & PCSub ) . Type) != nil
170
+ }
171
+ func genericAnyAsConditional< T> ( type: Any . Type , to: T . Type , expected: Bool ) -> Bool {
172
+ // If we're testing against a runtime that doesn't have the fix this tests,
173
+ // just pretend we got it right.
174
+ if #available( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * ) {
175
+ return ( type as? T . Type) != nil
176
+ } else {
177
+ return expected
178
+ }
179
+ }
180
+ // `as!` checks
181
+ func blackhole< T> ( _ : T ) { }
182
+
183
+ func nongenericAnyAsUnconditionalP( type: Any . Type ) -> Bool {
184
+ blackhole ( type as! P . Protocol )
185
+ return true
186
+ }
187
+ func nongenericAnyAsUnconditionalPAndAnyObject( type: Any . Type ) -> Bool {
188
+ blackhole ( type as! ( P & AnyObject ) . Protocol)
189
+ return true
190
+ }
191
+ func nongenericAnyAsUnconditionalPAndPCSub( type: Any . Type ) -> Bool {
192
+ blackhole ( type as! ( P & PCSub ) . Type)
193
+ return true
194
+ }
195
+ func genericAnyAsUnconditional< T> ( type: Any . Type , to: T . Type , expected: Bool ) -> Bool {
196
+ if #available( macOS 9999 , iOS 9999 , watchOS 9999 , tvOS 9999 , * ) {
197
+ blackhole ( type as! T . Type )
198
+ }
199
+ return true
200
+ }
201
+
160
202
// CHECK-LABEL: casting types to protocols with generics:
161
203
print ( " casting types to protocols with generics: " )
162
- print ( nongenericAnyIsP ( type: PS . self) ) // CHECK: true
204
+ print ( nongenericAnyIsP ( type: P . self) ) // CHECK: true
205
+ print ( genericAnyIs ( type: P . self, to: P . self, expected: true ) ) // CHECK: true
206
+ print ( nongenericAnyIsP ( type: PS . self) ) // CHECK-ONONE: true
163
207
print ( genericAnyIs ( type: PS . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
164
- print ( nongenericAnyIsP ( type: PE . self) ) // CHECK: true
208
+ print ( nongenericAnyIsP ( type: PE . self) ) // CHECK-ONONE : true
165
209
print ( genericAnyIs ( type: PE . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
166
- print ( nongenericAnyIsP ( type: PC . self) ) // CHECK: true
210
+ print ( nongenericAnyIsP ( type: PC . self) ) // CHECK-ONONE : true
167
211
print ( genericAnyIs ( type: PC . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
168
- print ( nongenericAnyIsP ( type: PCSub . self) ) // CHECK: true
212
+ print ( nongenericAnyIsP ( type: PCSub . self) ) // CHECK-ONONE : true
169
213
print ( genericAnyIs ( type: PCSub . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
170
214
215
+ // CHECK-LABEL: conditionally casting types to protocols with generics:
216
+ print ( " conditionally casting types to protocols with generics: " )
217
+ print ( nongenericAnyAsConditionalP ( type: P . self) ) // CHECK: true
218
+ print ( genericAnyAsConditional ( type: P . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
219
+ print ( nongenericAnyAsConditionalP ( type: PS . self) ) // CHECK: true
220
+ print ( genericAnyAsConditional ( type: PS . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
221
+ print ( nongenericAnyAsConditionalP ( type: PE . self) ) // CHECK-ONONE: true
222
+ print ( genericAnyAsConditional ( type: PE . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
223
+ print ( nongenericAnyAsConditionalP ( type: PC . self) ) // CHECK-ONONE: true
224
+ print ( genericAnyAsConditional ( type: PC . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
225
+ print ( nongenericAnyAsConditionalP ( type: PCSub . self) ) // CHECK-ONONE: true
226
+ print ( genericAnyAsConditional ( type: PCSub . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
227
+
228
+ // CHECK-LABEL: unconditionally casting types to protocols with generics:
229
+ print ( " unconditionally casting types to protocols with generics: " )
230
+ print ( nongenericAnyAsUnconditionalP ( type: P . self) ) // CHECK: true
231
+ print ( genericAnyAsUnconditional ( type: P . self, to: P . self, expected: true ) ) // CHECK: true
232
+ print ( nongenericAnyAsUnconditionalP ( type: PS . self) ) // CHECK: true
233
+ print ( genericAnyAsUnconditional ( type: PS . self, to: P . self, expected: true ) ) // CHECK: true
234
+ print ( nongenericAnyAsUnconditionalP ( type: PE . self) ) // CHECK: true
235
+ print ( genericAnyAsUnconditional ( type: PE . self, to: P . self, expected: true ) ) // CHECK: true
236
+ print ( nongenericAnyAsUnconditionalP ( type: PC . self) ) // CHECK: true
237
+ print ( genericAnyAsUnconditional ( type: PC . self, to: P . self, expected: true ) ) // CHECK: true
238
+ print ( nongenericAnyAsUnconditionalP ( type: PCSub . self) ) // CHECK: true
239
+ print ( genericAnyAsUnconditional ( type: PCSub . self, to: P . self, expected: true ) ) // CHECK: true
240
+
171
241
// CHECK-LABEL: casting types to protocol & AnyObject existentials:
172
242
print ( " casting types to protocol & AnyObject existentials: " )
173
243
print ( nongenericAnyIsPAndAnyObject ( type: PS . self) ) // CHECK: false
174
244
print ( genericAnyIs ( type: PS . self, to: ( P & AnyObject) . self, expected: false ) ) // CHECK: false
175
245
print ( nongenericAnyIsPAndAnyObject ( type: PE . self) ) // CHECK: false
176
246
print ( genericAnyIs ( type: PE . self, to: ( P & AnyObject) . self, expected: false ) ) // CHECK: false
177
- print ( nongenericAnyIsPAndAnyObject ( type: PC . self) ) // CHECK: true
247
+ print ( nongenericAnyIsPAndAnyObject ( type: PC . self) ) // CHECK-ONONE : true
178
248
print ( genericAnyIs ( type: PC . self, to: ( P & AnyObject) . self, expected: true ) ) // CHECK-ONONE: true
179
- print ( nongenericAnyIsPAndAnyObject ( type: PCSub . self) ) // CHECK: true
249
+ print ( nongenericAnyIsPAndAnyObject ( type: PCSub . self) ) // CHECK-ONONE : true
180
250
print ( genericAnyIs ( type: PCSub . self, to: ( P & AnyObject) . self, expected: true ) ) // CHECK-ONONE: true
251
+ print ( nongenericAnyAsConditionalPAndAnyObject ( type: PS . self) ) // CHECK: false
252
+ print ( genericAnyAsConditional ( type: PS . self, to: ( P & AnyObject) . self, expected: false ) ) // CHECK: false
253
+ print ( nongenericAnyAsConditionalPAndAnyObject ( type: PE . self) ) // CHECK: false
254
+ print ( genericAnyAsConditional ( type: PE . self, to: ( P & AnyObject) . self, expected: false ) ) // CHECK: false
255
+ print ( nongenericAnyAsConditionalPAndAnyObject ( type: PC . self) ) // CHECK-ONONE: true
256
+ print ( genericAnyAsConditional ( type: PC . self, to: ( P & AnyObject) . self, expected: true ) ) // CHECK-ONONE: true
257
+ print ( nongenericAnyAsConditionalPAndAnyObject ( type: PCSub . self) ) // CHECK-ONONE: true
258
+ print ( genericAnyAsConditional ( type: PCSub . self, to: ( P & AnyObject) . self, expected: true ) ) // CHECK-ONONE: true
181
259
182
260
// CHECK-LABEL: casting types to protocol & class existentials:
183
261
print ( " casting types to protocol & class existentials: " )
@@ -189,6 +267,14 @@ print(genericAnyIs(type: PE.self, to: (P & PCSub).self, expected: false)) // CHE
189
267
print ( genericAnyIs ( type: PC . self, to: ( P & PCSub) . self, expected: false ) ) // CHECK: false
190
268
print ( nongenericAnyIsPAndPCSub ( type: PCSub . self) ) // CHECK: true
191
269
print ( genericAnyIs ( type: PCSub . self, to: ( P & PCSub) . self, expected: true ) ) // CHECK-ONONE: true
270
+ print ( nongenericAnyAsConditionalPAndPCSub ( type: PS . self) ) // CHECK: false
271
+ print ( genericAnyAsConditional ( type: PS . self, to: ( P & PCSub) . self, expected: false ) ) // CHECK: false
272
+ print ( nongenericAnyAsConditionalPAndPCSub ( type: PE . self) ) // CHECK: false
273
+ print ( genericAnyAsConditional ( type: PE . self, to: ( P & PCSub) . self, expected: false ) ) // CHECK: false
274
+ //print(nongenericAnyAsConditionalPAndPCSub(type: PC.self)) // CHECK-SR-11565: false -- FIXME: reenable this when SR-11565 is fixed
275
+ print ( genericAnyAsConditional ( type: PC . self, to: ( P & PCSub) . self, expected: false ) ) // CHECK: false
276
+ print ( nongenericAnyAsConditionalPAndPCSub ( type: PCSub . self) ) // CHECK: true
277
+ print ( genericAnyAsConditional ( type: PCSub . self, to: ( P & PCSub) . self, expected: true ) ) // CHECK-ONONE: true
192
278
193
279
194
280
// CHECK-LABEL: type comparisons:
0 commit comments