Skip to content

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

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
2 changes: 1 addition & 1 deletion benchmark/single-source/ArrayOfGenericRef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/single-source/ArrayOfRef.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion benchmark/single-source/StringWalk.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 平台的编程语言,功能强大且直观易用,而本次更新对其进行了全面优化。"
Expand Down
2 changes: 1 addition & 1 deletion benchmark/single-source/StringWalk.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -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 平台的编程语言,功能强大且直观易用,而本次更新对其进行了全面优化。"
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/Darwin/CloudKit/CKRecord.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

6 changes: 3 additions & 3 deletions stdlib/public/Darwin/CloudKit/CKShare.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

2 changes: 1 addition & 1 deletion stdlib/public/Darwin/CloudKit/TypealiasStrings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Darwin/CoreMedia/CMTime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 1 addition & 5 deletions stdlib/public/Darwin/Foundation/DateComponents.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Darwin/Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Darwin/Foundation/IndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public struct IndexSet : ReferenceConvertible, Equatable, BidirectionalCollectio
stop.pointee = true
return false
}
}) as IndexSet
})
if let e = error {
throw e
} else {
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/Darwin/Foundation/NSCoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:)")
Expand Down Expand Up @@ -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 }
}
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Darwin/Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
3 changes: 1 addition & 2 deletions stdlib/public/Darwin/Foundation/NSError.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Darwin/Foundation/NSSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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?) {
Expand Down
8 changes: 4 additions & 4 deletions stdlib/public/Darwin/Foundation/NSStringAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -566,7 +566,7 @@ extension StringProtocol where Index == String.Index {
/// Case transformations aren’t 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);
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Darwin/Foundation/Progress.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/Darwin/Foundation/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/Darwin/Foundation/URL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/Darwin/Network/NWConnection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand All @@ -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))
}
}

Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/ErrorType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/core/Integers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/StringBridge.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down