Skip to content

Commit c28174a

Browse files
authored
Merge pull request swiftlang#26262 from xedin/new-diag-for-non-matching-overloads
[CSDiagnostics] Use special error message when call didn't match exactly
2 parents 07b3b8e + 783add0 commit c28174a

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ ERROR(ambiguous_member_overload_set,none,
5656
"ambiguous reference to member %0", (DeclName))
5757
ERROR(ambiguous_reference_to_decl,none,
5858
"ambiguous reference to %0 %1", (DescriptiveDeclKind, DeclName))
59+
ERROR(no_overloads_match_exactly_in_call,none,
60+
"no exact matches in call to %0 %1",
61+
(DescriptiveDeclKind, DeclName))
62+
ERROR(no_overloads_match_exactly_in_call_no_labels,none,
63+
"no exact matches in call to %0 %1",
64+
(DescriptiveDeclKind, DeclBaseName))
65+
ERROR(no_overloads_match_exactly_in_call_special,none,
66+
"no exact matches in call to %0",
67+
(DescriptiveDeclKind))
5968

6069
ERROR(ambiguous_subscript,none,
6170
"ambiguous subscript with base type %0 and index type %1",

lib/Sema/ConstraintSystem.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2490,8 +2490,29 @@ bool ConstraintSystem::diagnoseAmbiguityWithFixes(
24902490
diag::could_not_find_subscript_member_did_you_mean,
24912491
getType(UDE->getBase()));
24922492
} else {
2493-
TC.diagnose(commonAnchor->getLoc(), diag::ambiguous_reference_to_decl,
2494-
decl->getDescriptiveKind(), decl->getFullName());
2493+
auto name = decl->getFullName();
2494+
// Three choices here:
2495+
// 1. If this is a special name avoid printing it because
2496+
// printing kind is sufficient;
2497+
// 2. If all of the labels match, print a full name;
2498+
// 3. If labels in different choices are different, it means
2499+
// that we can only print a base name.
2500+
if (name.isSpecial()) {
2501+
TC.diagnose(commonAnchor->getLoc(),
2502+
diag::no_overloads_match_exactly_in_call_special,
2503+
decl->getDescriptiveKind());
2504+
} else if (llvm::all_of(distinctChoices,
2505+
[&name](const ValueDecl *choice) {
2506+
return choice->getFullName() == name;
2507+
})) {
2508+
TC.diagnose(commonAnchor->getLoc(),
2509+
diag::no_overloads_match_exactly_in_call,
2510+
decl->getDescriptiveKind(), name);
2511+
} else {
2512+
TC.diagnose(commonAnchor->getLoc(),
2513+
diag::no_overloads_match_exactly_in_call_no_labels,
2514+
decl->getDescriptiveKind(), name.getBaseName());
2515+
}
24952516
}
24962517

24972518
for (const auto &viable : viableSolutions) {

test/decl/ext/protocol.swift

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -236,13 +236,9 @@ extension P4 where Self.AssocP4 == Bool {
236236
}
237237

238238
func testP4(_ s4a: S4a, s4b: S4b, s4c: S4c, s4d: S4d) {
239-
// FIXME: Both of the 'ambiguous' examples below are indeed ambiguous,
240-
// because they don't match on conformance and same-type
241-
// requirement of different overloads, but diagnostic
242-
// could be improved to point out exactly what is missing in each case.
243-
s4a.extP4a() // expected-error{{ambiguous reference to instance method 'extP4a()'}}
239+
s4a.extP4a() // expected-error{{no exact matches in call to instance method 'extP4a()'}}
244240
s4b.extP4a() // ok
245-
s4c.extP4a() // expected-error{{ambiguous reference to instance method 'extP4a()'}}
241+
s4c.extP4a() // expected-error{{no exact matches in call to instance method 'extP4a()'}}
246242
s4c.extP4Int() // okay
247243
var b1 = s4d.extP4a() // okay, "Bool" version
248244
b1 = true // checks type above

validation-test/Sema/type_checker_crashers_fixed/rdar45470505.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ extension BinaryInteger {
44
init(bytes: [UInt8]) { fatalError() }
55

66
init<S: Sequence>(bytes: S) where S.Iterator.Element == UInt8 {
7-
self.init(bytes // expected-error {{ambiguous reference to initializer 'init(_:)'}}
8-
// expected-note@-1 {{}}
7+
self.init(bytes // expected-error {{no exact matches in call to initializer}}
8+
// expected-note@-1 {{}}
99

1010
extension
1111
// expected-error@-1 {{declaration is only valid at file scope}}

0 commit comments

Comments
 (0)