swift icon indicating copy to clipboard operation
swift copied to clipboard

[SR-2365] `Data` does not coerce to `NSData` in certain expressions

Open jadengeller opened this issue 10 years ago • 6 comments

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".

jadengeller avatar Aug 16 '16 19:08 jadengeller

Workaround:
`let x = "lol".data(using: String.Encoding.utf8)! as Data as NSData`

jadengeller avatar Aug 16 '16 19:08 jadengeller

Likely related to https://bugs.swift.org/browse/SR-2379

jadengeller avatar Aug 17 '16 21:08 jadengeller

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

rudkx avatar Aug 18 '16 19:08 rudkx

The test case also works for any ExpressibleBy*Literal and its corresponding "default" explicit type coercion. Seems like we need better defaulting rules.

CodaFi avatar Aug 19 '16 07:08 CodaFi

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.

rudkx avatar Aug 19 '16 08:08 rudkx