@@ -907,24 +907,34 @@ class SyntacticElementConstraintGenerator
907907 void visitBraceStmt (BraceStmt *braceStmt) {
908908 auto &ctx = cs.getASTContext ();
909909
910- if (auto closure = cast<ClosureExpr>(context.getAsAbstractClosureExpr ().getPtrOrNull ()); context.getBody () == braceStmt) {
911- // If this closure has an empty body and no explicit result type
912- // let's bind result type to `Void` since that's the only type empty body
913- // can produce. Otherwise, if (multi-statement) closure doesn't have
914- // an explicit result (no `return` statements) let's default it to `Void`.
915- if (!constraints::hasExplicitResult (closure)) {
916- auto constraintKind =
917- (closure->hasEmptyBody () && !closure->hasExplicitResultType ())
918- ? ConstraintKind::Bind
919- : ConstraintKind::Defaultable;
920-
921- cs.addConstraint (
922- constraintKind, cs.getClosureType (closure)->getResult (),
923- ctx.TheEmptyTupleType ,
924- cs.getConstraintLocator (closure, ConstraintLocator::ClosureResult));
910+ auto addResultDefault = [&](ClosureExpr *closure) {
911+ if (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
914+ // body can produce. Otherwise, if (multi-statement) closure doesn't
915+ // have an explicit result (no `return` statements) let's default it to
916+ // `Void`.
917+ if (!constraints::hasExplicitResult (closure)) {
918+ auto constraintKind =
919+ (closure->hasEmptyBody () && !closure->hasExplicitResultType ())
920+ ? ConstraintKind::Bind
921+ : ConstraintKind::Defaultable;
922+
923+ cs.addConstraint (constraintKind,
924+ cs.getClosureType (closure)->getResult (),
925+ ctx.TheEmptyTupleType ,
926+ cs.getConstraintLocator (
927+ closure, ConstraintLocator::ClosureResult));
928+ }
925929 }
926- if (!cs.participatesInInference (closure))
930+ };
931+
932+ if (auto closure = cast<ClosureExpr>(
933+ context.getAsAbstractClosureExpr ().getPtrOrNull ())) {
934+ if (!cs.participatesInInference (closure)) {
935+ addResultDefault (closure);
927936 return ;
937+ }
928938 }
929939
930940 if (context.isSingleExpressionClosure (cs)) {
@@ -941,6 +951,10 @@ class SyntacticElementConstraintGenerator
941951 visitDecl (node.get <Decl *>());
942952 }
943953 }
954+ if (!hadError)
955+ addResultDefault (cast<ClosureExpr>(
956+ context.getAsAbstractClosureExpr ().getPtrOrNull ()));
957+
944958 return ;
945959 }
946960
@@ -974,6 +988,10 @@ class SyntacticElementConstraintGenerator
974988 /* contextualInfo=*/ {}, isDiscarded));
975989 }
976990
991+ if (!hadError)
992+ addResultDefault (
993+ cast<ClosureExpr>(context.getAsAbstractClosureExpr ().getPtrOrNull ()));
994+
977995 createConjunction (cs, elements, locator);
978996 }
979997
0 commit comments