Skip to content

Commit a0693f3

Browse files
committed
[ConstraintSystem] Switch applySolutionToBody to accept AnyFunctionRef
Result builder body could be either a closure or function e.g. accessor, which means that solution application needs to handle both.
1 parent a6421fc commit a0693f3

File tree

2 files changed

+30
-11
lines changed

2 files changed

+30
-11
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,10 @@ T *getAsPattern(ASTNode node) {
647647
return nullptr;
648648
}
649649

650+
template <typename T = Stmt> T *castToStmt(ASTNode node) {
651+
return cast<T>(node.get<Stmt *>());
652+
}
653+
650654
SourceLoc getLoc(ASTNode node);
651655
SourceRange getSourceRange(ASTNode node);
652656

@@ -5656,14 +5660,14 @@ class ConstraintSystem {
56565660
///
56575661
///
56585662
/// \param solution The solution to apply.
5659-
/// \param closure The closure to which the solution is being applied.
5663+
/// \param fn The function or closure to which the solution is being applied.
56605664
/// \param currentDC The declaration context in which transformations
56615665
/// will be applied.
56625666
/// \param rewriteTarget Function that performs a rewrite of any
56635667
/// solution application target within the context.
56645668
///
56655669
/// \returns true if solution cannot be applied.
5666-
bool applySolutionToBody(Solution &solution, ClosureExpr *closure,
5670+
bool applySolutionToBody(Solution &solution, AnyFunctionRef fn,
56675671
DeclContext *&currentDC,
56685672
std::function<Optional<SolutionApplicationTarget>(
56695673
SolutionApplicationTarget)>

lib/Sema/CSClosure.cpp

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1755,25 +1755,40 @@ SolutionApplicationToFunctionResult ConstraintSystem::applySolution(
17551755
}
17561756

17571757
bool ConstraintSystem::applySolutionToBody(Solution &solution,
1758-
ClosureExpr *closure,
1758+
AnyFunctionRef fn,
17591759
DeclContext *&currentDC,
17601760
RewriteTargetFn rewriteTarget) {
1761-
auto &cs = solution.getConstraintSystem();
17621761
// Enter the context of the function before performing any additional
17631762
// transformations.
1764-
llvm::SaveAndRestore<DeclContext *> savedDC(currentDC, closure);
1763+
llvm::SaveAndRestore<DeclContext *> savedDC(currentDC, fn.getAsDeclContext());
1764+
1765+
Type resultTy;
1766+
1767+
if (auto transform = solution.getAppliedBuilderTransform(fn)) {
1768+
resultTy = solution.simplifyType(transform->bodyResultType);
1769+
} else if (auto *closure =
1770+
getAsExpr<ClosureExpr>(fn.getAbstractClosureExpr())) {
1771+
resultTy =
1772+
solution.getResolvedType(closure)->castTo<FunctionType>()->getResult();
1773+
} else {
1774+
resultTy = fn.getBodyResultType();
1775+
}
1776+
1777+
SyntacticElementSolutionApplication application(solution, fn, resultTy,
1778+
rewriteTarget);
17651779

1766-
auto closureType = cs.getType(closure)->castTo<FunctionType>();
1767-
SyntacticElementSolutionApplication application(
1768-
solution, closure, closureType->getResult(), rewriteTarget);
17691780
auto body = application.apply();
17701781

17711782
if (!body || application.hadError)
17721783
return true;
17731784

1774-
closure->setBody(cast<BraceStmt>(body.get<Stmt *>()),
1775-
closure->hasSingleExpressionBody());
1776-
closure->setBodyState(ClosureExpr::BodyState::TypeCheckedWithSignature);
1785+
fn.setTypecheckedBody(castToStmt<BraceStmt>(body),
1786+
fn.hasSingleExpressionBody());
1787+
1788+
if (auto *closure = getAsExpr<ClosureExpr>(fn.getAbstractClosureExpr())) {
1789+
closure->setBodyState(ClosureExpr::BodyState::TypeCheckedWithSignature);
1790+
}
1791+
17771792
return false;
17781793
}
17791794

0 commit comments

Comments
 (0)