Skip to content

[Diagnostics] In DefineMemberBasedOnUse::diagnoseForAmbiguity, use the base type from each solution #30844

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 8, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -489,10 +489,13 @@ bool DefineMemberBasedOnUse::diagnose(const Solution &solution,
}

bool
DefineMemberBasedOnUse::diagnoseForAmbiguity(ArrayRef<Solution> solutions) const {
DefineMemberBasedOnUse::diagnoseForAmbiguity(CommonFixesArray commonFixes) const {
Type concreteBaseType;
for (const auto &solution: solutions) {
auto baseType = solution.simplifyType(BaseType);
for (const auto &solutionAndFix : commonFixes) {
const auto *solution = solutionAndFix.first;
const auto *fix = solutionAndFix.second->getAs<DefineMemberBasedOnUse>();

auto baseType = solution->simplifyType(fix->BaseType);
if (!concreteBaseType)
concreteBaseType = baseType;

Expand All @@ -503,7 +506,7 @@ DefineMemberBasedOnUse::diagnoseForAmbiguity(ArrayRef<Solution> solutions) const
}
}

return diagnose(solutions.front());
return diagnose(*commonFixes.front().first);
}

DefineMemberBasedOnUse *
Expand Down
29 changes: 19 additions & 10 deletions lib/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,12 @@ class ConstraintFix {
virtual bool diagnose(const Solution &solution,
bool asNote = false) const = 0;

virtual bool diagnoseForAmbiguity(ArrayRef<Solution> solutions) const { return false; }
using CommonFixesArray =
ArrayRef<std::pair<const Solution *, const ConstraintFix *>>;

virtual bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const {
return false;
}

void print(llvm::raw_ostream &Out) const;

Expand Down Expand Up @@ -847,11 +852,15 @@ class DefineMemberBasedOnUse final : public ConstraintFix {

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

bool diagnoseForAmbiguity(ArrayRef<Solution> solutions) const override;
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override;

static DefineMemberBasedOnUse *create(ConstraintSystem &cs, Type baseType,
DeclNameRef member, bool alreadyDiagnosed,
ConstraintLocator *locator);

static bool classof(const ConstraintFix *fix) {
return fix->getKind() == FixKind::DefineMemberBasedOnUse;
}
};

class AllowInvalidMemberRef : public ConstraintFix {
Expand Down Expand Up @@ -1117,8 +1126,8 @@ class AddMissingArguments final

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

bool diagnoseForAmbiguity(ArrayRef<Solution> solutions) const override {
return diagnose(solutions.front());
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
return diagnose(*commonFixes.front().first);
}

static AddMissingArguments *create(ConstraintSystem &cs,
Expand Down Expand Up @@ -1161,8 +1170,8 @@ class RemoveExtraneousArguments final

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

bool diagnoseForAmbiguity(ArrayRef<Solution> solutions) const override {
return diagnose(solutions.front());
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
return diagnose(*commonFixes.front().first);
}

/// FIXME(diagnostics): Once `resolveDeclRefExpr` is gone this
Expand Down Expand Up @@ -1371,8 +1380,8 @@ class DefaultGenericArgument final : public ConstraintFix {

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

bool diagnoseForAmbiguity(ArrayRef<Solution> solutions) const override {
return diagnose(solutions.front());
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
return diagnose(*commonFixes.front().first);
}

static DefaultGenericArgument *create(ConstraintSystem &cs,
Expand Down Expand Up @@ -1446,8 +1455,8 @@ class IgnoreContextualType : public ContextualMismatch {

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

bool diagnoseForAmbiguity(ArrayRef<Solution> solutions) const override {
return diagnose(solutions.front());
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override {
return diagnose(*commonFixes.front().first);
}

static IgnoreContextualType *create(ConstraintSystem &cs, Type resultTy,
Expand Down
18 changes: 12 additions & 6 deletions lib/Sema/ConstraintSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2947,12 +2947,15 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
return true;

// Collect aggregated fixes from all solutions
llvm::SmallMapVector<std::pair<ConstraintLocator *, FixKind>,
llvm::TinyPtrVector<ConstraintFix *>, 4>
using LocatorAndKind = std::pair<ConstraintLocator *, FixKind>;
using SolutionAndFix = std::pair<const Solution *, const ConstraintFix *>;
llvm::SmallMapVector<LocatorAndKind, llvm::SmallVector<SolutionAndFix, 4>, 4>
aggregatedFixes;
for (const auto &solution : solutions) {
for (auto *fix : solution.Fixes)
aggregatedFixes[{fix->getLocator(), fix->getKind()}].push_back(fix);
for (const auto *fix : solution.Fixes) {
LocatorAndKind key(fix->getLocator(), fix->getKind());
aggregatedFixes[key].emplace_back(&solution, fix);
}
}

// If there is an overload difference, let's see if there's a common callee
Expand Down Expand Up @@ -2983,8 +2986,11 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
bool diagnosed = false;
for (auto fixes: aggregatedFixes) {
// A common fix must appear in all solutions
if (fixes.second.size() < solutions.size()) continue;
diagnosed |= fixes.second.front()->diagnoseForAmbiguity(solutions);
auto &commonFixes = fixes.second;
if (commonFixes.size() != solutions.size()) continue;

auto *firstFix = commonFixes.front().second;
diagnosed |= firstFix->diagnoseForAmbiguity(commonFixes);
}
return diagnosed;
}
Expand Down
8 changes: 8 additions & 0 deletions test/Sema/diag_ambiguous_overloads.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ struct S {
}
}

struct School {
var name: String
}
func testDiagnoseForAmbiguityCrash(schools: [School]) {
schools.map({ $0.name }).sorted(by: { $0.nothing < $1.notAThing })
// expected-error@-1 {{value of type 'String' has no member 'nothing'}}
// expected-error@-2 {{value of type 'String' has no member 'notAThing'}}
}

class DefaultValue {
static func foo(_ a: Int) {}
Expand Down