Skip to content

[PrintAsObjC] Add unavailable attribute to unavailable obj-c initializers #3852

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 2 commits into from
Aug 1, 2016
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
28 changes: 22 additions & 6 deletions lib/PrintAsObjC/PrintAsObjC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,11 +137,19 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
visit(const_cast<Decl *>(D));
}

bool shouldInclude(const ValueDecl *VD) {
return (VD->isObjC() || VD->getAttrs().hasAttribute<CDeclAttr>()) &&
VD->getFormalAccess() >= minRequiredAccess &&
!(isa<ConstructorDecl>(VD) &&
cast<ConstructorDecl>(VD)->hasStubImplementation());
bool shouldInclude(const ValueDecl *VD, bool checkParent = true) {
if (!(VD->isObjC() || VD->getAttrs().hasAttribute<CDeclAttr>()))
return false;
if (VD->getFormalAccess() >= minRequiredAccess) {
return true;
} else if (checkParent) {
if (auto ctor = dyn_cast<ConstructorDecl>(VD)) {
// Check if we're overriding an initializer that is visible to obj-c
if (auto parent = ctor->getOverriddenDecl())
return shouldInclude(parent, false);
}
}
return false;
}

private:
Expand Down Expand Up @@ -479,7 +487,12 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,

// Swift designated initializers are Objective-C designated initializers.
if (auto ctor = dyn_cast<ConstructorDecl>(AFD)) {
if (ctor->isDesignatedInit() &&
if (ctor->hasStubImplementation()
|| ctor->getFormalAccess() < minRequiredAccess) {
// This will only be reached if the overridden initializer has the
// required access
os << " SWIFT_UNAVAILABLE";
} else if (ctor->isDesignatedInit() &&
!isa<ProtocolDecl>(ctor->getDeclContext())) {
os << " OBJC_DESIGNATED_INITIALIZER";
}
Expand Down Expand Up @@ -1961,6 +1974,9 @@ class ModuleWriter {
"SWIFT_ENUM(_type, _name)\n"
"# endif\n"
"#endif\n"
"#if !defined(SWIFT_UNAVAILABLE)\n"
"# define SWIFT_UNAVAILABLE __attribute__((unavailable))\n"
"#endif\n"
;
static_assert(SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4,
"need to add SIMD typedefs here if max elements is increased");
Expand Down
35 changes: 35 additions & 0 deletions test/PrintAsObjC/classes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ class ClassWithCustomNameSub : ClassWithCustomName {}
// CHECK-NEXT: - (nonnull instancetype)initWithString:(NSString * _Nonnull)s boolean:(BOOL)b;
// CHECK-NEXT: - (nullable instancetype)initWithBoolean:(BOOL)b;
// CHECK-NEXT: - (nonnull instancetype)initForFun OBJC_DESIGNATED_INITIALIZER;
// CHECK-NEXT: - (nonnull instancetype)initWithMoreFun OBJC_DESIGNATED_INITIALIZER;
// CHECK-NEXT: - (nonnull instancetype)initWithEvenMoreFun OBJC_DESIGNATED_INITIALIZER;
// CHECK-NEXT: @end
@objc class Initializers {
init() {}
Expand All @@ -120,6 +122,39 @@ class ClassWithCustomNameSub : ClassWithCustomName {}
convenience init?(boolean b: ObjCBool) { self.init() }

init(forFun: ()) { }

init(moreFun: ()) { }

init(evenMoreFun: ()) { }
}

// CHECK-LABEL: @interface InheritedInitializers
// CHECK-NEXT: - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
// CHECK-NEXT: - (nonnull instancetype)initWithFloat:(float)f SWIFT_UNAVAILABLE;
// CHECK-NEXT: - (nonnull instancetype)initWithMoreFun SWIFT_UNAVAILABLE;
// CHECK-NEXT: - (nonnull instancetype)initForFun SWIFT_UNAVAILABLE;
// CHECK-NEXT: - (nonnull instancetype)initWithEvenMoreFun SWIFT_UNAVAILABLE;
// CHECK-NEXT: @end
@objc class InheritedInitializers : Initializers {
override init() {
super.init()
}

private convenience init(float f: Float) { self.init() }

private override init(moreFun: ()) { super.init() }
}

// CHECK-LABEL: @interface InheritedInitializersAgain
// CHECK-NEXT: - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
// CHECK-NEXT: - (nonnull instancetype)initWithEvenMoreFun OBJC_DESIGNATED_INITIALIZER;
// CHECK-NEXT: @end
@objc class InheritedInitializersAgain : InheritedInitializers {
override init() {
super.init()
}

init(evenMoreFun: ()) { super.init() }
}

// NEGATIVE-NOT: NotObjC
Expand Down
3 changes: 2 additions & 1 deletion test/PrintAsObjC/protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ protocol CustomNameType2 {}
}

// CHECK-LABEL: @interface MyObject : NSObject <NSCoding>
// CHECK-NEXT: init
// CHECK-NEXT: initWithCoder
// CHECK-NEXT: init SWIFT_UNAVAILABLE
// CHECK-NEXT: @end
// NEGATIVE-NOT: @protocol NSCoding
class MyObject : NSObject, NSCoding {
Expand Down