Skip to content

Commit df43268

Browse files
committed
[CSBindings] Inference cannot fail
Any constraints which would previously cause binding inference to fail should instead delay associated type variable and preserve all of the collected information. This is vital for incremental binding computation that cannot re-introduce constraints after "failure" because it's too expensive.
1 parent 62dc8ce commit df43268

File tree

2 files changed

+14
-22
lines changed

2 files changed

+14
-22
lines changed

include/swift/Sema/CSBindings.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ struct PotentialBindings {
464464
Optional<PotentialBinding> inferFromRelational(Constraint *constraint);
465465

466466
public:
467-
bool infer(Constraint *constraint);
467+
void infer(Constraint *constraint);
468468

469469
/// Finalize binding computation for this type variable by
470470
/// inferring bindings from context e.g. transitive bindings.

lib/Sema/CSBindings.cpp

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -812,13 +812,8 @@ PotentialBindings ConstraintSystem::inferBindingsFor(TypeVariableType *typeVar,
812812
auto constraints = CG.gatherConstraints(
813813
typeVar, ConstraintGraph::GatheringKind::EquivalenceClass);
814814

815-
for (auto *constraint : constraints) {
816-
bool failed = bindings.infer(constraint);
817-
818-
// Upon inference failure let's produce an empty set of bindings.
819-
if (failed)
820-
return {*this, typeVar};
821-
}
815+
for (auto *constraint : constraints)
816+
bindings.infer(constraint);
822817

823818
if (finalize) {
824819
llvm::SmallDenseMap<TypeVariableType *, PotentialBindings> inferred;
@@ -1068,7 +1063,7 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
10681063
/// Retrieve the set of potential type bindings for the given
10691064
/// representative type variable, along with flags indicating whether
10701065
/// those types should be opened.
1071-
bool PotentialBindings::infer(Constraint *constraint) {
1066+
void PotentialBindings::infer(Constraint *constraint) {
10721067
switch (constraint->getKind()) {
10731068
case ConstraintKind::Bind:
10741069
case ConstraintKind::Equal:
@@ -1152,8 +1147,10 @@ bool PotentialBindings::infer(Constraint *constraint) {
11521147
// [existential] metatype.
11531148
auto dynamicType = constraint->getFirstType();
11541149
if (auto *tv = dynamicType->getAs<TypeVariableType>()) {
1155-
if (tv->getImpl().getRepresentative(nullptr) == TypeVar)
1156-
return true;
1150+
if (tv->getImpl().getRepresentative(nullptr) == TypeVar) {
1151+
DelayedBy.push_back(constraint);
1152+
break;
1153+
}
11571154
}
11581155

11591156
// This is right-hand side, let's continue.
@@ -1174,19 +1171,14 @@ bool PotentialBindings::infer(Constraint *constraint) {
11741171
// associated with closure literal (e.g. coercion to some other
11751172
// type) let's delay resolving the closure until the disjunction
11761173
// is attempted.
1177-
if (TypeVar->getImpl().isClosureType())
1178-
return true;
1179-
11801174
DelayedBy.push_back(constraint);
11811175
break;
11821176

11831177
case ConstraintKind::ConformsTo:
11841178
case ConstraintKind::SelfObjectOfProtocol: {
11851179
auto protocolTy = constraint->getSecondType();
1186-
if (!protocolTy->is<ProtocolType>())
1187-
return false;
1188-
1189-
Protocols.push_back(constraint);
1180+
if (protocolTy->is<ProtocolType>())
1181+
Protocols.push_back(constraint);
11901182
break;
11911183
}
11921184

@@ -1247,15 +1239,15 @@ bool PotentialBindings::infer(Constraint *constraint) {
12471239
// side of a one-way binding.
12481240
auto firstType = constraint->getFirstType();
12491241
if (auto *tv = firstType->getAs<TypeVariableType>()) {
1250-
if (tv->getImpl().getRepresentative(nullptr) == TypeVar)
1251-
return true;
1242+
if (tv->getImpl().getRepresentative(nullptr) == TypeVar) {
1243+
DelayedBy.push_back(constraint);
1244+
break;
1245+
}
12521246
}
12531247

12541248
break;
12551249
}
12561250
}
1257-
1258-
return false;
12591251
}
12601252

12611253
LiteralBindingKind PotentialBindings::getLiteralKind() const {

0 commit comments

Comments
 (0)