Skip to content

Commit a7a954a

Browse files
committed
Perform collection force-casts by force-casting the elements
instead of forcing conditional casts of the elements. This should produce better and more compact code, allow more efficient runtime behavior, and generate much better runtime diagnostics if the cast fails.
1 parent d566d77 commit a7a954a

File tree

2 files changed

+3
-13
lines changed

2 files changed

+3
-13
lines changed

stdlib/public/core/ArrayCast.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public func _arrayForceCast<SourceElement, TargetElement>(
4747
return Array(_immutableCocoaArray: source._buffer._asCocoaArray())
4848
}
4949
#endif
50-
return _arrayConditionalCast(source)!
50+
return source.map { $0 as! TargetElement }
5151
}
5252

5353
internal struct _UnwrappingFailed : Error {}

stdlib/public/core/HashedCollections.swift.gyb

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1278,15 +1278,6 @@ internal func _stdlib_NSSet_allObjects(_ nss: _NSSet) ->
12781278

12791279
//===--- Compiler conversion/casting entry points for Set<Element> --------===//
12801280

1281-
func _impossible<T>(_:T.Type) -> T {
1282-
Builtin.unreachable()
1283-
}
1284-
1285-
func _unsafeUpcast<T, U>(_ x: T, to: U.Type) -> U {
1286-
_sanityCheck(x is U)
1287-
return x as? U ?? _impossible(U.self)
1288-
}
1289-
12901281
/// Perform a non-bridged upcast that always succeeds.
12911282
///
12921283
/// - Precondition: `BaseValue` is a base class or base `@objc`
@@ -1295,7 +1286,7 @@ public func _setUpCast<DerivedValue, BaseValue>(_ source: Set<DerivedValue>)
12951286
-> Set<BaseValue> {
12961287
var builder = _SetBuilder<BaseValue>(count: source.count)
12971288
for x in source {
1298-
builder.add(member: _unsafeUpcast(x, to: BaseValue.self))
1289+
builder.add(member: x as! BaseValue)
12991290
}
13001291
return builder.take()
13011292
}
@@ -2220,8 +2211,7 @@ public func _dictionaryUpCast<DerivedKey, DerivedValue, BaseKey, BaseValue>(
22202211
var result = Dictionary<BaseKey, BaseValue>(minimumCapacity: source.count)
22212212

22222213
for (k, v) in source {
2223-
result[_unsafeUpcast(k, to: BaseKey.self)]
2224-
= _unsafeUpcast(v, to: BaseValue.self)
2214+
result[k as! BaseKey] = v as! BaseValue
22252215
}
22262216
return result
22272217
}

0 commit comments

Comments
 (0)