Skip to content

Commit 78346c2

Browse files
Merge pull request #36418 from AnthonyLatsis/cov-self-diag
Diag: Reword some errors for invalid usage of covariant Self
2 parents 9b016cb + b7fcd2f commit 78346c2

File tree

4 files changed

+16
-13
lines changed

4 files changed

+16
-13
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2964,7 +2964,7 @@ ERROR(dynamic_self_non_method,none,
29642964
"%select{global|local}0 function cannot return 'Self'", (bool))
29652965

29662966
ERROR(dynamic_self_invalid,none,
2967-
"covariant 'Self' can only appear as the possibly optional type of a "
2967+
"covariant 'Self' or 'Self?' can only appear as the type of a "
29682968
"property, subscript or method result; did you mean '%0'?", (StringRef))
29692969
ERROR(dynamic_self_in_mutable_property,none,
29702970
"mutable property cannot have covariant 'Self' type", ())
@@ -2973,11 +2973,14 @@ ERROR(dynamic_self_in_stored_property,none,
29732973
ERROR(dynamic_self_in_mutable_subscript,none,
29742974
"mutable subscript cannot have covariant 'Self' type", ())
29752975
ERROR(dynamic_self_invalid_property,none,
2976-
"covariant 'Self' can only appear at the top level of property type", ())
2976+
"covariant 'Self' or 'Self?' can only appear at the top level of "
2977+
"property type", ())
29772978
ERROR(dynamic_self_invalid_subscript,none,
2978-
"covariant 'Self' can only appear at the top level of subscript element type", ())
2979+
"covariant 'Self' or 'Self?' can only appear at the top level of "
2980+
"subscript element type", ())
29792981
ERROR(dynamic_self_invalid_method,none,
2980-
"covariant 'Self' can only appear at the top level of method result type", ())
2982+
"covariant 'Self' or 'Self?' can only appear at the top level of "
2983+
"method result type", ())
29812984
ERROR(dynamic_self_stored_property_init,none,
29822985
"covariant 'Self' type cannot be referenced from a stored property initializer", ())
29832986
ERROR(dynamic_self_default_arg,none,

test/decl/ext/generic.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ extension Array where Element == String { }
149149
extension GenericClass : P3 where T : P3 { }
150150

151151
extension GenericClass where Self : P3 { }
152-
// expected-error@-1{{covariant 'Self' can only appear as the possibly optional type of a property, subscript or method result; did you mean 'GenericClass'?}} {{30-34=GenericClass}}
152+
// expected-error@-1{{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'GenericClass'?}} {{30-34=GenericClass}}
153153
// expected-error@-2{{'GenericClass<T>' in conformance requirement does not refer to a generic parameter or associated type}}
154154

155155
protocol P4 {

test/decl/func/dynamic_self.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ enum E0 {
2424
class C0 {
2525
func f() -> Self { } // okay
2626

27-
func g(_ ds: Self) { } // expected-error{{covariant 'Self' can only appear as the possibly optional type of a property, subscript or method result; did you mean 'C0'?}}
27+
func g(_ ds: Self) { } // expected-error{{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'C0'?}}
2828

29-
func h(_ ds: Self) -> Self { } // expected-error{{covariant 'Self' can only appear as the possibly optional type of a property, subscript or method result; did you mean 'C0'?}}
29+
func h(_ ds: Self) -> Self { } // expected-error{{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'C0'?}}
3030
}
3131

3232
protocol P0 {

test/type/self.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ struct S0<T> {
55
}
66

77
class C0<T> {
8-
func foo(_ other: Self) { } // expected-error{{covariant 'Self' can only appear as the possibly optional type of a property, subscript or method result; did you mean 'C0'?}}
8+
func foo(_ other: Self) { } // expected-error{{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'C0'?}}
99
}
1010

1111
enum E0<T> {
@@ -47,15 +47,15 @@ final class FinalMario : Mario {
4747

4848
class A<T> {
4949
typealias _Self = Self
50-
// expected-error@-1 {{covariant 'Self' can only appear as the possibly optional type of a property, subscript or method result; did you mean 'A'?}}
50+
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'A'?}}
5151
let b: Int
5252
required init(a: Int) {
5353
print("\(Self.self).\(#function)")
5454
Self.y()
5555
b = a
5656
}
5757
static func z(n: Self? = nil) {
58-
// expected-error@-1 {{covariant 'Self' can only appear as the possibly optional type of a property, subscript or method result; did you mean 'A'?}}
58+
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'A'?}}
5959
print("\(Self.self).\(#function)")
6060
}
6161
class func y() {
@@ -118,14 +118,14 @@ class C {
118118
// expected-warning@-1 {{conditional cast from 'Self' to 'Self' always succeeds}}
119119
}
120120
func h(j: () -> Self) -> () -> Self {
121-
// expected-error@-1 {{covariant 'Self' can only appear at the top level of method result type}}
121+
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear at the top level of method result type}}
122122
return { return self }
123123
}
124124
func i() -> (Self, Self) {}
125-
// expected-error@-1 {{covariant 'Self' can only appear at the top level of method result type}}
125+
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear at the top level of method result type}}
126126

127127
func j() -> Self.Type {}
128-
// expected-error@-1 {{covariant 'Self' can only appear at the top level of method result type}}
128+
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear at the top level of method result type}}
129129

130130
let p0: Self? // expected-error {{stored property cannot have covariant 'Self' type}}
131131
var p1: Self? // expected-error {{stored property cannot have covariant 'Self' type}}

0 commit comments

Comments
 (0)