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

[SR-13906] Error message is unclear when a type conforms to AnyObject or a composition of it #56304

Closed
WowbaggersLiquidLunch opened this issue Nov 27, 2020 · 6 comments · Fixed by #41537
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers improvement inheritance Feature → type declarations → class: Subclassing and inheritance of class members swift 5.3 type checker Area → compiler: Semantic analysis

Comments

@WowbaggersLiquidLunch
Copy link
Collaborator

WowbaggersLiquidLunch commented Nov 27, 2020

Previous ID SR-13906
Radar rdar://problem/71913170
Original Reporter @WowbaggersLiquidLunch
Type Improvement
Status In Progress
Resolution
Environment

Apple Swift version 5.3.2 (swiftlang-1200.0.44.1 clang-1200.0.32.28)

macOS 10.15.7 (19H15); x86_64-apple-darwin19.6.0

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI, StarterBug, TypeChecker
Assignee aniketiq (JIRA)
Priority Medium

md5: fc60657a00f25cb82f7af51339e8e32b

Issue Description:

Currently, the error message is "Inheritance from non-protocol type ..." when a type tries to conform to AnyObject or a composition of AnyObject and some other protocols:

protocol P {}
typealias Q = P & AnyObject
struct S: Q {}
enum T: P & AnyObject {}
struct U: P, AnyObject {}
error: inheritance from non-protocol type 'Q' (aka 'P & AnyObject')
struct S: Q {}
       ^
error: inheritance from non-protocol type 'P & AnyObject'
enum T: P & AnyObject {}
     ^
error: inheritance from non-protocol, non-class type 'AnyObject'
class U: P, AnyObject {}
       ^

It would be better if the error message is "non-protocol 'T' cannot conform to 'AnyObject'":

error: non-protocol 'S' cannot conform to composition 'Q' (aka 'P & AnyObject')
struct S: Q {}
       ^
error: non-protocol 'T' cannot conform to composition 'P & AnyObject'
enum T: P & AnyObject {}
     ^
error: non-protocol 'U' cannot conform to 'AnyObject'
class U: P, AnyObject {}
       ^
@WowbaggersLiquidLunch
Copy link
Collaborator Author

The source of the error messages are located at inheritance_from_non_protocol and inheritance_from_non_protocol_or_class in file include/swift/AST/DiagnosticsSema.def. However, we can't just edit the messages in-place, because they are not exclusive to types trying to conform to AnyObject. For example (copied from one of the test cases):

class classProperty: Int { 0 } // expected-error {{inheritance from non-protocol, non-class type 'Int'}}

@typesanitizer
Copy link

@swift-ci create

@CodaFi
Copy link
Member

CodaFi commented Dec 15, 2020

You shouldn't edit the first one, but the second should use a custom diagnostic string.

@swift-ci
Copy link
Collaborator

Comment by Aniket Ray (JIRA)

In the First and Third statements of error, they seem to be very apt. I guess, the main focus should be on changing the second error.

enum T: P & AnyObject {}
error: inheritance from non-protocol type 'P & AnyObject'
enum T: P & AnyObject {} 
     ^

It is finally to be changed to this.

error: non-protocol 'T' cannot conform to composition 'P & AnyObject'
enum T: P & AnyObject {}
     ^

theindigamer (JIRA User) CodaFi (JIRA User) What are your thoughts?
Should I work on a PR?

@swiftlysingh
Copy link

Hey folks 👋 , was checking the error message on swift playgrounds, I got Only protocols can inherit from 'AnyObject' instead of expected (mentioned in issue) error: inheritance from non-protocol type 'P & AnyObject'.

The message seams similar to what is required from the issue. What should be done here?

@AnthonyLatsis @WowbaggersLiquidLunch

@xwu
Copy link
Collaborator

xwu commented Apr 27, 2023

Good catch. Looks like #41537 has resolved this issue.

@xwu xwu closed this as completed Apr 27, 2023
@AnthonyLatsis AnthonyLatsis added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. inheritance Feature → type declarations → class: Subclassing and inheritance of class members swift 5.3 and removed bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. labels Apr 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers improvement inheritance Feature → type declarations → class: Subclassing and inheritance of class members swift 5.3 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants