Skip to content

Commit c7b1505

Browse files
authored
Merge pull request #58905 from xedin/improve-relabeling-diagnostic
[Diagnostics] Diagnose re-labeling failures in ambiguity conditions
2 parents 7435c93 + f3ff87b commit c7b1505

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed

include/swift/Sema/CSFix.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ class RelabelArguments final
536536

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

539+
bool diagnoseForAmbiguity(CommonFixesArray commonFixes) const override;
540+
539541
static RelabelArguments *create(ConstraintSystem &cs,
540542
llvm::ArrayRef<Identifier> correctLabels,
541543
ConstraintLocator *locator);

lib/Sema/CSFix.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,38 @@ bool RelabelArguments::diagnose(const Solution &solution, bool asNote) const {
241241
return failure.diagnose(asNote);
242242
}
243243

244+
bool RelabelArguments::diagnoseForAmbiguity(
245+
CommonFixesArray commonFixes) const {
246+
SmallPtrSet<ValueDecl *, 4> overloadChoices;
247+
248+
// First, let's find overload choice associated with each
249+
// re-labeling fix.
250+
for (const auto &fix : commonFixes) {
251+
auto &solution = *fix.first;
252+
253+
auto calleeLocator = solution.getCalleeLocator(getLocator());
254+
if (!calleeLocator)
255+
return false;
256+
257+
auto overloadChoice = solution.getOverloadChoiceIfAvailable(calleeLocator);
258+
if (!overloadChoice)
259+
return false;
260+
261+
auto *decl = overloadChoice->choice.getDeclOrNull();
262+
if (!decl)
263+
return false;
264+
265+
(void)overloadChoices.insert(decl);
266+
}
267+
268+
// If all of the fixes point to the same overload choice then it's
269+
// exactly the same issue since the call site is static.
270+
if (overloadChoices.size() == 1)
271+
return diagnose(*commonFixes.front().first);
272+
273+
return false;
274+
}
275+
244276
RelabelArguments *
245277
RelabelArguments::create(ConstraintSystem &cs,
246278
llvm::ArrayRef<Identifier> correctLabels,

test/Constraints/closures.swift

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1170,7 +1170,6 @@ func test(arr: [[Int]]) {
11701170
}
11711171

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

11761175
func closureWithCaseArchetype<T>(_: T.Type) {

test/Constraints/generics.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,8 @@ func rdar78781552() {
902902
// expected-error@-1 {{generic struct 'Test' requires that '(((Int) throws -> Bool) throws -> [Int])?' conform to 'RandomAccessCollection'}}
903903
// expected-error@-2 {{generic parameter 'Content' could not be inferred}} expected-note@-2 {{explicitly specify the generic arguments to fix this issue}}
904904
// expected-error@-3 {{cannot convert value of type '(((Int) throws -> Bool) throws -> [Int])?' to expected argument type '[(((Int) throws -> Bool) throws -> [Int])?]'}}
905-
// expected-error@-4 {{missing argument for parameter 'filter' in call}}
905+
// expected-error@-4 {{missing argument label 'data:' in call}}
906+
// expected-error@-5 {{missing argument for parameter 'filter' in call}}
906907
}
907908
}
908909

test/expr/unary/keypath/keypath.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -507,6 +507,7 @@ func testLabeledSubscript() {
507507
// TODO: These ought to work without errors.
508508
let _ = \AA.[keyPath: k]
509509
// expected-error@-1 {{cannot convert value of type 'KeyPath<AA, Int>' to expected argument type 'Int'}}
510+
// expected-error@-2 {{extraneous argument label 'keyPath:' in call}}
510511

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

0 commit comments

Comments
 (0)