[ConstraintSystem] NFC: Move repairFailure to be a member of ConstraintSystem

That makes it much easier to use various facilities provided by
`ConstraintSystem`.
This commit is contained in:
Pavel Yaskevich
2019-04-30 16:40:04 -07:00
parent 59a11983bc
commit 0ad0a844f3
2 changed files with 26 additions and 20 deletions

View File

@@ -1978,10 +1978,9 @@ static ConstraintFix *fixRequirementFailure(ConstraintSystem &cs, Type type1,
/// Attempt to repair typing failures and record fixes if needed.
/// \return true if at least some of the failures has been repaired
/// successfully, which allows type matcher to continue.
static bool
repairFailures(ConstraintSystem &cs, Type lhs, Type rhs,
SmallVectorImpl<RestrictionOrFix> &conversionsOrFixes,
ConstraintLocatorBuilder locator) {
bool ConstraintSystem::repairFailures(
Type lhs, Type rhs, SmallVectorImpl<RestrictionOrFix> &conversionsOrFixes,
ConstraintLocatorBuilder locator) {
SmallVector<LocatorPathElt, 4> path;
auto *anchor = locator.getLocatorParts(path);
@@ -2005,14 +2004,14 @@ repairFailures(ConstraintSystem &cs, Type lhs, Type rhs,
case ConstraintLocator::ApplyArgToParam: {
if (lhs->getOptionalObjectType() && !rhs->getOptionalObjectType()) {
conversionsOrFixes.push_back(
ForceOptional::create(cs, lhs, lhs->getOptionalObjectType(),
cs.getConstraintLocator(locator)));
ForceOptional::create(*this, lhs, lhs->getOptionalObjectType(),
getConstraintLocator(locator)));
}
break;
}
case ConstraintLocator::FunctionArgument: {
auto *argLoc = cs.getConstraintLocator(
auto *argLoc = getConstraintLocator(
locator.withPathElement(LocatorPathElt::getSynthesizedArgument(0)));
// Let's drop the last element which points to a single argument
@@ -2023,7 +2022,7 @@ repairFailures(ConstraintSystem &cs, Type lhs, Type rhs,
path.back().getKind() == ConstraintLocator::ContextualType))
return false;
auto arg = llvm::find_if(cs.getTypeVariables(),
auto arg = llvm::find_if(getTypeVariables(),
[&argLoc](const TypeVariableType *typeVar) {
return typeVar->getImpl().getLocator() == argLoc;
});
@@ -2040,27 +2039,27 @@ repairFailures(ConstraintSystem &cs, Type lhs, Type rhs,
//
// But if `T.Element` didn't get resolved to `Void` we'd like
// to diagnose this as a missing argument which can't be ignored.
if (arg != cs.getTypeVariables().end()) {
if (arg != getTypeVariables().end()) {
auto fnType = FunctionType::get({FunctionType::Param(lhs)},
cs.getASTContext().TheEmptyTupleType);
getASTContext().TheEmptyTupleType);
conversionsOrFixes.push_back(AddMissingArguments::create(
cs, fnType, {FunctionType::Param(*arg)},
cs.getConstraintLocator(anchor, path,
/*summaryFlags=*/0)));
*this, fnType, {FunctionType::Param(*arg)},
getConstraintLocator(anchor, path,
/*summaryFlags=*/0)));
}
break;
}
case ConstraintLocator::TypeParameterRequirement:
case ConstraintLocator::ConditionalRequirement: {
if (auto *fix = fixRequirementFailure(cs, lhs, rhs, anchor, path))
if (auto *fix = fixRequirementFailure(*this, lhs, rhs, anchor, path))
conversionsOrFixes.push_back(fix);
break;
}
case ConstraintLocator::ClosureResult: {
auto *fix = ContextualMismatch::create(cs, lhs, rhs,
cs.getConstraintLocator(locator));
auto *fix = ContextualMismatch::create(*this, lhs, rhs,
getConstraintLocator(locator));
conversionsOrFixes.push_back(fix);
break;
}
@@ -2070,14 +2069,14 @@ repairFailures(ConstraintSystem &cs, Type lhs, Type rhs,
// between them are mutability and/or root, value type mismatch.
if (isKnownKeyPathType(lhs) && isKnownKeyPathType(rhs)) {
auto *fix = KeyPathContextualMismatch::create(
cs, lhs, rhs, cs.getConstraintLocator(locator));
*this, lhs, rhs, getConstraintLocator(locator));
conversionsOrFixes.push_back(fix);
}
if (lhs->is<FunctionType>() && !rhs->is<AnyFunctionType>() &&
isa<ClosureExpr>(anchor)) {
auto *fix = ContextualMismatch::create(cs, lhs, rhs,
cs.getConstraintLocator(locator));
auto *fix = ContextualMismatch::create(*this, lhs, rhs,
getConstraintLocator(locator));
conversionsOrFixes.push_back(fix);
}
break;
@@ -2899,7 +2898,7 @@ ConstraintSystem::matchTypes(Type type1, Type type2, ConstraintKind kind,
// Attempt to repair any failures identifiable at this point.
if (attemptFixes) {
if (repairFailures(*this, type1, type2, conversionsOrFixes, locator)) {
if (repairFailures(type1, type2, conversionsOrFixes, locator)) {
if (conversionsOrFixes.empty())
return getTypeMatchSuccess();
}