Skip to content

[SR-15467] Confusing error message for unavailable member when conforming to protocol #57772

@swift-ci

Description

@swift-ci
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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions