1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
|
// RUN: %empty-directory(%t)
// RUN: %target-build-swift -Onone %s -o %t/a.out
// RUN: %target-build-swift -O %s -o %t/a.out.optimized
// RUN: %target-codesign %t/a.out
// RUN: %target-codesign %t/a.out.optimized
//
// RUN: %target-run %t/a.out | %FileCheck --check-prefix CHECK %s
// RUN: %target-run %t/a.out.optimized | %FileCheck --check-prefix CHECK %s
// RUN: %target-build-swift -Onone %s -o %t/a.out
// RUN: %target-build-swift -O %s -o %t/a.out.optimized
// RUN: %target-codesign %t/a.out
// RUN: %target-codesign %t/a.out.optimized
//
// RUN: %target-run %t/a.out | %FileCheck --check-prefix CHECK %s
// RUN: %target-run %t/a.out.optimized | %FileCheck --check-prefix CHECK %s
// REQUIRES: executable_test
// FIXME: rdar://problem/19648117 Needs splitting objc parts out
#if canImport(Foundation)
import Foundation
#endif
func allToInt<T>(_ x: T) -> Int {
return x as! Int
}
func allToIntOrZero<T>(_ x: T) -> Int {
if x is Int {
return x as! Int
}
return 0
}
func anyToInt(_ x: Any) -> Int {
return x as! Int
}
func anyToIntOrZero(_ x: Any) -> Int {
if x is Int {
return x as! Int
}
return 0
}
protocol Class : class {}
class C : Class {
func print() { Swift.print("C!") }
}
class D : C {
override func print() { Swift.print("D!") }
}
class E : C {
override func print() { Swift.print("E!") }
}
class X : Class {
}
func allToC<T>(_ x: T) -> C {
return x as! C
}
func allToCOrE<T>(_ x: T) -> C {
if x is C {
return x as! C
}
return E()
}
func anyToC(_ x: Any) -> C {
return x as! C
}
func anyToCOrE(_ x: Any) -> C {
if x is C {
return x as! C
}
return E()
}
func allClassesToC<T : Class>(_ x: T) -> C {
return x as! C
}
func allClassesToCOrE<T : Class>(_ x: T) -> C {
if x is C {
return x as! C
}
return E()
}
func anyClassToC(_ x: Class) -> C {
return x as! C
}
func anyClassToCOrE(_ x: Class) -> C {
if x is C {
return x as! C
}
return E()
}
func allToAll<T, U>(_ t: T, _: U.Type) -> Bool {
return t is U
}
func allMetasToAllMetas<T, U>(_: T.Type, _: U.Type) -> Bool {
return T.self is U.Type
}
print(allToInt(22)) // CHECK: 22
print(anyToInt(44)) // CHECK: 44
allToC(C()).print() // CHECK: C!
allToC(D()).print() // CHECK: D!
anyToC(C()).print() // CHECK: C!
anyToC(D()).print() // CHECK: D!
allClassesToC(C()).print() // CHECK: C!
allClassesToC(D()).print() // CHECK: D!
anyClassToC(C()).print() // CHECK: C!
anyClassToC(D()).print() // CHECK: D!
print(allToIntOrZero(55)) // CHECK: 55
print(allToIntOrZero("fifty-five")) // CHECK: 0
print(anyToIntOrZero(88)) // CHECK: 88
print(anyToIntOrZero("eighty-eight")) // CHECK: 0
allToCOrE(C()).print() // CHECK: C!
allToCOrE(D()).print() // CHECK: D!
allToCOrE(143).print() // CHECK: E!
allToCOrE(X()).print() // CHECK: E!
anyToCOrE(C()).print() // CHECK: C!
anyToCOrE(D()).print() // CHECK: D!
anyToCOrE(143).print() // CHECK: E!
anyToCOrE(X()).print() // CHECK: E!
allClassesToCOrE(C()).print() // CHECK: C!
allClassesToCOrE(D()).print() // CHECK: D!
allClassesToCOrE(X()).print() // CHECK: E!
anyClassToCOrE(C()).print() // CHECK: C!
anyClassToCOrE(D()).print() // CHECK: D!
anyClassToCOrE(X()).print() // CHECK: E!
protocol P {}
struct PS: P {}
enum PE: P {}
class PC: P {}
class PCSub: PC {}
// `is` checks
func nongenericAnyIsPType(type: Any.Type) -> Bool {
// `is P.Type` tests whether the argument conforms to `P`
// Note: this can only be true for a concrete type, never a protocol
return type is P.Type
}
func nongenericAnyIsPProtocol(type: Any.Type) -> Bool {
// `P.Protocol` is the metatype for `P` (the type of `P.self`)
// `is P.Protocol` tests whether the argument is a subtype of `P`
// In particular, it is true for `P.self`
return type is P.Protocol
}
func nongenericAnyIsPAndAnyObjectType(type: Any.Type) -> Bool {
return type is (P & AnyObject).Type
}
func nongenericAnyIsPAndAnyObjectProtocol(type: Any.Type) -> Bool {
return type is (P & AnyObject).Protocol
}
func nongenericAnyIsPAndPCSubType(type: Any.Type) -> Bool {
return type is (P & PCSub).Type
}
func genericAnyIs<T>(type: Any.Type, to: T.Type, expected: Bool) -> Bool {
// If we're testing against a runtime that doesn't have the fix this tests,
// just pretend we got it right.
if #available(SwiftStdlib 5.2, *) {
// Remember: If `T` is bound to `P`, then `T.Type` is `P.Protocol`
return type is T.Type
} else {
return expected
}
}
// `as?` checks
func nongenericAnyAsConditionalPType(type: Any.Type) -> Bool {
return (type as? P.Type) != nil
}
func nongenericAnyAsConditionalPProtocol(type: Any.Type) -> Bool {
return (type as? P.Protocol) != nil
}
func nongenericAnyAsConditionalPAndAnyObjectType(type: Any.Type) -> Bool {
return (type as? (P & AnyObject).Type) != nil
}
func nongenericAnyAsConditionalPAndAnyObjectProtocol(type: Any.Type) -> Bool {
return (type as? (P & AnyObject).Protocol) != nil
}
func nongenericAnyAsConditionalPAndPCSubType(type: Any.Type) -> Bool {
return (type as? (P & PCSub).Type) != nil
}
func genericAnyAsConditional<T>(type: Any.Type, to: T.Type, expected: Bool) -> Bool {
// If we're testing against a runtime that doesn't have the fix this tests,
// just pretend we got it right.
if #available(SwiftStdlib 5.3, *) {
return (type as? T.Type) != nil
} else {
return expected
}
}
// `as!` checks
func blackhole<T>(_ : T) { }
func nongenericAnyAsUnconditionalPType(type: Any.Type) -> Bool {
blackhole(type as! P.Type)
return true
}
func nongenericAnyAsUnconditionalPProtocol(type: Any.Type) -> Bool {
blackhole(type as! P.Protocol)
return true
}
func nongenericAnyAsUnconditionalPAndAnyObjectType(type: Any.Type) -> Bool {
blackhole(type as! (P & AnyObject).Type)
return true
}
func nongenericAnyAsUnconditionalPAndPCSubType(type: Any.Type) -> Bool {
blackhole(type as! (P & PCSub).Type)
return true
}
func genericAnyAsUnconditional<T>(type: Any.Type, to: T.Type, expected: Bool) -> Bool {
if #available(SwiftStdlib 5.3, *) {
blackhole(type as! T.Type)
}
return true
}
// CHECK-LABEL: casting types to protocols with generics:
print("casting types to protocols with generics:")
print(#line, nongenericAnyIsPType(type: P.self)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyIsPProtocol(type: P.self)) // CHECK: [[@LINE]] true
print(#line, genericAnyIs(type: P.self, to: P.self, expected: true)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyIsPType(type: PS.self)) // CHECK: [[@LINE]] true
print(#line, PS() is P) // CHECK: [[@LINE]] true
// One candidate for a Swift type theory holds that
// `A is a subtype of B iff A.self is metatype<B>`
// In that theory, `PS() is P` above would imply that
// `PS.self is P.Protocol` below must also be true.
// But that theory is not the one that Swift currently
// implements.
print(#line, nongenericAnyIsPProtocol(type: PS.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyIs(type: PS.self, to: P.self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyIsPType(type: PE.self)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyIsPProtocol(type: PE.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyIs(type: PE.self, to: P.self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyIsPType(type: PC.self)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyIsPProtocol(type: PC.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyIs(type: PC.self, to: P.self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyIsPType(type: PCSub.self)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyIsPProtocol(type: PCSub.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyIs(type: PCSub.self, to: P.self, expected: false)) // CHECK: [[@LINE]] false
// CHECK-LABEL: conditionally casting types to protocols with generics:
print(#line, "conditionally casting types to protocols with generics:")
print(#line, nongenericAnyAsConditionalPType(type: P.self)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyAsConditionalPProtocol(type: P.self)) // CHECK: [[@LINE]] true
print(#line, genericAnyAsConditional(type: P.self, to: P.self, expected: true)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyAsConditionalPType(type: PS.self)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyAsConditionalPProtocol(type: PS.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyAsConditional(type: PS.self, to: P.self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyAsConditionalPType(type: PE.self)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyAsConditionalPProtocol(type: PE.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyAsConditional(type: PE.self, to: P.self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyAsConditionalPType(type: PC.self)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyAsConditionalPProtocol(type: PC.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyAsConditional(type: PC.self, to: P.self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyAsConditionalPType(type: PCSub.self)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyAsConditionalPProtocol(type: PCSub.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyAsConditional(type: PCSub.self, to: P.self, expected: false)) // CHECK: [[@LINE]] false
// CHECK-LABEL: unconditionally casting types to protocols with generics:
print(#line, "unconditionally casting types to protocols with generics:")
//print(#line, nongenericAnyAsUnconditionalPType(type: P.self)) // expected to trap
print(#line, nongenericAnyAsUnconditionalPProtocol(type: P.self)) // CHECK: [[@LINE]] true
print(#line, genericAnyAsUnconditional(type: P.self, to: P.self, expected: true)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyAsUnconditionalPType(type: PS.self)) // CHECK: [[@LINE]] true
print(#line, genericAnyAsUnconditional(type: PS.self, to: P.self, expected: true)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyAsUnconditionalPType(type: PE.self)) // CHECK: [[@LINE]] true
print(#line, genericAnyAsUnconditional(type: PE.self, to: P.self, expected: true)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyAsUnconditionalPType(type: PC.self)) // CHECK: [[@LINE]] true
print(#line, genericAnyAsUnconditional(type: PC.self, to: P.self, expected: true)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyAsUnconditionalPType(type: PCSub.self)) // CHECK: [[@LINE]] true
print(#line, genericAnyAsUnconditional(type: PCSub.self, to: P.self, expected: true)) // CHECK: [[@LINE]] true
// CHECK-LABEL: casting types to protocol & AnyObject existentials:
print(#line, "casting types to protocol & AnyObject existentials:")
print(#line, nongenericAnyIsPAndAnyObjectType(type: PS.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyIs(type: PS.self, to: (P & AnyObject).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyIsPAndAnyObjectType(type: PE.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyIs(type: PE.self, to: (P & AnyObject).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyIsPAndAnyObjectType(type: PC.self)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyIsPAndAnyObjectProtocol(type: PC.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyIs(type: PC.self, to: (P & AnyObject).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyIsPAndAnyObjectType(type: PCSub.self)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyIsPAndAnyObjectProtocol(type: PCSub.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyIs(type: PCSub.self, to: (P & AnyObject).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyAsConditionalPAndAnyObjectType(type: PS.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyAsConditional(type: PS.self, to: (P & AnyObject).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyAsConditionalPAndAnyObjectType(type: PE.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyAsConditional(type: PE.self, to: (P & AnyObject).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyAsConditionalPAndAnyObjectType(type: PC.self)) // CHECK: [[@LINE]] true
print(#line, nongenericAnyAsConditionalPAndAnyObjectProtocol(type: PC.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyAsConditional(type: PC.self, to: (P & AnyObject).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyAsConditionalPAndAnyObjectType(type: PCSub.self)) // CHECK: [[@LINE]] true
print(#line, genericAnyAsConditional(type: PCSub.self, to: (P & AnyObject).self, expected: false)) // CHECK: [[@LINE]] false
// CHECK-LABEL: casting types to protocol & class existentials:
print(#line, "casting types to protocol & class existentials:")
print(#line, nongenericAnyIsPAndPCSubType(type: PS.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyIs(type: PS.self, to: (P & PCSub).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyIsPAndPCSubType(type: PE.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyIs(type: PE.self, to: (P & PCSub).self, expected: false)) // CHECK: [[@LINE]] false
// FIXME: reenable this when https://github.com/apple/swift/issues/53970 is fixed
//print(#line, nongenericAnyIsPAndPCSubType(type: PC.self)) // C HECK: [[@LINE]] false
print(#line, genericAnyIs(type: PC.self, to: (P & PCSub).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyIsPAndPCSubType(type: PCSub.self)) // CHECK: [[@LINE]] true
print(#line, genericAnyIs(type: PCSub.self, to: (P & PCSub).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyAsConditionalPAndPCSubType(type: PS.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyAsConditional(type: PS.self, to: (P & PCSub).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyAsConditionalPAndPCSubType(type: PE.self)) // CHECK: [[@LINE]] false
print(#line, genericAnyAsConditional(type: PE.self, to: (P & PCSub).self, expected: false)) // CHECK: [[@LINE]] false
// FIXME: reenable this when https://github.com/apple/swift/issues/53970 is fixed
// print(#line, nongenericAnyAsConditionalPAndPCSubType(type: PC.self)) // C HECK: [[@LINE]] false
print(#line, genericAnyAsConditional(type: PC.self, to: (P & PCSub).self, expected: false)) // CHECK: [[@LINE]] false
print(#line, nongenericAnyAsConditionalPAndPCSubType(type: PCSub.self)) // CHECK: [[@LINE]] true
print(#line, genericAnyAsConditional(type: PCSub.self, to: (P & PCSub).self, expected: false)) // CHECK: [[@LINE]] false
// CHECK-LABEL: type comparisons:
print(#line, "type comparisons:\n")
print(#line, allMetasToAllMetas(Int.self, Int.self)) // CHECK: [[@LINE]] true
print(#line, allMetasToAllMetas(Int.self, Float.self)) // CHECK: [[@LINE]] false
print(#line, allMetasToAllMetas(C.self, C.self)) // CHECK: [[@LINE]] true
print(#line, allMetasToAllMetas(D.self, C.self)) // CHECK: [[@LINE]] true
print(#line, allMetasToAllMetas(C.self, D.self)) // CHECK: [[@LINE]] false
print(#line, C.self is D.Type) // CHECK: [[@LINE]] false
print(#line, (D.self as C.Type) is D.Type) // CHECK: [[@LINE]] true
let t: Any.Type = type(of: 1 as Any)
print(#line, t is Int.Type) // CHECK: [[@LINE]] true
print(#line, t is Float.Type) // CHECK: [[@LINE]] false
print(#line, t is C.Type) // CHECK: [[@LINE]] false
let u: Any.Type = type(of: (D() as Any))
print(#line, u is C.Type) // CHECK: [[@LINE]] true
print(#line, u is D.Type) // CHECK: [[@LINE]] true
print(#line, u is E.Type) // CHECK: [[@LINE]] false
print(#line, u is Int.Type) // CHECK: [[@LINE]] false
// FIXME: Can't spell AnyObject.Protocol
// CHECK-LABEL: AnyObject casts:
print(#line, "AnyObject casts:")
print(#line, allToAll(C(), AnyObject.self)) // CHECK: [[@LINE]] true
// On Darwin, the object will be the ObjC-runtime-class object;
// out of Darwin, this should not succeed.
print(#line, allToAll(type(of: C()), AnyObject.self))
// CHECK-objc: true
// CHECK-native: false
// Bridging
// NSNumber on Darwin, __SwiftValue on Linux.
print(#line, allToAll(0, AnyObject.self)) // CHECK: [[@LINE]] true
// This will get bridged using __SwiftValue.
struct NotBridged { var x: Int }
print(#line, allToAll(NotBridged(x: 0), AnyObject.self)) // CHECK: [[@LINE]] true
#if canImport(Foundation)
// This requires Foundation (for NSCopying):
print(#line, allToAll(NotBridged(x: 0), NSCopying.self)) // CHECK-objc: [[@LINE]] true
#endif
// On Darwin, these casts fail (intentionally) even though __SwiftValue does
// technically conform to these protocols through NSObject.
// Off Darwin, it should not conform at all.
print(#line, allToAll(NotBridged(x: 0), CustomStringConvertible.self)) // CHECK: [[@LINE]] false
print(#line, allToAll(NotBridged(x: 0), (AnyObject & CustomStringConvertible).self)) // CHECK: [[@LINE]] false
#if canImport(Foundation)
// This requires Foundation (for NSArray):
//
// rdar://problem/19482567
//
func swiftOptimizesThisFunctionIncorrectly() -> Bool {
let anArray = [] as NSArray
if let whyThisIsNeverExecutedIfCalledFromFunctionAndNotFromMethod = anArray as? [NSObject] {
return true
}
return false
}
let result = swiftOptimizesThisFunctionIncorrectly()
print(#line, "Bridge cast result: \(result)") // CHECK-NEXT-objc: Bridge cast result: true
#endif
|