Skip to content
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

[Sema] Produce expected diagnostic for invalid operator usage in loop #80042

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -4751,6 +4751,8 @@ WARNING(use_of_void_pointer,none,
// Ambiguities
ERROR(ambiguous_decl_ref,none,
"ambiguous use of %0", (DeclNameRef))
NOTE(ambiguous_because_of_argument_mismatch,none,
"type mismatch between %0 and %1", (Type, Type))
ERROR(ambiguous_operator_ref,none,
"ambiguous use of operator %0", (DeclNameRef))
NOTE(ambiguous_because_of_trailing_closure,none,
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -2461,6 +2461,8 @@ class AllowArgumentMismatch : public ContextualMismatch {

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

bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override;

static AllowArgumentMismatch *create(ConstraintSystem &cs, Type argType,
Type paramType,
ConstraintLocator *locator);
Expand Down
36 changes: 36 additions & 0 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1848,6 +1848,42 @@ bool AllowArgumentMismatch::diagnose(const Solution &solution,
return failure.diagnose(asNote);
}

bool AllowArgumentMismatch::diagnoseForAmbiguity(CommonFixesArray commonFixes) const {
auto *primaryFix = commonFixes.front().second->getAs<AllowArgumentMismatch>();

if (llvm::all_of(commonFixes, [&primaryFix](const std::pair<const Solution *, const ConstraintFix *> &entry) {
auto *fix = entry.second->getAs<AllowArgumentMismatch>();
return primaryFix->getToType()->isEqual(fix->getToType());
})) {
auto primarySolution = commonFixes.front().first;
auto &CS = getConstraintSystem();
auto &DE = CS.getASTContext().Diags;

auto loc = CS.getConstraintLocator(simplifyLocatorToAnchor(getLocator()));
auto overload = primarySolution->getOverloadChoiceIfAvailable(primarySolution->getCalleeLocator(loc));
if (!overload)
return false;

DeclNameRef name = DeclNameRef(overload->choice.getName().getBaseName());
DE.diagnose(getLoc(getAnchor()), diag::ambiguous_decl_ref, name);

for (auto &entry : commonFixes) {
auto &solution = entry.first;
auto *fix = entry.second->getAs<AllowArgumentMismatch>();;
auto overload = solution->getOverloadChoiceIfAvailable(solution->getCalleeLocator(loc));
if (!overload)
continue;
DeclNameRef name = DeclNameRef(overload->choice.getName().getBaseName());
DE.diagnose(getLoc(getAnchor()), diag::ambiguous_because_of_argument_mismatch,
fix->getFromType(), fix->getToType());
}

return true;
}

return false;
}

AllowArgumentMismatch *
AllowArgumentMismatch::create(ConstraintSystem &cs, Type argType,
Type paramType, ConstraintLocator *locator) {
Expand Down