File: result_builders.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 (249 lines) | stat: -rw-r--r-- 13,749 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// RUN: %target-typecheck-verify-swift

@resultBuilder // expected-error {{'@resultBuilder' attribute cannot be applied to this declaration}}
var globalBuilder: Int

@resultBuilder // expected-error {{'@resultBuilder' attribute cannot be applied to this declaration}}
func globalBuilderFunction() -> Int { return 0 }

@resultBuilder
struct Maker {} // expected-error {{result builder must provide at least one static 'buildBlock' method}}

@resultBuilder
class Inventor {} // expected-error {{result builder must provide at least one static 'buildBlock' method}}

@Maker // expected-error {{result builder attribute 'Maker' can only be applied to a parameter, function, or computed property}}
typealias typename = Inventor

@Maker // expected-error {{result builder attribute 'Maker' can only be applied to a variable if it defines a getter}}
var global: Int

// FIXME: should this be allowed?
@Maker
var globalWithEmptyImplicitGetter: Int {}
// expected-error@-1 {{missing return in accessor expected to return 'Int'}}
// expected-error@-3 {{result builder attribute 'Maker' can only be applied to a variable if it defines a getter}}

@Maker
var globalWithEmptyExplicitGetter: Int { get {} }  // expected-error{{result builder 'Maker' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}

@Maker
var globalWithSingleGetter: Int { 0 } // expected-error {{result builder 'Maker' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}

@Maker
var globalWithMultiGetter: Int { 0; 0 } // expected-error {{result builder 'Maker' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}

@Maker
func globalFunction() {} // expected-error {{result builder 'Maker' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}

@Maker
func globalFunctionWithFunctionParam(fn: () -> ()) {}  // expected-error {{result builder 'Maker' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}

func makerParam(@Maker
                fn: () -> ()) {}

// FIXME: these diagnostics are reversed?
func makerParamRedundant(@Maker // expected-error {{only one result builder attribute can be attached to a parameter}}
                         @Maker // expected-note {{previous result builder specified here}}
                         fn: () -> ()) {}

func makerParamConflict(@Maker // expected-error {{only one result builder attribute can be attached to a parameter}}
                        @Inventor // expected-note {{previous result builder specified here}}
                        fn: () -> ()) {}

func makerParamMissing1(@Missing // expected-error {{unknown attribute 'Missing'}}
                        @Maker
                        fn: () -> ()) {}

func makerParamMissing2(@Maker
                        @Missing // expected-error {{unknown attribute 'Missing'}}
                        fn: () -> ()) {}

func makerParamExtra(@Maker(5) // expected-error {{result builder attributes cannot have arguments}}
                     fn: () -> ()) {}

func makerParamAutoclosure(@Maker // expected-error {{result builder attribute 'Maker' cannot be applied to an autoclosure parameter}}
                           fn: @autoclosure () -> ()) {}

@resultBuilder
struct GenericMaker<T> {} // expected-note {{generic type 'GenericMaker' declared here}} expected-error {{result builder must provide at least one static 'buildBlock' method}}

struct GenericContainer<T> {  // expected-note {{generic type 'GenericContainer' declared here}}
  @resultBuilder
  struct Maker {} // expected-error {{result builder must provide at least one static 'buildBlock' method}}
}

func makeParamUnbound(@GenericMaker // expected-error {{reference to generic type 'GenericMaker' requires arguments}}
                      fn: () -> ()) {}

func makeParamBound(@GenericMaker<Int>
                    fn: () -> ()) {}

func makeParamNestedUnbound(@GenericContainer.Maker // expected-error {{reference to generic type 'GenericContainer' requires arguments}}
                            fn: () -> ()) {}

func makeParamNestedBound(@GenericContainer<Int>.Maker
                          fn: () -> ()) {}


protocol P { }

@resultBuilder
struct ConstrainedGenericMaker<T: P> {} // expected-error {{result builder must provide at least one static 'buildBlock' method}}


struct WithinGeneric<U> {
  func makeParamBoundInContext(@GenericMaker<U> fn: () -> ()) {}

  // expected-error@+1{{type 'U' does not conform to protocol 'P'}}
  func makeParamBoundInContextBad(@ConstrainedGenericMaker<U>
    fn: () -> ()) {}
}

@resultBuilder
struct ValidBuilder1 {
  static func buildBlock(_ exprs: Any...) -> Int { return exprs.count }
}

protocol BuilderFuncHelper {}

extension BuilderFuncHelper {
  static func buildBlock(_ exprs: Any...) -> Int { return exprs.count }
}

@resultBuilder
struct ValidBuilder2: BuilderFuncHelper {}

class BuilderFuncBase {
  static func buildBlock(_ exprs: Any...) -> Int { return exprs.count }
}

@resultBuilder
class ValidBuilder3: BuilderFuncBase {}

@resultBuilder
struct ValidBuilder4 {}
extension ValidBuilder4 {
    static func buildBlock(_ exprs: Any...) -> Int { return exprs.count }
}

@resultBuilder
struct ValidBuilder5 {
    static func buildBlock() -> Int { 0 }
}

@resultBuilder
struct InvalidBuilder1 {} // expected-error {{result builder must provide at least one static 'buildBlock' method}}

@resultBuilder
struct InvalidBuilder2 { // expected-error {{result builder must provide at least one static 'buildBlock' method}}
  func buildBlock(_ exprs: Any...) -> Int { return exprs.count } // expected-note {{did you mean to make instance method 'buildBlock' static?}} {{3-3=static }}
}

@resultBuilder
struct InvalidBuilder3 { // expected-error {{result builder must provide at least one static 'buildBlock' method}}
  var buildBlock: (Any...) -> Int = { return $0.count } // expected-note {{potential match 'buildBlock' is not a static method}}
}

@resultBuilder
struct InvalidBuilder4 {} // expected-error {{result builder must provide at least one static 'buildBlock' method}}
extension InvalidBuilder4 {
  func buildBlock(_ exprs: Any...) -> Int { return exprs.count } // expected-note {{did you mean to make instance method 'buildBlock' static?}} {{3-3=static }}
}

protocol InvalidBuilderHelper {}
extension InvalidBuilderHelper {
  func buildBlock(_ exprs: Any...) -> Int { return exprs.count } // expected-note {{potential match 'buildBlock' is not a static method}}
}

@resultBuilder
struct InvalidBuilder5: InvalidBuilderHelper {} // expected-error {{result builder must provide at least one static 'buildBlock' method}}

@resultBuilder
struct InvalidBuilder6 { // expected-error {{result builder must provide at least one static 'buildBlock' method}}
    static var buildBlock: Int = 0 // expected-note {{potential match 'buildBlock' is not a static method}}
}

struct Callable {
    func callAsFunction(_ exprs: Any...) -> Int { return exprs.count }
}

@resultBuilder
struct InvalidBuilder7 { // expected-error {{result builder must provide at least one static 'buildBlock' method}}
    static var buildBlock = Callable() // expected-note {{potential match 'buildBlock' is not a static method}}
}

class BuilderVarBase {
  static var buildBlock: (Any...) -> Int = { return $0.count } // expected-note {{potential match 'buildBlock' is not a static method}}
}

@resultBuilder
class InvalidBuilder8: BuilderVarBase {} // expected-error {{result builder must provide at least one static 'buildBlock' method}}

protocol BuilderVarHelper {}

extension BuilderVarHelper {
  static var buildBlock: (Any...) -> Int { { return $0.count } } // expected-note {{potential match 'buildBlock' is not a static method}}
}

@resultBuilder
struct InvalidBuilder9: BuilderVarHelper {} // expected-error {{result builder must provide at least one static 'buildBlock' method}}

@resultBuilder
struct InvalidBuilder10 { // expected-error {{result builder must provide at least one static 'buildBlock' method}}
  static var buildBlock: (Any...) -> Int = { return $0.count } // expected-note {{potential match 'buildBlock' is not a static method}}
}

@resultBuilder
enum InvalidBuilder11 { // expected-error {{result builder must provide at least one static 'buildBlock' method}}
    case buildBlock(Any) // expected-note {{enum case 'buildBlock' cannot be used to satisfy the result builder requirement}}
}

@resultBuilder
struct ValidPairwiseBuilder1 {
  static func buildPartialBlock(first: Int) -> Int { fatalError() }
  static func buildPartialBlock(accumulated: Int, next: Int) -> Int { fatalError() }
}

@resultBuilder
struct UnavailablePairwiseBuilder {
  @available(*, unavailable)
  static func buildPartialBlock(first: Int) -> Int { fatalError() }
  @available(*, unavailable)
  static func buildPartialBlock(accumulated: Int, next: Int) -> Int { fatalError() }
  // available
  // static func buildBlock(_: Int...) -> Int { fatalError() }
}

@resultBuilder
struct InvalidPairwiseBuilder1 {} // expected-error {{result builder must provide at least one static 'buildBlock' method, or both 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)'}}

@resultBuilder
struct InvalidPairwiseBuilder2 { // expected-error {{result builder must provide at least one static 'buildBlock' method, or both 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)'}}
  static func buildPartialBlock(first: Any) -> Any { fatalError() }
}
@resultBuilder
struct InvalidPairwiseBuilder3 { // expected-error {{result builder must provide at least one static 'buildBlock' method, or both 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)'}}
  static func buildPartialBlock(accumulated: Any, next: Any) -> Any { fatalError() }
}

struct S {
  @ValidBuilder1 var v1: Int { 1 }
  @ValidBuilder2 var v2: Int { 1 }
  @ValidBuilder3 var v3: Int { 1 }
  @ValidBuilder4 var v4: Int { 1 }
  @ValidBuilder5 func v5() -> Int {}
  @InvalidBuilder1 var i1: Int { 1 } // expected-error {{result builder 'InvalidBuilder1' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
  @InvalidBuilder2 var i2: Int { 1 } // expected-error {{result builder 'InvalidBuilder2' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
  @InvalidBuilder3 var i3: Int { 1 } // expected-error {{result builder 'InvalidBuilder3' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
  @InvalidBuilder4 var i4: Int { 1 } // expected-error {{result builder 'InvalidBuilder4' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
  @InvalidBuilder5 var i5: Int { 1 } // expected-error {{result builder 'InvalidBuilder5' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
  @InvalidBuilder6 var i6: Int { 1 } // expected-error {{result builder 'InvalidBuilder6' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
  @InvalidBuilder7 var i7: Int { 1 } // expected-error {{result builder 'InvalidBuilder7' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
  @InvalidBuilder8 var i8: Int { 1 } // expected-error {{result builder 'InvalidBuilder8' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
  @InvalidBuilder9 var i9: Int { 1 } // expected-error {{result builder 'InvalidBuilder9' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
  @InvalidBuilder10 var i10: Int { 1 } // expected-error {{result builder 'InvalidBuilder10' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
  @InvalidBuilder11 var i11: InvalidBuilder11 { 1 } // expected-error {{result builder 'InvalidBuilder11' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
  @ValidPairwiseBuilder1 var i12: Int { 1 }
  @UnavailablePairwiseBuilder var i13: Int { 1; 1; 1 } // expected-error {{result builder 'UnavailablePairwiseBuilder' does not implement any 'buildBlock' or a combination of 'buildPartialBlock(first:)' and 'buildPartialBlock(accumulated:next:)' with sufficient availability for this call site}}
}