Skip to content

Commit 0bcf355

Browse files
committed
[PrintAsObjC] Use 'unsafe_unretained' to print 'unowned', not 'assign'
The ObjC generator previously preserved a subtle difference between 'unowned' and 'unowned(unsafe)' / Unmanaged by printing the former as 'assign' and the latter as 'unsafe_unretained'. Upstream Clang, however, has gotten a new warning to discourage the use of 'assign' with reference-countable types at all. Since it was always a subtle distinction, just go with the new convention and print 'unsafe_unretained' for 'unowned' properties as well. rdar://problem/44290715
1 parent 3e7e9f3 commit 0bcf355

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

lib/PrintAsObjC/PrintAsObjC.cpp

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1065,9 +1065,6 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
10651065
os << ", readonly";
10661066

10671067
// Print the ownership semantics, if relevant.
1068-
// We treat "unowned" as "assign" (even though it's more like
1069-
// "safe_unretained") because we want people to think twice about
1070-
// allowing that object to disappear.
10711068
Type ty = VD->getInterfaceType();
10721069
if (auto weakTy = ty->getAs<WeakStorageType>()) {
10731070
auto innerTy = weakTy->getReferentType()->getOptionalObjectType();
@@ -1077,9 +1074,11 @@ class ObjCPrinter : private DeclVisitor<ObjCPrinter>,
10771074
(innerTy->isObjCExistentialType() && !isCFTypeRef(innerTy))) {
10781075
os << ", weak";
10791076
}
1080-
} else if (ty->is<UnownedStorageType>()) {
1081-
os << ", assign";
1082-
} else if (ty->is<UnmanagedStorageType>()) {
1077+
} else if (ty->is<UnownedStorageType>()|| ty->is<UnmanagedStorageType>()) {
1078+
// We treat "unowned" as "unsafe_unretained" (even though it's more like
1079+
// "safe_unretained") because we want people to think twice about
1080+
// allowing that object to disappear. "unowned(unsafe)" (and Unmanaged,
1081+
// handled below) really are "unsafe_unretained".
10831082
os << ", unsafe_unretained";
10841083
} else {
10851084
Type copyTy = ty;

test/PrintAsObjC/classes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,7 @@ public class NonObjCClass { }
503503
// CHECK-NEXT: SWIFT_CLASS_PROPERTY(@property (nonatomic, class, readonly, strong) Properties * _Nonnull sharedRO;)
504504
// CHECK-NEXT: + (Properties * _Nonnull)sharedRO SWIFT_WARN_UNUSED_RESULT;
505505
// CHECK-NEXT: @property (nonatomic, weak) Properties * _Nullable weakOther;
506-
// CHECK-NEXT: @property (nonatomic, assign) Properties * _Nonnull unownedOther;
506+
// CHECK-NEXT: @property (nonatomic, unsafe_unretained) Properties * _Nonnull unownedOther;
507507
// CHECK-NEXT: @property (nonatomic, unsafe_unretained) Properties * _Nonnull unmanagedOther;
508508
// CHECK-NEXT: @property (nonatomic, unsafe_unretained) Properties * _Nullable unmanagedByDecl;
509509
// CHECK-NEXT: @property (nonatomic, weak) id <MyProtocol> _Nullable weakProto;

test/PrintAsObjC/protocols.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ extension NSString : A, ZZZ {}
191191
@objc class Subclass : RootClass1, ZZZ {}
192192

193193
// CHECK-LABEL: @protocol UnownedProperty
194-
// CHECK-NEXT: @property (nonatomic, assign) id _Nonnull unownedProp;
194+
// CHECK-NEXT: @property (nonatomic, unsafe_unretained) id _Nonnull unownedProp;
195195
@objc protocol UnownedProperty {
196196
unowned var unownedProp: AnyObject { get set }
197197
}

0 commit comments

Comments
 (0)