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
|
// RUN: %target-typecheck-verify-swift
// rdar://83717297
//
// Make sure that trailing closures are matches to the `callAsFunction`
// when used in the same expression as `.init` of a callable type.
protocol View {}
struct EmptyView: View {}
enum Align {
case top, center, bottom
}
struct MyLayout {
init(alignment: Align? = .center, spacing: Double? = 0.0) {}
func callAsFunction<V: View>(content: () -> V) -> MyLayout { .init() }
// expected-note@-1 {{where 'V' = 'Int'}}
func callAsFunction<V: View>(answer: () -> Int,
content: () -> V) -> MyLayout { .init() }
// expected-note@-2 {{where 'V' = 'Int'}}
}
struct Test {
var body1: MyLayout {
MyLayout(spacing: 1.0) {
EmptyView() // Ok
}
}
var body2: MyLayout {
MyLayout() {
42
} content: {
EmptyView() // Ok
}
}
var body3: MyLayout {
MyLayout(alignment: .top) {
let x = 42
return x
} content: {
EmptyView() // Ok
}
}
var body4: MyLayout {
MyLayout(spacing: 1.0) {
let x = 42
return x
} content: {
_ = 42
return EmptyView() // Ok
}
}
var body5: MyLayout {
MyLayout(alignment: .bottom, spacing: 1.0) {
42
} content: {
EmptyView() // Ok
}
}
var body6: MyLayout {
MyLayout(spacing: 1.0) {
_ = EmptyView()
return 42
} // expected-error {{instance method 'callAsFunction(content:)' requires that 'Int' conform to 'View'}}
}
var body7: MyLayout {
MyLayout(alignment: .center) {
42
} content: {
_ = EmptyView()
return 42
} // expected-error {{instance method 'callAsFunction(answer:content:)' requires that 'Int' conform to 'View'}}
}
var body8: MyLayout {
MyLayout {
let x = ""
return x // expected-error {{cannot convert value of type 'String' to closure result type 'Int'}}
} content: {
EmptyView()
}
}
}
// rdar://92912878 - filtering prevents disambiguation of `.callAsFunction`
func test_no_filtering_of_overloads() {
struct S {
init() {}
init(_: String) {}
func callAsFunction<T>(_ fn: () -> T) -> T {
fn()
}
}
func test(_: () -> Void) {
}
test {
_ = S() { // Ok
_ = 42
print("Hello")
}
}
}
func test_default_arguments_do_not_interfere() {
struct S {
init(a: Int? = 42, b: String = "") {}
func callAsFunction(_: () -> Void) -> S { S() }
}
_ = S { _ = 42 }
_ = S(a: 42) { _ = 42 }
_ = S(b: "") { _ = 42 }
}
|