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

Function-level nested types cannot conform to Equatable

    XMLWordPrintable

    Details

    • Type: Bug
    • Status: Reopened
    • Priority: Medium
    • Resolution: Unresolved
    • Component/s: Compiler
    • Labels:
      None
    • Environment:

      Swift build number: Xcode 8.1 (8B62) embedded version
      Target OS: iOS
      Build OS: macOS Sierra 16A323
      Build Tool: Xcode Version 8.1 (8B62)

      Description

      The compiler fails to compile a struct that conforms to `Equatable` inside a function.

      struct RootLevelStruct: Equatable { // compiles fine
          static func ==(lhs: RootLevelStruct, rhs: RootLevelStruct) -> Bool {
              return false
          }
      }
      
      class MyClass {
          struct ClassLevelStruct: Equatable { // compiles fine
              static func ==(lhs: ClassLevelStruct, rhs: ClassLevelStruct) -> Bool {
                  return false
              }
          }
          
          func someFunction() {
              struct FunctionLevelStruct: Equatable { // Error: Protocol 'Equatable' can only be used a generic constraint because it has Self or associated type requirements
                  static func ==(lhs: FunctionLevelStruct, rhs: FunctionLevelStruct) -> Bool {
                      return false
                  }
              }
          }
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ellneal Ell Neal
              Votes:
              5 Vote for this issue
              Watchers:
              13 Start watching this issue

                Dates

                Created:
                Updated: