Skip to content

Commit d9d5829

Browse files
authored
Merge pull request #3615 from joewillsher/master
Add [fixit] to suggest Self.member instead of Proto.member
2 parents 9575215 + 40b2c94 commit d9d5829

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2358,10 +2358,22 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
23582358
case MemberLookupResult::UR_TypeMemberOnInstance:
23592359
if (instanceTy->isExistentialType() && baseObjTy->is<AnyMetatypeType>()) {
23602360
// If the base of the lookup is an existential metatype, emit an
2361-
// error specific to that
2362-
diagnose(loc, diag::could_not_use_type_member_on_existential,
2363-
baseObjTy, memberName)
2364-
.highlight(baseRange).highlight(nameLoc.getSourceRange());
2361+
// error saying the lookup cannot be on a protocol metatype
2362+
auto Diag = diagnose(loc, diag::could_not_use_type_member_on_existential,
2363+
baseObjTy, memberName);
2364+
Diag.highlight(baseRange).highlight(nameLoc.getSourceRange());
2365+
2366+
// See through function decl context
2367+
if (auto parent = CS->DC->getParent())
2368+
// If we are in an protocol extension of 'Proto' and we see
2369+
// 'Proto.static', suggest 'Self.static'
2370+
if (auto extensionContext = parent->getAsProtocolExtensionContext()) {
2371+
if (extensionContext->getDeclaredType()->getCanonicalType()
2372+
== instanceTy->getCanonicalType()) {
2373+
Diag.fixItReplace(baseRange, "Self");
2374+
}
2375+
}
2376+
23652377
} else {
23662378
// Otherwise the static member lookup was invalid because it was
23672379
// called on an instance

test/Constraints/members.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,15 @@ func staticExistential(_ p: P.Type, pp: P.Protocol) {
304304
let _: P! = id(p.returnSelfIUOStatic(true))
305305
}
306306

307+
protocol StaticP {
308+
static func foo(a: Int)
309+
}
310+
extension StaticP {
311+
func bar() {
312+
_ = StaticP.foo(a:) // expected-error{{static member 'foo(a:)' cannot be used on protocol metatype 'StaticP.Protocol'}} {{9-16=Self}}
313+
}
314+
}
315+
307316
func existentialClassP(_ p: ClassP) {
308317
// Instance member of existential)
309318
let _: (Int) -> () = id(p.bas)

0 commit comments

Comments
 (0)