@@ -4628,6 +4628,13 @@ repairViaBridgingCast(ConstraintSystem &cs, Type fromType, Type toType,
4628
4628
return true;
4629
4629
}
4630
4630
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
+
4631
4638
static bool
4632
4639
repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType,
4633
4640
ConstraintKind matchKind,
@@ -4745,17 +4752,11 @@ repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType,
4745
4752
}
4746
4753
}
4747
4754
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
-
4754
4755
Type fromObjectType, toObjectType;
4755
4756
unsigned fromUnwraps, toUnwraps;
4756
4757
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);
4759
4760
4760
4761
// Since equality is symmetric and it decays into a `Bind`, eagerly
4761
4762
// unwrapping optionals from either side might be incorrect since
@@ -6484,6 +6485,19 @@ bool ConstraintSystem::repairFailures(
6484
6485
if (!fromType || !toType)
6485
6486
break;
6486
6487
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
+
6487
6501
// Drop both `GenericType` elements.
6488
6502
path.pop_back();
6489
6503
path.pop_back();
0 commit comments