Skip to content

[Diagnostics] Diagnose re-labeling failures in ambiguity conditions #58905

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 1 commit into from
May 18, 2022
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
2 changes: 2 additions & 0 deletions include/swift/Sema/CSFix.h
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,8 @@ class RelabelArguments final

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

bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override;

static RelabelArguments *create(ConstraintSystem &cs,
llvm::ArrayRef<Identifier> correctLabels,
ConstraintLocator *locator);
Expand Down
32 changes: 32 additions & 0 deletions lib/Sema/CSFix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,38 @@ bool RelabelArguments::diagnose(const Solution &solution, bool asNote) const {
return failure.diagnose(asNote);
}

bool RelabelArguments::diagnoseForAmbiguity(
CommonFixesArray commonFixes) const {
SmallPtrSet<ValueDecl *, 4> overloadChoices;

// First, let's find overload choice associated with each
// re-labeling fix.
for (const auto &fix : commonFixes) {
auto &solution = *fix.first;

auto calleeLocator = solution.getCalleeLocator(getLocator());
if (!calleeLocator)
return false;

auto overloadChoice = solution.getOverloadChoiceIfAvailable(calleeLocator);
if (!overloadChoice)
return false;

auto *decl = overloadChoice->choice.getDeclOrNull();
if (!decl)
return false;

(void)overloadChoices.insert(decl);
}

// If all of the fixes point to the same overload choice then it's
// exactly the same issue since the call site is static.
if (overloadChoices.size() == 1)
return diagnose(*commonFixes.front().first);

return false;
}

RelabelArguments *
RelabelArguments::create(ConstraintSystem &cs,
llvm::ArrayRef<Identifier> correctLabels,
Expand Down
1 change: 0 additions & 1 deletion test/Constraints/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1170,7 +1170,6 @@ func test(arr: [[Int]]) {
}

arr.map { ($0 as? [Int]).map { A($0) } } // expected-error {{missing argument label 'arg:' in call}} {{36-36=arg: }}
// expected-warning@-1 {{conditional cast from '[Int]' to '[Int]' always succeeds}}
}

func closureWithCaseArchetype<T>(_: T.Type) {
Expand Down
3 changes: 2 additions & 1 deletion test/Constraints/generics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,8 @@ func rdar78781552() {
// expected-error@-1 {{generic struct 'Test' requires that '(((Int) throws -> Bool) throws -> [Int])?' conform to 'RandomAccessCollection'}}
// expected-error@-2 {{generic parameter 'Content' could not be inferred}} expected-note@-2 {{explicitly specify the generic arguments to fix this issue}}
// expected-error@-3 {{cannot convert value of type '(((Int) throws -> Bool) throws -> [Int])?' to expected argument type '[(((Int) throws -> Bool) throws -> [Int])?]'}}
// expected-error@-4 {{missing argument for parameter 'filter' in call}}
// expected-error@-4 {{missing argument label 'data:' in call}}
// expected-error@-5 {{missing argument for parameter 'filter' in call}}
}
}

Expand Down
1 change: 1 addition & 0 deletions test/expr/unary/keypath/keypath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -507,6 +507,7 @@ func testLabeledSubscript() {
// TODO: These ought to work without errors.
let _ = \AA.[keyPath: k]
// expected-error@-1 {{cannot convert value of type 'KeyPath<AA, Int>' to expected argument type 'Int'}}
// expected-error@-2 {{extraneous argument label 'keyPath:' in call}}

let _ = \AA.[keyPath: \AA.[labeled: 0]] // expected-error {{extraneous argument label 'keyPath:' in call}}
// expected-error@-1 {{cannot convert value of type 'KeyPath<AA, Int>' to expected argument type 'Int'}}
Expand Down