Skip to content

Add [fixit] to suggest Self.member instead of Proto.member #3615

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 21, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2358,10 +2358,22 @@ diagnoseUnviableLookupResults(MemberLookupResult &result, Type baseObjTy,
case MemberLookupResult::UR_TypeMemberOnInstance:
if (instanceTy->isExistentialType() && baseObjTy->is<AnyMetatypeType>()) {
// If the base of the lookup is an existential metatype, emit an
// error specific to that
diagnose(loc, diag::could_not_use_type_member_on_existential,
baseObjTy, memberName)
.highlight(baseRange).highlight(nameLoc.getSourceRange());
// error saying the lookup cannot be on a protocol metatype
auto Diag = diagnose(loc, diag::could_not_use_type_member_on_existential,
baseObjTy, memberName);
Diag.highlight(baseRange).highlight(nameLoc.getSourceRange());

// See through function decl context
if (auto parent = CS->DC->getParent())
// If we are in an protocol extension of 'Proto' and we see
// 'Proto.static', suggest 'Self.static'
if (auto extensionContext = parent->getAsProtocolExtensionContext()) {
if (extensionContext->getDeclaredType()->getCanonicalType()
== instanceTy->getCanonicalType()) {
Diag.fixItReplace(baseRange, "Self");
}
}

} else {
// Otherwise the static member lookup was invalid because it was
// called on an instance
Expand Down
9 changes: 9 additions & 0 deletions test/Constraints/members.swift
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,15 @@ func staticExistential(_ p: P.Type, pp: P.Protocol) {
let _: P! = id(p.returnSelfIUOStatic(true))
}

protocol StaticP {
static func foo(a: Int)
}
extension StaticP {
func bar() {
_ = StaticP.foo(a:) // expected-error{{static member 'foo(a:)' cannot be used on protocol metatype 'StaticP.Protocol'}} {{9-16=Self}}
}
}

func existentialClassP(_ p: ClassP) {
// Instance member of existential)
let _: (Int) -> () = id(p.bas)
Expand Down