Skip to content

Commit a9e9211

Browse files
committed
[ConstraintSystem] Augment condition/ternary simplication to support statements
1 parent 9bd05b7 commit a9e9211

File tree

2 files changed

+22
-3
lines changed

2 files changed

+22
-3
lines changed

include/swift/Sema/ConstraintSystem.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,13 @@ template <typename T = Decl> T *getAsDecl(ASTNode node) {
566566
return nullptr;
567567
}
568568

569+
template <typename T = Stmt>
570+
T *getAsStmt(ASTNode node) {
571+
if (auto *S = node.dyn_cast<Stmt *>())
572+
return dyn_cast_or_null<T>(S);
573+
return nullptr;
574+
}
575+
569576
SourceLoc getLoc(ASTNode node);
570577
SourceRange getSourceRange(ASTNode node);
571578

lib/Sema/ConstraintSystem.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4817,16 +4817,28 @@ void constraints::simplifyLocator(ASTNode &anchor,
48174817
}
48184818

48194819
case ConstraintLocator::Condition: {
4820-
anchor = castToExpr<IfExpr>(anchor)->getCondExpr();
4820+
if (auto *condStmt = getAsStmt<LabeledConditionalStmt>(anchor)) {
4821+
anchor = &condStmt->getCond().front();
4822+
} else {
4823+
anchor = castToExpr<IfExpr>(anchor)->getCondExpr();
4824+
}
4825+
48214826
path = path.slice(1);
48224827
continue;
48234828
}
48244829

48254830
case ConstraintLocator::TernaryBranch: {
48264831
auto branch = path[0].castTo<LocatorPathElt::TernaryBranch>();
4827-
auto *ifExpr = castToExpr<IfExpr>(anchor);
48284832

4829-
anchor = branch.forThen() ? ifExpr->getThenExpr() : ifExpr->getElseExpr();
4833+
if (auto *ifStmt = getAsStmt<IfStmt>(anchor)) {
4834+
anchor =
4835+
branch.forThen() ? ifStmt->getThenStmt() : ifStmt->getElseStmt();
4836+
} else {
4837+
auto *ifExpr = castToExpr<IfExpr>(anchor);
4838+
anchor =
4839+
branch.forThen() ? ifExpr->getThenExpr() : ifExpr->getElseExpr();
4840+
}
4841+
48304842
path = path.slice(1);
48314843
continue;
48324844
}

0 commit comments

Comments
 (0)