Skip to content

@objc extension makes even 'private' members '@objc' #22436

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
Feb 7, 2019
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
15 changes: 10 additions & 5 deletions lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1054,7 +1054,7 @@ Optional<ObjCReason> shouldMarkAsObjC(const ValueDecl *VD, bool allowImplicit) {
protocolContext && protocolContext->isObjC();

// Local function to determine whether we can implicitly infer @objc.
auto canInferImplicitObjC = [&] {
auto canInferImplicitObjC = [&](bool allowAnyAccess) {
if (VD->isInvalid())
return false;
if (VD->isOperator())
Expand All @@ -1064,7 +1064,7 @@ Optional<ObjCReason> shouldMarkAsObjC(const ValueDecl *VD, bool allowImplicit) {
if (!allowImplicit && VD->isImplicit())
return false;

if (VD->getFormalAccess() <= AccessLevel::FilePrivate)
if (!allowAnyAccess && VD->getFormalAccess() <= AccessLevel::FilePrivate)
return false;

if (auto accessor = dyn_cast<AccessorDecl>(VD)) {
Expand Down Expand Up @@ -1115,17 +1115,22 @@ Optional<ObjCReason> shouldMarkAsObjC(const ValueDecl *VD, bool allowImplicit) {
// A member of an @objc protocol is implicitly @objc.
if (isMemberOfObjCProtocol)
return ObjCReason(ObjCReason::MemberOfObjCProtocol);

// A @nonobjc is not @objc, even if it is an override of an @objc, so check
// for @nonobjc first.
if (VD->getAttrs().hasAttribute<NonObjCAttr>() ||
(isa<ExtensionDecl>(VD->getDeclContext()) &&
cast<ExtensionDecl>(VD->getDeclContext())->getAttrs()
.hasAttribute<NonObjCAttr>()))
return None;
if (isMemberOfObjCClassExtension(VD) && canInferImplicitObjC())

if (isMemberOfObjCClassExtension(VD) &&
canInferImplicitObjC(/*allowAnyAccess*/true))
return ObjCReason(ObjCReason::MemberOfObjCExtension);
if (isMemberOfObjCMembersClass(VD) && canInferImplicitObjC())
if (isMemberOfObjCMembersClass(VD) &&
canInferImplicitObjC(/*allowAnyAccess*/false))
return ObjCReason(ObjCReason::MemberOfObjCMembersClass);

// An override of an @objc declaration is implicitly @objc.
if (VD->getOverriddenDecl() && VD->getOverriddenDecl()->isObjC())
return ObjCReason(ObjCReason::OverridesObjC);
Expand Down Expand Up @@ -1177,7 +1182,7 @@ Optional<ObjCReason> shouldMarkAsObjC(const ValueDecl *VD, bool allowImplicit) {
// (and extensions thereof) whose class hierarchies originate in Objective-C,
// e.g., which derive from NSObject, so long as the members have internal
// access or greater.
if (!canInferImplicitObjC())
if (!canInferImplicitObjC(/*allowAnyAccess*/false))
return None;

// If this declaration is part of a class with implicitly @objc members,
Expand Down
14 changes: 14 additions & 0 deletions test/attr/attr_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2325,4 +2325,18 @@ extension MyObjCClass {
}
set {}
}

// CHECK: {{^}} @objc private dynamic func stillExposedToObjCDespiteBeingPrivate()
private func stillExposedToObjCDespiteBeingPrivate() {}
}

@objc private extension MyObjCClass {
// CHECK: {{^}} @objc dynamic func alsoExposedToObjCDespiteBeingPrivate()
func alsoExposedToObjCDespiteBeingPrivate() {}
}

@objcMembers class VeryObjCClass: NSObject {
// CHECK: {{^}} private func notExposedToObjC()
private func notExposedToObjC() {}
}