Skip to content

[4.1] [SR-7182] Allow ownership keywords on properties in @objc protocols. #15275

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}