Skip to content

[Constraint solver] Fix function builders with single-expression closures #29626

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
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
32 changes: 15 additions & 17 deletions lib/Sema/BuilderTransform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,15 @@ class BuilderClosureVisitor
expressions.push_back(buildVarRef(childVar, childVar->getLoc()));
};

for (const auto &node : braceStmt->getElements()) {
for (auto node : braceStmt->getElements()) {
// Implicit returns in single-expression function bodies are treated
// as the expression.
if (auto returnStmt =
dyn_cast_or_null<ReturnStmt>(node.dyn_cast<Stmt *>())) {
assert(returnStmt->isImplicit());
node = returnStmt->getResult();
}

if (auto stmt = node.dyn_cast<Stmt *>()) {
addChild(visit(stmt));
continue;
Expand Down Expand Up @@ -291,14 +299,9 @@ class BuilderClosureVisitor
}

VarDecl *visitReturnStmt(ReturnStmt *stmt) {
// Allow implicit returns due to 'return' elision.
if (!stmt->isImplicit() || !stmt->hasResult()) {
if (!unhandledNode)
unhandledNode = stmt;
return nullptr;
}

return captureExpr(stmt->getResult(), /*oneWay=*/true);
if (!unhandledNode)
unhandledNode = stmt;
return nullptr;
}

VarDecl *visitDoStmt(DoStmt *doStmt) {
Expand Down Expand Up @@ -1117,7 +1120,8 @@ Optional<BraceStmt *> TypeChecker::applyFunctionBuilderBodyTransform(
return nullptr;
}

ConstraintSystem::TypeMatchResult ConstraintSystem::matchFunctionBuilder(
Optional<ConstraintSystem::TypeMatchResult>
ConstraintSystem::matchFunctionBuilder(
AnyFunctionRef fn, Type builderType, Type bodyResultType,
ConstraintKind bodyResultConstraintKind,
ConstraintLocator *calleeLocator, ConstraintLocatorBuilder locator) {
Expand All @@ -1141,7 +1145,7 @@ ConstraintSystem::TypeMatchResult ConstraintSystem::matchFunctionBuilder(
case FunctionBuilderBodyPreCheck::HasReturnStmt:
// If the body has a return statement, suppress the transform but
// continue solving the constraint system.
return getTypeMatchSuccess();
return None;
}

// Check the form of this body to see if we can apply the
Expand Down Expand Up @@ -1287,12 +1291,6 @@ class PreCheckFunctionBuilderApplication : public ASTWalker {
llvm::Expected<FunctionBuilderBodyPreCheck>
PreCheckFunctionBuilderRequest::evaluate(Evaluator &eval,
AnyFunctionRef fn) const {
// Single-expression closures should already have been pre-checked.
if (auto closure = fn.getAbstractClosureExpr()) {
if (closure->hasSingleExpressionBody())
return FunctionBuilderBodyPreCheck::Okay;
}

return PreCheckFunctionBuilderApplication(fn, false).run();
}

Expand Down
9 changes: 5 additions & 4 deletions lib/Sema/CSSimplify.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6635,10 +6635,11 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
auto *calleeLocator = getCalleeLocator(getConstraintLocator(locator));
if (auto functionBuilderType = getFunctionBuilderTypeFor(
*this, argToParam->getParamIdx(), calleeLocator)) {
auto result = matchFunctionBuilder(
closure, functionBuilderType, closureType->getResult(),
ConstraintKind::Conversion, calleeLocator, locator);
return result.isSuccess();
if (auto result = matchFunctionBuilder(
closure, functionBuilderType, closureType->getResult(),
ConstraintKind::Conversion, calleeLocator, locator)) {
return result->isSuccess();
}
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/ConstraintGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ namespace {
for (auto lhsTypeRep : lhsTypeReps) {
for (auto rhsTypeRep : rhsTypeReps) {
if (lhsTypeRep == rhsTypeRep)
break;
continue;

insertIfUnique(oneWayDigraph[rhsTypeRep].outAdjacencies,lhsTypeRep);
insertIfUnique(oneWayDigraph[lhsTypeRep].inAdjacencies,rhsTypeRep);
Expand Down
5 changes: 4 additions & 1 deletion lib/Sema/ConstraintSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -3726,7 +3726,10 @@ class ConstraintSystem {
void simplifyDisjunctionChoice(Constraint *choice);

/// Apply the given function builder to the closure expression.
TypeMatchResult matchFunctionBuilder(
///
/// \returns \c None when the function builder cannot be applied at all,
/// otherwise the result of applying the function builder.
Optional<TypeMatchResult> matchFunctionBuilder(
AnyFunctionRef fn, Type builderType, Type bodyResultType,
ConstraintKind bodyResultConstraintKind,
ConstraintLocator *calleeLocator, ConstraintLocatorBuilder locator);
Expand Down
15 changes: 15 additions & 0 deletions test/Constraints/function_builder_diags.swift
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,18 @@ func checkConditions(cond: Bool) {
}
}
}

// Check that a closure with a single "return" works with function builders.
func checkSingleReturn(cond: Bool) {
tuplify(cond) { value in
return (value, 17)
}

tuplify(cond) { value in
(value, 17)
}

tuplify(cond) {
($0, 17)
}
}