Skip to content

Commit a4104d8

Browse files
authored
Merge pull request #5300 from DougGregor/constraint-solver-cleanups
2 parents 8212646 + 2e800f4 commit a4104d8

File tree

4 files changed

+38
-65
lines changed

4 files changed

+38
-65
lines changed

lib/Sema/CSGen.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1727,7 +1727,6 @@ namespace {
17271727
if (arrayElementTy->isTypeVariableOrMember()) {
17281728
CS.addConstraint(ConstraintKind::Defaultable, arrayElementTy,
17291729
tc.Context.TheAnyType, locator);
1730-
CS.ArrayElementTypes[expr] = arrayElementTy;
17311730
}
17321731

17331732
return arrayTy;
@@ -1871,11 +1870,6 @@ namespace {
18711870
tc.Context.TheAnyType, locator);
18721871
}
18731872

1874-
// Record key/value type variables.
1875-
if (dictionaryKeyTy->isTypeVariableOrMember() ||
1876-
dictionaryValueTy->isTypeVariableOrMember())
1877-
CS.DictionaryElementTypes[expr] = {dictionaryKeyTy, dictionaryValueTy};
1878-
18791873
return dictionaryTy;
18801874
}
18811875

lib/Sema/CSSimplify.cpp

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,31 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
11781178
if (kind != TypeMatchKind::ConformsTo && desugar1->isEqual(desugar2))
11791179
return SolutionKind::Solved;
11801180

1181+
// Local function that should be used to produce the return value whenever
1182+
// this function was unable to resolve the constraint. It should be used
1183+
// within \c matchTypes() as
1184+
//
1185+
// return formUnsolvedResult();
1186+
//
1187+
// along any unsolved path. No other returns should produce
1188+
// SolutionKind::Unsolved or inspect TMF_GenerateConstraints.
1189+
auto formUnsolvedResult = [&] {
1190+
// If we're supposed to generate constraints (i.e., this is a
1191+
// newly-generated constraint), do so now.
1192+
if (flags & TMF_GenerateConstraints) {
1193+
// Add a new constraint between these types. We consider the current
1194+
// type-matching problem to the "solved" by this addition, because
1195+
// this new constraint will be solved at a later point.
1196+
// Obviously, this must not happen at the top level, or the
1197+
// algorithm would not terminate.
1198+
addConstraint(getConstraintKind(kind), type1, type2,
1199+
getConstraintLocator(locator));
1200+
return SolutionKind::Solved;
1201+
}
1202+
1203+
return SolutionKind::Unsolved;
1204+
};
1205+
11811206
// If either (or both) types are type variables, unify the type variables.
11821207
if (typeVar1 || typeVar2) {
11831208
switch (kind) {
@@ -1196,24 +1221,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
11961221
// If exactly one of the type variables can bind to an lvalue, we
11971222
// can't merge these two type variables.
11981223
if (rep1->getImpl().canBindToLValue()
1199-
!= rep2->getImpl().canBindToLValue()) {
1200-
if (flags & TMF_GenerateConstraints) {
1201-
if (kind == TypeMatchKind::BindToPointerType) {
1202-
increaseScore(ScoreKind::SK_ScalarPointerConversion);
1203-
}
1204-
1205-
// Add a new constraint between these types. We consider the current
1206-
// type-matching problem to the "solved" by this addition, because
1207-
// this new constraint will be solved at a later point.
1208-
// Obviously, this must not happen at the top level, or the
1209-
// algorithm would not terminate.
1210-
addConstraint(getConstraintKind(kind), rep1, rep2,
1211-
getConstraintLocator(locator));
1212-
return SolutionKind::Solved;
1213-
}
1214-
1215-
return SolutionKind::Unsolved;
1216-
}
1224+
!= rep2->getImpl().canBindToLValue())
1225+
return formUnsolvedResult();
12171226

12181227
// Merge the equivalence classes corresponding to these two variables.
12191228
mergeEquivalenceClasses(rep1, rep2);
@@ -1246,8 +1255,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
12461255

12471256
// A constraint that binds any pointer to a void pointer is
12481257
// ineffective, since any pointer can be converted to a void pointer.
1249-
if (kind == TypeMatchKind::BindToPointerType && desugar2->isVoid() &&
1250-
(flags & TMF_GenerateConstraints)) {
1258+
if (kind == TypeMatchKind::BindToPointerType && desugar2->isVoid()) {
12511259
// Bind type1 to Void only as a last resort.
12521260
addConstraint(ConstraintKind::Defaultable, typeVar1, type2,
12531261
getConstraintLocator(locator));
@@ -1301,7 +1309,8 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
13011309
return SolutionKind::Solved;
13021310
}
13031311
}
1304-
return SolutionKind::Unsolved;
1312+
1313+
return formUnsolvedResult();
13051314
}
13061315

13071316
case TypeMatchKind::ArgumentTupleConversion:
@@ -1323,22 +1332,13 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
13231332
case TypeMatchKind::ArgumentConversion:
13241333
case TypeMatchKind::OperatorArgumentTupleConversion:
13251334
case TypeMatchKind::OperatorArgumentConversion:
1326-
if (flags & TMF_GenerateConstraints) {
1327-
// Add a new constraint between these types. We consider the current
1328-
// type-matching problem to the "solved" by this addition, because
1329-
// this new constraint will be solved at a later point.
1330-
// Obviously, this must not happen at the top level, or the algorithm
1331-
// would not terminate.
1332-
addConstraint(getConstraintKind(kind), type1, type2,
1333-
getConstraintLocator(locator));
1334-
return SolutionKind::Solved;
1335-
}
1336-
13371335
// We couldn't solve this constraint. If only one of the types is a type
13381336
// variable, perhaps we can do something with it below.
1339-
if (typeVar1 && typeVar2)
1340-
return typeVar1 == typeVar2 ? SolutionKind::Solved
1341-
: SolutionKind::Unsolved;
1337+
if (typeVar1 && typeVar2) {
1338+
if (typeVar1 == typeVar2) return SolutionKind::Solved;
1339+
1340+
return formUnsolvedResult();
1341+
}
13421342

13431343
break;
13441344
}
@@ -1355,18 +1355,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
13551355
return ::matchCallArguments(*this, kind, type1, type2, locator);
13561356
}
13571357

1358-
if (flags & TMF_GenerateConstraints) {
1359-
// Add a new constraint between these types. We consider the current
1360-
// type-matching problem to the "solved" by this addition, because
1361-
// this new constraint will be solved at a later point.
1362-
// Obviously, this must not happen at the top level, or the algorithm
1363-
// would not terminate.
1364-
addConstraint(getConstraintKind(kind), type1, type2,
1365-
getConstraintLocator(locator));
1366-
return SolutionKind::Solved;
1367-
}
1368-
1369-
return SolutionKind::Unsolved;
1358+
return formUnsolvedResult();
13701359
}
13711360

13721361
// Decompose parallel structure.
@@ -2067,8 +2056,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, TypeMatchKind kind,
20672056

20682057
if (conversionsOrFixes.empty()) {
20692058
// If one of the types is a type variable, we leave this unsolved.
2070-
if (typeVar1 || typeVar2)
2071-
return SolutionKind::Unsolved;
2059+
if (typeVar1 || typeVar2) return formUnsolvedResult();
20722060

20732061
return SolutionKind::Error;
20742062
}

lib/Sema/ConstraintGraph.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,7 @@ static bool isStrictInoutSubtypeConstraint(Constraint *constraint) {
731731
if (!iot)
732732
return false;
733733

734-
return iot->getObjectType()->getAs<TypeVariableType>() == nullptr;
734+
return !iot->getObjectType()->isTypeVariableOrMember();
735735
}
736736

737737
bool ConstraintGraph::contractEdges() {

lib/Sema/ConstraintSystem.h

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -998,15 +998,6 @@ class ConstraintSystem {
998998
/// The locators of \c Defaultable constraints whose defaults were used.
999999
SmallVector<ConstraintLocator *, 8> DefaultedConstraints;
10001000

1001-
/// The types used to describe the element type of the given array
1002-
/// literal.
1003-
llvm::SmallDenseMap<ArrayExpr *, Type> ArrayElementTypes;
1004-
1005-
/// The types used to describe the key and value types of the given
1006-
/// dictionary literal.
1007-
llvm::SmallDenseMap<DictionaryExpr *, std::pair<Type, Type>>
1008-
DictionaryElementTypes;
1009-
10101001
private:
10111002
/// \brief Describe the candidate expression for partial solving.
10121003
/// This class used by shrink & solve methods which apply

0 commit comments

Comments
 (0)