swiftlang / swift

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

[SR-4414] Implicit type casting from `Error` to` NSError` works before other type casting. #46991

Open swift-ci opened 7 years ago

swift-ci commented 7 years ago
Previous ID SR-4414
Radar None
Original Reporter tarunon (JIRA User)
Type Bug
Environment Swift 3.1, Xcode 8.3, Playground
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, Runtime | |Assignee | @DougGregor | |Priority | Medium | md5: 6da17e3a189bae0abaaf29a0dc66b997

Issue Description:

1. Make a protocol `A`.

  1. Conform a protocol into `CustomError` and `NSError`.
  2. Cast `Error` to `A`, then implicit type cast `Error` to `NSError` work.

Minimum code is here.

import Foundation

protocol MyCustomErrorProtocol {
    var hoge: Int { get }
}

struct MyCustomError: Error, MyCustomErrorProtocol {
    var hoge: Int {
        return 1
    }
}

extension NSError: MyCustomErrorProtocol {
    var hoge: Int {
        return 0
    }
}

let error: Error = MyCustomError()

if let cError = error as? MyCustomErrorProtocol {
    cError.hoge // 0 (If Swift 3.0 then 1)
}
belkadan commented 7 years ago

Ooh, that's tricky. @DougGregor, @jckarter?

swift-ci commented 6 years ago

Comment by Nobuo Saito (JIRA)

Hi, I think this issue was fixed on Swift 4.0.3/Xcode9.2
It was my misunderstanding.