-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathErrorBridging.swift
75 lines (56 loc) · 1.91 KB
/
ErrorBridging.swift
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
// RUN: %target-swift-frontend %clang-importer-sdk -typecheck %s -verify
// REQUIRES: objc_interop
import Foundation
enum FooError: HairyError, Runcible {
case A
var hairiness: Int { return 0 }
func runce() {}
}
protocol HairyError : Error {
var hairiness: Int { get }
}
protocol Runcible {
func runce()
}
let foo = FooError.A
let error: Error = foo
let subError: HairyError = foo
let compo: HairyError & Runcible = foo
// Error-conforming concrete or existential types can be coerced explicitly
// to NSError.
let ns1 = foo as NSError
let ns2 = error as NSError
let ns3 = subError as NSError
var ns4 = compo as NSError
// NSError conversion must be explicit.
// TODO: fixit to insert 'as NSError'
ns4 = compo // expected-error{{cannot assign value of type 'HairyError & Runcible' to type 'NSError'}}
let e1 = ns1 as? FooError
let e1fix = ns1 as FooError // expected-error{{did you mean to use 'as!'}} {{17-19=as!}}
let esub = ns1 as Error
let esub2 = ns1 as? Error // expected-warning{{conditional cast from 'NSError' to 'Error' always succeeds}}
// SR-1562 / rdar://problem/26370984
enum MyError : Error {
case failed
}
func concrete1(myError: MyError) -> NSError {
return myError as NSError
}
func concrete2(myError: MyError) -> NSError {
return myError // expected-error{{cannot convert return expression of type 'MyError' to return type 'NSError'}}
}
func generic<T : Error>(error: T) -> NSError {
return error as NSError
}
extension Error {
var asNSError: NSError {
return self as NSError
}
var asNSError2: NSError {
return self // expected-error{{cannot convert return expression of type 'Self' to return type 'NSError'}}
}
}
// rdar://problem/27543121
func throwErrorCode() throws {
throw FictionalServerError.meltedDown // expected-error{{thrown error code type 'FictionalServerError.Code' does not conform to 'Error'; construct an 'FictionalServerError' instance}}{{29-29=(}}{{40-40=)}}
}