Skip to content

Improve Error to NSError conversion #2912

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

Merged

Conversation

kgominyuk
Copy link
Contributor

@kgominyuk kgominyuk commented Oct 26, 2020

Error->NSError conversion in Darwin Foundation fills domain and code even if provided Error does not conform to CustomNSError. This patch brings same behavior to swift-corelibs-foundation.

@kgominyuk kgominyuk force-pushed the improve-error-to-nserror-conversion branch from 9e27f04 to 7c09116 Compare October 26, 2020 13:34
@lxbndr
Copy link
Contributor

lxbndr commented Oct 26, 2020

cc @millenomi

@MaxDesiatov
Copy link
Contributor

@swift-ci please test

XCTAssertEqual((SwiftError.two as NSError).code, 3)
XCTAssertEqual((SwiftError.three("three") as NSError).code, 0)
XCTAssertEqual((SwiftError.four as NSError).code, 4)
XCTAssertEqual((SwiftError.five("five") as NSError).code, 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not quite clear why these values are what they are, especially for those with associated values. Could you add a comment to the test that clarifies this please?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test was initially based on actual observed behavior on Darwin platform. In search of details I've found that default error code for enum case is based on enum tag. Tag generation logic is located in EnumImplStrategy::getTagIndex ( https://github.com/apple/swift/blob/d6227dcca01b74b64f078949e69e91324e39f815/lib/IRGen/GenEnum.cpp#L239-L255 ). It starts from cases with payload (.three and .five), and then proceeds to cases without payload (.one , .two , .four ). This is why we are observing such error codes: .three = 0, .five = 1, .one = 2, .two = 3, .four = 4.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't know that, very interesting behavior. Glad that it could become consistent across platforms.

Copy link
Contributor

@MaxDesiatov MaxDesiatov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@MaxDesiatov
Copy link
Contributor

@swift-ci please test

Error->NSError conversion in Darwin Foundation fills `domain` and `code` even if provided Error does not conform to CustomNSError. This patch brings same behavior to swift-corelibs-foundation.
@kgominyuk kgominyuk force-pushed the improve-error-to-nserror-conversion branch from 759010e to 9dfb052 Compare October 27, 2020 12:15
@kgominyuk
Copy link
Contributor Author

@MaxDesiatov Could you please retrigger CI? I squashed commits into one.

@MaxDesiatov
Copy link
Contributor

@swift-ci please test

Copy link
Member

@compnerd compnerd left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems reasonable to me to preserve the domain and code. I wonder if @millenomi can shed some light on if that was intentionally being dropped here.

@millenomi
Copy link
Contributor

The only reason we didn't go deep on the first attempt was that we didn't have a lot of infrastructure set up yet and much was deferred to when we'd have good stories for conversion, for localization, etc., which now we do. More compatibility is always a good thing. LGTM

@millenomi millenomi merged commit fc09501 into swiftlang:main Oct 28, 2020
@kgominyuk kgominyuk deleted the improve-error-to-nserror-conversion branch November 9, 2020 13:04
kgominyuk pushed a commit to readdle/swift-corelibs-foundation that referenced this pull request Nov 16, 2020
FoundationTest build on macOS has been broken since swiftlang#2912.
Compilation fails with error cannot convert value of type 'SwiftError' to type 'NSError' in coercion.
I guess we have to exclude related tests on Darwin platform, but leave them in DarwinCompatibilityTests.
kgominyuk pushed a commit to readdle/swift-corelibs-foundation that referenced this pull request Nov 16, 2020
FoundationTest build on macOS has been broken since swiftlang#2912.
Compilation fails with error: cannot convert value of type 'SwiftError' to type 'NSError' in coercion.
I guess we have to exclude related tests on Darwin platform, but leave them in DarwinCompatibilityTests.
kgominyuk pushed a commit to readdle/swift-corelibs-foundation that referenced this pull request Nov 16, 2020
FoundationTest build on macOS has been broken since swiftlang#2912.
Compilation fails with error `cannot convert value of type 'SwiftError' to type 'NSError' in coercion`.
I guess we have to exclude related tests on Darwin platform, but leave them in DarwinCompatibilityTests.
lxbndr pushed a commit to readdle/swift-corelibs-foundation that referenced this pull request Dec 7, 2020
FoundationTest build on macOS has been broken since swiftlang#2912.
Compilation fails with error `cannot convert value of type 'SwiftError' to type 'NSError' in coercion`.
I guess we have to exclude related tests on Darwin platform, but leave them in DarwinCompatibilityTests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants