Skip to content

[Swift 3.0] Eliminate double-wrapping of NSErrors in _SwiftNativeNSError #3976

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

DougGregor
Copy link
Member

What's in this pull request?

This PR eliminates the double-wrapping of NSErrors in _SwiftNativeNSError, e.g., when an Error type that is an NSError (or subclasses NSError) or is a synthesized struct that wraps an NSError (for imported Cocoa errors) gets placed into an Error existential. Previously, these would end up getting boxed up against in a _SwiftNativeNSError, losing NSError identity and potentially causing a wrapping loop. Make sure that neither SILGen nor the runtime perform this double-wrapping.

Resolved bug number: (SR-1562)

This also resolves rdar://problem/27658748 and rdar://problem/26370984


Before merging this pull request to apple/swift repository:

  • Test pull request on Swift continuous integration.

Triggering Swift CI

The swift-ci is triggered by writing a comment on this PR addressed to the GitHub user @swift-ci. Different tests will run depending on the specific comment that you use. The currently available comments are:

Smoke Testing

Platform Comment
All supported platforms @swift-ci Please smoke test
All supported platforms @swift-ci Please smoke test and merge
OS X platform @swift-ci Please smoke test OS X platform
Linux platform @swift-ci Please smoke test Linux platform

A smoke test on macOS does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library only for macOS. Simulator standard libraries and
    device standard libraries are not built.
  3. lldb is not built.
  4. The test and validation-test targets are run only for macOS. The optimized
    version of these tests are not run.

A smoke test on Linux does the following:

  1. Builds the compiler incrementally.
  2. Builds the standard library incrementally.
  3. lldb is built incrementally.
  4. The swift test and validation-test targets are run. The optimized version of these
    tests are not run.
  5. lldb is tested.

Validation Testing

Platform Comment
All supported platforms @swift-ci Please test
All supported platforms @swift-ci Please test and merge
OS X platform @swift-ci Please test OS X platform
OS X platform @swift-ci Please benchmark
Linux platform @swift-ci Please test Linux platform

Lint Testing

Language Comment
Python @swift-ci Please Python lint

Note: Only members of the Apple organization can trigger swift-ci.

@DougGregor
Copy link
Member Author

@swift-ci please test

@jckarter
Copy link
Contributor

jckarter commented Aug 3, 2016

Does the runtime or optimizer ever do Error erasures? We should also look for _BridgedStoredNSError and do the same thing in those situations, such as when dynamic casting any as? Error.

@DougGregor
Copy link
Member Author

Ah, you're right that I missed a case in the runtime! The ExistentialTypeRepresentation::Error case of _dynamicCastToExistential goes straight to swift_allocError.

…asses.

In addition to using the 'Class' existential representation for
NSError, use it for subclasses of NSError, which can also be toll-free
bridged. Narrowly addresses rdar://problem/27658748.

(cherry picked from commit 3f960e3)
…xistentials.

Imported Cocoa error types are represented by structs wrapping an
NSError. The conversion from these structs to Error would end up
boxing the structs in _SwiftNativeNSError, losing identity and leading
to a wrapping loop.

Instead, extract the embedded NSError if there is one. In the Swift
runtime, do this as part of the dynamic cast to NSError, using a (new,
defaulted) requirement in the Error type so we can avoid an extra
runtime lookup of the protocol. In SILGEn, do this by looking for the
_BridgedStoredNSError protocol conformance when erasing to an Error
type. Fixes SR-1562 / rdar://problem/26370984.

(cherry picked from commit d219531)
When emitting an existential erasure to Error from an archetype, use
the _getEmbeddedNSError() witness. If it produces an NSError, erase
that; otherwise, go through the normal erasure path.

Of course, make NSError and CFError implement _getEmbeddedNSError() so
this kicks in for the obvious cases as well as the more obscure ones.

Fixes the rest of SR-1562 / rdar://problem/26370984.

(cherry picked from commit 75e85dc)
…SError.

This clarifies the 'Indirect' case. Thanks, Dmitri!

(cherry picked from commit 4b8b7bb)
…r existential.

SILGen already attempts to extract an embedded NSError when
type-erasing to an Error existential; make the runtime do the same
thing dynamically.

Huge thanks to Joe Groff who noticed that I missed this path.

(cherry picked from commit e83fb64)
@DougGregor DougGregor force-pushed the swift-3-nserror-wrapping branch from 0b177df to 171a18c Compare August 4, 2016 06:23
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor DougGregor merged commit 967a1d4 into swiftlang:swift-3.0-branch Aug 4, 2016
@jckarter
Copy link
Contributor

jckarter commented Aug 4, 2016

I'm a bit concerned about the cost of doing runtime lookup for a conformance. Building an Error is supposed to be cheap. Instead of a separate _BridgedStoredNSError protocol, can the method be a stdlib-internal requirement of Error with a default implementation?

@jckarter
Copy link
Contributor

jckarter commented Aug 4, 2016

I see, you do the lookup statically in SILGen. That won't work for a generic T: Error.

@DougGregor
Copy link
Member Author

@jckarter , that's why there are both paths... In SILGen, we statically check for _BridgedStoredNSError, and in that case we know we can go straight to NSError, so we do. Otherwise, if we have an archetype, we use the _getEmbeddedNSError() witness, which may return an NSError. It requires a branch (and fallback to the swift_allocError path), which is why we (statically) look for _BridgedStoredNSError first.

@DougGregor DougGregor deleted the swift-3-nserror-wrapping branch August 4, 2016 16:33
@DougGregor
Copy link
Member Author

So we never dynamically look for a _BridgedStoredNSError conformance. It's only a convenience for knowing statically that there is always an NSError representation we can get at cheaply . Error's _getEmbeddedNSError() witness is the customization point used dynamically to try to dig out the NSError from an arbitrary Error. We pay the cost of an indirect call to the witness + a branch on the optional result to try to get the NSError dynamically, but we're not paying for an additional conformance lookup.

@jckarter
Copy link
Contributor

jckarter commented Aug 4, 2016

I see, thanks for explaining. SGTM.

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.

2 participants