|
| 1 | +// RUN: %target-typecheck-verify-swift -disable-availability-checking |
| 2 | +// UNSUPPORTED: windows |
| 3 | +// Line-feeds in Fix-Its fail to check on Windows. |
| 4 | + |
| 5 | +@_functionBuilder |
| 6 | +struct Maker {} // expected-error {{function builder must provide at least one static 'buildBlock' method}}{{15-15=\n static func buildBlock(_ components: <#Component#>...) -> <#Component#> {\n <#code#>\n \}}} |
| 7 | + |
| 8 | +@_functionBuilder |
| 9 | +struct TupleBuilderWithoutIf { // expected-note 3{{struct 'TupleBuilderWithoutIf' declared here}} |
| 10 | + // expected-note@-1{{add 'buildOptional(_:)' to the function builder 'TupleBuilderWithoutIf' to add support for 'if' statements without an 'else'}}{{31-31=\n static func buildOptional(_ component: <#Component#>?) -> <#Component#> {\n <#code#>\n \}}} |
| 11 | + // expected-note@-2{{add 'buildEither(first:)' and 'buildEither(second:)' to the function builder 'TupleBuilderWithoutIf' to add support for 'if'-'else' and 'switch'}}{{31-31=\n static func buildEither(first component: <#Component#>) -> <#Component#> {\n <#code#>\n \}\n\n static func buildEither(second component: <#Component#>) -> <#Component#> {\n <#code#>\n \}}} |
| 12 | + // expected-note@-3{{add 'buildArray(_:)' to the function builder 'TupleBuilderWithoutIf' to add support for 'for'..'in' loops}}{{31-31=\n static func buildArray(_ components: [<#Component#>]) -> <#Component#> {\n <#code#>\n \}}} |
| 13 | + static func buildBlock() -> () { } |
| 14 | + |
| 15 | + static func buildBlock<T1>(_ t1: T1) -> T1 { |
| 16 | + return t1 |
| 17 | + } |
| 18 | + |
| 19 | + static func buildBlock<T1, T2>(_ t1: T1, _ t2: T2) -> (T1, T2) { |
| 20 | + return (t1, t2) |
| 21 | + } |
| 22 | + |
| 23 | + static func buildBlock<T1, T2, T3>(_ t1: T1, _ t2: T2, _ t3: T3) |
| 24 | + -> (T1, T2, T3) { |
| 25 | + return (t1, t2, t3) |
| 26 | + } |
| 27 | + |
| 28 | + static func buildBlock<T1, T2, T3, T4>(_ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4) |
| 29 | + -> (T1, T2, T3, T4) { |
| 30 | + return (t1, t2, t3, t4) |
| 31 | + } |
| 32 | + |
| 33 | + static func buildBlock<T1, T2, T3, T4, T5>( |
| 34 | + _ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4, _ t5: T5 |
| 35 | + ) -> (T1, T2, T3, T4, T5) { |
| 36 | + return (t1, t2, t3, t4, t5) |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +func tuplifyWithoutIf<T>(_ cond: Bool, @TupleBuilderWithoutIf body: (Bool) -> T) { |
| 41 | + print(body(cond)) |
| 42 | +} |
| 43 | + |
| 44 | +func testDiags() { |
| 45 | + // Statements unsupported by the particular builder. |
| 46 | + tuplifyWithoutIf(true) { |
| 47 | + if $0 { // expected-error{{closure containing control flow statement cannot be used with function builder 'TupleBuilderWithoutIf'}} |
| 48 | + "hello" |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + tuplifyWithoutIf(true) { |
| 53 | + if $0 { // expected-error{{closure containing control flow statement cannot be used with function builder 'TupleBuilderWithoutIf'}} |
| 54 | + "hello" |
| 55 | + } else { |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + tuplifyWithoutIf(true) { a in |
| 60 | + for x in 0..<100 { // expected-error{{closure containing control flow statement cannot be used with function builder 'TupleBuilderWithoutIf'}} |
| 61 | + x |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments