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

Duplicated error message for type that doesn't conform 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 }
          ^
      

      If the type also declares that it conforms to refined types such as BidirectionalCollection, RandomAccessCollection, and RangeReplaceableCollection, the error message and the notes will be duplicated once for every additional protocol:

       

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

      This may cause the user to incorrectly assume that there is more than one problem in the code, and makes it harder to understand that the real problem is in the conformance to Collection (as opposed to the other protocols).

       

      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:
              1 Start watching this issue

                Dates

                Created:
                Updated: