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

error: reference to invalid associated type 'Context' of type 'S'

    XMLWordPrintable

    Details

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

      Description

      This issue is related to https://bugs.swift.org/browse/SR-5546 and the '[swift-evolution] [RFC] Associated type inference' thread that Doug started.

      So first of all I must say I'm delighted that there's ongoing work in this area and we seem to be getting closer to fixing this issue which is blocking us pretty badly.

      The code below is almost the same as the one in SR-5546 but I changed the {{typealias Context = ... }} to an {{associatedtype Context = ... }} . In Swift 4.0 that doesn't make any difference and crashes the compiler.

      In a recent (4.1-dev (LLVM 5b54bd1e96, Clang 03ed64977b, Swift 88a7a55e83)) Swift 4.1 dev snapshot, however we get a different error which also seems bogus:

      test2.swift:33:25: error: reference to invalid associated type 'Context' of type 'S'
          public func f2(_ x: Context, _ y: PB) {
      

      That's really odd as it just works fine for f1 but breaks for f2, I'm confused.

      public struct Foo<A, B, C> {}
      
      public protocol P {
          associatedtype PA
          associatedtype PB = Never
          associatedtype PC = Never
      
          associatedtype Context = Foo<PA, PB, PC>
      
          func f1(_ x: Context, _ y: PA)
          func f2(_ x: Context, _ y: PB)
          func f3(_ x: Context, _ y: PC)
          func f4(_ x: Context)
      }
      
      public extension P {
          public func f1(_ x: Context, _ y: PA) {
          }
          public func f2(_ x: Context, _ y: PB) {
          }
          public func f3(_ x: Context, _ y: PC) {
          }
          public func f4(_ x: Context) {
          }
      }
      
      public struct S: P {
          public typealias PA = String
          public typealias PB = Int
      
          public func f1(_ x: Context, _ y: PA) {
          }
          public func f2(_ x: Context, _ y: PB) {
          }
      }
      

      Also quite interestingly, in a recent (LLVM 20c8fee562, Clang b227f55990, Swift 603e5359fd) Swift master dev snapshot I get the following messages for the code above:

      $ /Library/Developer/Toolchains/swift-DEVELOPMENT-SNAPSHOT-2017-11-28-a.xctoolchain/usr/bin/swiftc test2.swift 
      test2.swift:33:25: error: reference to invalid associated type 'Context' of type 'S'
          public func f2(_ x: Context, _ y: PB) {
                              ^
      test2.swift:33:17: warning: instance method 'f2' nearly matches defaulted requirement 'f2' of protocol 'P'
          public func f2(_ x: Context, _ y: PB) {
                      ^
      test2.swift:33:17: note: move 'f2' to an extension to silence this warning
          public func f2(_ x: Context, _ y: PB) {
                      ^
      test2.swift:11:10: note: requirement 'f2' declared here
          func f2(_ x: Context, _ y: PB)
               ^
      

      very odd, on top of the error that the 4.1 dev snapshot shows too it also gives me a near miss warning that

      public func f2(_ x: Context, _ y: PB)
      

      is almost

      func f2(_ x: Context, _ y: PB)
      

      to which I'd say that this isn't a near miss but an exact hit?

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned
              Reporter:
              jw Johannes Weiss
              Votes:
              1 Vote for this issue
              Watchers:
              4 Start watching this issue

                Dates

                Created:
                Updated: