Skip to content

Commit 345da29

Browse files
committed
Add diagnostic for non-objc functions declared as open inside extension
1 parent a5ba086 commit 345da29

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,6 +1504,9 @@ ERROR(access_control_extension_open,none,
15041504
ERROR(access_control_open_bad_decl,none,
15051505
"only classes and overridable class members can be declared 'open';"
15061506
" use 'public'", ())
1507+
ERROR(access_control_non_objc_open_func,none,
1508+
"non-@objc %select{properties|methods}0 in extensions cannot be overriden; use 'public' instead",
1509+
(bool))
15071510

15081511
ERROR(invalid_decl_attribute,none,
15091512
"'%0' attribute cannot be applied to this declaration", (DeclAttribute))

lib/Sema/TypeCheckAttr.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,15 @@ void AttributeChecker::visitAccessControlAttr(AccessControlAttr *attr) {
795795
.fixItRemove(attr->getRange());
796796
}
797797
}
798+
799+
if (auto VD = dyn_cast<ValueDecl>(D)) {
800+
if (!VD->isObjC() && attr->getAccess() == AccessLevel::Open) {
801+
diagnose(attr->getLocation(), diag::access_control_non_objc_open_func,
802+
isa<FuncDecl>(VD))
803+
.fixItReplace(attr->getRange(), "public");
804+
attr->setInvalid();
805+
}
806+
}
798807
}
799808

800809
if (attr->getAccess() == AccessLevel::Open) {

test/attr/open.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,3 +134,21 @@ open class OpenSubClass : OpenSuperClass {
134134
open override subscript(index: MarkerForNonOpenSubscripts) -> Int { return 0 }
135135

136136
}
137+
138+
open class InvalidOpenExtension {
139+
open func openMethod() { } // No error
140+
}
141+
extension InvalidOpenExtension {
142+
open func nonObjcOpenMethod() { } // expected-error {{non-@objc methods in extensions cannot be overriden; use 'public' instead}} {{3-7=public}}
143+
open var nonObjcOpenVar: Int { 3 } // expected-error {{non-@objc properties in extensions cannot be overriden; use 'public' instead}} {{3-7=public}}
144+
@objc open func objcOpenMethod() { } // No error
145+
@objc open var objcOpenVar: Int { 3 } // No error
146+
}
147+
148+
@objcMembers
149+
open class ValidOpenExtension { }
150+
extension ValidOpenExtension {
151+
open func objcOpenMethod() { } // No error
152+
open var objcOpenVar: Int { 3 } // No error
153+
}
154+

0 commit comments

Comments
 (0)