Skip to content

Commit 7501815

Browse files
Kill old set/dictionary-specific bridging entrypoints (swiftlang#18930)
1 parent a42fe56 commit 7501815

File tree

9 files changed

+48
-296
lines changed

9 files changed

+48
-296
lines changed

stdlib/public/SDK/Foundation/NSDictionary.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ extension Dictionary : _ObjectiveCBridgeable {
121121
return result != nil
122122
}
123123

124-
result = Swift._dictionaryBridgeFromObjectiveCConditional(anyDict)
124+
result = anyDict as? Dictionary
125125
return result != nil
126126
}
127127

stdlib/public/SDK/Foundation/NSSet.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ extension Set : _ObjectiveCBridgeable {
107107
return result != nil
108108
}
109109

110-
result = Swift._setBridgeFromObjectiveCConditional(anySet)
110+
result = anySet as? Set
111111
return result != nil
112112
}
113113

stdlib/public/core/Dictionary.swift

Lines changed: 0 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,65 +1641,6 @@ public func _dictionaryUpCast<DerivedKey, DerivedValue, BaseKey, BaseValue>(
16411641
return result
16421642
}
16431643

1644-
#if _runtime(_ObjC)
1645-
1646-
/// Implements an unconditional upcast that involves bridging.
1647-
///
1648-
/// The cast can fail if bridging fails.
1649-
///
1650-
/// - Precondition: `SwiftKey` and `SwiftValue` are bridged to Objective-C,
1651-
/// and at least one of them requires non-trivial bridging.
1652-
@inline(never)
1653-
public func _dictionaryBridgeToObjectiveC<
1654-
SwiftKey, SwiftValue, ObjCKey, ObjCValue
1655-
>(
1656-
_ source: Dictionary<SwiftKey, SwiftValue>
1657-
) -> Dictionary<ObjCKey, ObjCValue> {
1658-
1659-
// Note: We force this function to stay in the swift dylib because
1660-
// it is not performance sensitive and keeping it in the dylib saves
1661-
// a new kilobytes for each specialization for all users of dictionary.
1662-
1663-
_sanityCheck(
1664-
!_isBridgedVerbatimToObjectiveC(SwiftKey.self) ||
1665-
!_isBridgedVerbatimToObjectiveC(SwiftValue.self))
1666-
_sanityCheck(
1667-
_isClassOrObjCExistential(ObjCKey.self) ||
1668-
_isClassOrObjCExistential(ObjCValue.self))
1669-
1670-
var result = Dictionary<ObjCKey, ObjCValue>(minimumCapacity: source.count)
1671-
let keyBridgesDirectly =
1672-
_isBridgedVerbatimToObjectiveC(SwiftKey.self) ==
1673-
_isBridgedVerbatimToObjectiveC(ObjCKey.self)
1674-
let valueBridgesDirectly =
1675-
_isBridgedVerbatimToObjectiveC(SwiftValue.self) ==
1676-
_isBridgedVerbatimToObjectiveC(ObjCValue.self)
1677-
for (key, value) in source {
1678-
// Bridge the key
1679-
var bridgedKey: ObjCKey
1680-
if keyBridgesDirectly {
1681-
bridgedKey = unsafeBitCast(key, to: ObjCKey.self)
1682-
} else {
1683-
let bridged: AnyObject = _bridgeAnythingToObjectiveC(key)
1684-
bridgedKey = unsafeBitCast(bridged, to: ObjCKey.self)
1685-
}
1686-
1687-
// Bridge the value
1688-
var bridgedValue: ObjCValue
1689-
if valueBridgesDirectly {
1690-
bridgedValue = unsafeBitCast(value, to: ObjCValue.self)
1691-
} else {
1692-
let bridged: AnyObject? = _bridgeAnythingToObjectiveC(value)
1693-
bridgedValue = unsafeBitCast(bridged, to: ObjCValue.self)
1694-
}
1695-
1696-
result[bridgedKey] = bridgedValue
1697-
}
1698-
1699-
return result
1700-
}
1701-
#endif
1702-
17031644
/// Called by the casting machinery.
17041645
@_silgen_name("_swift_dictionaryDownCastIndirect")
17051646
internal func _dictionaryDownCastIndirect<SourceKey, SourceValue,
@@ -1778,92 +1719,6 @@ public func _dictionaryDownCastConditional<
17781719
return result
17791720
}
17801721

1781-
#if _runtime(_ObjC)
1782-
/// Implements an unconditional downcast that involves bridging.
1783-
///
1784-
/// - Precondition: At least one of `SwiftKey` or `SwiftValue` is a bridged value
1785-
/// type, and the corresponding `ObjCKey` or `ObjCValue` is a reference type.
1786-
@inlinable
1787-
public func _dictionaryBridgeFromObjectiveC<
1788-
ObjCKey, ObjCValue, SwiftKey, SwiftValue
1789-
>(
1790-
_ source: Dictionary<ObjCKey, ObjCValue>
1791-
) -> Dictionary<SwiftKey, SwiftValue> {
1792-
let result: Dictionary<SwiftKey, SwiftValue>? =
1793-
_dictionaryBridgeFromObjectiveCConditional(source)
1794-
_precondition(result != nil, "Dictionary cannot be bridged from Objective-C")
1795-
return result!
1796-
}
1797-
1798-
/// Implements a conditional downcast that involves bridging.
1799-
///
1800-
/// If the cast fails, the function returns `nil`. All checks should be
1801-
/// performed eagerly.
1802-
///
1803-
/// - Precondition: At least one of `SwiftKey` or `SwiftValue` is a bridged value
1804-
/// type, and the corresponding `ObjCKey` or `ObjCValue` is a reference type.
1805-
@inlinable
1806-
public func _dictionaryBridgeFromObjectiveCConditional<
1807-
ObjCKey, ObjCValue, SwiftKey, SwiftValue
1808-
>(
1809-
_ source: Dictionary<ObjCKey, ObjCValue>
1810-
) -> Dictionary<SwiftKey, SwiftValue>? {
1811-
_sanityCheck(
1812-
_isClassOrObjCExistential(ObjCKey.self) ||
1813-
_isClassOrObjCExistential(ObjCValue.self))
1814-
_sanityCheck(
1815-
!_isBridgedVerbatimToObjectiveC(SwiftKey.self) ||
1816-
!_isBridgedVerbatimToObjectiveC(SwiftValue.self))
1817-
1818-
let keyBridgesDirectly =
1819-
_isBridgedVerbatimToObjectiveC(SwiftKey.self) ==
1820-
_isBridgedVerbatimToObjectiveC(ObjCKey.self)
1821-
let valueBridgesDirectly =
1822-
_isBridgedVerbatimToObjectiveC(SwiftValue.self) ==
1823-
_isBridgedVerbatimToObjectiveC(ObjCValue.self)
1824-
1825-
var result = Dictionary<SwiftKey, SwiftValue>(minimumCapacity: source.count)
1826-
for (key, value) in source {
1827-
// Downcast the key.
1828-
var resultKey: SwiftKey
1829-
if keyBridgesDirectly {
1830-
if let bridgedKey = key as? SwiftKey {
1831-
resultKey = bridgedKey
1832-
} else {
1833-
return nil
1834-
}
1835-
} else {
1836-
if let bridgedKey = _conditionallyBridgeFromObjectiveC(
1837-
_reinterpretCastToAnyObject(key), SwiftKey.self) {
1838-
resultKey = bridgedKey
1839-
} else {
1840-
return nil
1841-
}
1842-
}
1843-
1844-
// Downcast the value.
1845-
var resultValue: SwiftValue
1846-
if valueBridgesDirectly {
1847-
if let bridgedValue = value as? SwiftValue {
1848-
resultValue = bridgedValue
1849-
} else {
1850-
return nil
1851-
}
1852-
} else {
1853-
if let bridgedValue = _conditionallyBridgeFromObjectiveC(
1854-
_reinterpretCastToAnyObject(value), SwiftValue.self) {
1855-
resultValue = bridgedValue
1856-
} else {
1857-
return nil
1858-
}
1859-
}
1860-
1861-
result[resultKey] = resultValue
1862-
}
1863-
return result
1864-
}
1865-
#endif
1866-
18671722
//===--- APIs templated for Dictionary and Set ----------------------------===//
18681723

18691724
/// This protocol is only used for compile-time checks that

stdlib/public/core/Set.swift

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1102,40 +1102,6 @@ public func _setUpCast<DerivedValue, BaseValue>(_ source: Set<DerivedValue>)
11021102
return builder.take()
11031103
}
11041104

1105-
#if _runtime(_ObjC)
1106-
1107-
/// Implements an unconditional upcast that involves bridging.
1108-
///
1109-
/// The cast can fail if bridging fails.
1110-
///
1111-
/// - Precondition: `SwiftValue` is bridged to Objective-C
1112-
/// and requires non-trivial bridging.
1113-
@inlinable
1114-
public func _setBridgeToObjectiveC<SwiftValue, ObjCValue>(
1115-
_ source: Set<SwiftValue>
1116-
) -> Set<ObjCValue> {
1117-
_sanityCheck(_isClassOrObjCExistential(ObjCValue.self))
1118-
_sanityCheck(!_isBridgedVerbatimToObjectiveC(SwiftValue.self))
1119-
1120-
var result = Set<ObjCValue>(minimumCapacity: source.count)
1121-
let valueBridgesDirectly =
1122-
_isBridgedVerbatimToObjectiveC(SwiftValue.self) ==
1123-
_isBridgedVerbatimToObjectiveC(ObjCValue.self)
1124-
1125-
for member in source {
1126-
var bridgedMember: ObjCValue
1127-
if valueBridgesDirectly {
1128-
bridgedMember = unsafeBitCast(member, to: ObjCValue.self)
1129-
} else {
1130-
let bridged: AnyObject = _bridgeAnythingToObjectiveC(member)
1131-
bridgedMember = unsafeBitCast(bridged, to: ObjCValue.self)
1132-
}
1133-
result.insert(bridgedMember)
1134-
}
1135-
return result
1136-
}
1137-
#endif
1138-
11391105
/// Called by the casting machinery.
11401106
@_silgen_name("_swift_setDownCastIndirect")
11411107
internal func _setDownCastIndirect<SourceValue, TargetValue>(
@@ -1204,65 +1170,6 @@ public func _setDownCastConditional<BaseValue, DerivedValue>(
12041170
return result
12051171
}
12061172

1207-
#if _runtime(_ObjC)
1208-
1209-
/// Implements an unconditional downcast that involves bridging.
1210-
///
1211-
/// - Precondition: At least one of `SwiftValue` is a bridged value
1212-
/// type, and the corresponding `ObjCValue` is a reference type.
1213-
@inlinable
1214-
public func _setBridgeFromObjectiveC<ObjCValue, SwiftValue>(
1215-
_ source: Set<ObjCValue>
1216-
) -> Set<SwiftValue> {
1217-
let result: Set<SwiftValue>? = _setBridgeFromObjectiveCConditional(source)
1218-
_precondition(result != nil, "This set cannot be bridged from Objective-C")
1219-
return result!
1220-
}
1221-
1222-
/// Implements a conditional downcast that involves bridging.
1223-
///
1224-
/// If the cast fails, the function returns `nil`. All checks should be
1225-
/// performed eagerly.
1226-
///
1227-
/// - Precondition: At least one of `SwiftValue` is a bridged value
1228-
/// type, and the corresponding `ObjCValue` is a reference type.
1229-
@inlinable
1230-
public func _setBridgeFromObjectiveCConditional<
1231-
ObjCValue, SwiftValue
1232-
>(
1233-
_ source: Set<ObjCValue>
1234-
) -> Set<SwiftValue>? {
1235-
_sanityCheck(_isClassOrObjCExistential(ObjCValue.self))
1236-
_sanityCheck(!_isBridgedVerbatimToObjectiveC(SwiftValue.self))
1237-
1238-
let valueBridgesDirectly =
1239-
_isBridgedVerbatimToObjectiveC(SwiftValue.self) ==
1240-
_isBridgedVerbatimToObjectiveC(ObjCValue.self)
1241-
1242-
var result = Set<SwiftValue>(minimumCapacity: source.count)
1243-
for value in source {
1244-
// Downcast the value.
1245-
var resultValue: SwiftValue
1246-
if valueBridgesDirectly {
1247-
if let bridgedValue = value as? SwiftValue {
1248-
resultValue = bridgedValue
1249-
} else {
1250-
return nil
1251-
}
1252-
} else {
1253-
if let bridgedValue = _conditionallyBridgeFromObjectiveC(
1254-
_reinterpretCastToAnyObject(value), SwiftValue.self) {
1255-
resultValue = bridgedValue
1256-
} else {
1257-
return nil
1258-
}
1259-
}
1260-
result.insert(resultValue)
1261-
}
1262-
return result
1263-
}
1264-
#endif
1265-
12661173
extension Set {
12671174
/// Removes the elements of the given set from this set.
12681175
///

test/IRGen/bitcast_specialization.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
// specialized version fo myDictionaryBridge.
66
// <rdar://problem/17821040>
77

8-
// A minimized version of _dictionaryBridgeToObjectiveC in the stdlib
8+
// A minimized version of _dictionaryBridgeToObjectiveC that used to be in the
9+
// stdlib
910
public func myDictionaryBridge<
1011
SrcType, DestType
1112
>(

test/stdlib/SetTrapsObjC.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ var SetTraps = TestSuite("SetTraps" + testSuiteSuffix)
5555

5656
SetTraps.test("sanity") {
5757
// Sanity checks. This code should not trap.
58-
var s = Set<BridgedVerbatimRefTy>()
59-
var nss = s as NSSet
58+
let s = Set<BridgedVerbatimRefTy>()
59+
_ = s as NSSet
6060
}
6161

6262
class TestObjCKeyTy : NSObject {
@@ -120,8 +120,8 @@ func ==(x: TestBridgedKeyTy, y: TestBridgedKeyTy) -> Bool {
120120

121121
SetTraps.test("BridgedKeyIsNotNSCopyable1") {
122122
// This Set is bridged in O(1).
123-
var s: Set<TestObjCKeyTy> = [ TestObjCKeyTy(10) ]
124-
var nss = s as NSSet
123+
let s: Set<TestObjCKeyTy> = [ TestObjCKeyTy(10) ]
124+
let nss = s as NSSet
125125

126126
// Unlike NSDictionary, NSSet does not require NSCopying from its element
127127
// type.
@@ -137,11 +137,11 @@ SetTraps.test("Downcast1")
137137
let s: Set<NSObject> = [ NSObject(), NSObject() ]
138138
let s2: Set<TestObjCKeyTy> = _setDownCast(s)
139139
expectCrashLater()
140-
let v1 = s2.contains(TestObjCKeyTy(10))
141-
let v2 = s2.contains(TestObjCKeyTy(20))
140+
_ = s2.contains(TestObjCKeyTy(10))
141+
_ = s2.contains(TestObjCKeyTy(20))
142142

143143
// This triggers failure.
144-
for m in s2 { }
144+
for _ in s2 { }
145145
}
146146

147147
SetTraps.test("Downcast2")
@@ -151,8 +151,8 @@ SetTraps.test("Downcast2")
151151
.code {
152152
let s: Set<NSObject> = [ TestObjCKeyTy(10), NSObject() ]
153153
expectCrashLater()
154-
let s2: Set<TestBridgedKeyTy> = _setBridgeFromObjectiveC(s)
155-
let v1 = s2.contains(TestBridgedKeyTy(10))
154+
let s2 = s as! Set<TestBridgedKeyTy>
155+
_ = s2.contains(TestBridgedKeyTy(10))
156156
}
157157

158158
runAllTests()

0 commit comments

Comments
 (0)