Skip to content

Commit 7c7ef39

Browse files
committed
[CSFix] Implement coalescing for generic argument mismatch fixes
1 parent 17b09a8 commit 7c7ef39

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

include/swift/Sema/CSFix.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,6 +1100,10 @@ class GenericArgumentsMismatch final
11001100
return {getTrailingObjects<unsigned>(), NumMismatches};
11011101
}
11021102

1103+
bool coalesceAndDiagnose(const Solution &solution,
1104+
ArrayRef<ConstraintFix *> secondaryFixes,
1105+
bool asNote = false) const override;
1106+
11031107
bool diagnose(const Solution &solution, bool asNote = false) const override;
11041108

11051109
static GenericArgumentsMismatch *create(ConstraintSystem &cs, Type actual,
@@ -1112,6 +1116,9 @@ class GenericArgumentsMismatch final
11121116
}
11131117

11141118
private:
1119+
bool diagnose(const Solution &solution, ArrayRef<unsigned> mismatches,
1120+
bool asNote = false) const;
1121+
11151122
MutableArrayRef<unsigned> getMismatchesBuf() {
11161123
return {getTrailingObjects<unsigned>(), NumMismatches};
11171124
}

lib/Sema/CSFix.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -732,10 +732,34 @@ AllowFunctionTypeMismatch::create(ConstraintSystem &cs, Type lhs, Type rhs,
732732
AllowFunctionTypeMismatch(cs, lhs, rhs, locator, index);
733733
}
734734

735+
bool GenericArgumentsMismatch::coalesceAndDiagnose(
736+
const Solution &solution, ArrayRef<ConstraintFix *> secondaryFixes,
737+
bool asNote) const {
738+
std::set<unsigned> scratch(getMismatches().begin(), getMismatches().end());
739+
740+
for (auto *fix : secondaryFixes) {
741+
auto *genericArgsFix = fix->castTo<GenericArgumentsMismatch>();
742+
for (auto mismatchIdx : genericArgsFix->getMismatches())
743+
scratch.insert(mismatchIdx);
744+
}
745+
746+
SmallVector<unsigned> mismatches(scratch.begin(), scratch.end());
747+
// Make sure that notes about generic arguments are always produced in order.
748+
llvm::array_pod_sort(mismatches.begin(), mismatches.end());
749+
750+
return diagnose(solution, mismatches, asNote);
751+
}
752+
753+
bool GenericArgumentsMismatch::diagnose(const Solution &solution,
754+
bool asNote) const {
755+
return diagnose(solution, getMismatches(), asNote);
756+
}
757+
735758
bool GenericArgumentsMismatch::diagnose(const Solution &solution,
759+
ArrayRef<unsigned> mismatches,
736760
bool asNote) const {
737761
GenericArgumentsMismatchFailure failure(solution, getFromType(), getToType(),
738-
getMismatches(), getLocator());
762+
mismatches, getLocator());
739763
return failure.diagnose(asNote);
740764
}
741765

0 commit comments

Comments
 (0)