-
Notifications
You must be signed in to change notification settings - Fork 10.5k
SR-0140: Bridge Optionals to nonnull ObjC objects by bridging their payload, or using a sentinel. #4782
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
Conversation
@swift-ci Please smoke test |
@rjmccall, do you mind reviewing this for 3.0.1? |
…ayload, or using a sentinel. id-as-Any lets you pass Optional to an ObjC API that takes `nonnull id`, and also lets you bridge containers of `Optional` to `NSArray` etc. When this occurs, we can unwrap the value and bridge it so that inhabited optionals still pass into ObjC in the expected way, but we need something to represent `none` other than the `nil` pointer. Cocoa provides `NSNull` as the canonical "null for containers" object, which is the least bad of many possible answers. If we happen to have the rare nested optional `T??`, there is no precedented analog for these in Cocoa, so just generate a unique sentinel object to preserve the `nil`-ness depth so we at least don't lose information round-tripping across the ObjC-Swift bridge. Making Optional conform to _ObjectiveCBridgeable is more or less enough to make this all work, though there are a few additional edge case things that need to be fixed up. We don't want to accept `AnyObject??` as an @objc-compatible type, so special-case Optional in `getForeignRepresentable`. Implements SR-0140 (rdar://problem/27905315).
d321e8b
to
cfa9cd9
Compare
@swift-ci Please smoke test |
@swift-ci Please smoke test Linux |
Test stdlib/OptionalBridge.swift is failing on i386 iOS simulator. https://ci.swift.org/job/swift-PR-osx/3631/
|
@DougGregor or @rudkx, any idea where that error would come from? Both |
@jckarter Pretty sure what's happening here is that the only isEqual(to:) in an NSObject-derived class takes an NSHashTable argument. If you remove the argument label it should find NSObjectProtocol's isEqual(). |
Right, |
Thanks. I'll try that. |
id-as-Any lets you pass Optional to an ObjC API that takes
nonnull id
, and also lets you bridge containers ofOptional
toNSArray
etc. When this occurs, we can unwrap the value and bridge it so that inhabited optionals still pass into ObjC in the expected way, but we need something to representnone
other than thenil
pointer. Cocoa providesNSNull
as the canonical "null for containers" object, which is the least bad of many possible answers. If we happen to have the rare nested optionalT??
, there is no precedented analog for these in Cocoa, so just generate a unique sentinel object to preserve thenil
-ness depth so we at least don't lose information round-tripping across the ObjC-Swift bridge.Making Optional conform to _ObjectiveCBridgeable is more or less enough to make this all work, though there are a few additional edge case things that need to be fixed up. We don't want to accept
AnyObject??
as an @objc-compatible type, so special-case Optional ingetForeignRepresentable
.Implements SR-0140 (rdar://problem/27905315).