Skip to content

Commit 6def030

Browse files
committed
[Constraint system] Separate out constraint generation for closures.
1 parent 2fc2b15 commit 6def030

File tree

3 files changed

+20
-13
lines changed

3 files changed

+20
-13
lines changed

lib/Sema/CSGen.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4418,10 +4418,20 @@ bool ConstraintSystem::generateConstraints(
44184418
llvm_unreachable("BOOM");
44194419
}
44204420

4421-
Expr *ConstraintSystem::generateConstraints(ClosureExpr *closure) {
4421+
bool ConstraintSystem::generateConstraints(
4422+
ClosureExpr *closure, Type resultType) {
44224423
assert(closure->hasSingleExpressionBody());
4423-
return generateConstraintsFor(*this, closure->getSingleExpressionBody(),
4424-
closure);
4424+
auto closureBody = generateConstraintsFor(
4425+
*this, closure->getSingleExpressionBody(), closure);
4426+
if (!closureBody)
4427+
return true;
4428+
4429+
bool hasReturn = hasExplicitResult(closure);
4430+
addConstraint(
4431+
ConstraintKind::Conversion, getType(closureBody),
4432+
resultType,
4433+
getConstraintLocator(closure, LocatorPathElt::ClosureBody(hasReturn)));
4434+
return false;
44254435
}
44264436

44274437
Expr *ConstraintSystem::generateConstraints(Expr *expr, DeclContext *dc) {

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7162,14 +7162,8 @@ bool ConstraintSystem::resolveClosure(TypeVariableType *typeVar,
71627162
// If this is a multi-statement closure its body doesn't participate
71637163
// in type-checking.
71647164
if (closure->hasSingleExpressionBody()) {
7165-
auto *closureBody = generateConstraints(closure);
7166-
if (!closureBody)
7165+
if (generateConstraints(closure, closureType->getResult()))
71677166
return false;
7168-
7169-
addConstraint(
7170-
ConstraintKind::Conversion, getType(closureBody),
7171-
closureType->getResult(),
7172-
getConstraintLocator(closure, LocatorPathElt::ClosureBody(hasReturn)));
71737167
} else if (!hasReturn) {
71747168
// If this closure has an empty body and no explicit result type
71757169
// let's bind result type to `Void` since that's the only type empty body

lib/Sema/ConstraintSystem.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3576,10 +3576,13 @@ class ConstraintSystem {
35763576
bool generateConstraints(SolutionApplicationTarget &target,
35773577
FreeTypeVariableBinding allowFreeTypeVariables);
35783578

3579-
/// Generate constraints for the body of the given single-statement closure.
3579+
/// Generate constraints for the body of the given closure.
35803580
///
3581-
/// \returns a possibly-sanitized expression, or null if an error occurred.
3582-
Expr *generateConstraints(ClosureExpr *closure);
3581+
/// \param closure the closure expression
3582+
/// \param resultType the closure's result type
3583+
///
3584+
/// \returns \c true if constraint generation failed, \c false otherwise
3585+
bool generateConstraints(ClosureExpr *closure, Type resultType);
35833586

35843587
/// Generate constraints for the given (unchecked) expression.
35853588
///

0 commit comments

Comments
 (0)