Skip to content

Commit 9dcb921

Browse files
committed
[TypeChecker] Reject @objc attribute on marker protocols
1 parent 5b2e534 commit 9dcb921

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
@@ -6047,8 +6047,9 @@ ERROR(invalid_objc_decl_context,none,
60476047
"@objc can only be used with members of classes, @objc protocols, and "
60486048
"concrete extensions of classes", ())
60496049
ERROR(invalid_objc_decl,none,
6050-
"only classes (and their extensions), protocols, methods, initializers, "
6051-
"properties, and subscript declarations can be declared @objc", ())
6050+
"only classes (and their extensions), non-marker protocols, methods, "
6051+
"initializers, properties, and subscript declarations can be declared"
6052+
" @objc", ())
60526053
ERROR(invalid_objc_swift_rooted_class,none,
60536054
"only classes that inherit from NSObject can be declared @objc", ())
60546055
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
@@ -1310,9 +1310,12 @@ void AttributeChecker::visitObjCAttr(ObjCAttr *attr) {
13101310

13111311
// Only certain decls can be ObjC.
13121312
llvm::Optional<Diag<>> error;
1313-
if (isa<ClassDecl>(D) ||
1314-
isa<ProtocolDecl>(D)) {
1313+
if (isa<ClassDecl>(D)) {
13151314
/* ok */
1315+
} else if (auto *P = dyn_cast<ProtocolDecl>(D)) {
1316+
if (P->isMarkerProtocol())
1317+
error = diag::invalid_objc_decl;
1318+
/* ok on non-marker protocols */
13161319
} else if (auto Ext = dyn_cast<ExtensionDecl>(D)) {
13171320
if (!Ext->getSelfClassDecl())
13181321
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)