Skip to content

Commit 760d78c

Browse files
committed
[Diagnostics] NFC: Extract type var -> generic param type restoration into a function
1 parent 0dc8633 commit 760d78c

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,22 @@ FailureDiagnostic::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
277277
fnInterfaceType, fnType, callee);
278278
}
279279

280+
Type FailureDiagnostic::restoreGenericParameters(
281+
Type type,
282+
llvm::function_ref<void(GenericTypeParamType *, Type)> substitution) {
283+
return type.transform([&](Type type) -> Type {
284+
if (auto *typeVar = type->getAs<TypeVariableType>()) {
285+
type = resolveType(typeVar);
286+
if (auto *GP = typeVar->getImpl().getGenericParameter()) {
287+
substitution(GP, type);
288+
return GP;
289+
}
290+
}
291+
292+
return type;
293+
});
294+
}
295+
280296
Type RequirementFailure::getOwnerType() const {
281297
auto *anchor = getRawAnchor();
282298

@@ -3508,19 +3524,12 @@ bool InvalidTupleSplatWithSingleParameterFailure::diagnoseAsError() {
35083524
return false;
35093525

35103526
using Substitution = std::pair<GenericTypeParamType *, Type>;
3511-
llvm::SetVector<Substitution> substitutions;
3512-
auto paramTy = ParamType.transform([&](Type type) -> Type {
3513-
if (auto *typeVar = type->getAs<TypeVariableType>()) {
3514-
type = resolveType(typeVar);
3515-
3516-
if (auto *GP = typeVar->getImpl().getGenericParameter()) {
3517-
substitutions.insert(std::make_pair(GP, type));
3518-
return GP;
3519-
}
3520-
}
3527+
llvm::DenseMap<GenericParamKey, Substitution> substitutions;
35213528

3522-
return type;
3523-
});
3529+
auto paramTy = restoreGenericParameters(
3530+
ParamType, [&](GenericTypeParamType *GP, Type resolvedType) {
3531+
substitutions[{GP}] = std::make_pair(GP, resolvedType);
3532+
});
35243533

35253534
DeclBaseName name = choice->getBaseName();
35263535

@@ -3529,7 +3538,8 @@ bool InvalidTupleSplatWithSingleParameterFailure::diagnoseAsError() {
35293538
subsStr += " [where ";
35303539
interleave(
35313540
substitutions,
3532-
[&subsStr](const Substitution &substitution) {
3541+
[&subsStr](const std::pair<GenericParamKey, Substitution> &e) {
3542+
const auto &substitution = e.second;
35333543
subsStr += substitution.first->getString();
35343544
subsStr += " = ";
35353545
subsStr += substitution.second->getString();

lib/Sema/CSDiagnostics.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,13 @@ class FailureDiagnostic {
185185
Optional<FunctionArgApplyInfo>
186186
getFunctionArgApplyInfo(ConstraintLocator *locator) const;
187187

188+
/// \returns A new type with all of the type variables associated with
189+
/// generic parameters substituted back into being generic parameter type.
190+
Type restoreGenericParameters(
191+
Type type,
192+
llvm::function_ref<void(GenericTypeParamType *, Type)> substitution =
193+
[](GenericTypeParamType *, Type) {});
194+
188195
private:
189196
/// Compute anchor expression associated with current diagnostic.
190197
std::pair<Expr *, bool> computeAnchor() const;

0 commit comments

Comments
 (0)