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

Swift 5 compiler automatic associatedtype resolution does not work

    XMLWordPrintable

    Details

      Description

      In Swift 5 (and 4.2 compability mode) compiler automatic associatedtype resolution does not work. Compiles fine by swift 4.2 compiler in XCode 10.1.

      Implementation of `checkTest(_ viewController: TestViewController, with context: String)` in `ProtocolImplementation` provides to the compiler all the necessary information about `ViewController`/`Context` types. Swift 5 compiler requires it to be specified manually:

      import UIKit
      
      public protocol SuperProtocol {
          
          associatedtype ViewController: UIViewController
          
          associatedtype Context
          
          func doTest(with context: Context) -> ViewController?
          
      }
      
      public protocol NestedProtocol: SuperProtocol {
          
          associatedtype ViewController
          
          associatedtype Context
          
          func checkTest(_ viewController: ViewController, with context: Context) -> Bool
          
      }
      
      public extension NestedProtocol {
          
          func doTest(with context: Context) -> ViewController? {
              return nil
          }
          
      }
      
      // Compilation error:
      // Type 'ProtocolImplementation' does not conform to protocol 'SuperProtocol'
      // Type 'ProtocolImplementation' does not conform to protocol 'NestedProtocol'
      class ProtocolImplementation: NestedProtocol {
      
      //  In swift 5 / 4.2 compability mode this will compile only if you uncomment this typealiases    
      //    typealias ViewController = TestViewController
      //
      //    typealias Context = String
          
          func checkTest(_ viewController: TestViewController, with context: String) -> Bool {
              return true
          }
          
      }
      
      
      class TestViewController: UIViewController {
          
      }
      
      // Compilation error:
      // Cannot convert value of type 'String' to expected argument type 'ProtocolImplementation.Context'
      let vc = ProtocolImplementation().doTest(with: "23")
      
      

      PS: Worked as expected in the beta version of Xcode 10.2

        Attachments

          Activity

            People

            Assignee:
            Unassigned Unassigned
            Reporter:
            spiceginger Evgeny Kaz
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Dates

              Created:
              Updated: