|
24 | 24 | #include "swift/AST/DiagnosticsClangImporter.h" |
25 | 25 | #include "swift/AST/ExistentialLayout.h" |
26 | 26 | #include "swift/AST/Expr.h" |
| 27 | +#include "swift/AST/GenericEnvironment.h" |
27 | 28 | #include "swift/AST/GenericSignature.h" |
28 | 29 | #include "swift/AST/ImportCache.h" |
29 | 30 | #include "swift/AST/Initializer.h" |
@@ -88,6 +89,54 @@ Type FailureDiagnostic::getRawType(ASTNode node) const { |
88 | 89 | return S.getType(node); |
89 | 90 | } |
90 | 91 |
|
| 92 | +Type FailureDiagnostic::resolveType(Type rawType, bool reconstituteSugar, |
| 93 | + bool wantRValue) const { |
| 94 | + rawType = rawType.transform([&](Type type) -> Type { |
| 95 | + if (auto *typeVar = type->getAs<TypeVariableType>()) { |
| 96 | + auto resolvedType = S.simplifyType(typeVar); |
| 97 | + |
| 98 | + if (!resolvedType->hasUnresolvedType()) |
| 99 | + return resolvedType; |
| 100 | + |
| 101 | + // If type variable was simplified to an unresolved pack expansion |
| 102 | + // type, let's examine its original pattern type because it could |
| 103 | + // contain type variables replaceable with their generic parameter |
| 104 | + // types. |
| 105 | + if (auto *expansion = resolvedType->getAs<PackExpansionType>()) { |
| 106 | + auto *locator = typeVar->getImpl().getLocator(); |
| 107 | + auto *openedExpansionTy = |
| 108 | + locator->castLastElementTo<LocatorPathElt::PackExpansionType>() |
| 109 | + .getOpenedType(); |
| 110 | + auto patternType = resolveType(openedExpansionTy->getPatternType()); |
| 111 | + return PackExpansionType::get(patternType, expansion->getCountType()); |
| 112 | + } |
| 113 | + |
| 114 | + Type GP = typeVar->getImpl().getGenericParameter(); |
| 115 | + return resolvedType->is<UnresolvedType>() && GP ? GP : resolvedType; |
| 116 | + } |
| 117 | + |
| 118 | + if (type->hasElementArchetype()) { |
| 119 | + auto *env = getDC()->getGenericEnvironmentOfContext(); |
| 120 | + return env->mapElementTypeIntoPackContext(type); |
| 121 | + } |
| 122 | + |
| 123 | + if (auto *packType = type->getAs<PackType>()) { |
| 124 | + if (packType->getNumElements() == 1) { |
| 125 | + auto eltType = resolveType(packType->getElementType(0)); |
| 126 | + if (auto expansion = eltType->getAs<PackExpansionType>()) |
| 127 | + return expansion->getPatternType(); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + return type->isPlaceholder() ? Type(type->getASTContext().TheUnresolvedType) |
| 132 | + : type; |
| 133 | + }); |
| 134 | + |
| 135 | + if (reconstituteSugar) |
| 136 | + rawType = rawType->reconstituteSugar(/*recursive*/ true); |
| 137 | + return wantRValue ? rawType->getRValueType() : rawType; |
| 138 | +} |
| 139 | + |
91 | 140 | template <typename... ArgTypes> |
92 | 141 | InFlightDiagnostic |
93 | 142 | FailureDiagnostic::emitDiagnostic(ArgTypes &&... Args) const { |
|
0 commit comments