Skip to content

Commit 6296be7

Browse files
author
Amritpan Kaur
committed
[CSSimplify] If bound type var is more optional
than a binding type var, attempt to match types and solve.
1 parent 5fa54df commit 6296be7

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4628,6 +4628,13 @@ repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
46284628
return true;
46294629
}
46304630

4631+
/// Return tuple of type and number of optionals on that type.
4632+
static std::pair<Type, unsigned> getObjectTypeAndNumUnwraps(Type type) {
4633+
SmallVector<Type, 2> optionals;
4634+
Type objType = type->lookThroughAllOptionalTypes(optionals);
4635+
return std::make_pair(objType, optionals.size());
4636+
}
4637+
46314638
static bool
46324639
repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType,
46334640
ConstraintKind matchKind,
@@ -4745,17 +4752,11 @@ repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType,
47454752
}
47464753
}
47474754

4748-
auto getObjectTypeAndUnwraps = [](Type type) -> std::pair<Type, unsigned> {
4749-
SmallVector<Type, 2> optionals;
4750-
Type objType = type->lookThroughAllOptionalTypes(optionals);
4751-
return std::make_pair(objType, optionals.size());
4752-
};
4753-
47544755
Type fromObjectType, toObjectType;
47554756
unsigned fromUnwraps, toUnwraps;
47564757

4757-
std::tie(fromObjectType, fromUnwraps) = getObjectTypeAndUnwraps(fromType);
4758-
std::tie(toObjectType, toUnwraps) = getObjectTypeAndUnwraps(toType);
4758+
std::tie(fromObjectType, fromUnwraps) = getObjectTypeAndNumUnwraps(fromType);
4759+
std::tie(toObjectType, toUnwraps) = getObjectTypeAndNumUnwraps(toType);
47594760

47604761
// Since equality is symmetric and it decays into a `Bind`, eagerly
47614762
// unwrapping optionals from either side might be incorrect since
@@ -6484,6 +6485,19 @@ bool ConstraintSystem::repairFailures(
64846485
if (!fromType || !toType)
64856486
break;
64866487

6488+
Type fromObjectType, toObjectType;
6489+
unsigned fromUnwraps, toUnwraps;
6490+
6491+
std::tie(fromObjectType, fromUnwraps) = getObjectTypeAndNumUnwraps(lhs);
6492+
std::tie(toObjectType, toUnwraps) = getObjectTypeAndNumUnwraps(rhs);
6493+
6494+
// If the bound contextual type is more optional than the binding type, then
6495+
// propogate binding type to contextual type and attempt to solve.
6496+
if (fromUnwraps < toUnwraps) {
6497+
(void)matchTypes(fromObjectType, toObjectType, ConstraintKind::Bind,
6498+
TMF_ApplyingFix, locator);
6499+
}
6500+
64876501
// Drop both `GenericType` elements.
64886502
path.pop_back();
64896503
path.pop_back();

0 commit comments

Comments
 (0)