@@ -4989,8 +4989,18 @@ repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType,
49894989
49904990 // First, let's check whether it has been determined that
49914991 // it was incorrect to use `?` in this position.
4992- if (cs.hasFixFor(cs.getConstraintLocator(subExpr), FixKind::RemoveUnwrap))
4992+ if (cs.hasFixFor(cs.getConstraintLocator(subExpr), FixKind::RemoveUnwrap)) {
4993+ if (auto *typeVar =
4994+ fromType->getOptionalObjectType()->getAs<TypeVariableType>()) {
4995+ // If the optional chain is invalid let's unwrap optional and
4996+ // re-introduce the constraint to be solved later once both sides
4997+ // are sufficiently resolved, this would allow to diagnose not only
4998+ // the invalid unwrap but an invalid conversion (if any) as well.
4999+ cs.addConstraint(matchKind, typeVar, toType,
5000+ cs.getConstraintLocator(locator));
5001+ }
49935002 return true;
5003+ }
49945004
49955005 auto type = cs.getType(subExpr);
49965006 // If the type of sub-expression is optional, type of the
@@ -5919,43 +5929,6 @@ bool ConstraintSystem::repairFailures(
59195929 if (repairByTreatingRValueAsLValue(lhs, rhs))
59205930 break;
59215931
5922- // If the problem is related to missing unwrap, there is a special
5923- // fix for that.
5924- if (lhs->getOptionalObjectType() && !rhs->getOptionalObjectType()) {
5925- // If this is an attempt to check whether optional conforms to a
5926- // particular protocol, let's do that before attempting to force
5927- // unwrap the optional.
5928- if (hasConversionOrRestriction(ConversionRestrictionKind::Existential))
5929- break;
5930-
5931- if (auto *typeVar =
5932- lhs->getOptionalObjectType()->getAs<TypeVariableType>()) {
5933- auto *argLoc = typeVar->getImpl().getLocator();
5934- if (argLoc->directlyAt<OptionalEvaluationExpr>()) {
5935- auto OEE = castToExpr<OptionalEvaluationExpr>(argLoc->getAnchor());
5936- // If the optional chain in the argument position is invalid
5937- // let's unwrap optional and re-introduce the constraint to
5938- // be solved later once both sides are sufficiently resolved,
5939- // this would allow to diagnose not only the invalid unwrap
5940- // but an invalid conversion (if any) as well.
5941- if (hasFixFor(getConstraintLocator(OEE->getSubExpr()),
5942- FixKind::RemoveUnwrap)) {
5943- addConstraint(matchKind, typeVar, rhs, loc);
5944- return true;
5945- }
5946- }
5947- }
5948-
5949- auto result = matchTypes(lhs->getOptionalObjectType(), rhs, matchKind,
5950- TMF_ApplyingFix, locator);
5951-
5952- if (result.isSuccess()) {
5953- conversionsOrFixes.push_back(
5954- ForceOptional::create(*this, lhs, rhs, loc));
5955- break;
5956- }
5957- }
5958-
59595932 // If argument in l-value type and parameter is `inout` or a pointer,
59605933 // let's see if it's generic parameter matches and suggest adding explicit
59615934 // `&`.
@@ -6082,7 +6055,7 @@ bool ConstraintSystem::repairFailures(
60826055
60836056 if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind, conversionsOrFixes,
60846057 locator))
6085- break ;
6058+ return true ;
60866059
60876060 {
60886061 auto *calleeLocator = getCalleeLocator(loc);
0 commit comments