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

Originator:info
Number:rdar://23832161 Date Originated:09-Dec-2015 04:55 PM
Status:Open Resolved:
Product:Developer Tools Product Version:Xcode 7.0 (7A218)
Classification:Other Bug Reproducible:Always
 
This is a duplicate of rdar://22708537

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


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!