Skip to content

Commit 3697bd5

Browse files
committed
[Diagnostics] While restoring generic parameter types run substitution callback once per parameter
1 parent 9969582 commit 3697bd5

File tree

1 file changed

+15
-5
lines changed

1 file changed

+15
-5
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,13 @@ FailureDiagnostic::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
280280
Type FailureDiagnostic::restoreGenericParameters(
281281
Type type,
282282
llvm::function_ref<void(GenericTypeParamType *, Type)> substitution) {
283+
llvm::SmallPtrSet<GenericTypeParamType *, 4> processed;
283284
return type.transform([&](Type type) -> Type {
284285
if (auto *typeVar = type->getAs<TypeVariableType>()) {
285286
type = resolveType(typeVar);
286287
if (auto *GP = typeVar->getImpl().getGenericParameter()) {
287-
substitution(GP, type);
288+
if (processed.insert(GP).second)
289+
substitution(GP, type);
288290
return GP;
289291
}
290292
}
@@ -3524,22 +3526,30 @@ bool InvalidTupleSplatWithSingleParameterFailure::diagnoseAsError() {
35243526
return false;
35253527

35263528
using Substitution = std::pair<GenericTypeParamType *, Type>;
3527-
llvm::DenseMap<GenericParamKey, Substitution> substitutions;
3529+
llvm::SmallVector<Substitution, 8> substitutions;
35283530

35293531
auto paramTy = restoreGenericParameters(
35303532
ParamType, [&](GenericTypeParamType *GP, Type resolvedType) {
3531-
substitutions[{GP}] = std::make_pair(GP, resolvedType);
3533+
substitutions.push_back(std::make_pair(GP, resolvedType));
35323534
});
35333535

35343536
DeclBaseName name = choice->getBaseName();
35353537

35363538
std::string subsStr;
35373539
if (!substitutions.empty()) {
3540+
llvm::array_pod_sort(
3541+
substitutions.begin(), substitutions.end(),
3542+
[](const std::pair<GenericTypeParamType *, Type> *lhs,
3543+
const std::pair<GenericTypeParamType *, Type> *rhs) -> int {
3544+
GenericParamKey key1(lhs->first);
3545+
GenericParamKey key2(rhs->first);
3546+
return key1 < key2 ? -1 : (key1 == key2) ? 0 : 1;
3547+
});
3548+
35383549
subsStr += " [with ";
35393550
interleave(
35403551
substitutions,
3541-
[&subsStr](const std::pair<GenericParamKey, Substitution> &e) {
3542-
const auto &substitution = e.second;
3552+
[&subsStr](const Substitution &substitution) {
35433553
subsStr += substitution.first->getString();
35443554
subsStr += " = ";
35453555
subsStr += substitution.second->getString();

0 commit comments

Comments
 (0)