Skip to content

Commit 7bbf9fe

Browse files
committed
[Diagnostics] If aggregate fix consists of identical fixes diagnose it specially
If aggregate fix (based on callee locator) differs only in picked overload choices, let's diagnose that via `diagnoseForAmbiguity` associated with a particular fix kind all solutions share, that would produce a tailored diagnostic instead of the most general one.
1 parent b9aa70b commit 7bbf9fe

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

lib/Sema/CSFix.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,10 @@ class ForceOptional final : public ContextualMismatch {
562562

563563
bool diagnose(const Solution &solution, bool asNote = false) const override;
564564

565+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
566+
return diagnose(*commonFixes.front().first);
567+
}
568+
565569
static ForceOptional *create(ConstraintSystem &cs, Type fromType, Type toType,
566570
ConstraintLocator *locator);
567571
};
@@ -671,6 +675,10 @@ class GenericArgumentsMismatch final
671675

672676
bool diagnose(const Solution &solution, bool asNote = false) const override;
673677

678+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
679+
return diagnose(*commonFixes.front().first);
680+
}
681+
674682
static GenericArgumentsMismatch *create(ConstraintSystem &cs, Type actual,
675683
Type required,
676684
llvm::ArrayRef<unsigned> mismatches,
@@ -1531,10 +1539,6 @@ class IgnoreContextualType : public ContextualMismatch {
15311539

15321540
bool diagnose(const Solution &solution, bool asNote = false) const override;
15331541

1534-
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
1535-
return diagnose(*commonFixes.front().first);
1536-
}
1537-
15381542
static IgnoreContextualType *create(ConstraintSystem &cs, Type resultTy,
15391543
Type specifiedTy,
15401544
ConstraintLocator *locator);

lib/Sema/ConstraintSystem.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3074,6 +3074,20 @@ static bool diagnoseAmbiguity(
30743074

30753075
auto &DE = cs.getASTContext().Diags;
30763076

3077+
{
3078+
auto fixKind = aggregateFix.front().second->getKind();
3079+
if (llvm::all_of(
3080+
aggregateFix, [&](const std::pair<const Solution *,
3081+
const ConstraintFix *> &entry) {
3082+
auto &fix = entry.second;
3083+
return fix->getKind() == fixKind && fix->getLocator() == locator;
3084+
})) {
3085+
auto *primaryFix = aggregateFix.front().second;
3086+
if (primaryFix->diagnoseForAmbiguity(aggregateFix))
3087+
return true;
3088+
}
3089+
}
3090+
30773091
auto *decl = ambiguity.choices.front().getDeclOrNull();
30783092
if (!decl)
30793093
return false;
@@ -3234,6 +3248,7 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
32343248
for (const auto &entry : fixes) {
32353249
const auto &solution = *entry.first;
32363250
const auto *fix = entry.second;
3251+
32373252
auto *calleeLocator = solution.getCalleeLocator(fix->getLocator());
32383253

32393254
fixesByCallee[calleeLocator].push_back({&solution, fix});

0 commit comments

Comments
 (0)