Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-14322] Bridging to NSError fails for instances of a class #56681

Open
martinr448 opened this issue Mar 10, 2021 · 2 comments
Open

[SR-14322] Bridging to NSError fails for instances of a class #56681

martinr448 opened this issue Mar 10, 2021 · 2 comments
Labels
bridging Feature → casting: type bridging bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. casting Feature: explicit casting (is, as, as? and as!) compiler The Swift compiler in itself objective-c interop Feature: Interoperability with Objective-C swift 5.9 type checker Area → compiler: Semantic analysis unexpected error Bug: Unexpected error

Comments

@martinr448
Copy link

martinr448 commented Mar 10, 2021

Previous ID SR-14322
Radar rdar://problem/75275123
Original Reporter @martinr448
Type Bug
Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, TypeChecker
Assignee None
Priority Medium

md5: d0169e4ca27e6cfd3360b31001ed01d0

Issue Description:

(Also posted in the Swift forum: Bridging to NSError fails for instances of a class.)

According to SE-0112, every type that conforms to the Error protocol is implicitly bridged to NSError. This does not work with reference types, the last line in the following example does not compile:

class MyError: Error {}

extension MyError: CustomNSError {
    public static var errorDomain: String { "MyDomain" }
    public var errorCode: Int { 13 }
    public var errorUserInfo: [String : Any] { ["Foo" : "Bar" ] }
}

let sError = MyError()

let nsError = sError as NSError
// error: 'MyError' is not convertible to 'NSError'; did you mean to use 'as!' to force downcast?

Inserting an “intermediate cast” to Error makes it compile and run as expected:

let nsError = sError as Error as NSError
print(nsError.userInfo)
// ["Foo": "Bar"]

That problem occurs only if the custom error type is a class, not with a struct or an enum.

@typesanitizer
Copy link

@swift-ci create

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@cristiancardosodexcom
Copy link

if let error = error as? NSError, error.code == NSURLErrorNotConnectedToInternet {
                return 
            }

@AnthonyLatsis AnthonyLatsis added casting Feature: explicit casting (is, as, as? and as!) objective-c interop Feature: Interoperability with Objective-C unexpected error Bug: Unexpected error bridging Feature → casting: type bridging swift 5.9 labels Apr 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bridging Feature → casting: type bridging bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. casting Feature: explicit casting (is, as, as? and as!) compiler The Swift compiler in itself objective-c interop Feature: Interoperability with Objective-C swift 5.9 type checker Area → compiler: Semantic analysis unexpected error Bug: Unexpected error
Projects
None yet
Development

No branches or pull requests

4 participants