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

Compiler fails to infer return type of the transform closure in a map call

    XMLWordPrintable

    Details

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

      Observed with Xcode 7.2.1 on OS X 10.11.3

      Description

      When the map method is called with closure for its transform argument, and the closure return type specification is omitted, everything works fine if the type of the returned expression is implicit – for example,

      let x1 = [1,2].map { _ in
          return "abc"
      }
      

      But if the same expression is assigned to a variable, and the variable is returned:

      let x2 = [1,2].map { _ in
          let x = "abc"
          return x
      }
      

      then the compiler gives an error. In Xcode 7.2.1, the error is

      Cannot invoke 'map' with an argument list of type '(@noescape (Int) throws -> _)'
      Expected an argument list of type '(@noescape (Self.Generator.Element) throws -> T)'
      

      It seems that in both calls, the transform closure is returning a String, and the compiler knows it is a String, so the error in the second call looks like a compiler bug.

      (Making the return type explicit – "map { _ -> String in ... }" – works around the bug.)

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              Neil Faiman Neil Faiman
              Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: