Skip to content

[Function builders] Add support for buildExpression(). #27716

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions include/swift/AST/KnownIdentifiers.def
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ IDENTIFIER_(bridgeToObjectiveC)
IDENTIFIER(buildBlock)
IDENTIFIER(buildDo)
IDENTIFIER(buildEither)
IDENTIFIER(buildExpression)
IDENTIFIER(buildIf)
IDENTIFIER(callAsFunction)
IDENTIFIER(Change)
Expand Down
11 changes: 9 additions & 2 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,15 @@ class BuilderClosureVisitor
}

auto expr = node.get<Expr *>();
if (wantExpr)
expr = new (ctx) OneWayExpr(expr);
if (wantExpr) {
if (builderSupports(ctx.Id_buildExpression)) {
expr = buildCallIfWanted(expr->getLoc(), ctx.Id_buildExpression,
{ expr }, { Identifier() },
/*allowOneWay=*/true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the parameter name is existing code, but why is this "allow" one-way, since it's an unconditional instruction to make the result one-way?

Anyway, this seems like the right interaction with one-way, although arguably it would be clearer in code if that was done unconditionally as a separate step.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It used to be conditional, and I just removed the staging flag. I can clean this up

} else {
expr = new (ctx) OneWayExpr(expr);
}
}

expressions.push_back(expr);
}
Expand Down
45 changes: 45 additions & 0 deletions test/Constraints/function_builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,51 @@ func testAcceptColorTagged(b: Bool, i: Int, s: String, d: Double) {

testAcceptColorTagged(b: true, i: 17, s: "Hello", d: 3.14159)

// Use buildExpression() when it's available.
enum Component {
case string(StaticString)
case floating(Double)
case color(Color)
indirect case array([Component])
indirect case optional(Component?)
}

@_functionBuilder
struct ComponentBuilder {
static func buildExpression(_ string: StaticString) -> Component {
return .string(string)
}

static func buildExpression(_ float: Double) -> Component {
return .floating(float)
}

static func buildExpression(_ color: Color) -> Component {
return .color(color)
}

static func buildBlock(_ components: Component...) -> Component {
return .array(components)
}

static func buildIf(_ value: Component?) -> Component {
return .optional(value)
}
}

func acceptComponentBuilder(@ComponentBuilder _ body: () -> Component) {
print(body())
}

acceptComponentBuilder {
"hello"
if true {
3.14159
}
.red
}
// CHECK: array([main.Component.string("hello"), main.Component.optional(Optional(main.Component.array([main.Component.floating(3.14159)]))), main.Component.color(main.Color.red)])

// rdar://53325810

// Test that we don't have problems with expression pre-checking when
Expand Down