Skip to content

[Diag] QoI: ownership in protocol check should not be 'else' clause #16093

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
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
22 changes: 9 additions & 13 deletions lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2183,21 +2183,17 @@ void TypeChecker::checkReferenceOwnershipAttr(VarDecl *var,

diagnose(var->getStartLoc(), D, ownershipKind, underlyingType);
attr->setInvalid();
} else if (isa<ProtocolDecl>(var->getDeclContext()) &&
!cast<ProtocolDecl>(var->getDeclContext())->isObjC()) {
}

auto PDC = dyn_cast<ProtocolDecl>((var->getDeclContext()));
if (PDC && !PDC->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,
ownershipKind)
.fixItRemove(attr->getRange());
else
diagnose(attr->getLocation(),
diag::ownership_invalid_in_protocols_compat_warning,
ownershipKind)
.fixItRemove(attr->getRange());

auto D = Context.isSwiftVersionAtLeast(5)
? diag::ownership_invalid_in_protocols
: diag::ownership_invalid_in_protocols_compat_warning;
diagnose(attr->getLocation(), D, ownershipKind)
.fixItRemove(attr->getRange());
attr->setInvalid();
}

Expand Down
14 changes: 10 additions & 4 deletions test/Compatibility/ownership_protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@
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'}}
// expected-warning@+1 {{'weak' should not be applied to a property declaration in a protocol and will be disallowed in future versions}}
weak var foo: SomeClass? { get set }
// expected-warning@+1 {{'unowned' 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@+2 {{'weak' should not be applied to a property declaration in a protocol and will be disallowed in future versions}}
// expected-error@+1 {{'weak' may only be applied to class and class-bound protocol types, not 'Int'}}
weak var foo3: Int? { get set }
// expected-warning@+2 {{'unowned' should not be applied to a property declaration in a protocol and will be disallowed in future versions}}
// expected-error@+1 {{'unowned' may only be applied to class and class-bound protocol types, not 'Int'}}
unowned var foo4: Int { get set }
}

14 changes: 10 additions & 4 deletions test/decl/protocol/ownership_protocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
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'}}
// expected-error@+1 {{'weak' cannot be applied to a property declaration in a protocol}}
weak var foo: SomeClass? { get set }
// expected-error@+1 {{'unowned' cannot be applied to a property declaration in a protocol}}
unowned var foo2: SomeClass { get set }
// expected-error@+2 {{'weak' cannot be applied to a property declaration in a protocol}}
// expected-error@+1 {{'weak' may only be applied to class and class-bound protocol types, not 'Int'}}
weak var foo3: Int? { get set }
// expected-error@+2 {{'unowned' cannot be applied to a property declaration in a protocol}}
// expected-error@+1 {{'unowned' may only be applied to class and class-bound protocol types, not 'Int'}}
unowned var foo4: Int { get set }
var foo9: SomeClass? { get }
}

Expand Down
1 change: 1 addition & 0 deletions test/decl/protocol/protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -465,6 +465,7 @@ protocol ShouldntCrash {

// rdar://problem/18168866
protocol FirstProtocol {
// expected-warning@+1 {{'weak' should not be applied to a property declaration in a protocol and will be disallowed in future versions}}
weak var delegate : SecondProtocol? { get } // expected-error{{'weak' must not be applied to non-class-bound 'SecondProtocol'; consider adding a protocol conformance that has a class bound}}
}

Expand Down