Skip to content

Commit

Permalink
fix <rdar://problem/22918558> Improve error message for weak protocol…
Browse files Browse the repository at this point in the history
… properties
  • Loading branch information
lattner committed Nov 15, 2015
1 parent ef8165e commit ea06af5
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 21 deletions.
11 changes: 6 additions & 5 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2202,11 +2202,12 @@ ERROR(tuple_ellipsis,sema_tcd,none,

// Ownership
ERROR(invalid_ownership_type,attribute_parsing,none,
"'%select{strong|weak|unowned|unowned}0' cannot be applied to "
"non-class type %1", (/*Ownership*/unsigned, Type))
ERROR(invalid_ownership_opaque_type,attribute_parsing,none,
"'%select{strong|weak|unowned|unowned}0' cannot be applied to "
"non-class type %1; consider adding a class bound",
"'%select{strong|weak|unowned|unowned}0' may only be applied to "
"class and class-bound protocol types, not %1",
(/*Ownership*/unsigned, Type))
ERROR(invalid_ownership_protocol_type,attribute_parsing,none,
"'%select{strong|weak|unowned|unowned}0' may not be applied to "
"non-class-bound protocol %1; consider adding a class bound",
(/*Ownership*/unsigned, Type))
ERROR(invalid_weak_ownership_not_optional,attribute_parsing,none,
"'weak' variable should have optional type %0", (Type))
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/TypeCheckAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1509,7 +1509,7 @@ void TypeChecker::checkOwnershipAttr(VarDecl *var, OwnershipAttr *attr) {
// If we have an opaque type, suggest the possibility of adding
// a class bound.
if (type->isExistentialType() || type->is<ArchetypeType>()) {
diagnose(var->getStartLoc(), diag::invalid_ownership_opaque_type,
diagnose(var->getStartLoc(), diag::invalid_ownership_protocol_type,
(unsigned) ownershipKind, underlyingType);
} else {
diagnose(var->getStartLoc(), diag::invalid_ownership_type,
Expand Down
12 changes: 6 additions & 6 deletions test/attr/attr_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1204,12 +1204,12 @@ class infer_instanceVar1 {

weak var var_Weak_fail1: PlainClass?
weak var var_Weak_bad2: PlainStruct?
// expected-error@-1 {{'weak' cannot be applied to non-class type 'PlainStruct'}}
// expected-error@-1 {{'weak' may only be applied to class and class-bound protocol types, not 'PlainStruct'}}

weak var var_Weak_bad3: PlainEnum?
// expected-error@-1 {{'weak' cannot be applied to non-class type 'PlainEnum'}}
// expected-error@-1 {{'weak' may only be applied to class and class-bound protocol types, not 'PlainEnum'}}
weak var var_Weak_bad4: String?
// expected-error@-1 {{'weak' cannot be applied to non-class type 'String'}}
// expected-error@-1 {{'weak' may only be applied to class and class-bound protocol types, not 'String'}}
// CHECK-NOT: @objc{{.*}}Weak_fail


Expand All @@ -1232,11 +1232,11 @@ class infer_instanceVar1 {

unowned var var_Unowned_fail1: PlainClass
unowned var var_Unowned_bad2: PlainStruct
// expected-error@-1 {{'unowned' cannot be applied to non-class type 'PlainStruct'}}
// expected-error@-1 {{'unowned' may only be applied to class and class-bound protocol types, not 'PlainStruct'}}
unowned var var_Unowned_bad3: PlainEnum
// expected-error@-1 {{'unowned' cannot be applied to non-class type 'PlainEnum'}}
// expected-error@-1 {{'unowned' may only be applied to class and class-bound protocol types, not 'PlainEnum'}}
unowned var var_Unowned_bad4: String
// expected-error@-1 {{'unowned' cannot be applied to non-class type 'String'}}
// expected-error@-1 {{'unowned' may only be applied to class and class-bound protocol types, not 'String'}}
// CHECK-NOT: @objc{{.*}}Unowned_fail


Expand Down
12 changes: 6 additions & 6 deletions test/attr/attributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -152,21 +152,21 @@ unowned unowned var weak4 : Ty0 // expected-error {{duplicate modifier}} expec
unowned weak var weak5 : Ty0 // expected-error {{duplicate modifier}} expected-note {{modifier already specified here}}

weak
var weak6 : Int // expected-error {{'weak' cannot be applied to non-class type 'Int'}}
var weak6 : Int // expected-error {{'weak' may only be applied to class and class-bound protocol types, not 'Int'}}
unowned
var weak7 : Int // expected-error {{'unowned' cannot be applied to non-class type 'Int'}}
var weak7 : Int // expected-error {{'unowned' may only be applied to class and class-bound protocol types, not 'Int'}}
weak
var weak8 : Class? = Ty0()
unowned var weak9 : Class = Ty0()
weak
var weak10 : NonClass = Ty0() // expected-error {{'weak' cannot be applied to non-class type 'NonClass'; consider adding a class bound}}
var weak10 : NonClass = Ty0() // expected-error {{'weak' may not be applied to non-class-bound protocol 'NonClass'; consider adding a class bound}}
unowned
var weak11 : NonClass = Ty0() // expected-error {{'unowned' cannot be applied to non-class type 'NonClass'; consider adding a class bound}}
var weak11 : NonClass = Ty0() // expected-error {{'unowned' may not be applied to non-class-bound protocol 'NonClass'; consider adding a class bound}}

unowned
var weak12 : NonClass = Ty0() // expected-error {{'unowned' cannot be applied to non-class type 'NonClass'; consider adding a class bound}}
var weak12 : NonClass = Ty0() // expected-error {{'unowned' may not be applied to non-class-bound protocol 'NonClass'; consider adding a class bound}}
unowned
var weak13 : NonClass = Ty0() // expected-error {{'unowned' cannot be applied to non-class type 'NonClass'; consider adding a class bound}}
var weak13 : NonClass = Ty0() // expected-error {{'unowned' may not be applied to non-class-bound protocol 'NonClass'; consider adding a class bound}}

weak
var weak14 : Ty0 // expected-error {{'weak' variable should have optional type 'Ty0?'}}
Expand Down
2 changes: 1 addition & 1 deletion test/decl/protocol/protocols.swift
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ protocol ShouldntCrash {

// rdar://problem/18168866
protocol FirstProtocol {
weak var delegate : SecondProtocol? { get } // expected-error{{'weak' cannot be applied to non-class type 'SecondProtocol'}}
weak var delegate : SecondProtocol? { get } // expected-error{{'weak' may only be applied to class and class-bound protocol types, not 'SecondProtocol'}}
}

protocol SecondProtocol {
Expand Down
2 changes: 1 addition & 1 deletion test/decl/var/properties.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1096,7 +1096,7 @@ class OwnershipBadSub : OwnershipBase {
override weak var strongVar: AnyObject? { // expected-error {{cannot override strong property with weak property}}
didSet {}
}
override unowned var weakVar: AnyObject? { // expected-error {{'unowned' cannot be applied to non-class type 'AnyObject?'}}
override unowned var weakVar: AnyObject? { // expected-error {{'unowned' may only be applied to class and class-bound protocol types, not 'AnyObject?'}}
didSet {}
}
override weak var unownedVar: AnyObject { // expected-error {{'weak' variable should have optional type 'AnyObject?'}}
Expand Down
2 changes: 1 addition & 1 deletion test/expr/closure/closures.swift
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func testCaptureBehavior(ptr : SomeClass) {

let i = 42
// expected-warning @+1 {{variable 'i' was never mutated}} {{19-20=let}}
doStuff { [weak i] in i! } // expected-error {{'weak' cannot be applied to non-class type 'Int'}}
doStuff { [weak i] in i! } // expected-error {{'weak' may only be applied to class and class-bound protocol types, not 'Int'}}
}

extension SomeClass {
Expand Down

0 comments on commit ea06af5

Please sign in to comment.