File: result_builder_availability.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (228 lines) | stat: -rw-r--r-- 7,502 bytes parent folder | download
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// RUN: %target-typecheck-verify-swift -target %target-cpu-apple-macosx50

// REQUIRES: OS=macosx

@available(*, unavailable)
@resultBuilder
struct UnavailableBuilder {
// expected-note@-1 2 {{'UnavailableBuilder' has been explicitly marked unavailable here}}
  static func buildBlock() {}
}

@UnavailableBuilder public func usesUnavailableBuilder() {}
// expected-error@-1 {{'UnavailableBuilder' is unavailable}}

public func takesUnavailableBuilder(@UnavailableBuilder _ fn: () -> ()) {}
// expected-error@-1 {{'UnavailableBuilder' is unavailable}}

enum Either<T,U> {
  case first(T)
  case second(U)
}

@resultBuilder
struct TupleBuilder {
// expected-note@-1{{add 'buildLimitedAvailability(_:)' to the result builder 'TupleBuilder' to erase type information for less-available types}}{{22-22=\n    static func buildLimitedAvailability(_ component: <#Component#>) -> <#Component#> {\n      <#code#>\n    \}}}
// expected-note@-2{{add 'buildLimitedAvailability(_:)' to the result builder 'TupleBuilder' to erase type information for less-available types}}{{22-22=\n    static func buildLimitedAvailability(_ component: <#Component#>) -> <#Component#> {\n      <#code#>\n    \}}}
  static func buildBlock<T1>(_ t1: T1) -> (T1) {
    return (t1)
  }

  static func buildBlock<T1, T2>(_ t1: T1, _ t2: T2) -> (T1, T2) {
    return (t1, t2)
  }

  static func buildBlock<T1, T2, T3>(_ t1: T1, _ t2: T2, _ t3: T3)
      -> (T1, T2, T3) {
    return (t1, t2, t3)
  }

  static func buildBlock<T1, T2, T3, T4>(_ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4)
      -> (T1, T2, T3, T4) {
    return (t1, t2, t3, t4)
  }

  static func buildBlock<T1, T2, T3, T4, T5>(
    _ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4, _ t5: T5
  ) -> (T1, T2, T3, T4, T5) {
    return (t1, t2, t3, t4, t5)
  }

  static func buildDo<T>(_ value: T) -> T { return value }
  static func buildIf<T>(_ value: T?) -> T? { return value }

  static func buildEither<T,U>(first value: T) -> Either<T,U> {
    return .first(value)
  }
  static func buildEither<T,U>(second value: U) -> Either<T,U> {
    return .second(value)
  }
}

func tuplify<T>(_ cond: Bool, @TupleBuilder body: (Bool) -> T) {
  print(body(cond))
}

@available(OSX, introduced: 10.9)
func globalFuncAvailableOn10_9() -> Int { return 9 }

@available(OSX, introduced: 51)
func globalFuncAvailableOn51() -> Int { return 10 }

@available(OSX, introduced: 52)
struct Only52 { }

@available(OSX, introduced: 52)
func globalFuncAvailableOn52() -> Only52 { .init() }

tuplify(true) { cond in
  globalFuncAvailableOn10_9()
  if #available(OSX 51, *) {
    globalFuncAvailableOn51()
    tuplify(false) { cond2 in
      if cond, #available(OSX 52, *) { 
        // expected-warning@-1{{result builder 'TupleBuilder' does not implement 'buildLimitedAvailability'; this code may crash on earlier versions of the OS}}
        cond2
        globalFuncAvailableOn52()
      } else if true {
        globalFuncAvailableOn52() // expected-error{{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
        // expected-note@-1{{add 'if #available' version check}}
      } else if false {
        globalFuncAvailableOn52() // expected-error{{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
        // expected-note@-1{{add 'if #available' version check}}
      } else {
        globalFuncAvailableOn52() // expected-error{{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
        // expected-note@-1{{add 'if #available' version check}}
      }
      if cond, #unavailable(OSX 52) { 
        // expected-warning@-1{{result builder 'TupleBuilder' does not implement 'buildLimitedAvailability'; this code may crash on earlier versions of the OS}}
        cond2
        globalFuncAvailableOn52() // expected-error{{'globalFuncAvailableOn52()' is only available in macOS 52 or newer}}
        // expected-note@-1{{add 'if #available' version check}}
      } else if true {
        globalFuncAvailableOn52()
      } else if false {
        globalFuncAvailableOn52()
      }
    }
  }
}

// Function builder that can perform type erasure for #available.
@resultBuilder
struct TupleBuilderAvailability {
  static func buildBlock<T1>(_ t1: T1) -> (T1) {
    return (t1)
  }

  static func buildBlock<T1, T2>(_ t1: T1, _ t2: T2) -> (T1, T2) {
    return (t1, t2)
  }

  static func buildBlock<T1, T2, T3>(_ t1: T1, _ t2: T2, _ t3: T3)
      -> (T1, T2, T3) {
    return (t1, t2, t3)
  }

  static func buildBlock<T1, T2, T3, T4>(_ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4)
      -> (T1, T2, T3, T4) {
    return (t1, t2, t3, t4)
  }

  static func buildBlock<T1, T2, T3, T4, T5>(
    _ t1: T1, _ t2: T2, _ t3: T3, _ t4: T4, _ t5: T5
  ) -> (T1, T2, T3, T4, T5) {
    return (t1, t2, t3, t4, t5)
  }

  static func buildDo<T>(_ value: T) -> T { return value }
  static func buildIf<T>(_ value: T?) -> T? { return value }

  static func buildEither<T,U>(first value: T) -> Either<T,U> {
    return .first(value)
  }
  static func buildEither<T,U>(second value: U) -> Either<T,U> {
    return .second(value)
  }

  static func buildLimitedAvailability<T>(_ value: T) -> Any {
    return value
  }
}

func tuplifyWithAvailabilityErasure<T>(_ cond: Bool, @TupleBuilderAvailability body: (Bool) -> T) {
  print(body(cond))
}

tuplifyWithAvailabilityErasure(true) { cond in
  if cond, #available(OSX 52, *) {
    globalFuncAvailableOn52()
  }

  if cond, #unavailable(OSX 52) {
    cond
  } else {
    globalFuncAvailableOn52()
  }

  // https://github.com/apple/swift/issues/63764
  if #unavailable(OSX 52) {
    cond // Ok
  }
}

// rdar://97533700 – Make sure we can prefer an unavailable buildPartialBlock if
// buildBlock also isn't available.

@resultBuilder
struct UnavailableBuildPartialBlock {
  static func buildPartialBlock(first: Int) -> Int { 0 }

  @available(*, unavailable)
  static func buildPartialBlock(accumulated: Int, next: Int) -> Int { 0 }

  static func buildBlock(_ x: Int...) -> Int { 0 }
}

@UnavailableBuildPartialBlock
func testUnavailableBuildPartialBlock() -> Int {
  // We can use buildBlock here.
  2
  3
}

@resultBuilder
struct UnavailableBuildPartialBlockAndBuildBlock {
  @available(*, unavailable)
  static func buildPartialBlock(first: Int) -> Int { 0 }
  // expected-note@-1 {{'buildPartialBlock(first:)' has been explicitly marked unavailable here}}

  static func buildPartialBlock(accumulated: Int, next: Int) -> Int { 0 }

  @available(*, unavailable)
  static func buildBlock(_ x: Int...) -> Int { 0 }
}

// We can still use buildPartialBlock here as both are unavailable.
@UnavailableBuildPartialBlockAndBuildBlock
func testUnavailableBuildPartialBlockAndBuildBlock() -> Int {
  // expected-error@-1 {{'buildPartialBlock(first:)' is unavailable}}
  2
  3
}

@available(*, unavailable)
@resultBuilder
struct UnavailableBuilderWithPartialBlock { // expected-note {{'UnavailableBuilderWithPartialBlock' has been explicitly marked unavailable here}}
  @available(*, unavailable)
  static func buildPartialBlock(first: String) -> Int { 0 }
  static func buildPartialBlock(accumulated: Int, next: Int) -> Int { 0 }
  static func buildBlock(_ x: Int...) -> Int { 0 }
}

@UnavailableBuilderWithPartialBlock // expected-error {{'UnavailableBuilderWithPartialBlock' is unavailable}}
func testUnavailableBuilderWithPartialBlock() -> Int {
  // The builder itself is unavailable, so we can still opt for buildPartialBlock.
  2 // expected-error {{cannot convert value of type 'Int' to expected argument type 'String'}}
  3
}