Skip to content

Commit a61a56a

Browse files
authored
Merge pull request #6183 from rudkx/cs-typemap
Update constraint generation to set types in the constraint system's …
2 parents e0a2d56 + 1f6b5ca commit a61a56a

File tree

1 file changed

+25
-13
lines changed

1 file changed

+25
-13
lines changed

lib/Sema/CSGen.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2690,7 +2690,7 @@ namespace {
26902690
CS.addConstraint(ConstraintKind::Defaultable,
26912691
placeholderTy, TupleType::getEmpty(CS.getASTContext()),
26922692
locator);
2693-
E->setType(placeholderTy);
2693+
CS.setType(E, placeholderTy);
26942694
}
26952695
// NOTE: The type loc may be there but have failed to validate, in which
26962696
// case we return the null type.
@@ -2803,10 +2803,11 @@ namespace {
28032803
// For closures containing only a single expression, the body participates
28042804
// in type checking.
28052805
if (auto closure = dyn_cast<ClosureExpr>(expr)) {
2806+
auto &CS = CG.getConstraintSystem();
28062807
if (closure->hasSingleExpressionBody()) {
28072808
// Visit the closure itself, which produces a function type.
28082809
auto funcTy = CG.visit(expr)->castTo<FunctionType>();
2809-
expr->setType(funcTy);
2810+
CS.setType(expr, funcTy);
28102811
}
28112812

28122813
return { true, expr };
@@ -2828,18 +2829,23 @@ namespace {
28282829
/// generate constraints from the expression.
28292830
Expr *walkToExprPost(Expr *expr) override {
28302831
if (auto closure = dyn_cast<ClosureExpr>(expr)) {
2831-
// If the function type has an error in it, we don't want to solve the
2832-
// system.
2833-
if (closure->getType() &&
2834-
(closure->getType()->hasError() ||
2835-
closure->getType()->getCanonicalType()->hasError()))
2836-
return nullptr;
2837-
28382832
if (closure->hasSingleExpressionBody()) {
2833+
auto &CS = CG.getConstraintSystem();
2834+
Type closureTy = closure->getType();
2835+
2836+
// If the function type has an error in it, we don't want to solve the
2837+
// system.
2838+
if (closureTy &&
2839+
(closureTy->hasError() ||
2840+
closureTy->getCanonicalType()->hasError()))
2841+
return nullptr;
2842+
2843+
CS.setType(closure, closureTy);
2844+
28392845
// Visit the body. It's type needs to be convertible to the function's
28402846
// return type.
2841-
auto resultTy = closure->getResultType();
2842-
auto bodyTy = closure->getSingleExpressionBody()->getType();
2847+
auto resultTy = closureTy->castTo<FunctionType>()->getResult();
2848+
Type bodyTy = closure->getSingleExpressionBody()->getType();
28432849
CG.getConstraintSystem().setFavoredType(expr, bodyTy.getPointer());
28442850
CG.getConstraintSystem()
28452851
.addConstraint(ConstraintKind::Conversion, bodyTy,
@@ -2853,7 +2859,11 @@ namespace {
28532859
}
28542860

28552861
if (auto type = CG.visit(expr)) {
2856-
expr->setType(CG.getConstraintSystem().simplifyType(type));
2862+
auto &CS = CG.getConstraintSystem();
2863+
auto simplifiedType = CS.simplifyType(type);
2864+
2865+
CS.setType(expr, simplifiedType);
2866+
28572867
return expr;
28582868
}
28592869

@@ -2954,7 +2964,9 @@ Expr *ConstraintSystem::generateConstraintsShallow(Expr *expr) {
29542964

29552965
this->optimizeConstraints(expr);
29562966

2957-
expr->setType(type);
2967+
auto &CS = CG.getConstraintSystem();
2968+
CS.setType(expr, type);
2969+
29582970
return expr;
29592971
}
29602972

0 commit comments

Comments
 (0)