Skip to content

Commit 04baf31

Browse files
authored
Merge pull request #15275 from DougGregor/sr-7182-4.1
[4.1] [SR-7182] Allow ownership keywords on properties in @objc protocols.
2 parents e5e5a80 + af43813 commit 04baf31

File tree

3 files changed

+26
-2
lines changed

3 files changed

+26
-2
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2091,8 +2091,10 @@ void TypeChecker::checkOwnershipAttr(VarDecl *var, OwnershipAttr *attr) {
20912091

20922092
diagnose(var->getStartLoc(), D, (unsigned) ownershipKind, underlyingType);
20932093
attr->setInvalid();
2094-
} else if (dyn_cast<ProtocolDecl>(var->getDeclContext())) {
2095-
// Ownership does not make sense in protocols.
2094+
} else if (isa<ProtocolDecl>(var->getDeclContext()) &&
2095+
!cast<ProtocolDecl>(var->getDeclContext())->isObjC()) {
2096+
// Ownership does not make sense in protocols, except for "weak" on
2097+
// properties of Objective-C protocols.
20962098
if (Context.isSwiftVersionAtLeast(5))
20972099
diagnose(attr->getLocation(),
20982100
diag::ownership_invalid_in_protocols,

test/PrintAsObjC/protocols.swift

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,17 @@ extension NSString : A, ZZZ {}
190190
// CHECK-LABEL: @interface Subclass : RootClass1 <ZZZ>{{$}}
191191
@objc class Subclass : RootClass1, ZZZ {}
192192

193+
// CHECK-LABEL: @protocol UnownedProperty
194+
// CHECK-NEXT: @property (nonatomic, assign) id _Nonnull unownedProp;
195+
@objc protocol UnownedProperty {
196+
unowned var unownedProp: AnyObject { get set }
197+
}
198+
199+
// CHECK-LABEL: @protocol WeakProperty
200+
// CHECK-NEXT: @property (nonatomic, weak) id _Nullable weakProp;
201+
@objc protocol WeakProperty {
202+
weak var weakProp: AnyObject? { get set }
203+
}
204+
193205
// Deliberately at the end of the file.
194206
@objc protocol ZZZ {}

test/attr/attr_objc.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2294,3 +2294,13 @@ class BadClass {
22942294
@_versioned @objc dynamic func badMethod2() {}
22952295
// expected-error@-1 {{'@_versioned' attribute cannot be applied to 'dynamic' declarations}}
22962296
}
2297+
2298+
@objc
2299+
protocol ObjCProtocolWithWeakProperty {
2300+
weak var weakProp: AnyObject? { get set } // okay
2301+
}
2302+
2303+
@objc
2304+
protocol ObjCProtocolWithUnownedProperty {
2305+
unowned var unownedProp: AnyObject { get set } // okay
2306+
}

0 commit comments

Comments
 (0)