Skip to content

Commit 6515483

Browse files
committed
Fix the tests
In particular, distinguish more clearly between casting for protocol conformance versus casting for protocol type testing. Restore the previously-correct tests back to their original status. Also, re-enable some checks in optimized builds that are passing now.
1 parent 993dfca commit 6515483

File tree

1 file changed

+61
-44
lines changed

1 file changed

+61
-44
lines changed

test/Interpreter/generic_casts.swift

Lines changed: 61 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -140,13 +140,20 @@ class PC: P {}
140140
class PCSub: PC {}
141141

142142
// `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`
144151
return type is P.Protocol
145152
}
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
148155
}
149-
func nongenericAnyIsPAndPCSub(type: Any.Type) -> Bool {
156+
func nongenericAnyIsPAndPCSubConforming(type: Any.Type) -> Bool {
150157
return type is (P & PCSub).Type
151158
}
152159
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 {
159166
}
160167
}
161168
// `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 {
163173
return (type as? P.Protocol) != nil
164174
}
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
167177
}
168-
func nongenericAnyAsConditionalPAndPCSub(type: Any.Type) -> Bool {
178+
func nongenericAnyAsConditionalPAndPCSubConforming(type: Any.Type) -> Bool {
169179
return (type as? (P & PCSub).Type) != nil
170180
}
171181
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
180190
// `as!` checks
181191
func blackhole<T>(_ : T) { }
182192

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 {
184198
blackhole(type as! P.Protocol)
185199
return true
186200
}
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)
189203
return true
190204
}
191-
func nongenericAnyAsUnconditionalPAndPCSub(type: Any.Type) -> Bool {
205+
func nongenericAnyAsUnconditionalPAndPCSubConforming(type: Any.Type) -> Bool {
192206
blackhole(type as! (P & PCSub).Type)
193207
return true
194208
}
@@ -201,79 +215,82 @@ func genericAnyAsUnconditional<T>(type: Any.Type, to: T.Type, expected: Bool) ->
201215

202216
// CHECK-LABEL: casting types to protocols with generics:
203217
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
205220
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
207222
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
209224
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
211226
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
213228
print(genericAnyIs(type: PCSub.self, to: P.self, expected: true)) // CHECK-ONONE: true
214229

215230
// CHECK-LABEL: conditionally casting types to protocols with generics:
216231
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
220236
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
222238
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
224240
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
226242
print(genericAnyAsConditional(type: PCSub.self, to: P.self, expected: true)) // CHECK-ONONE: true
227243

228244
// CHECK-LABEL: unconditionally casting types to protocols with generics:
229245
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
231248
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
233250
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
235252
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
237254
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
239256
print(genericAnyAsUnconditional(type: PCSub.self, to: P.self, expected: true)) // CHECK: true
240257

241258
// CHECK-LABEL: casting types to protocol & AnyObject existentials:
242259
print("casting types to protocol & AnyObject existentials:")
243-
print(nongenericAnyIsPAndAnyObject(type: PS.self)) // CHECK: false
260+
print(nongenericAnyIsPAndAnyObjectConforming(type: PS.self)) // CHECK: false
244261
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
246263
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
248265
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
250267
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
252269
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
254271
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
256273
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
258275
print(genericAnyAsConditional(type: PCSub.self, to: (P & AnyObject).self, expected: true)) // CHECK-ONONE: true
259276

260277
// CHECK-LABEL: casting types to protocol & class existentials:
261278
print("casting types to protocol & class existentials:")
262-
print(nongenericAnyIsPAndPCSub(type: PS.self)) // CHECK: false
279+
print(nongenericAnyIsPAndPCSubConforming(type: PS.self)) // CHECK: false
263280
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
265282
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
267284
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
269286
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
271288
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
273290
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
275292
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
277294
print(genericAnyAsConditional(type: PCSub.self, to: (P & PCSub).self, expected: true)) // CHECK-ONONE: true
278295

279296

0 commit comments

Comments
 (0)