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

Mismatched @escaping for closure parameter produces inaccurate diagnostic

    XMLWordPrintable

    Details

      Description

      The following program produces the 

      class A {
          let callback: (() -> Void) -> Void
      
      
          init(f: @escaping (@escaping () -> Void) -> Void) {
              self.callback = f
          }
      }
      

      following inaccurate diagnostic:

      bug.swift:5:23: error: assigning non-escaping parameter 'f' to an @escaping closure
              self.callback = f
                            ^
      bug.swift:4:10: note: parameter 'f' is implicitly non-escaping
          init(f: @escaping (@escaping () -> Void) -> Void) {
               ^
                  @escaping 

      The suggested replacement only worsens the problem.

      I would expect to see a message like:

       

      bug.swift:7:23: error: converting non-escaping value to '() -> Void' may allow it to escape
              self.callback = f

       

      which is the one produced by

      func f(_: @escaping () -> Void) {}
      
      class A {
          let callback: (() -> Void) -> Void   
         
          init() {
              self.callback = f
          }
      }
      

       

        Attachments

          Activity

            People

            Assignee:
            lucianoalmeida1 Luciano Almeida
            Reporter:
            tbw Theo Weidmann
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: