Skip to content

[SR-3168] Add fix-it for "'optional' can only be applied to protocol members" #5717

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
Nov 11, 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
4 changes: 2 additions & 2 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8400,8 +8400,8 @@ static void validateAttributes(TypeChecker &TC, Decl *D) {
// Only protocol members can be optional.
if (auto *OA = Attrs.getAttribute<OptionalAttr>()) {
if (!isa<ProtocolDecl>(D->getDeclContext())) {
TC.diagnose(OA->getLocation(),
diag::optional_attribute_non_protocol);
TC.diagnose(OA->getLocation(), diag::optional_attribute_non_protocol)
.fixItRemove(OA->getRange());
D->getAttrs().removeAttribute(OA);
} else if (!cast<ProtocolDecl>(D->getDeclContext())->isObjC()) {
TC.diagnose(OA->getLocation(),
Expand Down
4 changes: 4 additions & 0 deletions test/FixCode/fixits-apply.swift
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,7 @@ protocol P1 {}
protocol P2 {}
var a : protocol<P1, P2>?
var a2 : protocol<P1>= 17

class TestOptionalMethodFixit {
optional func test() {}
}
4 changes: 4 additions & 0 deletions test/FixCode/fixits-apply.swift.result
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,7 @@ protocol P1 {}
protocol P2 {}
var a : (P1 & P2)?
var a2 : P1= 17 as! P1

class TestOptionalMethodFixit {
func test() {}
}
12 changes: 6 additions & 6 deletions test/decl/protocol/req/optional.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,18 +204,18 @@ func optionalSubscriptExistential(_ t: P1) {
// -----------------------------------------------------------------------

// optional cannot be used on non-protocol declarations
optional var optError: Int = 10 // expected-error{{'optional' can only be applied to protocol members}}
optional var optError: Int = 10 // expected-error{{'optional' can only be applied to protocol members}} {{1-10=}}

optional struct optErrorStruct { // expected-error{{'optional' modifier cannot be applied to this declaration}} {{1-10=}}
optional var ivar: Int // expected-error{{'optional' can only be applied to protocol members}}
optional func foo() { } // expected-error{{'optional' can only be applied to protocol members}}
optional var ivar: Int // expected-error{{'optional' can only be applied to protocol members}} {{3-12=}}
optional func foo() { } // expected-error{{'optional' can only be applied to protocol members}} {{3-12=}}
}

optional class optErrorClass { // expected-error{{'optional' modifier cannot be applied to this declaration}} {{1-10=}}
optional var ivar: Int = 0 // expected-error{{'optional' can only be applied to protocol members}}
optional func foo() { } // expected-error{{'optional' can only be applied to protocol members}}
optional var ivar: Int = 0 // expected-error{{'optional' can only be applied to protocol members}} {{3-12=}}
optional func foo() { } // expected-error{{'optional' can only be applied to protocol members}} {{3-12=}}
}

protocol optErrorProtocol {
optional func foo(_ x: Int) // expected-error{{'optional' can only be applied to members of an @objc protocol}}
}
Expand Down