Skip to content

Commit 3883ff0

Browse files
committed
[Type checker] Suppress near-miss warnings within the type definition.
There are a number of things that can only be written within the main type definition (e.g., required initializers, stored properties), making it far more likely that we'll get false positives from the newly-introduced "near-miss" warnings for protocol conformances. Moreover, sometimes a number of protocol conformances are placed on the main type definition, increasing the potential for false positives. Suppress near-miss warnings for potential candidates when the protocol conformance is stated on the main type definition itself. Therefore, we only perform near-miss checking of non-@objc requirements when the conformance itself was declared on an extension, which is roughly the development style that near-miss warnings favor. Fixes rdar://problem/37283860.
1 parent 89bc3a6 commit 3883ff0

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4071,6 +4071,11 @@ static bool shouldWarnAboutPotentialWitness(
40714071
isUnlabeledInitializerOrSubscript(witness))
40724072
return false;
40734073

4074+
// For non-@objc requirements, only warn if the witness comes from an
4075+
// extension.
4076+
if (!req->isObjC() && !isa<ExtensionDecl>(witness->getDeclContext()))
4077+
return false;
4078+
40744079
// If the score is relatively high, don't warn: this is probably
40754080
// unrelated. Allow about one typo for every four properly-typed
40764081
// characters, which prevents completely-wacky suggestions in many

test/decl/ext/protocol.swift

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ struct SConforms7a : PConforms7 { }
526526
protocol PConforms8 {
527527
associatedtype Assoc
528528

529-
func method() -> Assoc // expected-note{{requirement 'method()' declared here}}
529+
func method() -> Assoc
530530
var property: Assoc { get }
531531
subscript (i: Assoc) -> Assoc { get }
532532
}
@@ -551,10 +551,7 @@ func testSConforms8b() {
551551
}
552552

553553
struct SConforms8c : PConforms8 {
554-
func method() -> String { return "" } // expected-warning{{instance method 'method()' nearly matches defaulted requirement 'method()' of protocol 'PConforms8'}}
555-
// expected-note@-1{{candidate has non-matching type '() -> String' [with Assoc = Int]}}
556-
// expected-note@-2{{move 'method()' to an extension to silence this warning}}
557-
// expected-note@-3{{make 'method()' private to silence this warning}}
554+
func method() -> String { return "" } // no warning in type definition
558555
}
559556

560557
func testSConforms8c() {

0 commit comments

Comments
 (0)