@@ -8100,6 +8100,38 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
8100
8100
if (conversionsOrFixes.empty())
8101
8101
return getTypeMatchFailure(locator);
8102
8102
8103
+ std::optional<ConversionRestrictionKind> redundantDeepEquality;
8104
+ bool sawDeepEquality = false;
8105
+
8106
+ for (auto potential : conversionsOrFixes) {
8107
+ if (auto restriction = potential.getRestriction()) {
8108
+ switch (*restriction) {
8109
+ case ConversionRestrictionKind::DeepEquality:
8110
+ sawDeepEquality = true;
8111
+ break;
8112
+ case ConversionRestrictionKind::OptionalToOptional:
8113
+ redundantDeepEquality = *restriction;
8114
+ break;
8115
+ default:
8116
+ break;
8117
+ }
8118
+ }
8119
+ }
8120
+
8121
+ auto favoredRestriction = ConversionRestrictionKind::DeepEquality;
8122
+ if (sawDeepEquality && redundantDeepEquality) {
8123
+ favoredRestriction = *redundantDeepEquality;
8124
+
8125
+ conversionsOrFixes.erase(
8126
+ llvm::remove_if(conversionsOrFixes,
8127
+ [&](RestrictionOrFix &potential) {
8128
+ if (auto restriction = potential.getRestriction())
8129
+ return *restriction == ConversionRestrictionKind::DeepEquality;
8130
+ return false;
8131
+ }),
8132
+ conversionsOrFixes.end());
8133
+ }
8134
+
8103
8135
// Where there is more than one potential conversion, create a disjunction
8104
8136
// so that we'll explore all of the options.
8105
8137
if (conversionsOrFixes.size() > 1) {
@@ -8119,7 +8151,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
8119
8151
Constraint::createRestricted(*this, constraintKind, *restriction,
8120
8152
type1, type2, fixedLocator));
8121
8153
8122
- if (constraints.back()->getKind() == ConstraintKind::Bind )
8154
+ if (*restriction == favoredRestriction )
8123
8155
constraints.back()->setFavored();
8124
8156
8125
8157
continue;
@@ -14482,8 +14514,6 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
14482
14514
// also:
14483
14515
// T <c U ===> T? <c U!
14484
14516
case ConversionRestrictionKind::OptionalToOptional: {
14485
- addContextualScore();
14486
-
14487
14517
assert(matchKind >= ConstraintKind::Subtype);
14488
14518
if (auto generic1 = type1->getAs<BoundGenericType>()) {
14489
14519
if (auto generic2 = type2->getAs<BoundGenericType>()) {
0 commit comments