-
Notifications
You must be signed in to change notification settings - Fork 10.5k
/
Copy pathfunction_conversion.swift
65 lines (47 loc) · 1.66 KB
/
function_conversion.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
// RUN: %target-typecheck-verify-swift -swift-version 4
// rdar://problem/31969605
class Base {}
class Derived : Base {}
protocol Refined {}
protocol Proto : Refined {}
extension Base : Refined {}
func baseFn(_: Base) {}
func superclassConversion(fn: @escaping (Base) -> ()) {
let _: (Derived) -> () = fn
}
func existentialConversion(fn: @escaping (Refined) -> ()) {
let _: (Proto) -> () = fn
let _: (Base) -> () = fn
let _: (Derived) -> () = fn
}
// rdar://problem/31725325
func a<b>(_: [(String, (b) -> () -> Void)]) {}
func a<b>(_: [(String, (b) -> () throws -> Void)]) {}
class c {
func e() {}
static var d = [("", e)]
}
a(c.d)
func b<T>(_: (T) -> () -> ()) {}
b(c.e)
func bar(_: () -> ()) {}
func bar(_: () throws -> ()) {}
func bar_empty() {}
bar(bar_empty)
func consumeNoEscape(_ f: (Int) -> Int) {}
func consumeEscaping(_ f: @escaping (Int) -> Int) {}
func takesAny(_ f: Any) {}
func twoFns(_ f: (Int) -> Int, _ g: @escaping (Int) -> Int) {
// expected-note@-1 {{parameter 'f' is implicitly non-escaping}}
takesAny(f) // expected-error {{converting non-escaping value to 'Any' may allow it to escape}}
takesAny(g)
var h = g
h = f // expected-error {{assigning non-escaping parameter 'f' to an @escaping closure}}
}
takesAny(consumeNoEscape)
takesAny(consumeEscaping)
var noEscapeParam: ((Int) -> Int) -> () = consumeNoEscape
var escapingParam: (@escaping (Int) -> Int) -> () = consumeEscaping
noEscapeParam = escapingParam // expected-error {{converting non-escaping value to '(Int) -> Int' may allow it to escape}}
escapingParam = takesAny
noEscapeParam = takesAny // expected-error {{converting non-escaping value to 'Any' may allow it to escape}}