mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
[CSSimplify] Remove ad-hoc optional mismatch handling for arguments
Since we have `repairViaOptionalUnwrap`, let's use that to fix argument/parameter mismatches instead of doing ad-hoc matching.
This commit is contained in:
@@ -4989,8 +4989,18 @@ repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType,
|
||||
|
||||
// First, let's check whether it has been determined that
|
||||
// it was incorrect to use `?` in this position.
|
||||
if (cs.hasFixFor(cs.getConstraintLocator(subExpr), FixKind::RemoveUnwrap))
|
||||
if (cs.hasFixFor(cs.getConstraintLocator(subExpr), FixKind::RemoveUnwrap)) {
|
||||
if (auto *typeVar =
|
||||
fromType->getOptionalObjectType()->getAs<TypeVariableType>()) {
|
||||
// If the optional chain is invalid let's unwrap optional and
|
||||
// re-introduce the constraint to be solved later once both sides
|
||||
// are sufficiently resolved, this would allow to diagnose not only
|
||||
// the invalid unwrap but an invalid conversion (if any) as well.
|
||||
cs.addConstraint(matchKind, typeVar, toType,
|
||||
cs.getConstraintLocator(locator));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
auto type = cs.getType(subExpr);
|
||||
// If the type of sub-expression is optional, type of the
|
||||
@@ -5919,43 +5929,6 @@ bool ConstraintSystem::repairFailures(
|
||||
if (repairByTreatingRValueAsLValue(lhs, rhs))
|
||||
break;
|
||||
|
||||
// If the problem is related to missing unwrap, there is a special
|
||||
// fix for that.
|
||||
if (lhs->getOptionalObjectType() && !rhs->getOptionalObjectType()) {
|
||||
// If this is an attempt to check whether optional conforms to a
|
||||
// particular protocol, let's do that before attempting to force
|
||||
// unwrap the optional.
|
||||
if (hasConversionOrRestriction(ConversionRestrictionKind::Existential))
|
||||
break;
|
||||
|
||||
if (auto *typeVar =
|
||||
lhs->getOptionalObjectType()->getAs<TypeVariableType>()) {
|
||||
auto *argLoc = typeVar->getImpl().getLocator();
|
||||
if (argLoc->directlyAt<OptionalEvaluationExpr>()) {
|
||||
auto OEE = castToExpr<OptionalEvaluationExpr>(argLoc->getAnchor());
|
||||
// If the optional chain in the argument position is invalid
|
||||
// let's unwrap optional and re-introduce the constraint to
|
||||
// be solved later once both sides are sufficiently resolved,
|
||||
// this would allow to diagnose not only the invalid unwrap
|
||||
// but an invalid conversion (if any) as well.
|
||||
if (hasFixFor(getConstraintLocator(OEE->getSubExpr()),
|
||||
FixKind::RemoveUnwrap)) {
|
||||
addConstraint(matchKind, typeVar, rhs, loc);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
auto result = matchTypes(lhs->getOptionalObjectType(), rhs, matchKind,
|
||||
TMF_ApplyingFix, locator);
|
||||
|
||||
if (result.isSuccess()) {
|
||||
conversionsOrFixes.push_back(
|
||||
ForceOptional::create(*this, lhs, rhs, loc));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// If argument in l-value type and parameter is `inout` or a pointer,
|
||||
// let's see if it's generic parameter matches and suggest adding explicit
|
||||
// `&`.
|
||||
@@ -6082,7 +6055,7 @@ bool ConstraintSystem::repairFailures(
|
||||
|
||||
if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind, conversionsOrFixes,
|
||||
locator))
|
||||
break;
|
||||
return true;
|
||||
|
||||
{
|
||||
auto *calleeLocator = getCalleeLocator(loc);
|
||||
|
||||
Reference in New Issue
Block a user