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-15467] Confusing error message for unavailable member when conforming to protocol #57772

Open
swift-ci opened this issue Nov 9, 2021 · 1 comment
Labels
compiler The Swift compiler in itself improvement

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Nov 9, 2021

Previous ID SR-15467
Radar None
Original Reporter vinivendra (JIRA User)
Type Improvement

Attachment: Download

Environment

MacOS 12.0.1 - Swift 5.5.1

Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement
Assignee None
Priority Medium

md5: b7664ab271752893116e32098a0c909a

relates to:

  • SR-15466 Duplicated error message for type that doesn't conform to protocol

Issue Description:

When creating a type that conforms to Collection, create implementations for all requirements except for `subscript(bounds: Range<Index>) -> Subsequence`. This will cause the compiler to try to satisfy this requirement with an unavailable subscript, which will raise the following error:

test.swift:2:15: error: type 'ListSlice<Element>' does not conform to protocol 'Collection'
public struct ListSlice<Element>: Collection {
              ^
test.swift:2:15: error: unavailable subscript 'subscript(_:)' was used to satisfy a requirement of protocol 'Collection'
public struct ListSlice<Element>: Collection {
              ^
Swift.Collection:3:12: note: 'subscript(_:)' declared here
    public subscript(bounds: Range<Self.Index>) -> Self.SubSequence { get }
           ^
Swift.Collection:12:5: note: requirement 'subscript(_:)' declared here
    subscript(bounds: Range<Self.Index>) -> Self.SubSequence { get }

This error has the following problems:

  1. It is unlikely that the user knows about this default implementation that is no longer available, and the error doesn't point to that implementation (which, in this case, contains a comment describing why it's no longer available).

  2. Saying the unavailable subscript "was used to satisfy a requirement" makes it seem as if a user's subscript was used to satisfy the requirement, when in fact this refers to a subscript in the compiler (that the user likely doesn't know about).

  3. The notes don't show up in Xcode, and by only reading the error message it's unclear which required subscript is the problem. For instance, Collection requires implementing both `subscript(position: Int) -> Element` and `subscript(bounds: Range<Index>) -> SubSequence`, both of which match the 'subscript(_🙂' signature in the error message.

This error could be improved if it read something like: “*default implementation of 'Collection' requirement 'subscript(*🙂' of type '(Range<Self.Index>) -> Self.SubSequence' is marked unavailable; did you mean to implement your own?_”. This message makes it clear that the error refers to a default implementation; that the problem is in the "Range" subscript; and that the user can implement their own subscript to solve it.

It would also be useful to add a fix-it to help the user add their own implementation.

The attached test.swift file causes these errors in Swift 5.5.1.

@WowbaggersLiquidLunch
Copy link
Collaborator

Sorry for attaching 2 images of emoji to this bug report. I was reading it and my mouse dragged on it and caused it to be uploaded as attachment twice. I tried to delete them, but somehow can't.

EDIT: Ok I deleted them. Sorry again!

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
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 improvement
Projects
None yet
Development

No branches or pull requests

2 participants