mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
ns_error_domain can now be used to communicate with the ClangImporter when an enum has an associated error domain string. In this case, when we import it as a Swift enum, we can also synthesize a conformance to _BridgedNSError. This allows the creation of something like NS_ERROR_ENUM, in which the developer can declare an enum for the purposes of error handling. Adds Sema and executable tests demonstrating this funcionality. In order for the imported ns_error_domain bridging to work, we have to at some point forcibly pull in a _BridgedNSError conformance, as one will not be pulled in normally. This is a problem, and is explicitly signaled in the provided test case
73 lines
1.6 KiB
Swift
73 lines
1.6 KiB
Swift
// RUN: mkdir -p %t
|
|
// RUN: %target-clang %S/Inputs/enum-error.m -c -o %t/enum-error.o
|
|
// RUN: %target-build-swift -import-objc-header %S/Inputs/enum-error.h -Xlinker %t/enum-error.o %s -o %t/a.out
|
|
// RUN: %target-run %t/a.out | FileCheck %s
|
|
|
|
// REQUIRES: executable_test
|
|
// REQUIRES: OS=macosx
|
|
|
|
import Foundation
|
|
|
|
func printError(err: TestError) {
|
|
switch (err) {
|
|
case .TENone:
|
|
print("TestError: TENone")
|
|
break
|
|
|
|
case .TEOne:
|
|
print("TestError: TEOne")
|
|
break
|
|
|
|
case .TETwo:
|
|
print("TestError: TETwo")
|
|
break
|
|
}
|
|
}
|
|
|
|
// FIXME: If I remove the constraint on _BridgedNSError, then the test will
|
|
// fail, as NSError will no longer bridge to TestError
|
|
func forceConformance<T : _BridgedNSError>(a: T) -> T {
|
|
return a
|
|
}
|
|
|
|
func testError() {
|
|
let terr = getErr()
|
|
switch (terr) { case .TENone, .TEOne, .TETwo: break } // ok
|
|
printError(terr)
|
|
// CHECK: TestError: TEOne
|
|
|
|
do {
|
|
throw forceConformance(TestError.TETwo)
|
|
} catch let error as TestError {
|
|
printError(error)
|
|
// CHECK-NEXT: TestError: TETwo
|
|
} catch {
|
|
assert(false)
|
|
}
|
|
|
|
do {
|
|
throw NSError(domain: TestErrorDomain,
|
|
code: Int(TestError.TENone.rawValue),
|
|
userInfo: nil)
|
|
} catch let error as TestError {
|
|
printError(error)
|
|
// CHECK-NEXT: TestError: TENone
|
|
} catch _ as NSError {
|
|
print("nserror")
|
|
} catch {
|
|
assert(false)
|
|
}
|
|
|
|
do {
|
|
enum LocalError : ErrorType { case Err }
|
|
throw LocalError.Err
|
|
} catch let error as TestError {
|
|
printError(error)
|
|
} catch {
|
|
print("other error found")
|
|
// CHECK-NEXT: other error found
|
|
}
|
|
|
|
}
|
|
|
|
testError() |