Skip to content

Commit c9c6311

Browse files
committed
[CSDiagnostics] NFC: Move FailureDiagnostic::resolveType out of header
1 parent 4a2d139 commit c9c6311

File tree

2 files changed

+50
-48
lines changed

2 files changed

+50
-48
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "swift/AST/DiagnosticsClangImporter.h"
2525
#include "swift/AST/ExistentialLayout.h"
2626
#include "swift/AST/Expr.h"
27+
#include "swift/AST/GenericEnvironment.h"
2728
#include "swift/AST/GenericSignature.h"
2829
#include "swift/AST/ImportCache.h"
2930
#include "swift/AST/Initializer.h"
@@ -88,6 +89,54 @@ Type FailureDiagnostic::getRawType(ASTNode node) const {
8889
return S.getType(node);
8990
}
9091

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+
91140
template <typename... ArgTypes>
92141
InFlightDiagnostic
93142
FailureDiagnostic::emitDiagnostic(ArgTypes &&... Args) const {

lib/Sema/CSDiagnostics.h

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include "swift/AST/Decl.h"
2323
#include "swift/AST/DiagnosticEngine.h"
2424
#include "swift/AST/Expr.h"
25-
#include "swift/AST/GenericEnvironment.h"
2625
#include "swift/AST/Identifier.h"
2726
#include "swift/AST/OperatorNameLookup.h"
2827
#include "swift/AST/Types.h"
@@ -99,53 +98,7 @@ class FailureDiagnostic {
9998

10099
/// Resolve type variables present in the raw type, if any.
101100
Type resolveType(Type rawType, bool reconstituteSugar = false,
102-
bool wantRValue = true) const {
103-
rawType = rawType.transform([&](Type type) -> Type {
104-
if (auto *typeVar = type->getAs<TypeVariableType>()) {
105-
auto resolvedType = S.simplifyType(typeVar);
106-
107-
if (!resolvedType->hasUnresolvedType())
108-
return resolvedType;
109-
110-
// If type variable was simplified to an unresolved pack expansion
111-
// type, let's examine its original pattern type because it could
112-
// contain type variables replaceable with their generic parameter
113-
// types.
114-
if (auto *expansion = resolvedType->getAs<PackExpansionType>()) {
115-
auto *locator = typeVar->getImpl().getLocator();
116-
auto *openedExpansionTy =
117-
locator->castLastElementTo<LocatorPathElt::PackExpansionType>()
118-
.getOpenedType();
119-
auto patternType = resolveType(openedExpansionTy->getPatternType());
120-
return PackExpansionType::get(patternType, expansion->getCountType());
121-
}
122-
123-
Type GP = typeVar->getImpl().getGenericParameter();
124-
return resolvedType->is<UnresolvedType>() && GP ? GP : resolvedType;
125-
}
126-
127-
if (type->hasElementArchetype()) {
128-
auto *env = getDC()->getGenericEnvironmentOfContext();
129-
return env->mapElementTypeIntoPackContext(type);
130-
}
131-
132-
if (auto *packType = type->getAs<PackType>()) {
133-
if (packType->getNumElements() == 1) {
134-
auto eltType = resolveType(packType->getElementType(0));
135-
if (auto expansion = eltType->getAs<PackExpansionType>())
136-
return expansion->getPatternType();
137-
}
138-
}
139-
140-
return type->isPlaceholder()
141-
? Type(type->getASTContext().TheUnresolvedType)
142-
: type;
143-
});
144-
145-
if (reconstituteSugar)
146-
rawType = rawType->reconstituteSugar(/*recursive*/ true);
147-
return wantRValue ? rawType->getRValueType() : rawType;
148-
}
101+
bool wantRValue = true) const;
149102

150103
template <typename... ArgTypes>
151104
InFlightDiagnostic emitDiagnostic(ArgTypes &&... Args) const;

0 commit comments

Comments
 (0)