[Diagnostics] Change FailureDiagnostic::resolveType to replace

holes with generic parameters directly when possible.
This commit is contained in:
Holly Borla
2019-10-31 13:10:33 -07:00
parent 99467b4cc9
commit 33b9d4c446
3 changed files with 32 additions and 23 deletions

View File

@@ -2250,9 +2250,8 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
}
}
template <typename Fn>
Type simplifyTypeImpl(const ConstraintSystem &cs, Type type,
Fn getFixedTypeFn) {
Type ConstraintSystem::simplifyTypeImpl(Type type,
llvm::function_ref<Type(TypeVariableType *)> getFixedTypeFn) const {
return type.transform([&](Type type) -> Type {
if (auto tvt = dyn_cast<TypeVariableType>(type.getPointer()))
return getFixedTypeFn(tvt);
@@ -2261,7 +2260,7 @@ Type simplifyTypeImpl(const ConstraintSystem &cs, Type type,
// the base to a non-type-variable, perform lookup.
if (auto depMemTy = dyn_cast<DependentMemberType>(type.getPointer())) {
// Simplify the base.
Type newBase = simplifyTypeImpl(cs, depMemTy->getBase(), getFixedTypeFn);
Type newBase = simplifyTypeImpl(depMemTy->getBase(), getFixedTypeFn);
// If nothing changed, we're done.
if (newBase.getPointer() == depMemTy->getBase().getPointer())
@@ -2279,7 +2278,7 @@ Type simplifyTypeImpl(const ConstraintSystem &cs, Type type,
if (lookupBaseType->mayHaveMembers()) {
auto *proto = assocType->getProtocol();
auto conformance = cs.DC->getParentModule()->lookupConformance(
auto conformance = DC->getParentModule()->lookupConformance(
lookupBaseType, proto);
if (!conformance)
return DependentMemberType::get(lookupBaseType, assocType);
@@ -2298,21 +2297,15 @@ Type simplifyTypeImpl(const ConstraintSystem &cs, Type type,
});
}
Type ConstraintSystem::simplifyType(Type type, bool forDiagnostics) const {
Type ConstraintSystem::simplifyType(Type type) const {
if (!type->hasTypeVariable())
return type;
// Map type variables down to the fixed types of their representatives.
return simplifyTypeImpl(
*this, type,
return simplifyTypeImpl(type,
[&](TypeVariableType *tvt) -> Type {
if (auto fixed = getFixedType(tvt)) {
if (forDiagnostics && fixed->isHole() &&
tvt->getImpl().getLocator()->isForGenericParameter())
return tvt->getImpl().getLocator()->getGenericParameter();
if (auto fixed = getFixedType(tvt))
return simplifyType(fixed);
}
return getRepresentative(tvt);
});
@@ -2323,8 +2316,7 @@ Type Solution::simplifyType(Type type) const {
return type;
// Map type variables to fixed types from bindings.
return simplifyTypeImpl(
getConstraintSystem(), type,
return getConstraintSystem().simplifyTypeImpl(type,
[&](TypeVariableType *tvt) -> Type {
auto known = typeBindings.find(tvt);
assert(known != typeBindings.end());