Skip to content

Commit a423937

Browse files
committed
[Constraint solver] Wire up "parents" of closure return statements.
1 parent 8d0014d commit a423937

File tree

2 files changed

+26
-2
lines changed

2 files changed

+26
-2
lines changed

lib/Sema/CSClosure.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ class ClosureConstraintGenerator
8585
return;
8686

8787
// FIXME: Use SolutionApplicationTarget?
88-
cs.setContextualType(
89-
expr, TypeLoc::withoutLoc(closureResultType), CTP_ClosureResult);
9088
expr = cs.generateConstraints(expr, closure, /*isInputExpression=*/false);
9189
if (!expr) {
9290
hadError = true;

lib/Sema/ConstraintSystem.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -590,6 +590,8 @@ static void extendDepthMap(
590590
Expr *expr,
591591
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &depthMap) {
592592
class RecordingTraversal : public ASTWalker {
593+
SmallVector<ClosureExpr *, 4> Closures;
594+
593595
public:
594596
llvm::DenseMap<Expr *, std::pair<unsigned, Expr *>> &DepthMap;
595597
unsigned Depth = 0;
@@ -601,13 +603,37 @@ static void extendDepthMap(
601603
std::pair<bool, Expr *> walkToExprPre(Expr *E) override {
602604
DepthMap[E] = {Depth, Parent.getAsExpr()};
603605
++Depth;
606+
607+
if (auto CE = dyn_cast<ClosureExpr>(E))
608+
Closures.push_back(CE);
609+
604610
return { true, E };
605611
}
606612

607613
Expr *walkToExprPost(Expr *E) override {
614+
if (auto CE = dyn_cast<ClosureExpr>(E)) {
615+
assert(Closures.back() == CE);
616+
Closures.pop_back();
617+
}
618+
608619
--Depth;
609620
return E;
610621
}
622+
623+
std::pair<bool, Stmt *> walkToStmtPre(Stmt *S) override {
624+
if (auto RS = dyn_cast<ReturnStmt>(S)) {
625+
// For return statements, treat the parent of the return expression
626+
// as the closure itself.
627+
if (RS->hasResult() && !Closures.empty()) {
628+
llvm::SaveAndRestore<ParentTy> SavedParent(Parent, Closures.back());
629+
auto E = RS->getResult();
630+
E->walk(*this);
631+
return { false, S };
632+
}
633+
}
634+
635+
return { true, S };
636+
}
611637
};
612638

613639
RecordingTraversal traversal(depthMap);

0 commit comments

Comments
 (0)