Skip to content

Commit

Permalink
Merge pull request #15275 from DougGregor/sr-7182-4.1
Browse files Browse the repository at this point in the history
[4.1] [SR-7182] Allow ownership keywords on properties in @objc protocols.
  • Loading branch information
DougGregor authored Mar 15, 2018
2 parents e5e5a80 + af43813 commit 04baf31
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2091,8 +2091,10 @@ void TypeChecker::checkOwnershipAttr(VarDecl *var, OwnershipAttr *attr) {

diagnose(var->getStartLoc(), D, (unsigned) ownershipKind, underlyingType);
attr->setInvalid();
} else if (dyn_cast<ProtocolDecl>(var->getDeclContext())) {
// Ownership does not make sense in protocols.
} else if (isa<ProtocolDecl>(var->getDeclContext()) &&
!cast<ProtocolDecl>(var->getDeclContext())->isObjC()) {
// Ownership does not make sense in protocols, except for "weak" on
// properties of Objective-C protocols.
if (Context.isSwiftVersionAtLeast(5))
diagnose(attr->getLocation(),
diag::ownership_invalid_in_protocols,
Expand Down
12 changes: 12 additions & 0 deletions test/PrintAsObjC/protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -190,5 +190,17 @@ extension NSString : A, ZZZ {}
// CHECK-LABEL: @interface Subclass : RootClass1 <ZZZ>{{$}}
@objc class Subclass : RootClass1, ZZZ {}

// CHECK-LABEL: @protocol UnownedProperty
// CHECK-NEXT: @property (nonatomic, assign) id _Nonnull unownedProp;
@objc protocol UnownedProperty {
unowned var unownedProp: AnyObject { get set }
}

// CHECK-LABEL: @protocol WeakProperty
// CHECK-NEXT: @property (nonatomic, weak) id _Nullable weakProp;
@objc protocol WeakProperty {
weak var weakProp: AnyObject? { get set }
}

// Deliberately at the end of the file.
@objc protocol ZZZ {}
10 changes: 10 additions & 0 deletions test/attr/attr_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2294,3 +2294,13 @@ class BadClass {
@_versioned @objc dynamic func badMethod2() {}
// expected-error@-1 {{'@_versioned' attribute cannot be applied to 'dynamic' declarations}}
}

@objc
protocol ObjCProtocolWithWeakProperty {
weak var weakProp: AnyObject? { get set } // okay
}

@objc
protocol ObjCProtocolWithUnownedProperty {
unowned var unownedProp: AnyObject { get set } // okay
}

0 comments on commit 04baf31

Please sign in to comment.