Skip to content

Perform collection force-casts by force-casting the elements #4389

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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/public/core/ArrayCast.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public func _arrayForceCast<SourceElement, TargetElement>(
return Array(_immutableCocoaArray: source._buffer._asCocoaArray())
}
#endif
return _arrayConditionalCast(source)!
return source.map { $0 as! TargetElement }
}

internal struct _UnwrappingFailed : Error {}
Expand Down
14 changes: 2 additions & 12 deletions stdlib/public/core/HashedCollections.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1278,15 +1278,6 @@ internal func _stdlib_NSSet_allObjects(_ nss: _NSSet) ->

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

func _impossible<T>(_:T.Type) -> T {
Builtin.unreachable()
}

func _unsafeUpcast<T, U>(_ x: T, to: U.Type) -> U {
_sanityCheck(x is U)
return x as? U ?? _impossible(U.self)
}

/// Perform a non-bridged upcast that always succeeds.
///
/// - Precondition: `BaseValue` is a base class or base `@objc`
Expand All @@ -1295,7 +1286,7 @@ public func _setUpCast<DerivedValue, BaseValue>(_ source: Set<DerivedValue>)
-> Set<BaseValue> {
var builder = _SetBuilder<BaseValue>(count: source.count)
for x in source {
builder.add(member: _unsafeUpcast(x, to: BaseValue.self))
builder.add(member: x as! BaseValue)
}
return builder.take()
}
Expand Down Expand Up @@ -2220,8 +2211,7 @@ public func _dictionaryUpCast<DerivedKey, DerivedValue, BaseKey, BaseValue>(
var result = Dictionary<BaseKey, BaseValue>(minimumCapacity: source.count)

for (k, v) in source {
result[_unsafeUpcast(k, to: BaseKey.self)]
= _unsafeUpcast(v, to: BaseValue.self)
result[k as! BaseKey] = v as! BaseValue
}
return result
}
Expand Down