Skip to content

Commit 12926eb

Browse files
committed
[Constraint solver] More type map updates.
More updates to read and write types from a side table in the constraint solver and only write back into expressions when we've finished type checking an expression. We still write directly into expressions, and will continue to do so until all of the code has been updated.
1 parent 9e76bc3 commit 12926eb

File tree

5 files changed

+18
-22
lines changed

5 files changed

+18
-22
lines changed

lib/Sema/CSApply.cpp

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,7 +1303,7 @@ namespace {
13031303
// Apply a key path if we have one.
13041304
if (choice.getKind() == OverloadChoiceKind::KeyPathApplication) {
13051305
// The index argument should be (keyPath: KeyPath<Root, Value>).
1306-
auto keyPathTTy = index->getType()->castTo<TupleType>()
1306+
auto keyPathTTy = cs.getType(index)->castTo<TupleType>()
13071307
->getElementType(0);
13081308

13091309
Type valueTy;
@@ -1722,7 +1722,7 @@ namespace {
17221722

17231723
// Form the arguments.
17241724
Expr *args[2] = {
1725-
cs.cacheType(object),
1725+
object,
17261726
cs.cacheType(
17271727
new (tc.Context) DotSelfExpr(
17281728
TypeExpr::createImplicitHack(object->getLoc(),
@@ -2816,8 +2816,10 @@ namespace {
28162816
/*Implicit=*/true,
28172817
argType);
28182818

2819-
cs.cacheExprTypes(typeRef);
2819+
cs.cacheExprTypes(arg);
2820+
28202821
cs.setExprTypes(typeRef);
2822+
cs.setExprTypes(arg);
28212823

28222824
Expr *result = tc.callWitness(typeRef, dc, arrayProto, *conformance,
28232825
name, arg, diag::array_protocol_broken);
@@ -2896,8 +2898,10 @@ namespace {
28962898
/*Implicit=*/false,
28972899
argType);
28982900

2899-
cs.cacheExprTypes(typeRef);
2901+
cs.cacheExprTypes(arg);
2902+
29002903
cs.setExprTypes(typeRef);
2904+
cs.setExprTypes(arg);
29012905

29022906
Expr *result = tc.callWitness(typeRef, dc, dictionaryProto,
29032907
*conformance, name, arg,
@@ -3611,7 +3615,7 @@ namespace {
36113615
// already.
36123616
Expr *simplified = simplifyExprType(expr);
36133617
if (!SuppressDiagnostics
3614-
&& !simplified->getType()->is<UnresolvedType>()) {
3618+
&& !cs.getType(simplified)->is<UnresolvedType>()) {
36153619
cs.TC.diagnose(simplified->getLoc(), diag::pattern_in_expr,
36163620
expr->getSubPattern()->getKind());
36173621
}

lib/Sema/CSDiag.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3353,8 +3353,6 @@ Expr *FailureDiagnosis::typeCheckChildIndependently(
33533353
convertTypePurpose, TCEOptions,
33543354
listener, CS);
33553355

3356-
CS->cacheExprTypes(subExpr);
3357-
33583356
// This is a terrible hack to get around the fact that typeCheckExpression()
33593357
// might change subExpr to point to a new OpenExistentialExpr. In that case,
33603358
// since the caller passed subExpr by value here, they would be left
@@ -3368,9 +3366,11 @@ Expr *FailureDiagnosis::typeCheckChildIndependently(
33683366
if (hadError)
33693367
return nullptr;
33703368

3369+
CS->cacheExprTypes(subExpr);
3370+
33713371
// If we type checked the result but failed to get a usable output from it,
33723372
// just pretend as though nothing happened.
3373-
if (subExpr->getType()->is<ErrorType>()) {
3373+
if (CS->getType(subExpr)->is<ErrorType>()) {
33743374
subExpr = preCheckedExpr;
33753375
SavedTypeData.restore();
33763376
}

lib/Sema/CSGen.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1710,7 +1710,7 @@ namespace {
17101710
unsigned index = 0;
17111711
for (auto element : expr->getElements()) {
17121712
CS.addConstraint(ConstraintKind::Conversion,
1713-
element->getType(),
1713+
CS.getType(element),
17141714
contextualArrayElementType,
17151715
CS.getConstraintLocator(expr,
17161716
LocatorPathElt::
@@ -1738,7 +1738,7 @@ namespace {
17381738
unsigned index = 0;
17391739
for (auto element : expr->getElements()) {
17401740
CS.addConstraint(ConstraintKind::Conversion,
1741-
element->getType(),
1741+
CS.getType(element),
17421742
arrayElementTy,
17431743
CS.getConstraintLocator(
17441744
expr,
@@ -2002,7 +2002,7 @@ namespace {
20022002
// This will avoid exponential typecheck behavior in the case of nested
20032003
// array and dictionary literals.
20042004
Type ty = haveBoundCollectionLiteral ?
2005-
boundExpr->getType() :
2005+
CS.getType(boundExpr) :
20062006
CS.createTypeVariable(CS.getConstraintLocator(locator),
20072007
TVO_CanBindToInOut);
20082008

@@ -2060,7 +2060,7 @@ namespace {
20602060

20612061
Type visitCaptureListExpr(CaptureListExpr *expr) {
20622062
// The type of the capture list is just the type of its closure.
2063-
return expr->getClosureBody()->getType();
2063+
return CS.getType(expr->getClosureBody());
20642064
}
20652065

20662066
/// \brief Walk a closure body to determine if it's possible for
@@ -2773,7 +2773,7 @@ namespace {
27732773

27742774
Type visitKeyPathExpr(KeyPathExpr *E) {
27752775
if (E->isObjC())
2776-
return E->getObjCStringLiteralExpr()->getType();
2776+
return CS.getType(E->getObjCStringLiteralExpr());
27772777

27782778
auto kpDecl = CS.getASTContext().getKeyPathDecl();
27792779

lib/Sema/ConstraintSystem.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,14 +1196,6 @@ class ConstraintSystem {
11961196
SetExprTypes(Expr *expr, ConstraintSystem &cs, bool excludeRoot)
11971197
: RootExpr(expr), CS(cs), ExcludeRoot(excludeRoot) {}
11981198

1199-
std::pair<bool, Expr *> walkToExprPre(Expr *expr) override {
1200-
if (auto *closure = dyn_cast<ClosureExpr>(expr))
1201-
if (!closure->hasSingleExpressionBody())
1202-
return { false, closure };
1203-
1204-
return { true, expr };
1205-
}
1206-
12071199
Expr *walkToExprPost(Expr *expr) override {
12081200
if (ExcludeRoot && expr == RootExpr)
12091201
return expr;

lib/Sema/TypeCheckConstraints.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2181,7 +2181,7 @@ bool TypeChecker::typeCheckBinding(Pattern *&pattern, Expr *&initializer,
21812181
return nullptr;
21822182
}
21832183

2184-
assert(expr->getType()->isEqual(InitType));
2184+
assert(solution.getConstraintSystem().getType(expr)->isEqual(InitType));
21852185

21862186
initializer = expr;
21872187
return expr;

0 commit comments

Comments
 (0)