Skip to content

Commit 71364d0

Browse files
robinkunderintaro
authored andcommitted
fix SR-3168: add fix-it for "'optional' can only be applied to protocol members" (#5717)
1 parent 865fee8 commit 71364d0

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

lib/Sema/TypeCheckDecl.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8400,8 +8400,8 @@ static void validateAttributes(TypeChecker &TC, Decl *D) {
84008400
// Only protocol members can be optional.
84018401
if (auto *OA = Attrs.getAttribute<OptionalAttr>()) {
84028402
if (!isa<ProtocolDecl>(D->getDeclContext())) {
8403-
TC.diagnose(OA->getLocation(),
8404-
diag::optional_attribute_non_protocol);
8403+
TC.diagnose(OA->getLocation(), diag::optional_attribute_non_protocol)
8404+
.fixItRemove(OA->getRange());
84058405
D->getAttrs().removeAttribute(OA);
84068406
} else if (!cast<ProtocolDecl>(D->getDeclContext())->isObjC()) {
84078407
TC.diagnose(OA->getLocation(),

test/FixCode/fixits-apply.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,7 @@ protocol P1 {}
306306
protocol P2 {}
307307
var a : protocol<P1, P2>?
308308
var a2 : protocol<P1>= 17
309+
310+
class TestOptionalMethodFixit {
311+
optional func test() {}
312+
}

test/FixCode/fixits-apply.swift.result

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,3 +309,7 @@ protocol P1 {}
309309
protocol P2 {}
310310
var a : (P1 & P2)?
311311
var a2 : P1= 17 as! P1
312+
313+
class TestOptionalMethodFixit {
314+
func test() {}
315+
}

test/decl/protocol/req/optional.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,18 +204,18 @@ func optionalSubscriptExistential(_ t: P1) {
204204
// -----------------------------------------------------------------------
205205

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

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

214214
optional class optErrorClass { // expected-error{{'optional' modifier cannot be applied to this declaration}} {{1-10=}}
215-
optional var ivar: Int = 0 // expected-error{{'optional' can only be applied to protocol members}}
216-
optional func foo() { } // expected-error{{'optional' can only be applied to protocol members}}
215+
optional var ivar: Int = 0 // expected-error{{'optional' can only be applied to protocol members}} {{3-12=}}
216+
optional func foo() { } // expected-error{{'optional' can only be applied to protocol members}} {{3-12=}}
217217
}
218-
218+
219219
protocol optErrorProtocol {
220220
optional func foo(_ x: Int) // expected-error{{'optional' can only be applied to members of an @objc protocol}}
221221
}

0 commit comments

Comments
 (0)