Skip to content

AST: Rework @objcMembers inheritance to not depend on validation order [5.0] #21261

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
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
23 changes: 19 additions & 4 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,7 @@ class alignas(1 << DeclAlignInBits) Decl {
NumRequirementsInSignature : 16
);

SWIFT_INLINE_BITFIELD(ClassDecl, NominalTypeDecl, 1+2+1+2+1+3+1+1,
SWIFT_INLINE_BITFIELD(ClassDecl, NominalTypeDecl, 1+2+1+2+1+3+1+1+1+1,
/// Whether this class requires all of its instance variables to
/// have in-class initializers.
RequiresStoredPropertyInits : 1,
Expand All @@ -548,6 +548,10 @@ class alignas(1 << DeclAlignInBits) Decl {
/// Whether the class has @objc ancestry.
ObjCKind : 3,

/// Whether this class has @objc members.
HasObjCMembersComputed : 1,
HasObjCMembers : 1,

HasMissingDesignatedInitializers : 1,
HasMissingVTableEntries : 1
);
Expand Down Expand Up @@ -3574,6 +3578,8 @@ class ClassDecl final : public NominalTypeDecl {
friend class SuperclassTypeRequest;
friend class TypeChecker;

bool hasObjCMembersSlow();

public:
ClassDecl(SourceLoc ClassLoc, Identifier Name, SourceLoc NameLoc,
MutableArrayRef<TypeLoc> Inherited,
Expand Down Expand Up @@ -3732,11 +3738,20 @@ class ClassDecl final : public NominalTypeDecl {
Bits.ClassDecl.InheritsSuperclassInits = true;
}

/// Figure out if this class has any @objc ancestors, in which case it should
/// have implicitly @objc members. Note that a class with generic ancestry
/// might have implicitly @objc members, but will never itself be @objc.
/// Returns if this class has any @objc ancestors, or if it is directly
/// visible to Objective-C. The latter is a stronger condition which is only
/// true if the class does not have any generic ancestry.
ObjCClassKind checkObjCAncestry() const;

/// Returns if the class has implicitly @objc members. This is true if any
/// ancestor class has the @objcMembers attribute.
bool hasObjCMembers() const {
if (Bits.ClassDecl.HasObjCMembersComputed)
return Bits.ClassDecl.HasObjCMembers;

return const_cast<ClassDecl *>(this)->hasObjCMembersSlow();
}

/// The type of metaclass to use for a class.
enum class MetaclassKind : uint8_t {
ObjC,
Expand Down
16 changes: 16 additions & 0 deletions lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3315,6 +3315,8 @@ ClassDecl::ClassDecl(SourceLoc ClassLoc, Identifier Name, SourceLoc NameLoc,
Bits.ClassDecl.RawForeignKind = 0;
Bits.ClassDecl.HasDestructorDecl = 0;
Bits.ClassDecl.ObjCKind = 0;
Bits.ClassDecl.HasObjCMembersComputed = 0;
Bits.ClassDecl.HasObjCMembers = 0;
Bits.ClassDecl.HasMissingDesignatedInitializers = 0;
Bits.ClassDecl.HasMissingVTableEntries = 0;
}
Expand Down Expand Up @@ -3477,6 +3479,20 @@ ObjCClassKind ClassDecl::checkObjCAncestry() const {
return kind;
}

bool ClassDecl::hasObjCMembersSlow() {
// Don't attempt to calculate this again.
Bits.ClassDecl.HasObjCMembersComputed = true;

bool result = false;
if (getAttrs().hasAttribute<ObjCMembersAttr>())
result = true;
else if (auto *superclassDecl = getSuperclassDecl())
result = superclassDecl->hasObjCMembers();

Bits.ClassDecl.HasObjCMembers = result;
return result;
}

ClassDecl::MetaclassKind ClassDecl::getMetaclassKind() const {
assert(getASTContext().LangOpts.EnableObjCInterop &&
"querying metaclass kind without objc interop");
Expand Down
10 changes: 3 additions & 7 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7416,13 +7416,9 @@ void ClangImporter::Implementation::importAttributes(
return;
}

// Map Clang's swift_objc_members attribute to @objcMembers. Also handle
// inheritance of @objcMembers by looking at the superclass.
if (ID->hasAttr<clang::SwiftObjCMembersAttr>() ||
(isa<ClassDecl>(MappedDecl) &&
cast<ClassDecl>(MappedDecl)->hasSuperclass() &&
cast<ClassDecl>(MappedDecl)->getSuperclassDecl()
->getAttrs().hasAttribute<ObjCMembersAttr>())) {
// Map Clang's swift_objc_members attribute to @objcMembers.
if (ID->hasAttr<clang::SwiftObjCMembersAttr>() &&
isa<ClassDecl>(MappedDecl)) {
if (!MappedDecl->getAttrs().hasAttribute<ObjCMembersAttr>()) {
auto attr = new (C) ObjCMembersAttr(/*IsImplicit=*/true);
MappedDecl->getAttrs().add(attr);
Expand Down
8 changes: 0 additions & 8 deletions lib/Sema/TypeCheckDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3957,14 +3957,6 @@ void TypeChecker::validateDecl(ValueDecl *D) {
(CD->hasSuperclass() &&
CD->getSuperclassDecl()->requiresStoredPropertyInits()))
CD->setRequiresStoredPropertyInits(true);

// Inherit @objcMembers.
if (auto superclass = CD->getSuperclassDecl()) {
if (superclass->getAttrs().hasAttribute<ObjCMembersAttr>() &&
!CD->getAttrs().hasAttribute<ObjCMembersAttr>()) {
CD->getAttrs().add(new (Context) ObjCMembersAttr(/*IsImplicit=*/true));
}
}
}

if (auto *ED = dyn_cast<EnumDecl>(nominal)) {
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckDeclObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ static bool isMemberOfObjCMembersClass(const ValueDecl *VD) {
auto classDecl = VD->getDeclContext()->getSelfClassDecl();
if (!classDecl) return false;

return classDecl->getAttrs().hasAttribute<ObjCMembersAttr>();
return classDecl->hasObjCMembers();
}

// A class is @objc if it does not have generic ancestry, and it either has
Expand Down
3 changes: 3 additions & 0 deletions test/attr/Inputs/attr_objcMembers_other.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@objcMembers class BaseClassWithObjCMembers {}

class OtherClassWithObjCMembers : BaseClassWithObjCMembers {}
7 changes: 6 additions & 1 deletion test/attr/attr_objcMembers.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// RUN: %target-swift-frontend -disable-objc-attr-requires-foundation-module -typecheck -verify %s
// RUN: %target-swift-frontend -disable-objc-attr-requires-foundation-module -typecheck -verify %s %S/Inputs/attr_objcMembers_other.swift
// REQUIRES: objc_interop

import Foundation
Expand All @@ -24,12 +24,17 @@ extension SubClassOfSomeClassWithObjCMembers {
func wibble() { }
}

class SubClassOfOtherClassWithObjCMembers : OtherClassWithObjCMembers {
func quux() { }
}

// @objc should be inferred for everything referenced here.
func selectorTest() {
_ = #selector(SomeClassWithObjCMembers.foo)
_ = #selector(getter: SomeClassWithObjCMembers.bar)
_ = #selector(SubClassOfSomeClassWithObjCMembers.baz)
_ = #selector(SubClassOfSomeClassWithObjCMembers.wibble)
_ = #selector(SubClassOfOtherClassWithObjCMembers.quux)
}

@nonobjc extension SomeClassWithObjCMembers {
Expand Down