@@ -8158,6 +8158,41 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
8158
8158
if (conversionsOrFixes.empty())
8159
8159
return getTypeMatchFailure(locator);
8160
8160
8161
+ auto favoredRestriction = ConversionRestrictionKind::DeepEquality;
8162
+ if (!(shouldAttemptFixes() ||
8163
+ getASTContext().TypeCheckerOpts.DisableOptimizedRestrictions)) {
8164
+ std::optional<ConversionRestrictionKind> redundantDeepEquality;
8165
+ bool sawDeepEquality = false;
8166
+
8167
+ for (auto potential : conversionsOrFixes) {
8168
+ if (auto restriction = potential.getRestriction()) {
8169
+ switch (*restriction) {
8170
+ case ConversionRestrictionKind::DeepEquality:
8171
+ sawDeepEquality = true;
8172
+ break;
8173
+ case ConversionRestrictionKind::OptionalToOptional:
8174
+ redundantDeepEquality = *restriction;
8175
+ break;
8176
+ default:
8177
+ break;
8178
+ }
8179
+ }
8180
+ }
8181
+
8182
+ if (sawDeepEquality && redundantDeepEquality) {
8183
+ favoredRestriction = *redundantDeepEquality;
8184
+
8185
+ conversionsOrFixes.erase(
8186
+ llvm::remove_if(conversionsOrFixes,
8187
+ [&](RestrictionOrFix &potential) {
8188
+ if (auto restriction = potential.getRestriction())
8189
+ return *restriction == ConversionRestrictionKind::DeepEquality;
8190
+ return false;
8191
+ }),
8192
+ conversionsOrFixes.end());
8193
+ }
8194
+ }
8195
+
8161
8196
// Where there is more than one potential conversion, create a disjunction
8162
8197
// so that we'll explore all of the options.
8163
8198
if (conversionsOrFixes.size() > 1) {
@@ -8177,7 +8212,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
8177
8212
Constraint::createRestricted(*this, constraintKind, *restriction,
8178
8213
type1, type2, fixedLocator));
8179
8214
8180
- if (constraints.back()->getKind() == ConstraintKind::Bind )
8215
+ if (*restriction == favoredRestriction )
8181
8216
constraints.back()->setFavored();
8182
8217
8183
8218
continue;
@@ -14627,7 +14662,9 @@ ConstraintSystem::simplifyRestrictedConstraintImpl(
14627
14662
// also:
14628
14663
// T <c U ===> T? <c U!
14629
14664
case ConversionRestrictionKind::OptionalToOptional: {
14630
- addContextualScore();
14665
+ if (shouldAttemptFixes() ||
14666
+ getASTContext().TypeCheckerOpts.DisableOptimizedRestrictions)
14667
+ addContextualScore();
14631
14668
14632
14669
assert(matchKind >= ConstraintKind::Subtype);
14633
14670
if (auto generic1 = type1->getAs<BoundGenericType>()) {
0 commit comments