[SR-2365] `Data` does not coerce to `NSData` in certain expressions
| Previous ID | SR-2365 |
| Radar | rdar://problem/26268242 |
| Original Reporter | @jadengeller |
| Type | Bug |
| Status | Resolved |
| Resolution | Done |
Environment
Xcode 8 beta 6, Swift 3
Additional Detail from JIRA
| Votes | 0 |
| Component/s | Compiler |
| Labels | Bug, TypeChecker |
| Assignee | @rudkx |
| Priority | Medium |
md5: 0e08ff26453f3239503db81a79f5e030
Issue Description:
let x = "hijoe".data(using: String.Encoding.utf8)! as NSData
will fail to compile with warning "Cannot convert value of type `Data` to type `NSData` in coercion".
Workaround:
`let x = "lol".data(using: String.Encoding.utf8)! as Data as NSData`
Likely related to https://bugs.swift.org/browse/SR-2379
This also fails:
class C {}
class D : C {}
extension String {
func asOptionalD() -> D? { return D() }
}
_ = "okay".asOptionalD()! as C
If we instead cast “okay” to a String (i.e. rather than it being a value that can be anything that is ExpressibleByStringLiteral), we compile without issue:
_ = (“okay" as String).asOptionalD()! as C
The test case also works for any ExpressibleBy*Literal and its corresponding "default" explicit type coercion. Seems like we need better defaulting rules.
Yes, it's a general problem that any of the ExpressibleBy*Literals and possibly even other expressions involving casts could run into. Defaulting isn't the problem here, though. I've made some progress understanding what's going on and am thinking about how we might solve it.