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

Type-constrained protocol cannot be used as a generic argument for similarly constrained generic parameter

    XMLWordPrintable

    Details

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

      macOS Sierra 10.12.5 (16F73), Xcode 9.0 beta 2 (9M137d)

      Description

      Given a generic type X<Y> where Y is constrained to some type Z and a protocol A that constrains Self to Z, a variable or constant declaration of X<A> should be considered valid. Currently, as of Xcode 9 beta 2, the compiler gives a warning similar to "'X' requires that 'A' inherit from 'Z'" even though that is already true.

      // Imagine these sheet classes and the bed struct in a separate module somewhere
      
      class Sheet {
          func pullOver() {
              print("sleep time")
          }
      }
      
      class ComfySheet : Sheet {
          public static let veryComfy: ComfySheet = {
              return ComfySheet()
          }()
      
          func comfort() {
              print("comfortable")
          }
      }
      
      class BouncySheet : Sheet {
          public static let veryBouncy: BouncySheet = {
              return BouncySheet()
          }()
      
          func bounce() {
              print("bounce")
          }
      }
      
      class AwfulSheet : Sheet {
          override func pullOver() {
              print("ew")
          }
      }
      
      struct Bed<SheetType> where SheetType : Sheet {
          public let size: Double
          public let sheets: SheetType
      }
      
      // This protocol and the corresponding extensions are editable
      
      protocol EnjoymentMeasurable where Self : Sheet {
          func enjoy()
      }
      
      extension ComfySheet : EnjoymentMeasurable {
          func enjoy() {
              print("8/10")
          }
      }
      
      extension BouncySheet : EnjoymentMeasurable {
          func enjoy() {
              print("11/10")
          }
      }
      
      func doSomething() {
          let bed: Bed<EnjoymentMeasurable> // error here: "'Bed' requires that 'EnjoymentMeasurable' inherit from 'Sheet'"
          bed = Bed(size: 4.0, sheets: ComfySheet.veryComfy)
          bed.sheets.enjoy()
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              jarrodldavis Jarrod Davis
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: