Skip to content

Commit 1cd5152

Browse files
committed
Move ConstraintSystem::setExprTypes() over to Solution
It belongs on Solutionn, which should contain all of the information needed to perform the operation. Simplify the implementation slightly while doing this, eliminating some dead code.
1 parent 7306d1a commit 1cd5152

File tree

3 files changed

+63
-62
lines changed

3 files changed

+63
-62
lines changed

lib/Sema/CSApply.cpp

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3566,7 +3566,7 @@ namespace {
35663566
// Convert the subexpression.
35673567
Expr *sub = expr->getSubExpr();
35683568

3569-
cs.setExprTypes(sub);
3569+
solution.setExprTypes(sub);
35703570

35713571
if (tc.convertToType(sub, toType, cs.DC))
35723572
return nullptr;
@@ -5600,7 +5600,7 @@ ClosureExpr *ExprRewriter::coerceClosureExprToVoid(ClosureExpr *closureExpr) {
56005600
cs.setType(singleExpr,
56015601
cs.getType(singleExpr)->getWithoutSpecifierType());
56025602

5603-
cs.setExprTypes(singleExpr);
5603+
solution.setExprTypes(singleExpr);
56045604
TypeChecker::checkIgnoredExpr(singleExpr);
56055605

56065606
SmallVector<ASTNode, 2> elements;
@@ -5639,7 +5639,7 @@ ClosureExpr *ExprRewriter::coerceClosureExprFromNever(ClosureExpr *closureExpr)
56395639
auto returnStmt = cast<ReturnStmt>(member.get<Stmt *>());
56405640
auto singleExpr = returnStmt->getResult();
56415641

5642-
cs.setExprTypes(singleExpr);
5642+
solution.setExprTypes(singleExpr);
56435643
TypeChecker::checkIgnoredExpr(singleExpr);
56445644

56455645
SmallVector<ASTNode, 1> elements;
@@ -7035,7 +7035,7 @@ Expr *ExprRewriter::finishApply(ApplyExpr *apply, ConcreteDeclRef callee,
70357035
cs.setType(apply, fnType->getResult());
70367036
apply->setIsSuper(isSuper);
70377037

7038-
cs.setExprTypes(apply);
7038+
solution.setExprTypes(apply);
70397039
Expr *result = TypeChecker::substituteInputSugarTypeForResult(apply);
70407040
cs.cacheExprTypes(result);
70417041

@@ -7336,7 +7336,7 @@ namespace {
73367336
} else {
73377337
// For other closures, type-check the body once we've finished with
73387338
// the expression.
7339-
cs.setExprTypes(closure);
7339+
Rewriter.solution.setExprTypes(closure);
73407340
ClosuresToTypeCheck.push_back(closure);
73417341
}
73427342

@@ -7557,9 +7557,7 @@ Expr *ConstraintSystem::applySolution(Solution &solution, Expr *expr,
75577557
getConstraintLocator(expr));
75587558
}
75597559

7560-
if (result)
7561-
setExprTypes(result);
7562-
7560+
solution.setExprTypes(result);
75637561
rewriter.finalize();
75647562

75657563
return result;
@@ -7574,7 +7572,59 @@ Expr *Solution::coerceToType(Expr *expr, Type toType,
75747572
if (!result)
75757573
return nullptr;
75767574

7577-
cs.setExprTypes(result);
7575+
setExprTypes(result);
75787576
rewriter.finalize();
75797577
return result;
75807578
}
7579+
7580+
namespace {
7581+
class SetExprTypes : public ASTWalker {
7582+
const Solution &solution;
7583+
7584+
public:
7585+
explicit SetExprTypes(const Solution &solution)
7586+
: solution(solution) {}
7587+
7588+
Expr *walkToExprPost(Expr *expr) override {
7589+
auto &cs = solution.getConstraintSystem();
7590+
auto exprType = cs.getType(expr);
7591+
exprType = solution.simplifyType(exprType);
7592+
// assert((!expr->getType() || expr->getType()->isEqual(exprType)) &&
7593+
// "Mismatched types!");
7594+
assert(!exprType->hasTypeVariable() &&
7595+
"Should not write type variable into expression!");
7596+
expr->setType(exprType);
7597+
7598+
if (auto kp = dyn_cast<KeyPathExpr>(expr)) {
7599+
for (auto i : indices(kp->getComponents())) {
7600+
Type componentType;
7601+
if (cs.hasType(kp, i)) {
7602+
componentType = solution.simplifyType(cs.getType(kp, i));
7603+
assert(!componentType->hasTypeVariable() &&
7604+
"Should not write type variable into key-path component");
7605+
}
7606+
7607+
kp->getMutableComponents()[i].setComponentType(componentType);
7608+
}
7609+
}
7610+
7611+
return expr;
7612+
}
7613+
7614+
/// Ignore statements.
7615+
std::pair<bool, Stmt *> walkToStmtPre(Stmt *stmt) override {
7616+
return { false, stmt };
7617+
}
7618+
7619+
/// Ignore declarations.
7620+
bool walkToDeclPre(Decl *decl) override { return false; }
7621+
};
7622+
}
7623+
7624+
void Solution::setExprTypes(Expr *expr) const {
7625+
if (!expr)
7626+
return;
7627+
7628+
SetExprTypes SET(*this);
7629+
expr->walk(SET);
7630+
}

lib/Sema/ConstraintSystem.h

Lines changed: 2 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,8 @@ class Solution {
741741
return None;
742742
}
743743

744+
void setExprTypes(Expr *expr) const;
745+
744746
SWIFT_DEBUG_DUMP;
745747

746748
/// Dump this solution.
@@ -1508,58 +1510,7 @@ class ConstraintSystem {
15081510
bool walkToDeclPre(Decl *decl) override { return false; }
15091511
};
15101512

1511-
class SetExprTypes : public ASTWalker {
1512-
Expr *RootExpr;
1513-
ConstraintSystem &CS;
1514-
bool ExcludeRoot;
1515-
1516-
public:
1517-
SetExprTypes(Expr *expr, ConstraintSystem &cs, bool excludeRoot)
1518-
: RootExpr(expr), CS(cs), ExcludeRoot(excludeRoot) {}
1519-
1520-
Expr *walkToExprPost(Expr *expr) override {
1521-
if (ExcludeRoot && expr == RootExpr)
1522-
return expr;
1523-
1524-
//assert((!expr->getType() || CS.getType(expr)->isEqual(expr->getType()))
1525-
// && "Mismatched types!");
1526-
assert(!CS.getType(expr)->hasTypeVariable() &&
1527-
"Should not write type variable into expression!");
1528-
expr->setType(CS.getType(expr));
1529-
1530-
if (auto kp = dyn_cast<KeyPathExpr>(expr)) {
1531-
for (auto i : indices(kp->getComponents())) {
1532-
Type componentType;
1533-
if (CS.hasType(kp, i))
1534-
componentType = CS.getType(kp, i);
1535-
kp->getMutableComponents()[i].setComponentType(componentType);
1536-
}
1537-
}
1538-
1539-
return expr;
1540-
}
1541-
1542-
/// Ignore statements.
1543-
std::pair<bool, Stmt *> walkToStmtPre(Stmt *stmt) override {
1544-
return { false, stmt };
1545-
}
1546-
1547-
/// Ignore declarations.
1548-
bool walkToDeclPre(Decl *decl) override { return false; }
1549-
};
1550-
15511513
public:
1552-
1553-
void setExprTypes(Expr *expr) {
1554-
SetExprTypes SET(expr, *this, /* excludeRoot = */ false);
1555-
expr->walk(SET);
1556-
}
1557-
1558-
void setSubExprTypes(Expr *expr) {
1559-
SetExprTypes SET(expr, *this, /* excludeRoot = */ true);
1560-
expr->walk(SET);
1561-
}
1562-
15631514
/// Cache the types of the given expression and all subexpressions.
15641515
void cacheExprTypes(Expr *expr) {
15651516
bool excludeRoot = false;

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2980,7 +2980,7 @@ bool TypeChecker::typeCheckForEachBinding(DeclContext *dc, ForEachStmt *stmt) {
29802980
Stmt->setPattern(pattern);
29812981
Stmt->setSequence(expr);
29822982

2983-
cs.setExprTypes(expr);
2983+
solution.setExprTypes(expr);
29842984
return expr;
29852985
}
29862986
};
@@ -3464,7 +3464,7 @@ bool TypeChecker::convertToType(Expr *&expr, Type type, DeclContext *dc,
34643464
return true;
34653465
}
34663466

3467-
cs.setExprTypes(expr);
3467+
solution.setExprTypes(expr);
34683468

34693469
if (getLangOpts().DebugConstraintSolver) {
34703470
auto &log = Context.TypeCheckerDebug->getStream();

0 commit comments

Comments
 (0)