UserInfo of NSError is discarded during type conversion in a generic function

Originator:raylillywhite
Number:rdar://22708537 Date Originated:15-Sep-2015 02:09 PM
Status:Duplicate of 22188396 (Open) Resolved:
Product:Developer Tools Product Version:Xcode 7.0 (7A218)
Classification: Reproducible:Always
 
Summary:
Casting an NSError that's in a generic function with type parameter `<E: ErrorType>` to an NSError strips the error's UserInfo dictionary.

Steps to Reproduce:
let userInfo: [NSObject: AnyObject] = ["TestKey": "TestValue"]

let e = NSError(domain: "TestDomain", code: 1, userInfo: userInfo)

func doSomethingGeneric<E: ErrorType>(error: E) -> Void {
    print(error) // ✅ user info present
    print(error as NSError)  // ❌ user info missing ❌
}

doSomethingGeneric(e)

Expected Results:
Expect the NSError to retain its UserInfo dictionary during the cast

Actual Results:
The NSError loses its UserInfo dictionary. There are many similar cases where casting an ErrorType variable that contains an NSError to an NSError variable _doesn't_ lose the userInfo dictionary. The attached playground has many more examples where this casting works and retains the NSError's UserInfo, as well as the above example and one more that also involves generic functions where the casting loses the UserInfo dictionary.

Version:
Xcode 7.0 (7A218)

Notes:


Configuration:


Attachments:
'ErrorCastingBug.playground.zip' was successfully uploaded.

Comments

ErrorCastingBug.playground can be found at http://d.pr/f/14OcO/3OueExDX

By raylillywhite at Sept. 15, 2015, 9:12 p.m. (reply...)

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!