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

Proper support for protocol 'where' clause with constraints on 'Self'

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Xcode Version 9.0 beta 4 (9M189t), Playground, MacBook Pro 10.12.6

      Description

      The following code fails to compile in a swift playground:

      protocol Toggling where Self: Equatable {
        static var all: [Self] { get }
        func toggled() -> Self
      }
      extension Toggling {
        func toggled() -> Self {
          let current = Self.all.index(of: self) ?? 0
          let next = (current + 1) % Self.all.count
          return Self.all[next]
        }
      }
      

      Changing to simple conformance makes it work:

      protocol Toggling : Equatable { .... }
      

      The two should be equivalent.

      Also, putting the constraint of Self: Equatable on the protocol extension (and removing it from the protocol declaration) also makes it work.

      Having the Self requirement on both the protocol definition and extension correctly identifies that the constraint is redundant but also fails to compile.

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                slavapestov Slava Pestov
                Reporter:
                rayfix Ray Fix
              • Votes:
                1 Vote for this issue
                Watchers:
                4 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: