@@ -1607,6 +1607,8 @@ class SyntacticElementSolutionApplication
16071607 }
16081608
16091609 ASTNode visitReturnStmt (ReturnStmt *returnStmt) {
1610+ auto &cs = solution.getConstraintSystem ();
1611+
16101612 if (!returnStmt->hasResult ()) {
16111613 // If contextual is not optional, there is nothing to do here.
16121614 if (resultType->isVoid ())
@@ -1621,8 +1623,6 @@ class SyntacticElementSolutionApplication
16211623 assert (resultType->getOptionalObjectType () &&
16221624 resultType->lookThroughAllOptionalTypes ()->isVoid ());
16231625
1624- auto &cs = solution.getConstraintSystem ();
1625-
16261626 auto target = *cs.getSolutionApplicationTarget (returnStmt);
16271627 returnStmt->setResult (target.getAsExpr ());
16281628 }
@@ -1652,13 +1652,24 @@ class SyntacticElementSolutionApplication
16521652 mode = convertToResult;
16531653 }
16541654
1655- SolutionApplicationTarget resultTarget (
1656- resultExpr, context.getAsDeclContext (),
1657- mode == convertToResult ? CTP_ReturnStmt : CTP_Unused,
1658- mode == convertToResult ? resultType : Type (),
1659- /* isDiscarded=*/ false );
1660- if (auto newResultTarget = rewriteTarget (resultTarget))
1655+ Optional<SolutionApplicationTarget> resultTarget;
1656+ if (auto target = cs.getSolutionApplicationTarget (returnStmt)) {
1657+ resultTarget = *target;
1658+ } else {
1659+ // Single-expression closures have to handle returns in a special
1660+ // way so the target has to be created for them during solution
1661+ // application based on the resolved type.
1662+ assert (isSingleExpression);
1663+ resultTarget = SolutionApplicationTarget (
1664+ resultExpr, context.getAsDeclContext (),
1665+ mode == convertToResult ? CTP_ReturnStmt : CTP_Unused,
1666+ mode == convertToResult ? resultType : Type (),
1667+ /* isDiscarded=*/ false );
1668+ }
1669+
1670+ if (auto newResultTarget = rewriteTarget (*resultTarget)) {
16611671 resultExpr = newResultTarget->getAsExpr ();
1672+ }
16621673
16631674 switch (mode) {
16641675 case convertToResult:
0 commit comments