Skip to content

Commit 8257cc1

Browse files
committed
[CSClosure] Generate constraints for empty closure body
We should do this in the ConstraintGenerator instead of in a ConstraintSystem method.
1 parent b4549d6 commit 8257cc1

File tree

2 files changed

+22
-22
lines changed

2 files changed

+22
-22
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10797,8 +10797,7 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
1079710797
setSolutionApplicationTarget(closure, target);
1079810798

1079910799
// Generate constraints from the body of this closure.
10800-
return !generateConstraints(
10801-
AnyFunctionRef(cast<AbstractClosureExpr>(closure)), closure->getBody());
10800+
return !generateConstraints(AnyFunctionRef{closure}, closure->getBody());
1080210801
}
1080310802

1080410803
ConstraintSystem::SolutionKind

lib/Sema/CSSyntacticElement.cpp

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -905,6 +905,27 @@ class SyntacticElementConstraintGenerator
905905
}
906906

907907
void visitBraceStmt(BraceStmt *braceStmt) {
908+
auto &ctx = cs.getASTContext();
909+
910+
if (auto closure = cast<ClosureExpr>(context.getAbstractClosureExpr());
911+
context.getBody() == braceStmt) {
912+
// If this closure has an empty body and no explicit result type
913+
// let's bind result type to `Void` since that's the only type empty body
914+
// can produce. Otherwise, if (multi-statement) closure doesn't have
915+
// an explicit result (no `return` statements) let's default it to `Void`.
916+
if (!constraints::hasExplicitResult(closure)) {
917+
auto constraintKind =
918+
(closure->hasEmptyBody() && !closure->hasExplicitResultType())
919+
? ConstraintKind::Bind
920+
: ConstraintKind::Defaultable;
921+
922+
cs.addConstraint(
923+
constraintKind, cs.getClosureType(closure)->getResult(),
924+
ctx.TheEmptyTupleType,
925+
cs.getConstraintLocator(closure, ConstraintLocator::ClosureResult));
926+
}
927+
}
928+
908929
if (context.isSingleExpressionClosure(cs)) {
909930
for (auto node : braceStmt->getElements()) {
910931
if (auto expr = node.dyn_cast<Expr *>()) {
@@ -922,8 +943,6 @@ class SyntacticElementConstraintGenerator
922943
return;
923944
}
924945

925-
auto &ctx = cs.getASTContext();
926-
927946
if (isChildOf(StmtKind::Case)) {
928947
auto *caseStmt = cast<CaseStmt>(
929948
locator->castLastElementTo<LocatorPathElt::SyntacticElement>()
@@ -1131,8 +1150,6 @@ bool ConstraintSystem::generateConstraints(AnyFunctionRef fn, BraceStmt *body) {
11311150
auto closure = cast<ClosureExpr>(fn.getAbstractClosureExpr());
11321151
locator = getConstraintLocator(closure);
11331152

1134-
auto &ctx = closure->getASTContext();
1135-
11361153
if (participatesInInference(closure)) {
11371154
SyntacticElementConstraintGenerator generator(
11381155
*this, closure, getConstraintLocator(closure));
@@ -1143,22 +1160,6 @@ bool ConstraintSystem::generateConstraints(AnyFunctionRef fn, BraceStmt *body) {
11431160
return generator.hadError;
11441161
}
11451162

1146-
// If this closure has an empty body and no explicit result type
1147-
// let's bind result type to `Void` since that's the only type empty body
1148-
// can produce. Otherwise, if (multi-statement) closure doesn't have
1149-
// an explicit result (no `return` statements) let's default it to `Void`.
1150-
if (!hasExplicitResult(closure)) {
1151-
auto constraintKind =
1152-
(closure->hasEmptyBody() && !closure->hasExplicitResultType())
1153-
? ConstraintKind::Bind
1154-
: ConstraintKind::Defaultable;
1155-
1156-
addConstraint(
1157-
constraintKind, getClosureType(closure)->getResult(),
1158-
ctx.TheEmptyTupleType,
1159-
getConstraintLocator(closure, ConstraintLocator::ClosureResult));
1160-
}
1161-
11621163
return false;
11631164
}
11641165

0 commit comments

Comments
 (0)