[stdlib][Qol] SR-11295 Removing stdlib unnecessary coercions (#27165)

* Removing unnecessary casts from stdlib

* Minor adjustments

* [stdlib][qol] Clean up error variable assign NSError

* Removing unnecessary coercions after removing DeclRefExpr restriction.
This commit is contained in:
Luciano Almeida 2019-12-11 12:30:25 -03:00 коммит произвёл Robert Widmann
Родитель 975aa53f08
Коммит 392baefc47
23 изменённых файлов: 36 добавлений и 41 удалений

Просмотреть файл

@ -34,7 +34,7 @@ class ConstructibleArray<T:Constructible> {
array = [T]()
array.reserveCapacity(1_000)
for _ in 0...1_000 {
array.append(T(e:e) as T)
array.append(T(e:e))
}
}
}

Просмотреть файл

@ -35,7 +35,7 @@ class ConstructibleArray<T:Constructible> {
array = [T]()
array.reserveCapacity(1_000)
for _ in 0...1_000 {
array.append(T(e:e) as T)
array.append(T(e:e))
}
}
}

Просмотреть файл

@ -66,7 +66,7 @@ import TestsUtils
let ascii =
"siebenhundertsiebenundsiebzigtausendsiebenhundertsiebenundsiebzig"
let emoji = "👍👩‍👩‍👧‍👧👨‍👨‍👦‍👦🇺🇸🇨🇦🇲🇽👍🏻👍🏼👍🏽👍🏾👍🏿"
let utf16 = emoji + "the quick brown fox" + String(emoji.reversed() as Array<Character>)
let utf16 = emoji + "the quick brown fox" + String(emoji.reversed())
let japanese = "今回のアップデートでSwiftに大幅な改良が施され、安定していてしかも直感的に使うことができるAppleプラットフォーム向けプログラミング言語になりました。"
let chinese = "Swift 是面向 Apple 平台的编程语言,功能强大且直观易用,而本次更新对其进行了全面优化。"

Просмотреть файл

@ -67,7 +67,7 @@ import TestsUtils
let ascii =
"siebenhundertsiebenundsiebzigtausendsiebenhundertsiebenundsiebzig"
let emoji = "👍👩‍👩‍👧‍👧👨‍👨‍👦‍👦🇺🇸🇨🇦🇲🇽👍🏻👍🏼👍🏽👍🏾👍🏿"
let utf16 = emoji + "the quick brown fox" + String(emoji.reversed() as Array<Character>)
let utf16 = emoji + "the quick brown fox" + String(emoji.reversed())
let japanese = "今回のアップデートでSwiftに大幅な改良が施され、安定していてしかも直感的に使うことができるAppleプラットフォーム向けプログラミング言語になりました。"
let chinese = "Swift 是面向 Apple 平台的编程语言,功能强大且直观易用,而本次更新对其进行了全面优化。"

Просмотреть файл

@ -58,17 +58,17 @@ extension CKRecord {
@available(swift 4.2)
public enum SystemType {
public static let userRecord: CKRecord.RecordType = __CKRecordTypeUserRecord as CKRecord.RecordType
public static let userRecord: CKRecord.RecordType = __CKRecordTypeUserRecord
@available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
public static let share: CKRecord.RecordType = __CKRecordTypeShare as CKRecord.RecordType
public static let share: CKRecord.RecordType = __CKRecordTypeShare
}
@available(swift 4.2)
public enum SystemFieldKey {
@available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
public static let parent: CKRecord.FieldKey = __CKRecordParentKey as CKRecord.RecordType
public static let parent: CKRecord.FieldKey = __CKRecordParentKey
@available(macOS 10.12, iOS 10.0, tvOS 10.0, watchOS 3.0, *)
public static let share: CKRecord.FieldKey = __CKRecordShareKey as CKRecord.RecordType
public static let share: CKRecord.FieldKey = __CKRecordShareKey
}
}

Просмотреть файл

@ -17,9 +17,9 @@
extension CKShare {
@available(swift 4.2)
public enum SystemFieldKey {
public static let title: CKRecord.FieldKey = __CKShareTitleKey as CKRecord.FieldKey
public static let thumbnailImageData: CKRecord.FieldKey = __CKShareThumbnailImageDataKey as CKRecord.FieldKey
public static let shareType: CKRecord.FieldKey = __CKShareTypeKey as CKRecord.FieldKey
public static let title: CKRecord.FieldKey = __CKShareTitleKey
public static let thumbnailImageData: CKRecord.FieldKey = __CKShareThumbnailImageDataKey
public static let shareType: CKRecord.FieldKey = __CKShareTypeKey
}
}

Просмотреть файл

@ -43,7 +43,7 @@ extension CKOperation {
*/
@available(swift 4.2)
public var operationID: CKOperation.ID {
get { return self.__operationID as CKOperation.ID }
get { return self.__operationID }
}
}

Просмотреть файл

@ -62,7 +62,7 @@ extension CMTime {
}
public var seconds: Double {
return CMTimeGetSeconds(self) as Double
return CMTimeGetSeconds(self)
}
public func convertScale(_ newTimescale: Int32, method: CMTimeRoundingMethod)

Просмотреть файл

@ -204,11 +204,7 @@ public struct DateComponents : ReferenceConvertible, Hashable, Equatable, _Mutab
/// Returns a `Date` calculated from the current components using the `calendar` property.
public var date: Date? {
if let d = _handle.map({$0.date}) {
return d as Date
} else {
return nil
}
return _handle.map { $0.date }
}
// MARK: - Generic Setter/Getters

Просмотреть файл

@ -48,7 +48,7 @@ extension FileManager {
return __NSFileManagerEnumeratorAtURL(self, url, keys, mask, { (url, error) in
var errorResult = true
if let h = handler {
errorResult = h(url as URL, error)
errorResult = h(url, error)
}
return errorResult
})

Просмотреть файл

@ -600,7 +600,7 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
stop.pointee = true
return false
}
}) as IndexSet
})
if let e = error {
throw e
} else {

Просмотреть файл

@ -53,7 +53,7 @@ extension NSCoder {
if let theClasses = classes {
classesAsNSObjects = NSSet(array: theClasses.map { $0 as AnyObject })
}
return __NSCoderDecodeObjectOfClassesForKey(self, classesAsNSObjects, key, nil).map { $0 as Any }
return __NSCoderDecodeObjectOfClassesForKey(self, classesAsNSObjects, key, nil).map { $0 }
}
@nonobjc
@ -62,7 +62,7 @@ extension NSCoder {
var error: NSError?
let result = __NSCoderDecodeObject(self, &error)
try resolveError(error)
return result.map { $0 as Any }
return result.map { $0 }
}
@available(*, unavailable, renamed: "decodeTopLevelObject(forKey:)")
@ -125,7 +125,7 @@ extension NSCoder {
}
let result = __NSCoderDecodeObjectOfClassesForKey(self, classesAsNSObjects, key, &error)
try resolveError(error)
return result.map { $0 as Any }
return result.map { $0 }
}
}

Просмотреть файл

@ -64,7 +64,7 @@ extension Dictionary {
extension Dictionary : _ObjectiveCBridgeable {
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSDictionary {
return unsafeBitCast(_bridgeToObjectiveCImpl() as AnyObject,
return unsafeBitCast(_bridgeToObjectiveCImpl(),
to: NSDictionary.self)
}

Просмотреть файл

@ -234,8 +234,7 @@ public func _getErrorDefaultUserInfo<T: Error>(_ error: T)
if domain != NSCocoaErrorDomain {
_errorDomainUserInfoProviderQueue.sync {
if NSError.userInfoValueProvider(forDomain: domain) != nil { return }
NSError.setUserInfoValueProvider(forDomain: domain) { (nsError, key) in
let error = nsError as Error
NSError.setUserInfoValueProvider(forDomain: domain) { (error, key) in
switch key {
case NSLocalizedDescriptionKey:

Просмотреть файл

@ -55,7 +55,7 @@ extension NSOrderedSet : Sequence {
extension Set : _ObjectiveCBridgeable {
@_semantics("convertToObjectiveC")
public func _bridgeToObjectiveC() -> NSSet {
return unsafeBitCast(_bridgeToObjectiveCImpl() as AnyObject, to: NSSet.self)
return unsafeBitCast(_bridgeToObjectiveCImpl(), to: NSSet.self)
}
public static func _forceBridgeFromObjectiveC(_ s: NSSet, result: inout Set?) {

Просмотреть файл

@ -353,7 +353,7 @@ extension String {
usedEncoding: inout Encoding
) throws {
var enc: UInt = 0
let ns = try NSString(contentsOf: url as URL, usedEncoding: &enc)
let ns = try NSString(contentsOf: url, usedEncoding: &enc)
usedEncoding = Encoding(rawValue: enc)
self = String._unconditionallyBridgeFromObjectiveC(ns)
}
@ -566,7 +566,7 @@ extension StringProtocol where Index == String.Index {
/// Case transformations arent guaranteed to be symmetrical or to produce
/// strings of the same lengths as the originals.
public var capitalized: String {
return _ns.capitalized as String
return _ns.capitalized
}
// @property (readonly, copy) NSString *localizedCapitalizedString NS_AVAILABLE(10_11, 9_0);
@ -583,7 +583,7 @@ extension StringProtocol where Index == String.Index {
/// Returns a capitalized representation of the string
/// using the specified locale.
public func capitalized(with locale: Locale?) -> String {
return _ns.capitalized(with: locale) as String
return _ns.capitalized(with: locale)
}
// - (NSComparisonResult)caseInsensitiveCompare:(NSString *)aString
@ -1166,7 +1166,7 @@ extension StringProtocol where Index == String.Index {
/// locale.
@available(macOS 10.11, iOS 9.0, *)
public var localizedUppercase: String {
return _ns.localizedUppercase as String
return _ns.localizedUppercase
}
// - (NSString *)uppercaseStringWithLocale:(Locale *)locale

Просмотреть файл

@ -17,7 +17,7 @@ extension Progress {
public var estimatedTimeRemaining: TimeInterval? {
get {
guard let v = self.__estimatedTimeRemaining else { return nil }
return v.doubleValue as TimeInterval
return v.doubleValue
}
set {
guard let nv = newValue else {

Просмотреть файл

@ -32,7 +32,7 @@ extension String : _ObjectiveCBridgeable {
// This method should not do anything extra except calling into the
// implementation inside core. (These two entry points should be
// equivalent.)
return unsafeBitCast(_bridgeToObjectiveCImpl() as AnyObject, to: NSString.self)
return unsafeBitCast(_bridgeToObjectiveCImpl(), to: NSString.self)
}
public static func _forceBridgeFromObjectiveC(

Просмотреть файл

@ -1090,7 +1090,7 @@ public struct URL : ReferenceConvertible, Equatable {
/// Returns bookmark data for the URL, created with specified options and resource values.
public func bookmarkData(options: BookmarkCreationOptions = [], includingResourceValuesForKeys keys: Set<URLResourceKey>? = nil, relativeTo url: URL? = nil) throws -> Data {
let result = try _url.bookmarkData(options: options, includingResourceValuesForKeys: keys.flatMap { Array($0) }, relativeTo: url)
return result as Data
return result
}
/// Returns the resource values for properties identified by a specified array of keys contained in specified bookmark data. If the result dictionary does not contain a resource value for one or more of the requested resource keys, it means those resource properties are not available in the bookmark data.
@ -1107,7 +1107,7 @@ public struct URL : ReferenceConvertible, Equatable {
/// Initializes and returns bookmark data derived from an alias file pointed to by a specified URL. If bookmarkFileURL refers to an alias file created prior to OS X v10.6 that contains Alias Manager information but no bookmark data, this method synthesizes bookmark data for the file.
public static func bookmarkData(withContentsOf url: URL) throws -> Data {
let result = try NSURL.bookmarkData(withContentsOf: url)
return result as Data
return result
}
/// Given an NSURL created by resolving a bookmark data created with security scope, make the resource referenced by the url accessible to the process. When access to this resource is no longer needed the client must call stopAccessingSecurityScopedResource. Each call to startAccessingSecurityScopedResource must be balanced with a call to stopAccessingSecurityScopedResource (Note: this is not reference counted).

Просмотреть файл

@ -479,7 +479,7 @@ public final class NWConnection : CustomDebugStringConvertible {
_ isComplete: Bool, _ error: NWError?) -> Void) {
nw_connection_receive(self.nw, UInt32(minimumIncompleteLength), UInt32(maximumLength)) {
(content, context, complete, nwError) in
completion(NWCreateNSDataFromDispatchData(content) as Data?, ContentContext(context), complete, NWError(nwError));
completion(NWCreateNSDataFromDispatchData(content), ContentContext(context), complete, NWError(nwError));
}
}
@ -491,7 +491,7 @@ public final class NWConnection : CustomDebugStringConvertible {
_ contentContext: NWConnection.ContentContext?,
_ isComplete: Bool, _ error: NWError?) -> Void) {
nw_connection_receive_message(self.nw) { (content, context, complete, nwError) in
completion(NWCreateNSDataFromDispatchData(content) as Data?, ContentContext(context), complete, NWError(nwError))
completion(NWCreateNSDataFromDispatchData(content), ContentContext(context), complete, NWError(nwError))
}
}

Просмотреть файл

@ -148,7 +148,7 @@ internal func _getErrorCode<T: Error>(_ x: UnsafePointer<T>) -> Int {
@_silgen_name("")
internal func _getErrorUserInfoNSDictionary<T: Error>(_ x: UnsafePointer<T>)
-> AnyObject? {
return x.pointee._userInfo.map { $0 as AnyObject }
return x.pointee._userInfo.map { $0 }
}
// Called by the casting machinery to extract an NSError from an Error value.

Просмотреть файл

@ -2746,7 +2746,7 @@ extension FixedWidthInteger {
// If we used &+ instead, the result would be zero, which isn't helpful,
// so we actually need to handle this case separately.
if delta == Magnitude.max {
return Self(truncatingIfNeeded: generator.next() as Magnitude)
return Self(truncatingIfNeeded: generator.next())
}
// Need to widen delta to account for the right-endpoint of a closed range.
delta += 1
@ -3366,7 +3366,7 @@ extension FixedWidthInteger {
using generator: inout R
) -> Self {
if bitWidth <= UInt64.bitWidth {
return Self(truncatingIfNeeded: generator.next() as UInt64)
return Self(truncatingIfNeeded: generator.next())
}
let (quotient, remainder) = bitWidth.quotientAndRemainder(

Просмотреть файл

@ -383,7 +383,7 @@ internal func _bridgeCocoaString(_ cocoaString: _CocoaString) -> _StringGuts {
// happen; might as well eagerly bridge it in.
// 3) If it's mutable with associated information, must make the call
let immutableCopy
= _stdlib_binary_CFStringCreateCopy(cocoaString) as AnyObject
= _stdlib_binary_CFStringCreateCopy(cocoaString)
#if !(arch(i386) || arch(arm))
if _isObjCTaggedPointer(immutableCopy) {