Skip to content

Commit e12e730

Browse files
authored
Merge pull request #4454 from DougGregor/remove-bridging-entrypoints
[Set/Dictionary] Eliminate "bridging" collection entrypoints.
2 parents f5f6905 + dc0ae67 commit e12e730

File tree

6 files changed

+32
-286
lines changed

6 files changed

+32
-286
lines changed

stdlib/public/SDK/Foundation/Foundation.swift

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -638,13 +638,7 @@ extension Dictionary : _ObjectiveCBridgeable {
638638
result: inout Dictionary?
639639
) -> Bool {
640640
let anyDict = x as [NSObject : AnyObject]
641-
if _isBridgedVerbatimToObjectiveC(Key.self) &&
642-
_isBridgedVerbatimToObjectiveC(Value.self) {
643-
result = Swift._dictionaryDownCastConditional(anyDict)
644-
return result != nil
645-
}
646-
647-
result = Swift._dictionaryBridgeFromObjectiveCConditional(anyDict)
641+
result = Swift._dictionaryDownCastConditional(anyDict)
648642
return result != nil
649643
}
650644

@@ -888,12 +882,7 @@ extension Set : _ObjectiveCBridgeable {
888882
_ x: NSSet, result: inout Set?
889883
) -> Bool {
890884
let anySet = x as Set<NSObject>
891-
if _isBridgedVerbatimToObjectiveC(Element.self) {
892-
result = Swift._setDownCastConditional(anySet)
893-
return result != nil
894-
}
895-
896-
result = Swift._setBridgeFromObjectiveCConditional(anySet)
885+
result = Swift._setDownCastConditional(anySet)
897886
return result != nil
898887
}
899888

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 0 additions & 235 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,40 +1291,6 @@ public func _setUpCast<DerivedValue, BaseValue>(_ source: Set<DerivedValue>)
12911291
return builder.take()
12921292
}
12931293

1294-
#if _runtime(_ObjC)
1295-
1296-
/// Implements an unconditional upcast that involves bridging.
1297-
///
1298-
/// The cast can fail if bridging fails.
1299-
///
1300-
/// - Precondition: `SwiftValue` is bridged to Objective-C
1301-
/// and requires non-trivial bridging.
1302-
public func _setBridgeToObjectiveC<SwiftValue, ObjCValue>(
1303-
_ source: Set<SwiftValue>
1304-
) -> Set<ObjCValue> {
1305-
_sanityCheck(_isClassOrObjCExistential(ObjCValue.self))
1306-
_sanityCheck(!_isBridgedVerbatimToObjectiveC(SwiftValue.self))
1307-
1308-
var result = Set<ObjCValue>(minimumCapacity: source.count)
1309-
let valueBridgesDirectly =
1310-
_isBridgedVerbatimToObjectiveC(SwiftValue.self) ==
1311-
_isBridgedVerbatimToObjectiveC(ObjCValue.self)
1312-
1313-
for member in source {
1314-
var bridgedMember: ObjCValue
1315-
if valueBridgesDirectly {
1316-
bridgedMember = unsafeBitCast(member, to: ObjCValue.self)
1317-
} else {
1318-
let bridged: AnyObject = _bridgeAnythingToObjectiveC(member)
1319-
bridgedMember = unsafeBitCast(bridged, to: ObjCValue.self)
1320-
}
1321-
result.insert(bridgedMember)
1322-
}
1323-
return result
1324-
}
1325-
1326-
#endif
1327-
13281294
@_silgen_name("_swift_setDownCastIndirect")
13291295
public func _setDownCastIndirect<SourceValue, TargetValue>(
13301296
_ source: UnsafePointer<Set<SourceValue>>,
@@ -1389,64 +1355,6 @@ public func _setDownCastConditional<BaseValue, DerivedValue>(
13891355
})
13901356
}
13911357

1392-
#if _runtime(_ObjC)
1393-
1394-
/// Implements an unconditional downcast that involves bridging.
1395-
///
1396-
/// - Precondition: At least one of `SwiftValue` is a bridged value
1397-
/// type, and the corresponding `ObjCValue` is a reference type.
1398-
public func _setBridgeFromObjectiveC<ObjCValue, SwiftValue>(
1399-
_ source: Set<ObjCValue>
1400-
) -> Set<SwiftValue> {
1401-
let result: Set<SwiftValue>? = _setBridgeFromObjectiveCConditional(source)
1402-
_precondition(result != nil, "This set cannot be bridged from Objective-C")
1403-
return result!
1404-
}
1405-
1406-
/// Implements a conditional downcast that involves bridging.
1407-
///
1408-
/// If the cast fails, the function returns `nil`. All checks should be
1409-
/// performed eagerly.
1410-
///
1411-
/// - Precondition: At least one of `SwiftValue` is a bridged value
1412-
/// type, and the corresponding `ObjCValue` is a reference type.
1413-
public func _setBridgeFromObjectiveCConditional<
1414-
ObjCValue, SwiftValue
1415-
>(
1416-
_ source: Set<ObjCValue>
1417-
) -> Set<SwiftValue>? {
1418-
_sanityCheck(_isClassOrObjCExistential(ObjCValue.self))
1419-
_sanityCheck(!_isBridgedVerbatimToObjectiveC(SwiftValue.self))
1420-
1421-
let valueBridgesDirectly =
1422-
_isBridgedVerbatimToObjectiveC(SwiftValue.self) ==
1423-
_isBridgedVerbatimToObjectiveC(ObjCValue.self)
1424-
1425-
var result = Set<SwiftValue>(minimumCapacity: source.count)
1426-
for value in source {
1427-
// Downcast the value.
1428-
var resultValue: SwiftValue
1429-
if valueBridgesDirectly {
1430-
if let bridgedValue = value as? SwiftValue {
1431-
resultValue = bridgedValue
1432-
} else {
1433-
return nil
1434-
}
1435-
} else {
1436-
if let bridgedValue = _conditionallyBridgeFromObjectiveC(
1437-
_reinterpretCastToAnyObject(value), SwiftValue.self) {
1438-
resultValue = bridgedValue
1439-
} else {
1440-
return nil
1441-
}
1442-
}
1443-
result.insert(resultValue)
1444-
}
1445-
return result
1446-
}
1447-
1448-
#endif
1449-
14501358
//===--- APIs unique to Dictionary<Key, Value> ----------------------------===//
14511359

14521360
/// A collection whose elements are key-value pairs.
@@ -2216,66 +2124,6 @@ public func _dictionaryUpCast<DerivedKey, DerivedValue, BaseKey, BaseValue>(
22162124
return result
22172125
}
22182126

2219-
#if _runtime(_ObjC)
2220-
2221-
/// Implements an unconditional upcast that involves bridging.
2222-
///
2223-
/// The cast can fail if bridging fails.
2224-
///
2225-
/// - Precondition: `SwiftKey` and `SwiftValue` are bridged to Objective-C,
2226-
/// and at least one of them requires non-trivial bridging.
2227-
@inline(never)
2228-
@_semantics("stdlib_binary_only")
2229-
public func _dictionaryBridgeToObjectiveC<
2230-
SwiftKey, SwiftValue, ObjCKey, ObjCValue
2231-
>(
2232-
_ source: Dictionary<SwiftKey, SwiftValue>
2233-
) -> Dictionary<ObjCKey, ObjCValue> {
2234-
2235-
// Note: We force this function to stay in the swift dylib because
2236-
// it is not performance sensitive and keeping it in the dylib saves
2237-
// a new kilobytes for each specialization for all users of dictionary.
2238-
2239-
_sanityCheck(
2240-
!_isBridgedVerbatimToObjectiveC(SwiftKey.self) ||
2241-
!_isBridgedVerbatimToObjectiveC(SwiftValue.self))
2242-
_sanityCheck(
2243-
_isClassOrObjCExistential(ObjCKey.self) ||
2244-
_isClassOrObjCExistential(ObjCValue.self))
2245-
2246-
var result = Dictionary<ObjCKey, ObjCValue>(minimumCapacity: source.count)
2247-
let keyBridgesDirectly =
2248-
_isBridgedVerbatimToObjectiveC(SwiftKey.self) ==
2249-
_isBridgedVerbatimToObjectiveC(ObjCKey.self)
2250-
let valueBridgesDirectly =
2251-
_isBridgedVerbatimToObjectiveC(SwiftValue.self) ==
2252-
_isBridgedVerbatimToObjectiveC(ObjCValue.self)
2253-
for (key, value) in source {
2254-
// Bridge the key
2255-
var bridgedKey: ObjCKey
2256-
if keyBridgesDirectly {
2257-
bridgedKey = unsafeBitCast(key, to: ObjCKey.self)
2258-
} else {
2259-
let bridged: AnyObject = _bridgeAnythingToObjectiveC(key)
2260-
bridgedKey = unsafeBitCast(bridged, to: ObjCKey.self)
2261-
}
2262-
2263-
// Bridge the value
2264-
var bridgedValue: ObjCValue
2265-
if valueBridgesDirectly {
2266-
bridgedValue = unsafeBitCast(value, to: ObjCValue.self)
2267-
} else {
2268-
let bridged: AnyObject? = _bridgeAnythingToObjectiveC(value)
2269-
bridgedValue = unsafeBitCast(bridged, to: ObjCValue.self)
2270-
}
2271-
2272-
result[bridgedKey] = bridgedValue
2273-
}
2274-
2275-
return result
2276-
}
2277-
#endif
2278-
22792127
@_silgen_name("_swift_dictionaryDownCastIndirect")
22802128
public func _dictionaryDownCastIndirect<SourceKey, SourceValue,
22812129
TargetKey, TargetValue>(
@@ -2364,89 +2212,6 @@ public func _dictionaryDownCastConditional<
23642212
return result
23652213
}
23662214

2367-
#if _runtime(_ObjC)
2368-
/// Implements an unconditional downcast that involves bridging.
2369-
///
2370-
/// - Precondition: At least one of `SwiftKey` or `SwiftValue` is a bridged value
2371-
/// type, and the corresponding `ObjCKey` or `ObjCValue` is a reference type.
2372-
public func _dictionaryBridgeFromObjectiveC<
2373-
ObjCKey, ObjCValue, SwiftKey, SwiftValue
2374-
>(
2375-
_ source: Dictionary<ObjCKey, ObjCValue>
2376-
) -> Dictionary<SwiftKey, SwiftValue> {
2377-
let result: Dictionary<SwiftKey, SwiftValue>? =
2378-
_dictionaryBridgeFromObjectiveCConditional(source)
2379-
_precondition(result != nil, "dictionary cannot be bridged from Objective-C")
2380-
return result!
2381-
}
2382-
2383-
/// Implements a conditional downcast that involves bridging.
2384-
///
2385-
/// If the cast fails, the function returns `nil`. All checks should be
2386-
/// performed eagerly.
2387-
///
2388-
/// - Precondition: At least one of `SwiftKey` or `SwiftValue` is a bridged value
2389-
/// type, and the corresponding `ObjCKey` or `ObjCValue` is a reference type.
2390-
public func _dictionaryBridgeFromObjectiveCConditional<
2391-
ObjCKey, ObjCValue, SwiftKey, SwiftValue
2392-
>(
2393-
_ source: Dictionary<ObjCKey, ObjCValue>
2394-
) -> Dictionary<SwiftKey, SwiftValue>? {
2395-
_sanityCheck(
2396-
_isClassOrObjCExistential(ObjCKey.self) ||
2397-
_isClassOrObjCExistential(ObjCValue.self))
2398-
_sanityCheck(
2399-
!_isBridgedVerbatimToObjectiveC(SwiftKey.self) ||
2400-
!_isBridgedVerbatimToObjectiveC(SwiftValue.self))
2401-
2402-
let keyBridgesDirectly =
2403-
_isBridgedVerbatimToObjectiveC(SwiftKey.self) ==
2404-
_isBridgedVerbatimToObjectiveC(ObjCKey.self)
2405-
let valueBridgesDirectly =
2406-
_isBridgedVerbatimToObjectiveC(SwiftValue.self) ==
2407-
_isBridgedVerbatimToObjectiveC(ObjCValue.self)
2408-
2409-
var result = Dictionary<SwiftKey, SwiftValue>(minimumCapacity: source.count)
2410-
for (key, value) in source {
2411-
// Downcast the key.
2412-
var resultKey: SwiftKey
2413-
if keyBridgesDirectly {
2414-
if let bridgedKey = key as? SwiftKey {
2415-
resultKey = bridgedKey
2416-
} else {
2417-
return nil
2418-
}
2419-
} else {
2420-
if let bridgedKey = _conditionallyBridgeFromObjectiveC(
2421-
_reinterpretCastToAnyObject(key), SwiftKey.self) {
2422-
resultKey = bridgedKey
2423-
} else {
2424-
return nil
2425-
}
2426-
}
2427-
2428-
// Downcast the value.
2429-
var resultValue: SwiftValue
2430-
if valueBridgesDirectly {
2431-
if let bridgedValue = value as? SwiftValue {
2432-
resultValue = bridgedValue
2433-
} else {
2434-
return nil
2435-
}
2436-
} else {
2437-
if let bridgedValue = _conditionallyBridgeFromObjectiveC(
2438-
_reinterpretCastToAnyObject(value), SwiftValue.self) {
2439-
resultValue = bridgedValue
2440-
} else {
2441-
return nil
2442-
}
2443-
}
2444-
2445-
result[resultKey] = resultValue
2446-
}
2447-
return result
2448-
}
2449-
#endif
24502215
//===--- APIs templated for Dictionary and Set ----------------------------===//
24512216

24522217
%{

test/1_stdlib/DictionaryTrapsObjC.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ DictionaryTraps.test("BridgedKeyIsNotNSCopyable2")
144144
DictionaryTraps.test("Downcast1") {
145145
let d: Dictionary<NSObject, NSObject> = [ TestObjCKeyTy(10): NSObject(),
146146
NSObject() : NSObject() ]
147-
let d2: Dictionary<TestObjCKeyTy, NSObject> = _dictionaryDownCast(d)
147+
let d2: Dictionary<TestObjCKeyTy, NSObject> = d as! Dictionary<TestObjCKeyTy, NSObject>
148148
expectCrashLater()
149149
let v1 = d2[TestObjCKeyTy(10)]
150150
let v2 = d2[TestObjCKeyTy(20)]
@@ -163,7 +163,7 @@ DictionaryTraps.test("Downcast2")
163163

164164
expectCrashLater()
165165
let d2: Dictionary<TestBridgedKeyTy, NSObject>
166-
= _dictionaryBridgeFromObjectiveC(d)
166+
= d as! Dictionary<TestBridgedKeyTy, NSObject>
167167
let v1 = d2[TestBridgedKeyTy(10)]
168168
}
169169

test/1_stdlib/SetTrapsObjC.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ SetTraps.test("Downcast1")
134134
reason: "this trap is not guaranteed to happen in -Ounchecked"))
135135
.code {
136136
let s: Set<NSObject> = [ NSObject(), NSObject() ]
137-
let s2: Set<TestObjCKeyTy> = _setDownCast(s)
137+
let s2: Set<TestObjCKeyTy> = s as! Set<TestObjCKeyTy>
138138
expectCrashLater()
139139
let v1 = s2.contains(TestObjCKeyTy(10))
140140
let v2 = s2.contains(TestObjCKeyTy(20))
@@ -150,7 +150,7 @@ SetTraps.test("Downcast2")
150150
.code {
151151
let s: Set<NSObject> = [ TestObjCKeyTy(10), NSObject() ]
152152
expectCrashLater()
153-
let s2: Set<TestBridgedKeyTy> = _setBridgeFromObjectiveC(s)
153+
let s2: Set<TestBridgedKeyTy> = s as! Set<TestBridgedKeyTy>
154154
let v1 = s2.contains(TestBridgedKeyTy(10))
155155
}
156156

0 commit comments

Comments
 (0)