@@ -1607,6 +1607,8 @@ class SyntacticElementSolutionApplication
1607
1607
}
1608
1608
1609
1609
ASTNode visitReturnStmt (ReturnStmt *returnStmt) {
1610
+ auto &cs = solution.getConstraintSystem ();
1611
+
1610
1612
if (!returnStmt->hasResult ()) {
1611
1613
// If contextual is not optional, there is nothing to do here.
1612
1614
if (resultType->isVoid ())
@@ -1621,8 +1623,6 @@ class SyntacticElementSolutionApplication
1621
1623
assert (resultType->getOptionalObjectType () &&
1622
1624
resultType->lookThroughAllOptionalTypes ()->isVoid ());
1623
1625
1624
- auto &cs = solution.getConstraintSystem ();
1625
-
1626
1626
auto target = *cs.getSolutionApplicationTarget (returnStmt);
1627
1627
returnStmt->setResult (target.getAsExpr ());
1628
1628
}
@@ -1652,13 +1652,24 @@ class SyntacticElementSolutionApplication
1652
1652
mode = convertToResult;
1653
1653
}
1654
1654
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)) {
1661
1671
resultExpr = newResultTarget->getAsExpr ();
1672
+ }
1662
1673
1663
1674
switch (mode) {
1664
1675
case convertToResult:
0 commit comments