Skip to content

Commit 0db31c9

Browse files
committed
Don't attempt invalid bindings that we expect to need fixes if shouldAttemptFixes is off.
1 parent 4d862d5 commit 0db31c9

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -713,22 +713,28 @@ bool ConstraintSystem::tryTypeVariableBindings(
713713
newBindings.push_back({subtype, binding.Kind, binding.BindingSource});
714714
}
715715

716-
// If we were unsuccessful solving for T?, try solving for T.
717-
if (auto objTy = type->getOptionalObjectType()) {
718-
if (exploredTypes.insert(objTy->getCanonicalType()).second) {
719-
// If T is a type variable, only attempt this if both the
720-
// type variable we are trying bindings for, and the type
721-
// variable we will attempt to bind, both have the same
722-
// polarity with respect to being able to bind lvalues.
723-
if (auto otherTypeVar = objTy->getAs<TypeVariableType>()) {
724-
if (typeVar->getImpl().canBindToLValue() ==
725-
otherTypeVar->getImpl().canBindToLValue()) {
716+
// Allow solving for T even for a binding kind where that's invalid
717+
// if fixes are allowed, because that gives us the opportunity to
718+
// match T? values to the T binding by adding an unwrap fix.
719+
if (binding.Kind == AllowedBindingKind::Subtypes ||
720+
shouldAttemptFixes()) {
721+
// If we were unsuccessful solving for T?, try solving for T.
722+
if (auto objTy = type->getOptionalObjectType()) {
723+
if (exploredTypes.insert(objTy->getCanonicalType()).second) {
724+
// If T is a type variable, only attempt this if both the
725+
// type variable we are trying bindings for, and the type
726+
// variable we will attempt to bind, both have the same
727+
// polarity with respect to being able to bind lvalues.
728+
if (auto otherTypeVar = objTy->getAs<TypeVariableType>()) {
729+
if (typeVar->getImpl().canBindToLValue() ==
730+
otherTypeVar->getImpl().canBindToLValue()) {
731+
newBindings.push_back(
732+
{objTy, binding.Kind, binding.BindingSource});
733+
}
734+
} else {
726735
newBindings.push_back(
727736
{objTy, binding.Kind, binding.BindingSource});
728737
}
729-
} else {
730-
newBindings.push_back(
731-
{objTy, binding.Kind, binding.BindingSource});
732738
}
733739
}
734740
}

0 commit comments

Comments
 (0)