Uploaded image for project: 'Swift'
  1. Swift
  2. SR-15467

Confusing error message for unavailable member when conforming to protocol

    XMLWordPrintable

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      MacOS 12.0.1 - Swift 5.5.1

      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.

       

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              vinivendra Vinicius Vendramini
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: