Skip to content

Commit 99be45f

Browse files
committed
[PrintAsObjC] Add unavailable attribute to non-inherited initializers
Initializers that aren't inherited by subclasses cannot be called, so we should make this visible to Obj-C. Due to SR-2211, non-inherited convenience initializers do not get this same treatment.
1 parent 4a872ed commit 99be45f

File tree

3 files changed

+28
-5
lines changed

3 files changed

+28
-5
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
139139

140140
bool shouldInclude(const ValueDecl *VD) {
141141
return (VD->isObjC() || VD->getAttrs().hasAttribute<CDeclAttr>()) &&
142-
VD->getFormalAccess() >= minRequiredAccess &&
143-
!(isa<ConstructorDecl>(VD) &&
144-
cast<ConstructorDecl>(VD)->hasStubImplementation());
142+
VD->getFormalAccess() >= minRequiredAccess;
145143
}
146144

147145
private:
@@ -479,7 +477,9 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
479477

480478
// Swift designated initializers are Objective-C designated initializers.
481479
if (auto ctor = dyn_cast<ConstructorDecl>(AFD)) {
482-
if (ctor->isDesignatedInit() &&
480+
if (ctor->hasStubImplementation()) {
481+
os << " SWIFT_UNAVAILABLE";
482+
} else if (ctor->isDesignatedInit() &&
483483
!isa<ProtocolDecl>(ctor->getDeclContext())) {
484484
os << " OBJC_DESIGNATED_INITIALIZER";
485485
}
@@ -1961,6 +1961,9 @@ class ModuleWriter {
19611961
"SWIFT_ENUM(_type, _name)\n"
19621962
"# endif\n"
19631963
"#endif\n"
1964+
"#if !defined(SWIFT_UNAVAILABLE)\n"
1965+
"# define SWIFT_UNAVAILABLE __attribute__((unavailable))\n"
1966+
"#endif\n"
19641967
;
19651968
static_assert(SWIFT_MAX_IMPORTED_SIMD_ELEMENTS == 4,
19661969
"need to add SIMD typedefs here if max elements is increased");

test/PrintAsObjC/classes.swift

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,25 @@ class ClassWithCustomNameSub : ClassWithCustomName {}
122122
init(forFun: ()) { }
123123
}
124124

125+
// CHECK-LABEL: @interface InheritedInitializers
126+
// CHECK-NEXT: - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
127+
// CHECK-NEXT: - (nonnull instancetype)initForFun SWIFT_UNAVAILABLE;
128+
// CHECK-NEXT: @end
129+
@objc class InheritedInitializers : Initializers {
130+
override init() {
131+
super.init()
132+
}
133+
}
134+
135+
// CHECK-LABEL: @interface InheritedInitializersAgain
136+
// CHECK-NEXT: - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER;
137+
// CHECK-NEXT: @end
138+
@objc class InheritedInitializersAgain : InheritedInitializers {
139+
override init() {
140+
super.init()
141+
}
142+
}
143+
125144
// NEGATIVE-NOT: NotObjC
126145
class NotObjC {}
127146

test/PrintAsObjC/protocols.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ protocol CustomNameType2 {}
7575
}
7676

7777
// CHECK-LABEL: @interface MyObject : NSObject <NSCoding>
78-
// CHECK-NEXT: init
78+
// CHECK-NEXT: initWithCoder
79+
// CHECK-NEXT: init SWIFT_UNAVAILABLE
7980
// CHECK-NEXT: @end
8081
// NEGATIVE-NOT: @protocol NSCoding
8182
class MyObject : NSObject, NSCoding {

0 commit comments

Comments
 (0)