diff --git a/lib/Sema/CSBindings.cpp b/lib/Sema/CSBindings.cpp index 8eaeacc32d4df..09c2407bb32f2 100644 --- a/lib/Sema/CSBindings.cpp +++ b/lib/Sema/CSBindings.cpp @@ -1671,6 +1671,16 @@ PotentialBindings::inferFromRelational(Constraint *constraint) { break; } + case ConstraintKind::OptionalObject: { + // Type variable that represents an object type of + // an un-inferred optional is adjacent to a type + // variable that presents such optional (`bindingTypeVar` + // in this case). + if (kind == AllowedBindingKind::Supertypes) + AdjacentVars.insert({bindingTypeVar, constraint}); + break; + } + default: break; } diff --git a/lib/Sema/CSSimplify.cpp b/lib/Sema/CSSimplify.cpp index 6bb8b50f2d855..251f6e74818cb 100644 --- a/lib/Sema/CSSimplify.cpp +++ b/lib/Sema/CSSimplify.cpp @@ -5424,8 +5424,14 @@ bool ConstraintSystem::repairFailures( auto contextualTy = simplifyType(rhs)->getOptionalObjectType(); if (!lhs->getOptionalObjectType() && !lhs->hasTypeVariable() && contextualTy && !contextualTy->isTypeVariableOrMember()) { - conversionsOrFixes.push_back(IgnoreContextualType::create( - *this, lhs, rhs, getConstraintLocator(OEE->getSubExpr()))); + auto *fixLocator = getConstraintLocator(OEE->getSubExpr()); + // If inner expression already has a fix, consider this two-way + // mismatch as un-salvageable. + if (hasFixFor(fixLocator)) + return false; + + conversionsOrFixes.push_back( + IgnoreContextualType::create(*this, lhs, rhs, fixLocator)); return true; } } diff --git a/test/stmt/foreach.swift b/test/stmt/foreach.swift index 043fafb0c0a02..a0745a41125f1 100644 --- a/test/stmt/foreach.swift +++ b/test/stmt/foreach.swift @@ -341,3 +341,13 @@ do { } } } + +// https://github.com/apple/swift/issues/73207 +do { + func test(_ levels: [Range]) { + for (i, leaves): (Int, Range) in levels[8 ..< 15].enumerated() { // Ok + _ = i + _ = leaves + } + } +}