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

Diagnostics are pretty much always useless when errors involve type inference and closures



    • Type: Bug
    • Status: Resolved
    • Priority: Medium
    • Resolution: Done
    • Component/s: Compiler
    • Environment:


      I'm filing a particular example, but this is just one of the hundreds I've ran into regarding this since Swift 1.0. I honestly don't think this has gotten any better since the very first version.

      This is a reduction from my own codebase. As I was going through the Swift 4.0 migrations, lots of places relying on tuple deconstruction in closure parameters stopped working, and diagnostics leave A LOT to be desired (https://twitter.com/NachoSoto/status/874336197568454656, https://twitter.com/NachoSoto/status/874336844757975040).

      Consider this:

      protocol P {
          associatedtype E    
      struct S<A> {
          init() {}   
      struct B<Y>: P {
          typealias E = Y
      class X {
          func f1() -> S<Void> {
              return f(
                  d: { _ in () }, 
                  b: B(), 
                  isRepeat: { _, _ in false }
          private func f<A, B: P>(
              d: @escaping (String) throws -> A, 
              b: B?, 
              isRepeat: @escaping (A, A) -> Bool
          ) -> S<A> where B.E == A {
              return S()

      This was compiling in Swift 3.x. Notice the `isRepeat` parameter I'm passing only has one _.

      error: cannot convert value of type '()' to closure result type '_.E'
              d: { _ in () },

      Say what? What's the problem in d? That looks correct to me...
      Spoiler alert, the correct fix is changing isRepeat to

      _, _

      Instead of just _.

      Again, like I said in the title, this is not an isolated bug, this happens ALL THE TIME when types can't be inferred. Diagnostic always point to the wrong thing with a useless message.

      When are you going to start focusing on diagnostics? I have filed SO MANY diagnostic bugs that are still open:




            NachoSoto Nacho Soto
            NachoSoto Nacho Soto
            0 Vote for this issue
            6 Start watching this issue