Skip to content

Commit ed184e9

Browse files
authored
Merge pull request #12295 from JaviSoto/SR-6060
Fix SR-6060: marking +new unavailable when -init is unavailable
2 parents 53ca3b7 + f400eca commit ed184e9

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -604,6 +604,7 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
604604
}
605605

606606
bool skipAvailability = false;
607+
bool makeNewUnavailable = false;
607608
// Swift designated initializers are Objective-C designated initializers.
608609
if (auto ctor = dyn_cast<ConstructorDecl>(AFD)) {
609610
if (ctor->hasStubImplementation()
@@ -612,6 +613,9 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
612613
// required access
613614
os << " SWIFT_UNAVAILABLE";
614615
skipAvailability = true;
616+
// If -init is unavailable, then +new should be, too:
617+
const bool selectorIsInit = selector.getNumArgs() == 0 && selectorPieces.front().str() == "init";
618+
makeNewUnavailable = selectorIsInit;
615619
} else if (ctor->isDesignatedInit() &&
616620
!isa<ProtocolDecl>(ctor->getDeclContext())) {
617621
os << " OBJC_DESIGNATED_INITIALIZER";
@@ -643,6 +647,10 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
643647
}
644648

645649
os << ";\n";
650+
651+
if (makeNewUnavailable) {
652+
os << "+ (nonnull instancetype)new SWIFT_UNAVAILABLE;\n";
653+
}
646654
}
647655

648656
void printAbstractFunctionAsFunction(FuncDecl *FD) {

test/PrintAsObjC/availability.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757

5858
// CHECK-LABEL: @interface AvailabilitySub
5959
// CHECK-NEXT: - (nonnull instancetype)init SWIFT_UNAVAILABLE;
60+
// CHECK-NEXT: + (nonnull instancetype)new SWIFT_UNAVAILABLE;
6061
// CHECK-NEXT: - (nonnull instancetype)initWithX:(NSInteger)_ SWIFT_UNAVAILABLE;
6162
// CHECK-NEXT: @end
6263

test/PrintAsObjC/classes.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,15 @@ class DiscardableResult : NSObject {
175175
@objc init(evenMoreFun: ()) { super.init() }
176176
}
177177

178+
// CHECK-LABEL: @interface InheritedInitializersRequired
179+
// CHECK-NEXT: - (nonnull instancetype)initWithEvenMoreFun OBJC_DESIGNATED_INITIALIZER;
180+
// CHECK-NEXT: - (nonnull instancetype)init SWIFT_UNAVAILABLE;
181+
// CHECK-NEXT: + (nonnull instancetype)new SWIFT_UNAVAILABLE;
182+
// CHECK-NEXT: @end
183+
@objc class InheritedInitializersRequired : InheritedInitializers {
184+
@objc required init(evenMoreFun: ()) { super.init() }
185+
}
186+
178187
// NEGATIVE-NOT: NotObjC
179188
class NotObjC {}
180189

test/PrintAsObjC/protocols.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ protocol CustomNameType2 {}
5555
// CHECK-LABEL: @interface MyObject : NSObject <NSCoding, Fungible>
5656
// CHECK-NEXT: initWithCoder
5757
// CHECK-NEXT: init SWIFT_UNAVAILABLE
58+
// CHECK-NEXT: new SWIFT_UNAVAILABLE
5859
// CHECK-NEXT: @end
5960
// NEGATIVE-NOT: @protocol NSCoding
6061
class MyObject : NSObject, NSCoding, Fungible {

0 commit comments

Comments
 (0)