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

Problems with generics + AnyObject or class protocol conformance

    XMLWordPrintable

    Details

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

      Xcode 9.0 (9A235)

      Description

      Test cases:

      This fails to compile
      ```
      public protocol MyDelegate: AnyObject {
      }

      class A<P1: AnyObject> {
      init(param: P1) { }
      }

      func prettyPleaseMakeItCompile() {
      let a: MyDelegate = (nil as MyDelegate?)!
      A(param: a)
      }
      ```

      This also fails to compile:
      ```
      public protocol MyDelegate: class {
      }

      class A<P1: AnyObject> {
      init(param: P1) { }
      }

      func prettyPleaseMakeItCompile() {
      let a: MyDelegate = (nil as MyDelegate?)!
      A(param: a)
      }
      ```

      but this compiles
      ```
      @objc public protocol MyDelegate {
      }

      class A<P1: AnyObject> {
      init(param: P1) { }
      }

      func prettyPleaseMakeItCompile() {
      let a: MyDelegate = (nil as MyDelegate?)!
      A(param: a)
      }
      ```

      and this also does

      ```
      public protocol MyDelegate: AnyObject {
      }

      class A<P1: AnyObject> {
      init(param: P1) { }
      }

      class B: MyDelegate {

      }

      func prettyPleaseMakeItCompile()

      { A(param: B()) }

      ```

      and so does this
      ```
      public protocol MyDelegate: class {
      }

      class A<P1: AnyObject> {
      init(param: P1) { }
      }

      class B: MyDelegate {

      }

      func prettyPleaseMakeItCompile()

      { A(param: B()) }

      ```

      Expected result, all provided code snippets should compile successfully.

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              kzaher Krunoslav Zaher
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: