// RUN: %target-run-simple-swift | %FileCheck %s // REQUIRES: executable_test struct BoxedError { var contents: T } enum MyError: Error { case nothingImportant case reallyImportant(String) } // CHECK: start print("start") let value = MyError.reallyImportant("hello") // CHECK-NEXT: reallyImportant("hello") print(value) func takeBoxedError(error: BoxedError) -> Error { return error.contents } func makeBoxedError(error: T) -> BoxedError { return BoxedError(contents: error) } let unboxedValue = takeBoxedError(error: makeBoxedError(error: value)) // CHECK-NEXT: reallyImportant("hello") print(unboxedValue) let errorValue: Error = MyError.reallyImportant("goodbye") func castValueToError(error: T) -> Error? { return error as? Error } // CHECK-NEXT: reallyImportant("goodbye") print(castValueToError(error: errorValue) ?? value) struct Carrier { var name: String } protocol ErrorCarrier {} extension Carrier: ErrorCarrier where T: Error {} func castValueToErrorCarrier(_ value: T) -> ErrorCarrier? { return value as? ErrorCarrier } // CHECK-NEXT: nil print(castValueToErrorCarrier(Carrier(name: "A carrier of numbers"))) // CHECK-NEXT: A carrier of my errors print(castValueToErrorCarrier(Carrier(name: "A carrier of my errors"))) // CHECK-NEXT: A carrier of all errors print(castValueToErrorCarrier(Carrier(name: "A carrier of all errors"))) // CHECK-NEXT: nil protocol ErrorRefinement : Error {} print(castValueToErrorCarrier(Carrier(name: "A carrier of refined errors"))) // CHECK-NEXT: nil protocol OtherProtocol {} print(castValueToErrorCarrier(Carrier(name: "A carrier of composed errors"))) // CHECK-NEXT: nil class C {} print(castValueToErrorCarrier(Carrier(name: "A carrier of classic composed errors"))) // CHECK-NEXT: end print("end")