Skip to content

Commit 8f4658f

Browse files
committed
[CSClosure] Start using recorded targets for ReturnStmts
Multi-statement closures and result builder transformed entities record a target for `return` statement during constraint generation. Single-statement closure do not but they'd be handled in a special way.
1 parent b2593ee commit 8f4658f

File tree

1 file changed

+19
-8
lines changed

1 file changed

+19
-8
lines changed

lib/Sema/CSClosure.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1584,6 +1584,8 @@ class SyntacticElementSolutionApplication
15841584
}
15851585

15861586
ASTNode visitReturnStmt(ReturnStmt *returnStmt) {
1587+
auto &cs = solution.getConstraintSystem();
1588+
15871589
if (!returnStmt->hasResult()) {
15881590
// If contextual is not optional, there is nothing to do here.
15891591
if (resultType->isVoid())
@@ -1598,8 +1600,6 @@ class SyntacticElementSolutionApplication
15981600
assert(resultType->getOptionalObjectType() &&
15991601
resultType->lookThroughAllOptionalTypes()->isVoid());
16001602

1601-
auto &cs = solution.getConstraintSystem();
1602-
16031603
auto target = *cs.getSolutionApplicationTarget(returnStmt);
16041604
returnStmt->setResult(target.getAsExpr());
16051605
}
@@ -1629,13 +1629,24 @@ class SyntacticElementSolutionApplication
16291629
mode = convertToResult;
16301630
}
16311631

1632-
SolutionApplicationTarget resultTarget(
1633-
resultExpr, context.getAsDeclContext(),
1634-
mode == convertToResult ? CTP_ReturnStmt : CTP_Unused,
1635-
mode == convertToResult ? resultType : Type(),
1636-
/*isDiscarded=*/false);
1637-
if (auto newResultTarget = rewriteTarget(resultTarget))
1632+
Optional<SolutionApplicationTarget> resultTarget;
1633+
if (auto target = cs.getSolutionApplicationTarget(returnStmt)) {
1634+
resultTarget = *target;
1635+
} else {
1636+
// Single-expression closures have to handle returns in a special
1637+
// way so the target has to be created for them during solution
1638+
// application based on the resolved type.
1639+
assert(isSingleExpression);
1640+
resultTarget = SolutionApplicationTarget(
1641+
resultExpr, context.getAsDeclContext(),
1642+
mode == convertToResult ? CTP_ReturnStmt : CTP_Unused,
1643+
mode == convertToResult ? resultType : Type(),
1644+
/*isDiscarded=*/false);
1645+
}
1646+
1647+
if (auto newResultTarget = rewriteTarget(*resultTarget)) {
16381648
resultExpr = newResultTarget->getAsExpr();
1649+
}
16391650

16401651
switch (mode) {
16411652
case convertToResult:

0 commit comments

Comments
 (0)