Skip to content
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

Updated Bad diagnostic errors #65531

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 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
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -3056,7 +3056,7 @@ ERROR(duplicate_inheritance,none,
WARNING(duplicate_anyobject_class_inheritance,none,
"redundant inheritance from 'AnyObject' and Swift 3 'class' keyword", ())
ERROR(inheritance_from_protocol_with_superclass,none,
"inheritance from class-constrained protocol composition type %0", (Type))
"cannot inherit from class-constrained protocol composition type %0", (Type))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to split this into multiple diagnostics and change phrasing. There are a couple of issues here which are not clearly stated by the diagnostic:

  • There are only curtain types allowed in inheritance clause, this could be fixed by splitting diagnostic into multiple ones;
  • Inheritance in extension is even more restricted to protocols only;
  • Message is not very clear about the why to give actionable guidance to the developer how to fix the problem;
    • I think it might be useful to separate type kind + name information into a note and give error more information about what is going on here;

WARNING(anyobject_class_inheritance_deprecated,Deprecation,
"using 'class' keyword to define a class-constrained protocol is deprecated; "
"use 'AnyObject' instead", ())
Expand All @@ -3065,7 +3065,7 @@ ERROR(multiple_inheritance,none,
ERROR(inheritance_from_non_protocol_or_class,none,
"inheritance from non-protocol, non-class type %0", (Type))
ERROR(inheritance_from_non_protocol,none,
"inheritance from non-protocol type %0", (Type))
"class can only inherit from protocol in extension declaration %0", (Type))
ERROR(inheritance_from_anyobject,none,
"only protocols can inherit from 'AnyObject'", ())
ERROR(inheritance_from_parameterized_protocol,none,
Expand Down
2 changes: 1 addition & 1 deletion test/Generics/inheritance.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func testGenericInherit() {
}


struct SS<T> : T { } // expected-error{{inheritance from non-protocol type 'T'}}
struct SS<T> : T { } // expected-error{{class can only inherit from protocol in extension declaration 'T'}}
LucianoPAlmeida marked this conversation as resolved.
Show resolved Hide resolved
enum SE<T> : T { case X } // expected-error{{raw type 'T' is not expressible by a string, integer, or floating-point literal}} // expected-error {{SE<T>' declares raw type 'T', but does not conform to RawRepresentable and conformance could not be synthesized}} expected-error{{RawRepresentable conformance cannot be synthesized because raw type 'T' is not Equatable}}

// Also need Equatable for init?(RawValue)
Expand Down
2 changes: 1 addition & 1 deletion test/Sema/moveonly_restrictions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ extension ProtocolCheckMoveOnlyEnum : Any {}

extension ProtocolCheckMoveOnlyEnum : AnyHashable {}
// expected-error@-1 {{move-only enum 'ProtocolCheckMoveOnlyEnum' cannot conform to 'AnyHashable'}}
// expected-error@-2 {{inheritance from non-protocol type 'AnyHashable'}}
// expected-error@-2 {{class can only inherit from protocol in extension declaration 'AnyHashable'}}

// But a normal extension is ok.
extension ProtocolCheckMoveOnlyKlass {}
Expand Down
2 changes: 1 addition & 1 deletion test/decl/enum/enumtest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ class C {}
protocol P {}

enum E : C & P {}
// expected-error@-1 {{inheritance from class-constrained protocol composition type 'C & P'}}
// expected-error@-1 {{cannot inherit from class-constrained protocol composition type 'C & P'}}

// https://github.com/apple/swift/issues/53923

Expand Down
4 changes: 2 additions & 2 deletions test/decl/inherit/inherit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class D3 : Any, A { } // expected-error{{superclass 'A' must appear first in the
class D4 : P & P1, A { } // expected-error{{superclass 'A' must appear first in the inheritance clause}}{{18-21=}}{{12-12=A, }}

// Struct inheriting a class
struct S : A { } // expected-error{{inheritance from non-protocol type 'A'}}
struct S : A { } // expected-error{{class can only inherit from protocol in extension declaration 'A'}}

// Protocol inheriting a class
protocol Q : A { }

// Extension inheriting a class
extension C : A { } // expected-error{{inheritance from non-protocol type 'A'}}
extension C : A { } // expected-error{{class can only inherit from protocol in extension declaration 'A'}}

// Keywords in inheritance clauses
struct S2 : struct { } // expected-error{{expected type}}
Expand Down
6 changes: 3 additions & 3 deletions test/decl/protocol/protocol_with_superclass_objc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
class Base {}

@objc protocol Protocol1 : Base {}
// expected-error@-1 {{inheritance from non-protocol type 'Base'}}
// expected-error@-1 {{class can only inherit from protocol in extension declaration 'Base'}}

@objc protocol OtherProtocol {}

typealias Composition = OtherProtocol & Base

@objc protocol Protocol2 : Composition {}
// expected-error@-1 {{inheritance from class-constrained protocol composition type 'Composition' (aka 'Base & OtherProtocol')}}
// expected-error@-1 {{cannot inherit from class-constrained protocol composition type 'Composition' (aka 'Base & OtherProtocol')}}

@objc protocol Protocol3 : OtherProtocol & Base {}
// expected-error@-1 {{inheritance from class-constrained protocol composition type 'Base & OtherProtocol'}}
// expected-error@-1 {{cannot inherit from class-constrained protocol composition type 'Base & OtherProtocol'}}
2 changes: 1 addition & 1 deletion test/type/explicit_existential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ func testExistentialAlias(x: Existential, y: any Constraint) {}
// Reject explicit existential types in inheritance clauses
protocol Empty {}

struct S : any Empty {} // expected-error {{inheritance from non-protocol type 'any Empty'}}
struct S : any Empty {} // expected-error {{class can only inherit from protocol in extension declaration 'any Empty'}}
class C : any Empty {} // expected-error {{inheritance from non-protocol, non-class type 'any Empty'}}

// FIXME: Diagnostics are not great in the enum case because we confuse this with a raw type
Expand Down
2 changes: 1 addition & 1 deletion test/type/explicit_existential_swift6.swift
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ func testExistentialAlias(x: Existential, y: any Constraint) {}
// Reject explicit existential types in inheritance clauses
protocol Empty {}

struct S : any Empty {} // expected-error {{inheritance from non-protocol type 'any Empty'}}
struct S : any Empty {} // expected-error {{class can only inherit from protocol in extension declaration 'any Empty'}}
class C : any Empty {} // expected-error {{inheritance from non-protocol, non-class type 'any Empty'}}

// FIXME: Diagnostics are not great in the enum case because we confuse this with a raw type
Expand Down
2 changes: 1 addition & 1 deletion test/type/implicit_some/explicit_existential.swift
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ func testExistentialAlias(x: Existential, y: any Constraint) {}
// Reject explicit existential types in inheritance clauses
protocol Empty {}

struct S : any Empty {} // expected-error {{inheritance from non-protocol type 'any Empty'}}
struct S : any Empty {} // expected-error {{class can only inherit from protocol in extension declaration 'any Empty'}}
class C : any Empty {} // expected-error {{inheritance from non-protocol, non-class type 'any Empty'}}

// FIXME: Diagnostics are not great in the enum case because we confuse this with a raw type
Expand Down