Skip to content

Commit 33b9d4c

Browse files
committed
[Diagnostics] Change FailureDiagnostic::resolveType to replace
holes with generic parameters directly when possible.
1 parent 99467b4 commit 33b9d4c

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

lib/Sema/CSDiagnostics.h

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,23 @@ class FailureDiagnostic {
9898
/// Resolve type variables present in the raw type, if any.
9999
Type resolveType(Type rawType, bool reconstituteSugar = false,
100100
bool wantRValue = true) const {
101-
auto resolvedType = CS.simplifyType(rawType, /*forDiagnostics*/true);
102-
103-
if (reconstituteSugar)
104-
resolvedType = resolvedType->reconstituteSugar(/*recursive*/ true);
101+
if (!rawType->hasTypeVariable()) {
102+
if (reconstituteSugar)
103+
rawType = rawType->reconstituteSugar(/*recursive*/ true);
104+
return wantRValue ? rawType->getRValueType() : rawType;
105+
}
105106

106-
return wantRValue ? resolvedType->getRValueType() : resolvedType;
107+
auto &cs = getConstraintSystem();
108+
return cs.simplifyTypeImpl(rawType,
109+
[&](TypeVariableType *typeVar) -> Type {
110+
if (auto fixed = cs.getFixedType(typeVar)) {
111+
auto *genericParam = typeVar->getImpl().getGenericParameter();
112+
if (fixed->isHole() && genericParam)
113+
return genericParam;
114+
return resolveType(fixed, reconstituteSugar, wantRValue);
115+
}
116+
return cs.getRepresentative(typeVar);
117+
});
107118
}
108119

109120
/// Resolve type variables present in the raw type, using generic parameter

lib/Sema/ConstraintSystem.cpp

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,9 +2250,8 @@ void ConstraintSystem::resolveOverload(ConstraintLocator *locator,
22502250
}
22512251
}
22522252

2253-
template <typename Fn>
2254-
Type simplifyTypeImpl(const ConstraintSystem &cs, Type type,
2255-
Fn getFixedTypeFn) {
2253+
Type ConstraintSystem::simplifyTypeImpl(Type type,
2254+
llvm::function_ref<Type(TypeVariableType *)> getFixedTypeFn) const {
22562255
return type.transform([&](Type type) -> Type {
22572256
if (auto tvt = dyn_cast<TypeVariableType>(type.getPointer()))
22582257
return getFixedTypeFn(tvt);
@@ -2261,7 +2260,7 @@ Type simplifyTypeImpl(const ConstraintSystem &cs, Type type,
22612260
// the base to a non-type-variable, perform lookup.
22622261
if (auto depMemTy = dyn_cast<DependentMemberType>(type.getPointer())) {
22632262
// Simplify the base.
2264-
Type newBase = simplifyTypeImpl(cs, depMemTy->getBase(), getFixedTypeFn);
2263+
Type newBase = simplifyTypeImpl(depMemTy->getBase(), getFixedTypeFn);
22652264

22662265
// If nothing changed, we're done.
22672266
if (newBase.getPointer() == depMemTy->getBase().getPointer())
@@ -2279,7 +2278,7 @@ Type simplifyTypeImpl(const ConstraintSystem &cs, Type type,
22792278

22802279
if (lookupBaseType->mayHaveMembers()) {
22812280
auto *proto = assocType->getProtocol();
2282-
auto conformance = cs.DC->getParentModule()->lookupConformance(
2281+
auto conformance = DC->getParentModule()->lookupConformance(
22832282
lookupBaseType, proto);
22842283
if (!conformance)
22852284
return DependentMemberType::get(lookupBaseType, assocType);
@@ -2298,21 +2297,15 @@ Type simplifyTypeImpl(const ConstraintSystem &cs, Type type,
22982297
});
22992298
}
23002299

2301-
Type ConstraintSystem::simplifyType(Type type, bool forDiagnostics) const {
2300+
Type ConstraintSystem::simplifyType(Type type) const {
23022301
if (!type->hasTypeVariable())
23032302
return type;
23042303

23052304
// Map type variables down to the fixed types of their representatives.
2306-
return simplifyTypeImpl(
2307-
*this, type,
2305+
return simplifyTypeImpl(type,
23082306
[&](TypeVariableType *tvt) -> Type {
2309-
if (auto fixed = getFixedType(tvt)) {
2310-
if (forDiagnostics && fixed->isHole() &&
2311-
tvt->getImpl().getLocator()->isForGenericParameter())
2312-
return tvt->getImpl().getLocator()->getGenericParameter();
2313-
2307+
if (auto fixed = getFixedType(tvt))
23142308
return simplifyType(fixed);
2315-
}
23162309

23172310
return getRepresentative(tvt);
23182311
});
@@ -2323,8 +2316,7 @@ Type Solution::simplifyType(Type type) const {
23232316
return type;
23242317

23252318
// Map type variables to fixed types from bindings.
2326-
return simplifyTypeImpl(
2327-
getConstraintSystem(), type,
2319+
return getConstraintSystem().simplifyTypeImpl(type,
23282320
[&](TypeVariableType *tvt) -> Type {
23292321
auto known = typeBindings.find(tvt);
23302322
assert(known != typeBindings.end());

lib/Sema/ConstraintSystem.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -986,7 +986,8 @@ class ConstraintSystem {
986986
DeclContext *DC;
987987
ConstraintSystemOptions Options;
988988
Optional<ExpressionTimer> Timer;
989-
989+
990+
friend class Solution;
990991
friend class ConstraintFix;
991992
friend class OverloadChoice;
992993
friend class ConstraintGraph;
@@ -3033,7 +3034,7 @@ class ConstraintSystem {
30333034
///
30343035
/// The resulting types can be compared canonically, so long as additional
30353036
/// type equivalence requirements aren't introduced between comparisons.
3036-
Type simplifyType(Type type, bool forDiagnostics = false) const;
3037+
Type simplifyType(Type type) const;
30373038

30383039
/// Simplify a type, by replacing type variables with either their
30393040
/// fixed types (if available) or their representatives.
@@ -3073,6 +3074,11 @@ class ConstraintSystem {
30733074
isConversionEphemeral(ConversionRestrictionKind conversion,
30743075
ConstraintLocatorBuilder locator);
30753076

3077+
/// Simplifies a type by replacing type variables with the result of
3078+
/// \c getFixedTypeFn and performing lookup on dependent member types.
3079+
Type simplifyTypeImpl(Type type,
3080+
llvm::function_ref<Type(TypeVariableType *)> getFixedTypeFn) const;
3081+
30763082
/// Attempt to simplify the given construction constraint.
30773083
///
30783084
/// \param valueType The type being constructed.

0 commit comments

Comments
 (0)