Skip to content

Commit ea06af5

Browse files
committed
fix <rdar://problem/22918558> Improve error message for weak protocol properties
1 parent ef8165e commit ea06af5

File tree

7 files changed

+22
-21
lines changed

7 files changed

+22
-21
lines changed

include/swift/AST/DiagnosticsSema.def

+6-5
Original file line numberDiff line numberDiff line change
@@ -2202,11 +2202,12 @@ ERROR(tuple_ellipsis,sema_tcd,none,
22022202

22032203
// Ownership
22042204
ERROR(invalid_ownership_type,attribute_parsing,none,
2205-
"'%select{strong|weak|unowned|unowned}0' cannot be applied to "
2206-
"non-class type %1", (/*Ownership*/unsigned, Type))
2207-
ERROR(invalid_ownership_opaque_type,attribute_parsing,none,
2208-
"'%select{strong|weak|unowned|unowned}0' cannot be applied to "
2209-
"non-class type %1; consider adding a class bound",
2205+
"'%select{strong|weak|unowned|unowned}0' may only be applied to "
2206+
"class and class-bound protocol types, not %1",
2207+
(/*Ownership*/unsigned, Type))
2208+
ERROR(invalid_ownership_protocol_type,attribute_parsing,none,
2209+
"'%select{strong|weak|unowned|unowned}0' may not be applied to "
2210+
"non-class-bound protocol %1; consider adding a class bound",
22102211
(/*Ownership*/unsigned, Type))
22112212
ERROR(invalid_weak_ownership_not_optional,attribute_parsing,none,
22122213
"'weak' variable should have optional type %0", (Type))

lib/Sema/TypeCheckAttr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1509,7 +1509,7 @@ void TypeChecker::checkOwnershipAttr(VarDecl *var, OwnershipAttr *attr) {
15091509
// If we have an opaque type, suggest the possibility of adding
15101510
// a class bound.
15111511
if (type->isExistentialType() || type->is<ArchetypeType>()) {
1512-
diagnose(var->getStartLoc(), diag::invalid_ownership_opaque_type,
1512+
diagnose(var->getStartLoc(), diag::invalid_ownership_protocol_type,
15131513
(unsigned) ownershipKind, underlyingType);
15141514
} else {
15151515
diagnose(var->getStartLoc(), diag::invalid_ownership_type,

test/attr/attr_objc.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -1204,12 +1204,12 @@ class infer_instanceVar1 {
12041204

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

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

12151215

@@ -1232,11 +1232,11 @@ class infer_instanceVar1 {
12321232

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

12421242

test/attr/attributes.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -152,21 +152,21 @@ unowned unowned var weak4 : Ty0 // expected-error {{duplicate modifier}} expec
152152
unowned weak var weak5 : Ty0 // expected-error {{duplicate modifier}} expected-note {{modifier already specified here}}
153153

154154
weak
155-
var weak6 : Int // expected-error {{'weak' cannot be applied to non-class type 'Int'}}
155+
var weak6 : Int // expected-error {{'weak' may only be applied to class and class-bound protocol types, not 'Int'}}
156156
unowned
157-
var weak7 : Int // expected-error {{'unowned' cannot be applied to non-class type 'Int'}}
157+
var weak7 : Int // expected-error {{'unowned' may only be applied to class and class-bound protocol types, not 'Int'}}
158158
weak
159159
var weak8 : Class? = Ty0()
160160
unowned var weak9 : Class = Ty0()
161161
weak
162-
var weak10 : NonClass = Ty0() // expected-error {{'weak' cannot be applied to non-class type 'NonClass'; consider adding a class bound}}
162+
var weak10 : NonClass = Ty0() // expected-error {{'weak' may not be applied to non-class-bound protocol 'NonClass'; consider adding a class bound}}
163163
unowned
164-
var weak11 : NonClass = Ty0() // expected-error {{'unowned' cannot be applied to non-class type 'NonClass'; consider adding a class bound}}
164+
var weak11 : NonClass = Ty0() // expected-error {{'unowned' may not be applied to non-class-bound protocol 'NonClass'; consider adding a class bound}}
165165

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

171171
weak
172172
var weak14 : Ty0 // expected-error {{'weak' variable should have optional type 'Ty0?'}}

test/decl/protocol/protocols.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ protocol ShouldntCrash {
440440

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

446446
protocol SecondProtocol {

test/decl/var/properties.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -1096,7 +1096,7 @@ class OwnershipBadSub : OwnershipBase {
10961096
override weak var strongVar: AnyObject? { // expected-error {{cannot override strong property with weak property}}
10971097
didSet {}
10981098
}
1099-
override unowned var weakVar: AnyObject? { // expected-error {{'unowned' cannot be applied to non-class type 'AnyObject?'}}
1099+
override unowned var weakVar: AnyObject? { // expected-error {{'unowned' may only be applied to class and class-bound protocol types, not 'AnyObject?'}}
11001100
didSet {}
11011101
}
11021102
override weak var unownedVar: AnyObject { // expected-error {{'weak' variable should have optional type 'AnyObject?'}}

test/expr/closure/closures.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func testCaptureBehavior(ptr : SomeClass) {
194194

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

200200
extension SomeClass {

0 commit comments

Comments
 (0)