Merge pull request #36445 from xedin/rdar-75367157

[Diagnostics] Check whether missing conformance could be fixed by using `.rawValue`
This commit is contained in:
Pavel Yaskevich
2021-03-17 15:24:33 -07:00
committed by GitHub
7 changed files with 84 additions and 5 deletions

View File

@@ -3261,7 +3261,14 @@ repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType,
// behind itself which we can use to better understand
// how many levels of optionality have to be unwrapped.
if (auto *OEE = dyn_cast<OptionalEvaluationExpr>(anchor)) {
auto type = cs.getType(OEE->getSubExpr());
auto *subExpr = OEE->getSubExpr();
// 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))
return true;
auto type = cs.getType(subExpr);
// If the type of sub-expression is optional, type of the
// `OptionalEvaluationExpr` could be safely ignored because
// it doesn't add any type information.
@@ -6031,6 +6038,14 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyConformsToConstraint(
}
}
if (auto rawValue = isRawRepresentable(*this, type)) {
if (!rawValue->isTypeVariableOrMember() &&
TypeChecker::conformsToProtocol(rawValue, protocol, DC)) {
auto *fix = UseRawValue::create(*this, type, protocolTy, loc);
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
}
}
auto anchor = locator.getAnchor();
if (isExpr<UnresolvedMemberExpr>(anchor) &&
@@ -10769,7 +10784,6 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
case FixKind::AddPropertyWrapperAttribute:
case FixKind::ExpandArrayIntoVarargs:
case FixKind::UseRawValue:
case FixKind::ExplicitlyConstructRawRepresentable:
case FixKind::SpecifyBaseTypeForContextualMember:
case FixKind::CoerceToCheckedCast:
case FixKind::SpecifyObjectLiteralTypeImport:
@@ -10790,6 +10804,17 @@ ConstraintSystem::SolutionKind ConstraintSystem::simplifyFixConstraint(
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
}
case FixKind::ExplicitlyConstructRawRepresentable: {
// Let's increase impact of this fix for binary operators because
// it's possible to get both `.rawValue` and construction fixes for
// different overloads of a binary operator and `.rawValue` is a
// better fix because raw representable has a failable constructor.
return recordFix(fix,
/*impact=*/isExpr<BinaryExpr>(locator.getAnchor()) ? 2 : 1)
? SolutionKind::Error
: SolutionKind::Solved;
}
case FixKind::TreatRValueAsLValue: {
unsigned impact = 1;
// If this is an attempt to use result of a function/subscript call as