stdlib: Remove _isBridgedToObjectiveC from _ObjectiveCBridgeable.

All generic bridgeable types can bridge for all their instantiations now. Removing this ferrets out some now-unnecessary traps that check for unbridgeable parameter types.
This commit is contained in:
Joe Groff
2016-07-24 09:00:30 -07:00
parent 93d2d299fd
commit 32b50c624d
31 changed files with 33 additions and 297 deletions

View File

@@ -251,7 +251,6 @@ process start and the function returns.
should be implementable in the standard library now. should be implementable in the standard library now.
``` ```
0000000000003b60 T _swift_bridgeNonVerbatimFromObjectiveC
0000000000003c80 T _swift_bridgeNonVerbatimFromObjectiveCConditional 0000000000003c80 T _swift_bridgeNonVerbatimFromObjectiveCConditional
00000000000037e0 T _swift_bridgeNonVerbatimToObjectiveC 00000000000037e0 T _swift_bridgeNonVerbatimToObjectiveC
00000000000039c0 T _swift_getBridgedNonVerbatimObjectiveCType 00000000000039c0 T _swift_getBridgedNonVerbatimObjectiveCType

View File

@@ -262,10 +262,6 @@ public struct DispatchDataIterator : IteratorProtocol, Sequence {
} }
extension DispatchData { extension DispatchData {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> __DispatchData { public func _bridgeToObjectiveC() -> __DispatchData {
return unsafeBitCast(__wrapped, to: __DispatchData.self) return unsafeBitCast(__wrapped, to: __DispatchData.self)

View File

@@ -264,10 +264,6 @@ extension AffineTransform : ReferenceConvertible, Hashable, CustomStringConverti
} }
extension AffineTransform : _ObjectiveCBridgeable { extension AffineTransform : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type { public static func _getObjectiveCType() -> Any.Type {
return NSAffineTransform.self return NSAffineTransform.self
} }

View File

@@ -1082,10 +1082,6 @@ public struct Calendar : CustomStringConvertible, CustomDebugStringConvertible,
} }
extension Calendar : _ObjectiveCBridgeable { extension Calendar : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSCalendar { public func _bridgeToObjectiveC() -> NSCalendar {
return _handle._copiedReference() return _handle._copiedReference()

View File

@@ -439,10 +439,6 @@ public struct CharacterSet : ReferenceConvertible, Equatable, Hashable, SetAlgeb
// MARK: Objective-C Bridging // MARK: Objective-C Bridging
extension CharacterSet : _ObjectiveCBridgeable { extension CharacterSet : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type { public static func _getObjectiveCType() -> Any.Type {
return NSCharacterSet.self return NSCharacterSet.self
} }

View File

@@ -678,10 +678,6 @@ public struct Data : ReferenceConvertible, CustomStringConvertible, Equatable, H
/// Provides bridging functionality for struct Data to class NSData and vice-versa. /// Provides bridging functionality for struct Data to class NSData and vice-versa.
extension Data : _ObjectiveCBridgeable { extension Data : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSData { public func _bridgeToObjectiveC() -> NSData {
return unsafeBitCast(_wrapped, to: NSData.self) return unsafeBitCast(_wrapped, to: NSData.self)

View File

@@ -229,10 +229,6 @@ public struct Date : ReferenceConvertible, Comparable, Equatable, CustomStringCo
} }
extension Date : _ObjectiveCBridgeable { extension Date : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSDate { public func _bridgeToObjectiveC() -> NSDate {
return NSDate(timeIntervalSinceReferenceDate: _time) return NSDate(timeIntervalSinceReferenceDate: _time)

View File

@@ -292,10 +292,6 @@ public struct DateComponents : ReferenceConvertible, Hashable, Equatable, _Mutab
// MARK: - Bridging // MARK: - Bridging
extension DateComponents : _ObjectiveCBridgeable { extension DateComponents : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type { public static func _getObjectiveCType() -> Any.Type {
return NSDateComponents.self return NSDateComponents.self
} }

View File

@@ -183,10 +183,6 @@ public struct DateInterval : ReferenceConvertible, Comparable, Hashable {
@available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) @available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
extension DateInterval : _ObjectiveCBridgeable { extension DateInterval : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type { public static func _getObjectiveCType() -> Any.Type {
return NSDateInterval.self return NSDateInterval.self
} }

View File

@@ -436,10 +436,6 @@ extension Decimal : CustomStringConvertible {
} }
extension Decimal : _ObjectiveCBridgeable { extension Decimal : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSDecimalNumber { public func _bridgeToObjectiveC() -> NSDecimalNumber {
return NSDecimalNumber(decimal: self) return NSDecimalNumber(decimal: self)

View File

@@ -86,10 +86,6 @@ extension String {
} }
extension String : _ObjectiveCBridgeable { extension String : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSString { public func _bridgeToObjectiveC() -> NSString {
// This method should not do anything extra except calling into the // This method should not do anything extra except calling into the
@@ -132,10 +128,6 @@ extension String : _ObjectiveCBridgeable {
// back to a specific numeric type requires a cast. // back to a specific numeric type requires a cast.
// FIXME: Incomplete list of types. // FIXME: Incomplete list of types.
extension Int : _ObjectiveCBridgeable { extension Int : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public init(_ number: NSNumber) { public init(_ number: NSNumber) {
self = number.intValue self = number.intValue
} }
@@ -168,10 +160,6 @@ extension Int : _ObjectiveCBridgeable {
} }
extension UInt : _ObjectiveCBridgeable { extension UInt : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public init(_ number: NSNumber) { public init(_ number: NSNumber) {
self = number.uintValue self = number.uintValue
} }
@@ -204,10 +192,6 @@ extension UInt : _ObjectiveCBridgeable {
} }
extension Float : _ObjectiveCBridgeable { extension Float : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public init(_ number: NSNumber) { public init(_ number: NSNumber) {
self = number.floatValue self = number.floatValue
} }
@@ -240,10 +224,6 @@ extension Float : _ObjectiveCBridgeable {
} }
extension Double : _ObjectiveCBridgeable { extension Double : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public init(_ number: NSNumber) { public init(_ number: NSNumber) {
self = number.doubleValue self = number.doubleValue
} }
@@ -276,10 +256,6 @@ extension Double : _ObjectiveCBridgeable {
} }
extension Bool: _ObjectiveCBridgeable { extension Bool: _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public init(_ number: NSNumber) { public init(_ number: NSNumber) {
self = number.boolValue self = number.boolValue
} }
@@ -313,10 +289,6 @@ extension Bool: _ObjectiveCBridgeable {
// CGFloat bridging. // CGFloat bridging.
extension CGFloat : _ObjectiveCBridgeable { extension CGFloat : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public init(_ number: NSNumber) { public init(_ number: NSNumber) {
self.native = CGFloat.NativeType(number) self.native = CGFloat.NativeType(number)
} }
@@ -408,10 +380,6 @@ extension Array : _ObjectiveCBridgeable {
unsafeBitCast(_cocoaArray.copy() as AnyObject, to: _NSArrayCore.self)) unsafeBitCast(_cocoaArray.copy() as AnyObject, to: _NSArrayCore.self))
} }
public static func _isBridgedToObjectiveC() -> Bool {
return Swift._isBridgedToObjectiveC(Element.self)
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSArray { public func _bridgeToObjectiveC() -> NSArray {
return unsafeBitCast(self._buffer._asCocoaArray() as AnyObject, to: NSArray.self) return unsafeBitCast(self._buffer._asCocoaArray() as AnyObject, to: NSArray.self)
@@ -570,11 +538,6 @@ extension Dictionary : _ObjectiveCBridgeable {
return result != nil return result != nil
} }
public static func _isBridgedToObjectiveC() -> Bool {
return Swift._isBridgedToObjectiveC(Key.self) &&
Swift._isBridgedToObjectiveC(Value.self)
}
public static func _unconditionallyBridgeFromObjectiveC( public static func _unconditionallyBridgeFromObjectiveC(
_ d: NSDictionary? _ d: NSDictionary?
) -> Dictionary { ) -> Dictionary {
@@ -850,10 +813,6 @@ extension Set : _ObjectiveCBridgeable {
}) })
return builder.take() return builder.take()
} }
public static func _isBridgedToObjectiveC() -> Bool {
return Swift._isBridgedToObjectiveC(Element.self)
}
} }
extension NSDictionary : Sequence { extension NSDictionary : Sequence {

View File

@@ -209,10 +209,6 @@ public struct IndexPath : ReferenceConvertible, Equatable, Hashable, MutableColl
} }
extension IndexPath : _ObjectiveCBridgeable { extension IndexPath : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type { public static func _getObjectiveCType() -> Any.Type {
return NSIndexPath.self return NSIndexPath.self
} }

View File

@@ -852,10 +852,6 @@ private func _toNSRange(_ r : Range<IndexSet.Element>) -> NSRange {
} }
extension IndexSet : _ObjectiveCBridgeable { extension IndexSet : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type { public static func _getObjectiveCType() -> Any.Type {
return NSIndexSet.self return NSIndexSet.self
} }

View File

@@ -441,10 +441,6 @@ public struct Locale : CustomStringConvertible, CustomDebugStringConvertible, Ha
extension Locale : _ObjectiveCBridgeable { extension Locale : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSLocale { public func _bridgeToObjectiveC() -> NSLocale {
return _wrapped return _wrapped

View File

@@ -253,10 +253,6 @@ extension Measurement {
@available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *) @available(OSX 10.12, iOS 10.0, watchOS 3.0, tvOS 10.0, *)
extension Measurement : _ObjectiveCBridgeable { extension Measurement : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSMeasurement { public func _bridgeToObjectiveC() -> NSMeasurement {
return NSMeasurement(doubleValue: value, unit: unit) return NSMeasurement(doubleValue: value, unit: unit)

View File

@@ -11,10 +11,6 @@
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
extension NSRange : _ObjectiveCBridgeable { extension NSRange : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public func _bridgeToObjectiveC() -> NSValue { public func _bridgeToObjectiveC() -> NSValue {
return NSValue(range: self) return NSValue(range: self)
} }

View File

@@ -95,10 +95,6 @@ public struct Notification : ReferenceConvertible, Equatable, Hashable {
} }
extension Notification : _ObjectiveCBridgeable { extension Notification : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type { public static func _getObjectiveCType() -> Any.Type {
return NSNotification.self return NSNotification.self
} }

View File

@@ -87,10 +87,6 @@ public struct PersonNameComponents : ReferenceConvertible, Hashable, Equatable,
@available(OSX 10.11, iOS 9.0, *) @available(OSX 10.11, iOS 9.0, *)
extension PersonNameComponents : _ObjectiveCBridgeable { extension PersonNameComponents : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type { public static func _getObjectiveCType() -> Any.Type {
return NSPersonNameComponents.self return NSPersonNameComponents.self
} }

View File

@@ -230,10 +230,6 @@ public struct TimeZone : CustomStringConvertible, CustomDebugStringConvertible,
} }
extension TimeZone : _ObjectiveCBridgeable { extension TimeZone : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSTimeZone { public func _bridgeToObjectiveC() -> NSTimeZone {
// _wrapped is immutable // _wrapped is immutable

View File

@@ -1124,10 +1124,6 @@ public struct URL : ReferenceConvertible, CustomStringConvertible, Equatable {
} }
extension URL : _ObjectiveCBridgeable { extension URL : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSURL { public func _bridgeToObjectiveC() -> NSURL {
return _url return _url

View File

@@ -316,10 +316,6 @@ public struct URLComponents : ReferenceConvertible, Hashable, CustomStringConver
} }
extension URLComponents : _ObjectiveCBridgeable { extension URLComponents : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type { public static func _getObjectiveCType() -> Any.Type {
return NSURLComponents.self return NSURLComponents.self
} }
@@ -384,10 +380,6 @@ public struct URLQueryItem : ReferenceConvertible, Hashable, Equatable, CustomSt
@available(OSX 10.10, iOS 8.0, *) @available(OSX 10.10, iOS 8.0, *)
extension URLQueryItem : _ObjectiveCBridgeable { extension URLQueryItem : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type { public static func _getObjectiveCType() -> Any.Type {
return NSURLQueryItem.self return NSURLQueryItem.self
} }

View File

@@ -247,10 +247,6 @@ public struct URLRequest : ReferenceConvertible, CustomStringConvertible, Equata
} }
extension URLRequest : _ObjectiveCBridgeable { extension URLRequest : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public static func _getObjectiveCType() -> Any.Type { public static func _getObjectiveCType() -> Any.Type {
return NSURLRequest.self return NSURLRequest.self
} }

View File

@@ -107,10 +107,6 @@ public struct UUID : ReferenceConvertible, Hashable, Equatable, CustomStringConv
} }
extension UUID : _ObjectiveCBridgeable { extension UUID : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
@_semantics("convertToObjectiveC") @_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSUUID { public func _bridgeToObjectiveC() -> NSUUID {
return reference return reference

View File

@@ -112,13 +112,8 @@ extension _ArrayBuffer {
/// Convert to an NSArray. /// Convert to an NSArray.
/// ///
/// - Precondition: `_isBridgedToObjectiveC(Element.self)`. /// O(1) if the element type is bridged verbatim, O(N) otherwise.
/// O(1) if the element type is bridged verbatim, O(N) otherwise.
public func _asCocoaArray() -> _NSArrayCore { public func _asCocoaArray() -> _NSArrayCore {
_sanityCheck(
_isBridgedToObjectiveC(Element.self),
"Array element type is not bridged to Objective-C")
return _fastPath(_isNative) ? _native._asCocoaArray() : _nonNative return _fastPath(_isNative) ? _native._asCocoaArray() : _nonNative
} }

View File

@@ -19,14 +19,6 @@
public protocol _ObjectiveCBridgeable { public protocol _ObjectiveCBridgeable {
associatedtype _ObjectiveCType : AnyObject associatedtype _ObjectiveCType : AnyObject
/// Returns `true` iff instances of `Self` can be converted to
/// Objective-C. Even if this method returns `true`, a given
/// instance of `Self._ObjectiveCType` may, or may not, convert
/// successfully to `Self`; for example, an `NSArray` will only
/// convert successfully to `[String]` if it contains only
/// `NSString`s.
static func _isBridgedToObjectiveC() -> Bool
/// Convert `self` to Objective-C. /// Convert `self` to Objective-C.
func _bridgeToObjectiveC() -> _ObjectiveCType func _bridgeToObjectiveC() -> _ObjectiveCType
@@ -91,6 +83,13 @@ public protocol _ObjectiveCBridgeable {
-> Self -> Self
} }
// TODO: stub for unit testing purposes
extension _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
}
//===--- Bridging for metatypes -------------------------------------------===// //===--- Bridging for metatypes -------------------------------------------===//
/// A stand-in for a value of metatype type. /// A stand-in for a value of metatype type.
@@ -106,10 +105,6 @@ public struct _BridgeableMetatype: _ObjectiveCBridgeable {
public typealias _ObjectiveCType = AnyObject public typealias _ObjectiveCType = AnyObject
public static func _isBridgedToObjectiveC() -> Bool {
return true
}
public func _bridgeToObjectiveC() -> AnyObject { public func _bridgeToObjectiveC() -> AnyObject {
return value return value
} }
@@ -149,66 +144,19 @@ public struct _BridgeableMetatype: _ObjectiveCBridgeable {
// implementations. // implementations.
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// Attempt to convert `x` to its Objective-C representation.
///
/// - If `T` is a class type, it is always bridged verbatim, the function
/// returns `x`;
///
/// - otherwise, `T` conforms to `_ObjectiveCBridgeable`:
/// + if `T._isBridgedToObjectiveC()` returns `false`, then the
/// result is empty;
/// + otherwise, returns the result of `x._bridgeToObjectiveC()`;
///
/// - otherwise, the result is empty.
public func _bridgeToObjectiveC<T>(_ x: T) -> AnyObject? {
if _fastPath(_isClassOrObjCExistential(T.self)) {
return unsafeBitCast(x, to: AnyObject.self)
}
return _bridgeNonVerbatimToObjectiveC(x)
}
public func _bridgeToObjectiveCUnconditional<T>(_ x: T) -> AnyObject {
let optResult: AnyObject? = _bridgeToObjectiveC(x)
_precondition(optResult != nil,
"value failed to bridge from Swift type to a Objective-C type")
return optResult!
}
/// Same as `_bridgeToObjectiveCUnconditional`, but autoreleases the
/// return value if `T` is bridged non-verbatim.
func _bridgeToObjectiveCUnconditionalAutorelease<T>(_ x: T) -> AnyObject
{
if _fastPath(_isClassOrObjCExistential(T.self)) {
return unsafeBitCast(x, to: AnyObject.self)
}
guard let bridged = _bridgeNonVerbatimToObjectiveC(x) else {
_preconditionFailure(
"Dictionary key failed to bridge from Swift type to a Objective-C type")
}
_autorelease(bridged)
return bridged
}
@_silgen_name("_swift_bridgeNonVerbatimToObjectiveC")
func _bridgeNonVerbatimToObjectiveC<T>(_ x: T) -> AnyObject?
/// Bridge an arbitrary value to an Objective-C object. /// Bridge an arbitrary value to an Objective-C object.
/// ///
/// - If `T` is a class type, it is always bridged verbatim, the function /// - If `T` is a class type, it is always bridged verbatim, the function
/// returns `x`; /// returns `x`;
/// ///
/// - otherwise, `T` conforms to `_ObjectiveCBridgeable`: /// - otherwise, if `T` conforms to `_ObjectiveCBridgeable`,
/// + if `T._isBridgedToObjectiveC()` returns `false`, then /// returns the result of `x._bridgeToObjectiveC()`;
/// we fall back to boxing (below);
/// + otherwise, returns the result of `x._bridgeToObjectiveC()`;
/// ///
/// - otherwise, we use **boxing** to bring the value into Objective-C. /// - otherwise, we use **boxing** to bring the value into Objective-C.
/// The value is wrapped in an instance of a private Objective-C class /// The value is wrapped in an instance of a private Objective-C class
/// that is `id`-compatible and dynamically castable back to the type of /// that is `id`-compatible and dynamically castable back to the type of
/// the boxed value, but is otherwise opaque. /// the boxed value, but is otherwise opaque.
/// ///
/// TODO: This should subsume `_bridgeToObjectiveC` above.
/// COMPILER_INTRINSIC /// COMPILER_INTRINSIC
public func _bridgeAnythingToObjectiveC<T>(_ x: T) -> AnyObject { public func _bridgeAnythingToObjectiveC<T>(_ x: T) -> AnyObject {
if _fastPath(_isClassOrObjCExistential(T.self)) { if _fastPath(_isClassOrObjCExistential(T.self)) {
@@ -217,7 +165,6 @@ public func _bridgeAnythingToObjectiveC<T>(_ x: T) -> AnyObject {
return _bridgeAnythingNonVerbatimToObjectiveC(x) return _bridgeAnythingNonVerbatimToObjectiveC(x)
} }
// TODO: This should subsume `_bridgeNonVerbatimToObjectiveC` above.
/// COMPILER_INTRINSIC /// COMPILER_INTRINSIC
@_silgen_name("_swift_bridgeAnythingNonVerbatimToObjectiveC") @_silgen_name("_swift_bridgeAnythingNonVerbatimToObjectiveC")
public func _bridgeAnythingNonVerbatimToObjectiveC<T>(_ x: T) -> AnyObject public func _bridgeAnythingNonVerbatimToObjectiveC<T>(_ x: T) -> AnyObject
@@ -262,8 +209,6 @@ public func _forceBridgeFromObjectiveC_bridgeable<T:_ObjectiveCBridgeable> (
/// - if the dynamic type of `x` is `T` or a subclass of it, it is bridged /// - if the dynamic type of `x` is `T` or a subclass of it, it is bridged
/// verbatim, the function returns `x`; /// verbatim, the function returns `x`;
/// - otherwise, if `T` conforms to `_ObjectiveCBridgeable`: /// - otherwise, if `T` conforms to `_ObjectiveCBridgeable`:
/// + if `T._isBridgedToObjectiveC()` returns `false`, then the result is
/// empty;
/// + otherwise, if the dynamic type of `x` is not `T._ObjectiveCType` /// + otherwise, if the dynamic type of `x` is not `T._ObjectiveCType`
/// or a subclass of it, the result is empty; /// or a subclass of it, the result is empty;
/// + otherwise, returns the result of /// + otherwise, returns the result of
@@ -319,8 +264,7 @@ func _bridgeNonVerbatimFromObjectiveCConditional<T>(
/// representation. /// representation.
/// ///
/// - If `T` is a class type, returns `true`; /// - If `T` is a class type, returns `true`;
/// - otherwise, if `T` conforms to `_ObjectiveCBridgeable`, returns /// - otherwise, returns whether `T` conforms to `_ObjectiveCBridgeable`.
/// `T._isBridgedToObjectiveC()`.
public func _isBridgedToObjectiveC<T>(_: T.Type) -> Bool { public func _isBridgedToObjectiveC<T>(_: T.Type) -> Bool {
if _fastPath(_isClassOrObjCExistential(T.self)) { if _fastPath(_isClassOrObjCExistential(T.self)) {
return true return true

View File

@@ -145,7 +145,7 @@ final class _ContiguousArrayStorage<Element> : _ContiguousArrayStorage1 {
let resultPtr = result.baseAddress let resultPtr = result.baseAddress
let p = __manager._elementPointer let p = __manager._elementPointer
for i in 0..<count { for i in 0..<count {
(resultPtr + i).initialize(to: _bridgeToObjectiveCUnconditional(p[i])) (resultPtr + i).initialize(to: _bridgeAnythingToObjectiveC(p[i]))
} }
_fixLifetime(__manager) _fixLifetime(__manager)
return result return result
@@ -428,9 +428,6 @@ struct _ContiguousArrayBuffer<Element> : _ArrayBufferProtocol {
/// ///
/// - Complexity: O(1). /// - Complexity: O(1).
public func _asCocoaArray() -> _NSArrayCore { public func _asCocoaArray() -> _NSArrayCore {
_sanityCheck(
_isBridgedToObjectiveC(Element.self),
"Array element type is not bridged to Objective-C")
if count == 0 { if count == 0 {
return _emptyArrayStorage return _emptyArrayStorage
} }

View File

@@ -1184,7 +1184,7 @@ extension Set {
var i = lhsNative.startIndex var i = lhsNative.startIndex
while i != endIndex { while i != endIndex {
let key = lhsNative.assertingGet(i) let key = lhsNative.assertingGet(i)
let bridgedKey: AnyObject = _bridgeToObjectiveCUnconditional(key) let bridgedKey: AnyObject = _bridgeAnythingToObjectiveC(key)
let optRhsValue: AnyObject? = rhsCocoa.maybeGet(bridgedKey) let optRhsValue: AnyObject? = rhsCocoa.maybeGet(bridgedKey)
if let rhsValue = optRhsValue { if let rhsValue = optRhsValue {
if key == _forceBridgeFromObjectiveC(rhsValue, Element.self) { if key == _forceBridgeFromObjectiveC(rhsValue, Element.self) {
@@ -1301,10 +1301,8 @@ public func _setBridgeToObjectiveC<SwiftValue, ObjCValue>(
if valueBridgesDirectly { if valueBridgesDirectly {
bridgedMember = unsafeBitCast(member, to: ObjCValue.self) bridgedMember = unsafeBitCast(member, to: ObjCValue.self)
} else { } else {
let bridged: AnyObject? = _bridgeToObjectiveC(member) let bridged: AnyObject = _bridgeAnythingToObjectiveC(member)
_precondition(bridged != nil, bridgedMember = unsafeBitCast(bridged, to: ObjCValue.self)
"set member cannot be bridged to Objective-C")
bridgedMember = unsafeBitCast(bridged!, to: ObjCValue.self)
} }
result.insert(bridgedMember) result.insert(bridgedMember)
} }
@@ -2109,7 +2107,7 @@ extension Dictionary where Key : Equatable, Value : Equatable {
while index != endIndex { while index != endIndex {
let (key, value) = lhsNative.assertingGet(index) let (key, value) = lhsNative.assertingGet(index)
let optRhsValue: AnyObject? = let optRhsValue: AnyObject? =
rhsCocoa.maybeGet(_bridgeToObjectiveCUnconditional(key)) rhsCocoa.maybeGet(_bridgeAnythingToObjectiveC(key))
// TODO: swift-3-indexing-model: change 'if' into 'guard'. // TODO: swift-3-indexing-model: change 'if' into 'guard'.
if let rhsValue = optRhsValue { if let rhsValue = optRhsValue {
if value == _forceBridgeFromObjectiveC(rhsValue, Value.self) { if value == _forceBridgeFromObjectiveC(rhsValue, Value.self) {
@@ -2245,9 +2243,8 @@ public func _dictionaryBridgeToObjectiveC<
if keyBridgesDirectly { if keyBridgesDirectly {
bridgedKey = unsafeBitCast(key, to: ObjCKey.self) bridgedKey = unsafeBitCast(key, to: ObjCKey.self)
} else { } else {
let bridged: AnyObject? = _bridgeToObjectiveC(key) let bridged: AnyObject = _bridgeAnythingToObjectiveC(key)
_precondition(bridged != nil, "dictionary key cannot be bridged to Objective-C") bridgedKey = unsafeBitCast(bridged, to: ObjCKey.self)
bridgedKey = unsafeBitCast(bridged!, to: ObjCKey.self)
} }
// Bridge the value // Bridge the value
@@ -2255,10 +2252,8 @@ public func _dictionaryBridgeToObjectiveC<
if valueBridgesDirectly { if valueBridgesDirectly {
bridgedValue = unsafeBitCast(value, to: ObjCValue.self) bridgedValue = unsafeBitCast(value, to: ObjCValue.self)
} else { } else {
let bridged: AnyObject? = _bridgeToObjectiveC(value) let bridged: AnyObject? = _bridgeAnythingToObjectiveC(value)
_precondition(bridged != nil, bridgedValue = unsafeBitCast(bridged, to: ObjCValue.self)
"dictionary value cannot be bridged to Objective-C")
bridgedValue = unsafeBitCast(bridged!, to: ObjCValue.self)
} }
result[bridgedKey] = bridgedValue result[bridgedKey] = bridgedValue
@@ -3410,11 +3405,11 @@ final internal class _Native${Self}StorageOwner<${TypeParametersDecl}>
// Bridge everything. // Bridge everything.
for i in 0..<nativeStorage.capacity { for i in 0..<nativeStorage.capacity {
if nativeStorage.isInitializedEntry(at: i) { if nativeStorage.isInitializedEntry(at: i) {
let key = _bridgeToObjectiveCUnconditional(nativeStorage.key(at: i)) let key = _bridgeAnythingToObjectiveC(nativeStorage.key(at: i))
%if Self == 'Set': %if Self == 'Set':
bridged.initializeKey(key, at: i) bridged.initializeKey(key, at: i)
%elif Self == 'Dictionary': %elif Self == 'Dictionary':
let val = _bridgeToObjectiveCUnconditional(nativeStorage.value(at: i)) let val = _bridgeAnythingToObjectiveC(nativeStorage.value(at: i))
bridged.initializeKey(key, value: val, at: i) bridged.initializeKey(key, value: val, at: i)
%end %end
} }
@@ -3433,9 +3428,9 @@ final internal class _Native${Self}StorageOwner<${TypeParametersDecl}>
AnyObject { AnyObject {
if _fastPath(_isClassOrObjCExistential(Key.self)) { if _fastPath(_isClassOrObjCExistential(Key.self)) {
%if Self == 'Set': %if Self == 'Set':
return _bridgeToObjectiveCUnconditional(nativeStorage.assertingGet(i)) return _bridgeAnythingToObjectiveC(nativeStorage.assertingGet(i))
%elif Self == 'Dictionary': %elif Self == 'Dictionary':
return _bridgeToObjectiveCUnconditional(nativeStorage.assertingGet(i).0) return _bridgeAnythingToObjectiveC(nativeStorage.assertingGet(i).0)
%end %end
} }
bridgeEverything() bridgeEverything()
@@ -3451,7 +3446,7 @@ final internal class _Native${Self}StorageOwner<${TypeParametersDecl}>
internal func _getBridgedValue(_ i: _Native${Self}Index<${TypeParameters}>) -> internal func _getBridgedValue(_ i: _Native${Self}Index<${TypeParameters}>) ->
AnyObject { AnyObject {
if _fastPath(_isClassOrObjCExistential(Value.self)) { if _fastPath(_isClassOrObjCExistential(Value.self)) {
return _bridgeToObjectiveCUnconditional(nativeStorage.assertingGet(i)) return _bridgeAnythingToObjectiveC(nativeStorage.assertingGet(i))
} }
bridgeEverything() bridgeEverything()
return bridgedNativeStorage.assertingGet(i.offset) return bridgedNativeStorage.assertingGet(i.offset)
@@ -3462,7 +3457,7 @@ final internal class _Native${Self}StorageOwner<${TypeParametersDecl}>
internal func _getBridgedValue(_ i: _Native${Self}Index<${TypeParameters}>) internal func _getBridgedValue(_ i: _Native${Self}Index<${TypeParameters}>)
-> AnyObject { -> AnyObject {
if _fastPath(_isClassOrObjCExistential(Value.self)) { if _fastPath(_isClassOrObjCExistential(Value.self)) {
return _bridgeToObjectiveCUnconditional(nativeStorage.assertingGet(i).1) return _bridgeAnythingToObjectiveC(nativeStorage.assertingGet(i).1)
} }
bridgeEverything() bridgeEverything()
return bridgedNativeStorage.assertingGet(i.offset).1 return bridgedNativeStorage.assertingGet(i.offset).1
@@ -3952,7 +3947,7 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorage {
return nil return nil
case .cocoa(let cocoaStorage): case .cocoa(let cocoaStorage):
#if _runtime(_ObjC) #if _runtime(_ObjC)
let anyObjectKey: AnyObject = _bridgeToObjectiveCUnconditional(key) let anyObjectKey: AnyObject = _bridgeAnythingToObjectiveC(key)
if let cocoaIndex = cocoaStorage.index(forKey: anyObjectKey) { if let cocoaIndex = cocoaStorage.index(forKey: anyObjectKey) {
return ._cocoa(cocoaIndex) return ._cocoa(cocoaIndex)
} }
@@ -4001,7 +3996,7 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorage {
case .cocoa(let cocoaStorage): case .cocoa(let cocoaStorage):
#if _runtime(_ObjC) #if _runtime(_ObjC)
// FIXME: This assumes that Key and Value are bridged verbatim. // FIXME: This assumes that Key and Value are bridged verbatim.
let anyObjectKey: AnyObject = _bridgeToObjectiveCUnconditional(key) let anyObjectKey: AnyObject = _bridgeAnythingToObjectiveC(key)
let anyObjectValue: AnyObject = cocoaStorage.assertingGet(anyObjectKey) let anyObjectValue: AnyObject = cocoaStorage.assertingGet(anyObjectKey)
return _forceBridgeFromObjectiveC(anyObjectValue, Value.self) return _forceBridgeFromObjectiveC(anyObjectValue, Value.self)
#else #else
@@ -4016,7 +4011,7 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorage {
internal static func maybeGetFromCocoaStorage( internal static func maybeGetFromCocoaStorage(
_ cocoaStorage : CocoaStorage, forKey key: Key _ cocoaStorage : CocoaStorage, forKey key: Key
) -> Value? { ) -> Value? {
let anyObjectKey: AnyObject = _bridgeToObjectiveCUnconditional(key) let anyObjectKey: AnyObject = _bridgeAnythingToObjectiveC(key)
if let anyObjectValue = cocoaStorage.maybeGet(anyObjectKey) { if let anyObjectValue = cocoaStorage.maybeGet(anyObjectKey) {
return _forceBridgeFromObjectiveC(anyObjectValue, Value.self) return _forceBridgeFromObjectiveC(anyObjectValue, Value.self)
} }
@@ -4322,7 +4317,7 @@ internal enum _Variant${Self}Storage<${TypeParametersDecl}> : _HashStorage {
return nativeRemoveObject(forKey: key) return nativeRemoveObject(forKey: key)
case .cocoa(let cocoaStorage): case .cocoa(let cocoaStorage):
#if _runtime(_ObjC) #if _runtime(_ObjC)
let anyObjectKey: AnyObject = _bridgeToObjectiveCUnconditional(key) let anyObjectKey: AnyObject = _bridgeAnythingToObjectiveC(key)
if cocoaStorage.maybeGet(anyObjectKey) == nil { if cocoaStorage.maybeGet(anyObjectKey) == nil {
return nil return nil
} }
@@ -5033,13 +5028,6 @@ extension ${Self} {
public func _bridgeToObjectiveCImpl() -> _NS${Self}Core { public func _bridgeToObjectiveCImpl() -> _NS${Self}Core {
switch _variantStorage { switch _variantStorage {
case _Variant${Self}Storage.native(let nativeOwner): case _Variant${Self}Storage.native(let nativeOwner):
%if Self == 'Set':
_precondition(_isBridgedToObjectiveC(Element.self),
"Key is not bridged to Objective-C")
%elif Self == 'Dictionary':
_precondition(_isBridgedToObjectiveC(Value.self),
"Value is not bridged to Objective-C")
%end
return nativeOwner as _Native${Self}StorageOwner<${TypeParameters}> return nativeOwner as _Native${Self}StorageOwner<${TypeParameters}>
case _Variant${Self}Storage.cocoa(let cocoaStorage): case _Variant${Self}Storage.cocoa(let cocoaStorage):

View File

@@ -71,7 +71,7 @@ extension ImplicitlyUnwrappedOptional : _ObjectiveCBridgeable {
_preconditionFailure("attempt to bridge an implicitly unwrapped optional containing nil") _preconditionFailure("attempt to bridge an implicitly unwrapped optional containing nil")
case .some(let x): case .some(let x):
return Swift._bridgeToObjectiveC(x)! return Swift._bridgeAnythingToObjectiveC(x)
} }
} }

View File

@@ -30,10 +30,6 @@ public func ${op} <T: _SwiftNewtypeWrapper>(lhs: T, rhs: T) -> Bool
#if _runtime(_ObjC) #if _runtime(_ObjC)
extension _SwiftNewtypeWrapper where Self.RawValue : _ObjectiveCBridgeable { extension _SwiftNewtypeWrapper where Self.RawValue : _ObjectiveCBridgeable {
public static func _isBridgedToObjectiveC() -> Bool {
return Self.RawValue._isBridgedToObjectiveC()
}
public func _bridgeToObjectiveC() -> Self.RawValue._ObjectiveCType { public func _bridgeToObjectiveC() -> Self.RawValue._ObjectiveCType {
return rawValue._bridgeToObjectiveC() return rawValue._bridgeToObjectiveC()
} }

View File

@@ -510,7 +510,7 @@ public func _swift_ClassMirror_quickLookObject(_: _MagicMirrorData) -> AnyObject
func _swift_isKind(_ object: AnyObject, of: AnyObject) -> Bool func _swift_isKind(_ object: AnyObject, of: AnyObject) -> Bool
func _isKind(_ object: AnyObject, of: String) -> Bool { func _isKind(_ object: AnyObject, of: String) -> Bool {
return _swift_isKind(object, of: _bridgeToObjectiveC(of)!) return _swift_isKind(object, of: _bridgeAnythingToObjectiveC(of))
} }
func _getClassPlaygroundQuickLook(_ object: AnyObject) -> PlaygroundQuickLook? { func _getClassPlaygroundQuickLook(_ object: AnyObject) -> PlaygroundQuickLook? {

View File

@@ -2516,11 +2516,6 @@ struct _ObjectiveCBridgeableWitnessTable {
const Metadata *parentMetadata, const Metadata *parentMetadata,
const _ObjectiveCBridgeableWitnessTable *witnessTable); const _ObjectiveCBridgeableWitnessTable *witnessTable);
// class func _isBridgedToObjectiveC() -> bool
bool (*isBridgedToObjectiveC)(
const Metadata *value, const Metadata *T,
const _ObjectiveCBridgeableWitnessTable *witnessTable);
// func _bridgeToObjectiveC() -> _ObjectiveCType // func _bridgeToObjectiveC() -> _ObjectiveCType
HeapObject *(*bridgeToObjectiveC)( HeapObject *(*bridgeToObjectiveC)(
OpaqueValue *self, const Metadata *Self, OpaqueValue *self, const Metadata *Self,
@@ -2561,12 +2556,6 @@ static bool _dynamicCastValueToClassViaObjCBridgeable(
const Metadata *targetType, const Metadata *targetType,
const _ObjectiveCBridgeableWitnessTable *srcBridgeWitness, const _ObjectiveCBridgeableWitnessTable *srcBridgeWitness,
DynamicCastFlags flags) { DynamicCastFlags flags) {
// Check whether the source is bridged to Objective-C.
if (!srcBridgeWitness->isBridgedToObjectiveC(srcType, srcType,
srcBridgeWitness)) {
return _fail(src, srcType, targetType, flags);
}
// Bridge the source value to an object. // Bridge the source value to an object.
auto srcBridgedObject = auto srcBridgedObject =
srcBridgeWitness->bridgeToObjectiveC(src, srcType, srcBridgeWitness); srcBridgeWitness->bridgeToObjectiveC(src, srcType, srcBridgeWitness);
@@ -2604,12 +2593,6 @@ static bool _dynamicCastValueToClassExistentialViaObjCBridgeable(
const ExistentialTypeMetadata *targetType, const ExistentialTypeMetadata *targetType,
const _ObjectiveCBridgeableWitnessTable *srcBridgeWitness, const _ObjectiveCBridgeableWitnessTable *srcBridgeWitness,
DynamicCastFlags flags) { DynamicCastFlags flags) {
// Check whether the source is bridged to Objective-C.
if (!srcBridgeWitness->isBridgedToObjectiveC(srcType, srcType,
srcBridgeWitness)) {
return _fail(src, srcType, targetType, flags);
}
// Bridge the source value to an object. // Bridge the source value to an object.
auto srcBridgedObject = auto srcBridgedObject =
srcBridgeWitness->bridgeToObjectiveC(src, srcType, srcBridgeWitness); srcBridgeWitness->bridgeToObjectiveC(src, srcType, srcBridgeWitness);
@@ -2646,12 +2629,6 @@ static bool _dynamicCastClassToValueViaObjCBridgeable(
const Metadata *targetType, const Metadata *targetType,
const _ObjectiveCBridgeableWitnessTable *targetBridgeWitness, const _ObjectiveCBridgeableWitnessTable *targetBridgeWitness,
DynamicCastFlags flags) { DynamicCastFlags flags) {
// Check whether the target is bridged to Objective-C.
if (!targetBridgeWitness->isBridgedToObjectiveC(targetType, targetType,
targetBridgeWitness)) {
return _fail(src, srcType, targetType, flags);
}
// Determine the class type to which the target value type is bridged. // Determine the class type to which the target value type is bridged.
auto targetBridgedClass = auto targetBridgedClass =
targetBridgeWitness->ObjectiveCType(targetType, targetBridgeWitness); targetBridgeWitness->ObjectiveCType(targetType, targetBridgeWitness);
@@ -2772,12 +2749,6 @@ static id bridgeAnythingNonVerbatimToObjectiveC(OpaqueValue *src,
} }
if (auto srcBridgeWitness = findBridgeWitness(srcType)) { if (auto srcBridgeWitness = findBridgeWitness(srcType)) {
// Check whether the source is bridged to Objective-C.
if (!srcBridgeWitness->isBridgedToObjectiveC(srcType, srcType,
srcBridgeWitness)) {
return nil;
}
// Bridge the source value to an object. // Bridge the source value to an object.
auto srcBridgedObject = auto srcBridgedObject =
srcBridgeWitness->bridgeToObjectiveC(src, srcType, srcBridgeWitness); srcBridgeWitness->bridgeToObjectiveC(src, srcType, srcBridgeWitness);
@@ -2850,30 +2821,6 @@ findBridgeWitness(const Metadata *T) {
return nullptr; return nullptr;
} }
/// \param value passed at +1, consumed.
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
extern "C" HeapObject *_swift_bridgeNonVerbatimToObjectiveC(
OpaqueValue *value, const Metadata *T
) {
assert(!swift_isClassOrObjCExistentialTypeImpl(T));
if (const auto *bridgeWitness = findBridgeWitness(T)) {
if (!bridgeWitness->isBridgedToObjectiveC(T, T, bridgeWitness)) {
// Witnesses take 'self' at +0, so we still need to consume the +1 argument.
T->vw_destroy(value);
return nullptr;
}
auto result = bridgeWitness->bridgeToObjectiveC(value, T, bridgeWitness);
// Witnesses take 'self' at +0, so we still need to consume the +1 argument.
T->vw_destroy(value);
return result;
}
// Consume the +1 argument.
T->vw_destroy(value);
return nullptr;
}
SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE SWIFT_CC(swift) SWIFT_RUNTIME_STDLIB_INTERFACE
extern "C" const Metadata *_swift_getBridgedNonVerbatimObjectiveCType( extern "C" const Metadata *_swift_getBridgedNonVerbatimObjectiveCType(
const Metadata *value, const Metadata *T const Metadata *value, const Metadata *T
@@ -2987,8 +2934,7 @@ extern "C" bool _swift_isBridgedNonVerbatimToObjectiveC(
assert(!swift_isClassOrObjCExistentialTypeImpl(T)); assert(!swift_isClassOrObjCExistentialTypeImpl(T));
auto bridgeWitness = findBridgeWitness(T); auto bridgeWitness = findBridgeWitness(T);
return bridgeWitness && bridgeWitness->isBridgedToObjectiveC(value, T, return (bool)bridgeWitness;
bridgeWitness);
} }
#endif #endif