Skip to content

Diag: Reword some errors for invalid usage of covariant Self #36418

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 1 commit into from
Mar 13, 2021
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
11 changes: 7 additions & 4 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -2964,7 +2964,7 @@ ERROR(dynamic_self_non_method,none,
"%select{global|local}0 function cannot return 'Self'", (bool))

ERROR(dynamic_self_invalid,none,
"covariant 'Self' can only appear as the possibly optional type of a "
"covariant 'Self' or 'Self?' can only appear as the type of a "
"property, subscript or method result; did you mean '%0'?", (StringRef))
ERROR(dynamic_self_in_mutable_property,none,
"mutable property cannot have covariant 'Self' type", ())
Expand All @@ -2973,11 +2973,14 @@ ERROR(dynamic_self_in_stored_property,none,
ERROR(dynamic_self_in_mutable_subscript,none,
"mutable subscript cannot have covariant 'Self' type", ())
ERROR(dynamic_self_invalid_property,none,
"covariant 'Self' can only appear at the top level of property type", ())
"covariant 'Self' or 'Self?' can only appear at the top level of "
"property type", ())
ERROR(dynamic_self_invalid_subscript,none,
"covariant 'Self' can only appear at the top level of subscript element type", ())
"covariant 'Self' or 'Self?' can only appear at the top level of "
"subscript element type", ())
ERROR(dynamic_self_invalid_method,none,
"covariant 'Self' can only appear at the top level of method result type", ())
"covariant 'Self' or 'Self?' can only appear at the top level of "
"method result type", ())
ERROR(dynamic_self_stored_property_init,none,
"covariant 'Self' type cannot be referenced from a stored property initializer", ())
ERROR(dynamic_self_default_arg,none,
Expand Down
2 changes: 1 addition & 1 deletion test/decl/ext/generic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ extension Array where Element == String { }
extension GenericClass : P3 where T : P3 { }

extension GenericClass where Self : P3 { }
// 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}}
// 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}}
// expected-error@-2{{'GenericClass<T>' in conformance requirement does not refer to a generic parameter or associated type}}

protocol P4 {
Expand Down
4 changes: 2 additions & 2 deletions test/decl/func/dynamic_self.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ enum E0 {
class C0 {
func f() -> Self { } // okay

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'?}}
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'?}}

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'?}}
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'?}}
}

protocol P0 {
Expand Down
12 changes: 6 additions & 6 deletions test/type/self.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ struct S0<T> {
}

class C0<T> {
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'?}}
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'?}}
}

enum E0<T> {
Expand Down Expand Up @@ -47,15 +47,15 @@ final class FinalMario : Mario {

class A<T> {
typealias _Self = Self
// expected-error@-1 {{covariant 'Self' can only appear as the possibly optional type of a property, subscript or method result; did you mean 'A'?}}
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'A'?}}
let b: Int
required init(a: Int) {
print("\(Self.self).\(#function)")
Self.y()
b = a
}
static func z(n: Self? = nil) {
// expected-error@-1 {{covariant 'Self' can only appear as the possibly optional type of a property, subscript or method result; did you mean 'A'?}}
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear as the type of a property, subscript or method result; did you mean 'A'?}}
print("\(Self.self).\(#function)")
}
class func y() {
Expand Down Expand Up @@ -130,14 +130,14 @@ class C {
// expected-warning@-1 {{conditional cast from 'Self' to 'Self' always succeeds}}
}
func h(j: () -> Self) -> () -> Self {
// expected-error@-1 {{covariant 'Self' can only appear at the top level of method result type}}
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear at the top level of method result type}}
return { return self }
}
func i() -> (Self, Self) {}
// expected-error@-1 {{covariant 'Self' can only appear at the top level of method result type}}
// expected-error@-1 {{covariant 'Self' or 'Self?' can only appear at the top level of method result type}}

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

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