Skip to content

Commit 6a40a3a

Browse files
committed
[SE-0289] Add support for @resultBuilder.
"Function builders" are being renamed to "result builders". Add the corresponding `@resultBuilder` attribute, with `@_functionBuilder` as an alias for it, Update test cases to use @resultBuilder.
1 parent 09fc82a commit 6a40a3a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+98
-82
lines changed

include/swift/AST/Attr.def

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -493,8 +493,8 @@ SIMPLE_DECL_ATTR(_disfavoredOverload, DisfavoredOverload,
493493
OnAbstractFunction | OnVar | OnSubscript | UserInaccessible |
494494
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
495495
87)
496-
SIMPLE_DECL_ATTR(_functionBuilder, FunctionBuilder,
497-
OnNominalType | UserInaccessible |
496+
SIMPLE_DECL_ATTR(resultBuilder, FunctionBuilder,
497+
OnNominalType |
498498
ABIStableToAdd | ABIStableToRemove | APIStableToAdd | APIStableToRemove,
499499
88)
500500
DECL_ATTR(_projectedValueProperty, ProjectedValueProperty,

lib/Parse/ParseDecl.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,6 +2708,11 @@ ParserStatus Parser::parseDeclAttribute(DeclAttributes &Attributes, SourceLoc At
27082708
checkInvalidAttrName("_propertyWrapper", "propertyWrapper",
27092709
DAK_PropertyWrapper, diag::attr_renamed_warning);
27102710

2711+
// Historical name for result builders.
2712+
checkInvalidAttrName(
2713+
"_functionBuilder", "resultBuilder", DAK_FunctionBuilder,
2714+
diag::attr_renamed_warning);
2715+
27112716
if (DK == DAK_Count && Tok.getText() == "warn_unused_result") {
27122717
// The behavior created by @warn_unused_result is now the default. Emit a
27132718
// Fix-It to remove.

test/Constraints/function_builder.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ enum Either<T,U> {
66
case second(U)
77
}
88

9-
@_functionBuilder
9+
@resultBuilder
1010
struct TupleBuilder {
1111
static func buildBlock<T1>(_ t1: T1) -> (T1) {
1212
return (t1)
@@ -247,7 +247,7 @@ extension Int: Taggable { }
247247
extension String: Taggable { }
248248
extension Double: Taggable { }
249249

250-
@_functionBuilder
250+
@resultBuilder
251251
struct TaggedBuilder<Tag> {
252252
static func buildBlock() -> () { }
253253

@@ -330,7 +330,7 @@ enum Component {
330330
indirect case optional(Component?)
331331
}
332332

333-
@_functionBuilder
333+
@resultBuilder
334334
struct ComponentBuilder {
335335
static func buildExpression(_ string: StaticString) -> Component {
336336
return .string(string)
@@ -422,7 +422,7 @@ testForEach1.show()
422422
// CHECK: ("testForEach1", main.Either<(Swift.String, Swift.Bool), (Swift.Bool, Swift.String)>.second(true, "end"))
423423

424424
func test_single_stmt_closure_support() {
425-
@_functionBuilder
425+
@resultBuilder
426426
struct MyBuilder {
427427
static func buildBlock(_ numbers: Int...) -> Int {
428428
return 42
@@ -621,7 +621,7 @@ testSwitchCombined(getE(2))
621621

622622

623623
// Test buildOptional(_:) as an alternative to buildIf(_:).
624-
@_functionBuilder
624+
@resultBuilder
625625
struct TupleBuilderWithOpt {
626626
static func buildBlock<T1>(_ t1: T1) -> (T1) {
627627
return (t1)
@@ -715,7 +715,7 @@ extension FunctionBuilderProtocol {
715715
static func buildLimitedAvailability(_ component: Component) -> Component { component }
716716
}
717717

718-
@_functionBuilder
718+
@resultBuilder
719719
enum ArrayBuilder<E>: FunctionBuilderProtocol {
720720
typealias Expression = E
721721
typealias Component = FunctionBuilder<E>

test/Constraints/function_builder_availability.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ enum Either<T,U> {
77
case second(U)
88
}
99

10-
@_functionBuilder
10+
@resultBuilder
1111
struct TupleBuilder { // expected-note{{add 'buildLimitedAvailability(_:)' to the function builder 'TupleBuilder' to erase type information for less-available types}}{{22-22=\n static func buildLimitedAvailability(_ component: <#Component#>) -> <#Component#> {\n <#code#>\n \}}}
1212
static func buildBlock<T1>(_ t1: T1) -> (T1) {
1313
return (t1)
@@ -77,7 +77,7 @@ tuplify(true) { cond in
7777
}
7878

7979
// Function builder that can perform type erasure for #available.
80-
@_functionBuilder
80+
@resultBuilder
8181
struct TupleBuilderAvailability {
8282
static func buildBlock<T1>(_ t1: T1) -> (T1) {
8383
return (t1)

test/Constraints/function_builder_diags.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ enum Either<T,U> {
55
case second(U)
66
}
77

8-
@_functionBuilder
8+
@resultBuilder
99
struct TupleBuilder { // expected-note 2 {{struct 'TupleBuilder' declared here}}
1010
static func buildBlock() -> () { }
1111

@@ -44,7 +44,7 @@ struct TupleBuilder { // expected-note 2 {{struct 'TupleBuilder' declared here}}
4444
}
4545
}
4646

47-
@_functionBuilder
47+
@resultBuilder
4848
struct TupleBuilderWithoutIf { // expected-note 3{{struct 'TupleBuilderWithoutIf' declared here}}
4949
// expected-note@-1{{add 'buildOptional(_:)' to the function builder 'TupleBuilderWithoutIf' to add support for 'if' statements without an 'else'}}
5050
// expected-note@-2{{add 'buildEither(first:)' and 'buildEither(second:)' to the function builder 'TupleBuilderWithoutIf' to add support for 'if'-'else' and 'switch'}}
@@ -171,7 +171,7 @@ struct TupleP<U> : P {
171171
init(_: U) {}
172172
}
173173

174-
@_functionBuilder
174+
@resultBuilder
175175
struct Builder {
176176
static func buildBlock<S0, S1>(_ stmt1: S0, _ stmt2: S1) // expected-note {{required by static method 'buildBlock' where 'S1' = 'Label<_>.Type'}}
177177
-> TupleP<(S0, S1)> where S0: P, S1: P {
@@ -331,7 +331,7 @@ func checkSingleReturn(cond: Bool) {
331331

332332
// rdar://problem/59116520
333333
func checkImplicitSelfInClosure() {
334-
@_functionBuilder
334+
@resultBuilder
335335
struct Builder {
336336
static func buildBlock(_ children: String...) -> Element { Element() }
337337
}
@@ -518,7 +518,7 @@ func testCaseVarTypes(e: E3) {
518518
}
519519

520520
// Test for buildFinalResult.
521-
@_functionBuilder
521+
@resultBuilder
522522
struct WrapperBuilder {
523523
static func buildBlock() -> () { }
524524

@@ -581,7 +581,7 @@ func testWrapperBuilder() {
581581
func rdar61347993() {
582582
struct Result {}
583583

584-
@_functionBuilder
584+
@resultBuilder
585585
struct Builder {
586586
static func buildBlock() -> Result {
587587
Result()

test/Constraints/function_builder_infer.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ enum Either<T,U> {
55
case second(U)
66
}
77

8-
@_functionBuilder
8+
@resultBuilder
99
struct TupleBuilder {
1010
static func buildBlock() -> () {
1111
return ()
@@ -79,7 +79,7 @@ struct DoNotTupleMe {
7979

8080
extension DoNotTupleMe: Tupled { }
8181

82-
@_functionBuilder
82+
@resultBuilder
8383
struct OtherTupleBuilder {
8484
static func buildBlock() -> () {
8585
return ()

test/Constraints/function_builder_one_way.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ enum Either<T,U> {
77
case second(U)
88
}
99

10-
@_functionBuilder
10+
@resultBuilder
1111
struct TupleBuilder {
1212
static func buildBlock<T1>(_ t1: T1) -> T1 {
1313
return t1

test/Constraints/function_builder_opaque_result.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
protocol Taggable {}
44
extension String: Taggable {}
55

6-
@_functionBuilder
6+
@resultBuilder
77
struct TaggableBuilder {
88
static func buildBlock(_ params: Taggable...) -> String {
99
return "Your tags weren't worth keeping anyway"

test/Constraints/rdar64890308.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import Swift
66

7-
@_functionBuilder
7+
@resultBuilder
88
class ArrayBuilder<Element> {
99
static func buildBlock() -> [Element] { [] }
1010
static func buildBlock(_ elt: Element) -> [Element] { [elt] }
@@ -32,7 +32,7 @@ bar("") { x, ty in
3232
protocol P {}
3333
extension String : P {}
3434

35-
@_functionBuilder
35+
@resultBuilder
3636
struct FooBuilder<T> {}
3737

3838
extension FooBuilder where T : P {

test/Constraints/rdar65320500.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
struct Result {}
44

5-
@_functionBuilder
5+
@resultBuilder
66
struct Builder {
77
static func buildBlock() -> Result {
88
Result()

0 commit comments

Comments
 (0)