@@ -140,13 +140,20 @@ class PC: P {}
140
140
class PCSub : PC { }
141
141
142
142
// `is` checks
143
- func nongenericAnyIsP( type: Any . Type ) -> Bool {
143
+ func nongenericAnyIsPConforming( type: Any . Type ) -> Bool {
144
+ // `is P.Type` tests whether the argument conforms to `P`
145
+ // Note: this can only be true for a concrete type, never a protocol
146
+ return type is P . Type
147
+ }
148
+ func nongenericAnyIsPSubtype( type: Any . Type ) -> Bool {
149
+ // `is P.Protocol` tests whether the argument is a subtype of `P`
150
+ // In particular, it is true for `P.self`
144
151
return type is P . Protocol
145
152
}
146
- func nongenericAnyIsPAndAnyObject ( type: Any . Type ) -> Bool {
147
- return type is ( P & AnyObject ) . Protocol
153
+ func nongenericAnyIsPAndAnyObjectConforming ( type: Any . Type ) -> Bool {
154
+ return type is ( P & AnyObject ) . Type
148
155
}
149
- func nongenericAnyIsPAndPCSub ( type: Any . Type ) -> Bool {
156
+ func nongenericAnyIsPAndPCSubConforming ( type: Any . Type ) -> Bool {
150
157
return type is ( P & PCSub ) . Type
151
158
}
152
159
func genericAnyIs< T> ( type: Any . Type , to: T . Type , expected: Bool ) -> Bool {
@@ -159,13 +166,16 @@ func genericAnyIs<T>(type: Any.Type, to: T.Type, expected: Bool) -> Bool {
159
166
}
160
167
}
161
168
// `as?` checks
162
- func nongenericAnyAsConditionalP( type: Any . Type ) -> Bool {
169
+ func nongenericAnyAsConditionalPConforming( type: Any . Type ) -> Bool {
170
+ return ( type as? P . Type) != nil
171
+ }
172
+ func nongenericAnyAsConditionalPSubtype( type: Any . Type ) -> Bool {
163
173
return ( type as? P . Protocol) != nil
164
174
}
165
- func nongenericAnyAsConditionalPAndAnyObject ( type: Any . Type ) -> Bool {
166
- return ( type as? ( P & AnyObject ) . Protocol ) != nil
175
+ func nongenericAnyAsConditionalPAndAnyObjectConforming ( type: Any . Type ) -> Bool {
176
+ return ( type as? ( P & AnyObject ) . Type ) != nil
167
177
}
168
- func nongenericAnyAsConditionalPAndPCSub ( type: Any . Type ) -> Bool {
178
+ func nongenericAnyAsConditionalPAndPCSubConforming ( type: Any . Type ) -> Bool {
169
179
return ( type as? ( P & PCSub ) . Type) != nil
170
180
}
171
181
func genericAnyAsConditional< T> ( type: Any . Type , to: T . Type , expected: Bool ) -> Bool {
@@ -180,15 +190,19 @@ func genericAnyAsConditional<T>(type: Any.Type, to: T.Type, expected: Bool) -> B
180
190
// `as!` checks
181
191
func blackhole< T> ( _ : T ) { }
182
192
183
- func nongenericAnyAsUnconditionalP( type: Any . Type ) -> Bool {
193
+ func nongenericAnyAsUnconditionalPConforming( type: Any . Type ) -> Bool {
194
+ blackhole ( type as! P . Type )
195
+ return true
196
+ }
197
+ func nongenericAnyAsUnconditionalPSubtype( type: Any . Type ) -> Bool {
184
198
blackhole ( type as! P . Protocol )
185
199
return true
186
200
}
187
- func nongenericAnyAsUnconditionalPAndAnyObject ( type: Any . Type ) -> Bool {
188
- blackhole ( type as! ( P & AnyObject ) . Protocol )
201
+ func nongenericAnyAsUnconditionalPAndAnyObjectConforming ( type: Any . Type ) -> Bool {
202
+ blackhole ( type as! ( P & AnyObject ) . Type )
189
203
return true
190
204
}
191
- func nongenericAnyAsUnconditionalPAndPCSub ( type: Any . Type ) -> Bool {
205
+ func nongenericAnyAsUnconditionalPAndPCSubConforming ( type: Any . Type ) -> Bool {
192
206
blackhole ( type as! ( P & PCSub ) . Type)
193
207
return true
194
208
}
@@ -201,79 +215,82 @@ func genericAnyAsUnconditional<T>(type: Any.Type, to: T.Type, expected: Bool) ->
201
215
202
216
// CHECK-LABEL: casting types to protocols with generics:
203
217
print ( " casting types to protocols with generics: " )
204
- print ( nongenericAnyIsP ( type: P . self) ) // CHECK: true
218
+ print ( nongenericAnyIsPConforming ( type: P . self) ) // CHECK: false
219
+ print ( nongenericAnyIsPSubtype ( type: P . self) ) // CHECK: true
205
220
print ( genericAnyIs ( type: P . self, to: P . self, expected: true ) ) // CHECK: true
206
- print ( nongenericAnyIsP ( type: PS . self) ) // CHECK-ONONE : true
221
+ print ( nongenericAnyIsPConforming ( type: PS . self) ) // CHECK: true
207
222
print ( genericAnyIs ( type: PS . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
208
- print ( nongenericAnyIsP ( type: PE . self) ) // CHECK-ONONE : true
223
+ print ( nongenericAnyIsPConforming ( type: PE . self) ) // CHECK: true
209
224
print ( genericAnyIs ( type: PE . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
210
- print ( nongenericAnyIsP ( type: PC . self) ) // CHECK-ONONE : true
225
+ print ( nongenericAnyIsPConforming ( type: PC . self) ) // CHECK: true
211
226
print ( genericAnyIs ( type: PC . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
212
- print ( nongenericAnyIsP ( type: PCSub . self) ) // CHECK-ONONE : true
227
+ print ( nongenericAnyIsPConforming ( type: PCSub . self) ) // CHECK: true
213
228
print ( genericAnyIs ( type: PCSub . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
214
229
215
230
// CHECK-LABEL: conditionally casting types to protocols with generics:
216
231
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
232
+ print ( nongenericAnyAsConditionalPConforming ( type: P . self) ) // CHECK: false
233
+ print ( nongenericAnyAsConditionalPSubtype ( type: P . self) ) // CHECK: true
234
+ print ( genericAnyAsConditional ( type: P . self, to: P . self, expected: true ) ) // CHECK: true
235
+ print ( nongenericAnyAsConditionalPConforming ( type: PS . self) ) // CHECK: true
220
236
print ( genericAnyAsConditional ( type: PS . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
221
- print ( nongenericAnyAsConditionalP ( type: PE . self) ) // CHECK-ONONE : true
237
+ print ( nongenericAnyAsConditionalPConforming ( type: PE . self) ) // CHECK: true
222
238
print ( genericAnyAsConditional ( type: PE . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
223
- print ( nongenericAnyAsConditionalP ( type: PC . self) ) // CHECK-ONONE : true
239
+ print ( nongenericAnyAsConditionalPConforming ( type: PC . self) ) // CHECK: true
224
240
print ( genericAnyAsConditional ( type: PC . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
225
- print ( nongenericAnyAsConditionalP ( type: PCSub . self) ) // CHECK-ONONE : true
241
+ print ( nongenericAnyAsConditionalPConforming ( type: PCSub . self) ) // CHECK: true
226
242
print ( genericAnyAsConditional ( type: PCSub . self, to: P . self, expected: true ) ) // CHECK-ONONE: true
227
243
228
244
// CHECK-LABEL: unconditionally casting types to protocols with generics:
229
245
print ( " unconditionally casting types to protocols with generics: " )
230
- print ( nongenericAnyAsUnconditionalP ( type: P . self) ) // CHECK: true
246
+ //print(nongenericAnyAsUnconditionalPConforming(type: P.self)) // expected to trap
247
+ print ( nongenericAnyAsUnconditionalPSubtype ( type: P . self) ) // CHECK: true
231
248
print ( genericAnyAsUnconditional ( type: P . self, to: P . self, expected: true ) ) // CHECK: true
232
- print ( nongenericAnyAsUnconditionalP ( type: PS . self) ) // CHECK: true
249
+ print ( nongenericAnyAsUnconditionalPConforming ( type: PS . self) ) // CHECK: true
233
250
print ( genericAnyAsUnconditional ( type: PS . self, to: P . self, expected: true ) ) // CHECK: true
234
- print ( nongenericAnyAsUnconditionalP ( type: PE . self) ) // CHECK: true
251
+ print ( nongenericAnyAsUnconditionalPConforming ( type: PE . self) ) // CHECK: true
235
252
print ( genericAnyAsUnconditional ( type: PE . self, to: P . self, expected: true ) ) // CHECK: true
236
- print ( nongenericAnyAsUnconditionalP ( type: PC . self) ) // CHECK: true
253
+ print ( nongenericAnyAsUnconditionalPConforming ( type: PC . self) ) // CHECK: true
237
254
print ( genericAnyAsUnconditional ( type: PC . self, to: P . self, expected: true ) ) // CHECK: true
238
- print ( nongenericAnyAsUnconditionalP ( type: PCSub . self) ) // CHECK: true
255
+ print ( nongenericAnyAsUnconditionalPConforming ( type: PCSub . self) ) // CHECK: true
239
256
print ( genericAnyAsUnconditional ( type: PCSub . self, to: P . self, expected: true ) ) // CHECK: true
240
257
241
258
// CHECK-LABEL: casting types to protocol & AnyObject existentials:
242
259
print ( " casting types to protocol & AnyObject existentials: " )
243
- print ( nongenericAnyIsPAndAnyObject ( type: PS . self) ) // CHECK: false
260
+ print ( nongenericAnyIsPAndAnyObjectConforming ( type: PS . self) ) // CHECK: false
244
261
print ( genericAnyIs ( type: PS . self, to: ( P & AnyObject) . self, expected: false ) ) // CHECK: false
245
- print ( nongenericAnyIsPAndAnyObject ( type: PE . self) ) // CHECK: false
262
+ print ( nongenericAnyIsPAndAnyObjectConforming ( type: PE . self) ) // CHECK: false
246
263
print ( genericAnyIs ( type: PE . self, to: ( P & AnyObject) . self, expected: false ) ) // CHECK: false
247
- print ( nongenericAnyIsPAndAnyObject ( type: PC . self) ) // CHECK-ONONE : true
264
+ print ( nongenericAnyIsPAndAnyObjectConforming ( type: PC . self) ) // CHECK: true
248
265
print ( genericAnyIs ( type: PC . self, to: ( P & AnyObject) . self, expected: true ) ) // CHECK-ONONE: true
249
- print ( nongenericAnyIsPAndAnyObject ( type: PCSub . self) ) // CHECK-ONONE : true
266
+ print ( nongenericAnyIsPAndAnyObjectConforming ( type: PCSub . self) ) // CHECK: true
250
267
print ( genericAnyIs ( type: PCSub . self, to: ( P & AnyObject) . self, expected: true ) ) // CHECK-ONONE: true
251
- print ( nongenericAnyAsConditionalPAndAnyObject ( type: PS . self) ) // CHECK: false
268
+ print ( nongenericAnyAsConditionalPAndAnyObjectConforming ( type: PS . self) ) // CHECK: false
252
269
print ( genericAnyAsConditional ( type: PS . self, to: ( P & AnyObject) . self, expected: false ) ) // CHECK: false
253
- print ( nongenericAnyAsConditionalPAndAnyObject ( type: PE . self) ) // CHECK: false
270
+ print ( nongenericAnyAsConditionalPAndAnyObjectConforming ( type: PE . self) ) // CHECK: false
254
271
print ( genericAnyAsConditional ( type: PE . self, to: ( P & AnyObject) . self, expected: false ) ) // CHECK: false
255
- print ( nongenericAnyAsConditionalPAndAnyObject ( type: PC . self) ) // CHECK-ONONE : true
272
+ print ( nongenericAnyAsConditionalPAndAnyObjectConforming ( type: PC . self) ) // CHECK: true
256
273
print ( genericAnyAsConditional ( type: PC . self, to: ( P & AnyObject) . self, expected: true ) ) // CHECK-ONONE: true
257
- print ( nongenericAnyAsConditionalPAndAnyObject ( type: PCSub . self) ) // CHECK-ONONE : true
274
+ print ( nongenericAnyAsConditionalPAndAnyObjectConforming ( type: PCSub . self) ) // CHECK: true
258
275
print ( genericAnyAsConditional ( type: PCSub . self, to: ( P & AnyObject) . self, expected: true ) ) // CHECK-ONONE: true
259
276
260
277
// CHECK-LABEL: casting types to protocol & class existentials:
261
278
print ( " casting types to protocol & class existentials: " )
262
- print ( nongenericAnyIsPAndPCSub ( type: PS . self) ) // CHECK: false
279
+ print ( nongenericAnyIsPAndPCSubConforming ( type: PS . self) ) // CHECK: false
263
280
print ( genericAnyIs ( type: PS . self, to: ( P & PCSub) . self, expected: false ) ) // CHECK: false
264
- print ( nongenericAnyIsPAndPCSub ( type: PE . self) ) // CHECK: false
281
+ print ( nongenericAnyIsPAndPCSubConforming ( type: PE . self) ) // CHECK: false
265
282
print ( genericAnyIs ( type: PE . self, to: ( P & PCSub) . self, expected: false ) ) // CHECK: false
266
- //print(nongenericAnyIsPAndPCSub (type: PC.self)) // CHECK-SR-11565: false -- FIXME: reenable this when SR-11565 is fixed
283
+ //print(nongenericAnyIsPAndPCSubConforming (type: PC.self)) // CHECK-SR-11565: false -- FIXME: reenable this when SR-11565 is fixed
267
284
print ( genericAnyIs ( type: PC . self, to: ( P & PCSub) . self, expected: false ) ) // CHECK: false
268
- print ( nongenericAnyIsPAndPCSub ( type: PCSub . self) ) // CHECK: true
285
+ print ( nongenericAnyIsPAndPCSubConforming ( type: PCSub . self) ) // CHECK: true
269
286
print ( genericAnyIs ( type: PCSub . self, to: ( P & PCSub) . self, expected: true ) ) // CHECK-ONONE: true
270
- print ( nongenericAnyAsConditionalPAndPCSub ( type: PS . self) ) // CHECK: false
287
+ print ( nongenericAnyAsConditionalPAndPCSubConforming ( type: PS . self) ) // CHECK: false
271
288
print ( genericAnyAsConditional ( type: PS . self, to: ( P & PCSub) . self, expected: false ) ) // CHECK: false
272
- print ( nongenericAnyAsConditionalPAndPCSub ( type: PE . self) ) // CHECK: false
289
+ print ( nongenericAnyAsConditionalPAndPCSubConforming ( type: PE . self) ) // CHECK: false
273
290
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
291
+ //print(nongenericAnyAsConditionalPAndPCSubConforming (type: PC.self)) // CHECK-SR-11565: false -- FIXME: reenable this when SR-11565 is fixed
275
292
print ( genericAnyAsConditional ( type: PC . self, to: ( P & PCSub) . self, expected: false ) ) // CHECK: false
276
- print ( nongenericAnyAsConditionalPAndPCSub ( type: PCSub . self) ) // CHECK: true
293
+ print ( nongenericAnyAsConditionalPAndPCSubConforming ( type: PCSub . self) ) // CHECK: true
277
294
print ( genericAnyAsConditional ( type: PCSub . self, to: ( P & PCSub) . self, expected: true ) ) // CHECK-ONONE: true
278
295
279
296
0 commit comments