Skip to content

Commit 7218a49

Browse files
committed
[TypeChecker] Reject @objc attribute on marker protocols
(cherry picked from commit 9dcb921)
1 parent f93de71 commit 7218a49

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5906,8 +5906,9 @@ ERROR(invalid_objc_decl_context,none,
59065906
"@objc can only be used with members of classes, @objc protocols, and "
59075907
"concrete extensions of classes", ())
59085908
ERROR(invalid_objc_decl,none,
5909-
"only classes (and their extensions), protocols, methods, initializers, "
5910-
"properties, and subscript declarations can be declared @objc", ())
5909+
"only classes (and their extensions), non-marker protocols, methods, "
5910+
"initializers, properties, and subscript declarations can be declared"
5911+
" @objc", ())
59115912
ERROR(invalid_objc_swift_rooted_class,none,
59125913
"only classes that inherit from NSObject can be declared @objc", ())
59135914
NOTE(invalid_objc_swift_root_class_insert_nsobject,none,

lib/Sema/TypeCheckAttr.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1303,9 +1303,12 @@ void AttributeChecker::visitObjCAttr(ObjCAttr *attr) {
13031303

13041304
// Only certain decls can be ObjC.
13051305
llvm::Optional<Diag<>> error;
1306-
if (isa<ClassDecl>(D) ||
1307-
isa<ProtocolDecl>(D)) {
1306+
if (isa<ClassDecl>(D)) {
13081307
/* ok */
1308+
} else if (auto *P = dyn_cast<ProtocolDecl>(D)) {
1309+
if (P->isMarkerProtocol())
1310+
error = diag::invalid_objc_decl;
1311+
/* ok on non-marker protocols */
13091312
} else if (auto Ext = dyn_cast<ExtensionDecl>(D)) {
13101313
if (!Ext->getSelfClassDecl())
13111314
error = diag::objc_extension_not_class;

test/attr/attr_marker_protocol.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,3 +67,6 @@ protocol P10 { }
6767

6868
extension Array: P10 where Element: P10, Element: P8 { }
6969
// expected-error@-1{{conditional conformance to non-marker protocol 'P10' cannot depend on conformance of 'Element' to marker protocol 'P8'}}
70+
71+
@objc @_marker protocol P11 {}
72+
// expected-error@-1 {{only classes (and their extensions), non-marker protocols, methods, initializers, properties, and subscript declarations can be declared @objc}}

0 commit comments

Comments
 (0)