Skip to content

[Function builders] Align buildDo() implementation with the pitch. #32100

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 3 commits into from
May 31, 2020
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
8 changes: 4 additions & 4 deletions include/swift/AST/Stmt.h
Original file line number Diff line number Diff line change
Expand Up @@ -539,11 +539,11 @@ class LabeledStmt : public Stmt {
/// DoStmt - do statement, without any trailing clauses.
class DoStmt : public LabeledStmt {
SourceLoc DoLoc;
Stmt *Body;
BraceStmt *Body;

public:
DoStmt(LabeledStmtInfo labelInfo, SourceLoc doLoc,
Stmt *body, Optional<bool> implicit = None)
BraceStmt *body, Optional<bool> implicit = None)
: LabeledStmt(StmtKind::Do, getDefaultImplicitFlag(implicit, doLoc),
labelInfo),
DoLoc(doLoc), Body(body) {}
Expand All @@ -553,8 +553,8 @@ class DoStmt : public LabeledStmt {
SourceLoc getStartLoc() const { return getLabelLocOrKeywordLoc(DoLoc); }
SourceLoc getEndLoc() const { return Body->getEndLoc(); }

Stmt *getBody() const { return Body; }
void setBody(Stmt *s) { Body = s; }
BraceStmt *getBody() const { return Body; }
void setBody(BraceStmt *s) { Body = s; }

static bool classof(const Stmt *S) { return S->getKind() == StmtKind::Do; }
};
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/ASTWalker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1493,7 +1493,7 @@ Stmt *Traversal::visitGuardStmt(GuardStmt *US) {
}

Stmt *Traversal::visitDoStmt(DoStmt *DS) {
if (Stmt *S2 = doIt(DS->getBody()))
if (BraceStmt *S2 = cast_or_null<BraceStmt>(doIt(DS->getBody())))
DS->setBody(S2);
else
return nullptr;
Expand Down
14 changes: 7 additions & 7 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,10 @@ class BuilderClosureVisitor
}

VarDecl *visitBraceStmt(BraceStmt *braceStmt) {
return visitBraceStmt(braceStmt, ctx.Id_buildBlock);
}

VarDecl *visitBraceStmt(BraceStmt *braceStmt, Identifier builderFunction) {
SmallVector<Expr *, 4> expressions;
auto addChild = [&](VarDecl *childVar) {
if (!childVar)
Expand Down Expand Up @@ -359,7 +363,7 @@ class BuilderClosureVisitor

// Call Builder.buildBlock(... args ...)
auto call = buildCallIfWanted(braceStmt->getStartLoc(),
ctx.Id_buildBlock, expressions,
builderFunction, expressions,
/*argLabels=*/{ });
if (!call)
return nullptr;
Expand All @@ -380,17 +384,13 @@ class BuilderClosureVisitor
return nullptr;
}

auto childVar = visit(doStmt->getBody());
auto childVar = visitBraceStmt(doStmt->getBody(), ctx.Id_buildDo);
if (!childVar)
return nullptr;

auto childRef = buildVarRef(childVar, doStmt->getEndLoc());
auto call = buildCallIfWanted(doStmt->getStartLoc(), ctx.Id_buildDo,
childRef, /*argLabels=*/{ });
if (!call)
return nullptr;

return captureExpr(call, /*oneWay=*/true, doStmt);
return captureExpr(childRef, /*oneWay=*/true, doStmt);
}

CONTROL_FLOW_STMT(Yield)
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -658,7 +658,7 @@ class StmtChecker : public StmtVisitor<StmtChecker, Stmt*> {

Stmt *visitDoStmt(DoStmt *DS) {
AddLabeledStmt loopNest(*this, DS);
Stmt *S = DS->getBody();
BraceStmt *S = DS->getBody();
typeCheckStmt(S);
DS->setBody(S);
return DS;
Expand Down
20 changes: 18 additions & 2 deletions test/Constraints/function_builder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ enum Either<T,U> {
case second(U)
}

struct Do<T> {
var value: T
}

@_functionBuilder
struct TupleBuilder {
static func buildBlock<T1>(_ t1: T1) -> (T1) {
Expand All @@ -32,7 +36,19 @@ struct TupleBuilder {
return (t1, t2, t3, t4, t5)
}

static func buildDo<T>(_ value: T) -> T { return value }
static func buildDo<T1>(_ t1: T1) -> Do<(T1)> {
.init(value: t1)
}

static func buildDo<T1, T2>(_ t1: T1, _ t2: T2) -> Do<(T1, T2)> {
.init(value: (t1, t2))
}

static func buildDo<T1, T2, T3>(_ t1: T1, _ t2: T2, _ t3: T3)
-> Do<(T1, T2, T3)> {
.init(value: (t1, t2, t3))
}

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

static func buildEither<T,U>(first value: T) -> Either<T,U> {
Expand All @@ -49,7 +65,7 @@ func tuplify<T>(_ cond: Bool, @TupleBuilder body: (Bool) -> T) {
print(body(cond))
}

// CHECK: (17, 3.14159, "Hello, DSL", (["nested", "do"], 6), Optional((2.71828, ["if", "stmt"])))
// CHECK: (17, 3.14159, "Hello, DSL", main.Do<(Swift.Array<Swift.String>, Swift.Int)>(value: (["nested", "do"], 6)), Optional((2.71828, ["if", "stmt"])))
let name = "dsl"
tuplify(true) {
17
Expand Down
10 changes: 9 additions & 1 deletion test/ModuleInterface/function_builders.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ public struct TupleBuilder {
return (t1, t2, t3, t4, t5)
}

public static func buildDo<T>(_ value: T) -> T { return value }
public static func buildDo<T1, T2>(_ t1: T1, _ t2: T2) -> (T1, T2) {
return (t1, t2)
}

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

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

Expand Down