Skip to content
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

[SE implementation] Ownership keyword removal in protocols #11744

Merged
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
7 changes: 7 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -3115,6 +3115,13 @@ ERROR(invalid_weak_ownership_not_optional,none,
"'weak' variable should have optional type %0", (Type))
ERROR(invalid_weak_let,none,
"'weak' must be a mutable variable, because it may change at runtime", ())
ERROR(ownership_invalid_in_protocols,none,
"'%select{strong|weak|unowned|unowned}0' cannot be applied to a property declaration in a protocol",
(/*Ownership*/unsigned))
WARNING(ownership_invalid_in_protocols_compat_warning,none,
"'%select{strong|weak|unowned|unowned}0' should not be applied to a property declaration "
"in a protocol and will be disallowed in future versions",
(/*Ownership*/unsigned))

// required
ERROR(required_initializer_nonclass,none,
Expand Down
18 changes: 18 additions & 0 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2006,6 +2006,10 @@ void TypeChecker::checkTypeModifyingDeclAttributes(VarDecl *var) {
}

void TypeChecker::checkOwnershipAttr(VarDecl *var, OwnershipAttr *attr) {
// Don't check ownership attribute if the declaration is already marked invalid.
if (var->isInvalid())
return;

Type type = var->getType();
Type interfaceType = var->getInterfaceType();

Expand Down Expand Up @@ -2053,6 +2057,20 @@ 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.
if (Context.isSwiftVersionAtLeast(5))
diagnose(attr->getLocation(),
diag::ownership_invalid_in_protocols,
(unsigned) ownershipKind)
.fixItRemove(attr->getRange());
else
diagnose(attr->getLocation(),
diag::ownership_invalid_in_protocols_compat_warning,
(unsigned) ownershipKind)
.fixItRemove(attr->getRange());

attr->setInvalid();
}

if (attr->isInvalid())
Expand Down
12 changes: 12 additions & 0 deletions test/Compatibility/ownership_protocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %target-typecheck-verify-swift -swift-version 4
// Generate a swift 4 compatible warning if ownership is specified in a protocol

class SomeClass {}

protocol P {
weak var foo: SomeClass? { get set } // expected-warning {{'weak' should not be applied to a property declaration in a protocol and will be disallowed in future versions}}
unowned var foo2: SomeClass { get set } // expected-warning {{'unowned' should not be applied to a property declaration in a protocol and will be disallowed in future versions}}
weak var foo3: Int? { get set } // expected-error {{'weak' may only be applied to class and class-bound protocol types, not 'Int'}}
unowned var foo4: Int { get set } // expected-error {{'unowned' may only be applied to class and class-bound protocol types, not 'Int'}}
}

2 changes: 1 addition & 1 deletion test/SILGen/Inputs/weak_other.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ extension Router {
}

public protocol Environment : class {
unowned var router: Router { get }
var router: Router { get }
}

open class UI {
Expand Down
2 changes: 1 addition & 1 deletion test/SILGen/properties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ struct ObservingPropertiesWithOwnershipTypesInferred {

// <rdar://problem/16554876> property accessor synthesization of weak variables doesn't work
protocol WeakPropertyProtocol {
weak var maybePresent : Ref? { get set }
var maybePresent : Ref? { get set }
}

struct WeakPropertyStruct : WeakPropertyProtocol {
Expand Down
29 changes: 29 additions & 0 deletions test/decl/protocol/ownership_protocol.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// RUN: %target-typecheck-verify-swift -swift-version 5

class SomeClass {}

protocol P {
weak var foo: SomeClass? { get set } // expected-error {{'weak' cannot be applied to a property declaration in a protocol}}
unowned var foo2: SomeClass { get set } // expected-error {{'unowned' cannot be applied to a property declaration in a protocol}}
weak var foo3: Int? { get set } // expected-error {{'weak' may only be applied to class and class-bound protocol types, not 'Int'}}
unowned var foo4: Int { get set } // expected-error {{'unowned' may only be applied to class and class-bound protocol types, not 'Int'}}
var foo9: SomeClass? { get }
}

extension P {
weak var foo5: SomeClass? // expected-error {{extensions must not contain stored properties}}
unowned var foo6: SomeClass // expected-error {{extensions must not contain stored properties}}

weak var foo7: SomeClass? { // Okay
return SomeClass()
}

unowned var foo8: SomeClass { // Okay
return SomeClass()
}

weak var foo9: SomeClass? { // Okay
return nil
}
}

2 changes: 1 addition & 1 deletion test/stdlib/WeakMirror.swift
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ mirrors.test("struct/StructHasNativeWeakReference") {
import Foundation

@objc protocol ObjCClassExistential : class {
weak var weakProperty: AnyObject? { get set }
var weakProperty: AnyObject? { get set }
var x: Int { get }
}

Expand Down