$0 treated differently than "x in" regarding @noescape

Originator:robnapier
Number:rdar://21404405 Date Originated:16-Jun-2015 01:33 PM
Status:Open Resolved:
Product:Developer Tools Product Version:
Classification:Enhancement Reproducible:Always
 
This code compiles:

extension Array {
    func withUnsafeBufferPointer<R>(@noescape body: (UnsafeBufferPointer<T>) throws -> R) throws -> R {
        return try self.withUnsafeBufferPointer { buf in
            return Result{try body(buf)}}.value()
    }
}

This code, which should be identical, returns an error “Closure use of @noescape parameter 'body' may allow it to escape” (also “could not find member ‘value’, but I believe that comes from the first):

extension Array {
    func withUnsafeBufferPointer<R>(@noescape body: (UnsafeBufferPointer<T>) throws -> R) throws -> R {
        return try self.withUnsafeBufferPointer {
            return Result{try body($0)}}.value()
    }
}

The code should be treated the same whether we name the closure parameter or use $0.

Additional code that this calls:

enum Result<T> {
    case Success(T)
    case Failure(ErrorType)

    func value() throws -> T {
        switch self {
        case .Success(let value): return value
        case .Failure(let err): throw err
        }
    }

    init(@noescape f: () throws -> T) {
        do {
            self = .Success(try f())
        } catch {
            self = .Failure(error)
        }
    }
}

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!