swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.27k stars 10.33k forks source link

[SR-952] String, NSString, CFString casting bugs #43564

Open 05109ee7-7cd9-4cd4-92d0-698e676fc6af opened 8 years ago

05109ee7-7cd9-4cd4-92d0-698e676fc6af commented 8 years ago
Previous ID SR-952
Radar None
Original Reporter @an0
Type Bug
Environment Swift 3 in Xcode 8.0 beta 6
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 1 | |Component/s | | |Labels | Bug | |Assignee | None | |Priority | Medium | md5: 2b64786b151ae4485b41c34c064a5cbb

Issue Description:

Source: https://twitter.com/an0/status/709783896263536640

Currently the behaviors are inconsistent. Run the following code to see.

let a: String? = "hello"
let b: NSString? = "hello"
let c: CFString? = "hello"

a as? NSString
//a as? CFString
a as? NSString?
a as? CFString?

b as? String
//b as? CFString
b as? String?
b as? CFString?

c as? String
c as? NSString
c as? String?
c as? NSString?

How I found the bug:
One line of my real code casting CFData? to NSData unexpectedly returns nil.

05109ee7-7cd9-4cd4-92d0-698e676fc6af commented 8 years ago

New error at line 3 in Xcode 8.0 beta 6:

cannot convert value of type 'String' to specified type 'CFString?'