Skip to content

Commit 318a455

Browse files
committed
[Type checker] Suppress near-miss warnings for unlabeled inits and subscripts.
Suggested by @brentdax, thanks!
1 parent 8614620 commit 318a455

File tree

2 files changed

+64
-0
lines changed

2 files changed

+64
-0
lines changed

lib/Sema/TypeCheckProtocol.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6345,6 +6345,24 @@ static bool isGeneric(ValueDecl *decl) {
63456345
return false;
63466346
}
63476347

6348+
/// Determine whether this is an unlabeled initializer or subscript.
6349+
static bool isUnlabeledInitializerOrSubscript(ValueDecl *value) {
6350+
ParameterList *paramList = nullptr;
6351+
if (auto constructor = dyn_cast<ConstructorDecl>(value))
6352+
paramList = constructor->getParameterList(1);
6353+
else if (auto subscript = dyn_cast<SubscriptDecl>(value))
6354+
paramList = subscript->getIndices();
6355+
else
6356+
return false;
6357+
6358+
for (auto param : *paramList) {
6359+
if (!param->getArgumentName().empty()) return false;
6360+
}
6361+
6362+
return true;
6363+
}
6364+
6365+
/// Determine whether this declaration is an initializer
63486366
/// Determine whether we should warn about the given witness being a close
63496367
/// match for the given optional requirement.
63506368
static bool shouldWarnAboutPotentialWitness(
@@ -6379,6 +6397,12 @@ static bool shouldWarnAboutPotentialWitness(
63796397
if (!attr->isImplicit()) return false;
63806398
}
63816399

6400+
// Don't warn if the requirement or witness is an initializer or subscript
6401+
// with no argument labels.
6402+
if (isUnlabeledInitializerOrSubscript(req) ||
6403+
isUnlabeledInitializerOrSubscript(witness))
6404+
return false;
6405+
63826406
// If the score is relatively high, don't warn: this is probably
63836407
// unrelated. Allow about one typo for every four properly-typed
63846408
// characters, which prevents completely-wacky suggestions in many

test/decl/ext/protocol.swift

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -959,3 +959,43 @@ typealias B = BadProto1
959959
extension A & B { // okay
960960

961961
}
962+
963+
// Suppress near-miss warning for unlabeled initializers.
964+
protocol P9 {
965+
init(_: Int)
966+
init(_: Double)
967+
}
968+
969+
extension P9 {
970+
init(_ i: Int) {
971+
self.init(Double(i))
972+
}
973+
}
974+
975+
struct X9 : P9 {
976+
init(_: Float) { }
977+
}
978+
979+
extension X9 {
980+
init(_: Double) { }
981+
}
982+
983+
// Suppress near-miss warning for unlabeled subscripts.
984+
protocol P10 {
985+
subscript (_: Int) -> Int { get }
986+
subscript (_: Double) -> Double { get }
987+
}
988+
989+
extension P10 {
990+
subscript(i: Int) -> Int {
991+
return Int(self[Double(i)])
992+
}
993+
}
994+
995+
struct X10 : P10 {
996+
subscript(f: Float) -> Float { return f }
997+
}
998+
999+
extension X10 {
1000+
subscript(d: Double) -> Double { return d }
1001+
}

0 commit comments

Comments
 (0)