Skip to content

[stdlib] Remove unneeded numericCasts #31653

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 1 commit into from
May 9, 2020
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
16 changes: 11 additions & 5 deletions stdlib/public/core/ContiguousArrayBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -607,12 +607,14 @@ internal func += <Element, C: Collection>(
) where C.Element == Element {

let oldCount = lhs.count
let newCount = oldCount + numericCast(rhs.count)
let newCount = oldCount + rhs.count

let buf: UnsafeMutableBufferPointer<Element>

if _fastPath(newCount <= lhs.capacity) {
buf = UnsafeMutableBufferPointer(start: lhs.firstElementAddress + oldCount, count: numericCast(rhs.count))
buf = UnsafeMutableBufferPointer(
start: lhs.firstElementAddress + oldCount,
count: rhs.count)
lhs.count = newCount
}
else {
Expand All @@ -624,7 +626,9 @@ internal func += <Element, C: Collection>(
from: lhs.firstElementAddress, count: oldCount)
lhs.count = 0
(lhs, newLHS) = (newLHS, lhs)
buf = UnsafeMutableBufferPointer(start: lhs.firstElementAddress + oldCount, count: numericCast(rhs.count))
buf = UnsafeMutableBufferPointer(
start: lhs.firstElementAddress + oldCount,
count: rhs.count)
}

var (remainders,writtenUpTo) = buf.initialize(from: rhs)
Expand Down Expand Up @@ -716,7 +720,7 @@ internal func _copyCollectionToContiguousArray<
C: Collection
>(_ source: C) -> ContiguousArray<C.Element>
{
let count: Int = numericCast(source.count)
let count = source.count
if count == 0 {
return ContiguousArray()
}
Expand All @@ -725,7 +729,9 @@ internal func _copyCollectionToContiguousArray<
_uninitializedCount: count,
minimumCapacity: 0)

let p = UnsafeMutableBufferPointer(start: result.firstElementAddress, count: count)
let p = UnsafeMutableBufferPointer(
start: result.firstElementAddress,
count: count)
var (itr, end) = source._copyContents(initializing: p)

_debugPrecondition(itr.next() == nil,
Expand Down
58 changes: 19 additions & 39 deletions stdlib/public/core/ExistentialCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2019 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -743,7 +743,7 @@ internal final class _CollectionBox<S: Collection>: _AnyCollectionBox<S.Element>
internal override func _index(
_ i: _AnyIndexBox, offsetBy n: Int
) -> _AnyIndexBox {
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: numericCast(n)))
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: n))
}

@inlinable
Expand All @@ -752,10 +752,7 @@ internal final class _CollectionBox<S: Collection>: _AnyCollectionBox<S.Element>
offsetBy n: Int,
limitedBy limit: _AnyIndexBox
) -> _AnyIndexBox? {
return _base.index(
_unbox(i),
offsetBy: numericCast(n),
limitedBy: _unbox(limit))
return _base.index(_unbox(i), offsetBy: n, limitedBy: _unbox(limit))
.map { _IndexBox(_base: $0) }
}

Expand All @@ -764,7 +761,7 @@ internal final class _CollectionBox<S: Collection>: _AnyCollectionBox<S.Element>
_ i: inout _AnyIndexBox, offsetBy n: Int
) {
if let box = i as? _IndexBox<S.Index> {
return _base.formIndex(&box._base, offsetBy: numericCast(n))
return _base.formIndex(&box._base, offsetBy: n)
}
fatalError("Index type mismatch!")
}
Expand All @@ -774,10 +771,7 @@ internal final class _CollectionBox<S: Collection>: _AnyCollectionBox<S.Element>
_ i: inout _AnyIndexBox, offsetBy n: Int, limitedBy limit: _AnyIndexBox
) -> Bool {
if let box = i as? _IndexBox<S.Index> {
return _base.formIndex(
&box._base,
offsetBy: numericCast(n),
limitedBy: _unbox(limit))
return _base.formIndex(&box._base, offsetBy: n, limitedBy: _unbox(limit))
}
fatalError("Index type mismatch!")
}
Expand All @@ -787,12 +781,12 @@ internal final class _CollectionBox<S: Collection>: _AnyCollectionBox<S.Element>
from start: _AnyIndexBox,
to end: _AnyIndexBox
) -> Int {
return numericCast(_base.distance(from: _unbox(start), to: _unbox(end)))
return _base.distance(from: _unbox(start), to: _unbox(end))
}

@inlinable
internal override var _count: Int {
return numericCast(_base.count)
return _base.count
}

@usableFromInline
Expand Down Expand Up @@ -949,7 +943,7 @@ internal final class _BidirectionalCollectionBox<S: BidirectionalCollection>
internal override func _index(
_ i: _AnyIndexBox, offsetBy n: Int
) -> _AnyIndexBox {
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: numericCast(n)))
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: n))
}

@inlinable
Expand All @@ -958,11 +952,7 @@ internal final class _BidirectionalCollectionBox<S: BidirectionalCollection>
offsetBy n: Int,
limitedBy limit: _AnyIndexBox
) -> _AnyIndexBox? {
return _base.index(
_unbox(i),
offsetBy: numericCast(n),
limitedBy: _unbox(limit)
)
return _base.index(_unbox(i), offsetBy: n, limitedBy: _unbox(limit))
.map { _IndexBox(_base: $0) }
}

Expand All @@ -971,7 +961,7 @@ internal final class _BidirectionalCollectionBox<S: BidirectionalCollection>
_ i: inout _AnyIndexBox, offsetBy n: Int
) {
if let box = i as? _IndexBox<S.Index> {
return _base.formIndex(&box._base, offsetBy: numericCast(n))
return _base.formIndex(&box._base, offsetBy: n)
}
fatalError("Index type mismatch!")
}
Expand All @@ -981,10 +971,7 @@ internal final class _BidirectionalCollectionBox<S: BidirectionalCollection>
_ i: inout _AnyIndexBox, offsetBy n: Int, limitedBy limit: _AnyIndexBox
) -> Bool {
if let box = i as? _IndexBox<S.Index> {
return _base.formIndex(
&box._base,
offsetBy: numericCast(n),
limitedBy: _unbox(limit))
return _base.formIndex(&box._base, offsetBy: n, limitedBy: _unbox(limit))
}
fatalError("Index type mismatch!")
}
Expand All @@ -994,12 +981,12 @@ internal final class _BidirectionalCollectionBox<S: BidirectionalCollection>
from start: _AnyIndexBox,
to end: _AnyIndexBox
) -> Int {
return numericCast(_base.distance(from: _unbox(start), to: _unbox(end)))
return _base.distance(from: _unbox(start), to: _unbox(end))
}

@inlinable
internal override var _count: Int {
return numericCast(_base.count)
return _base.count
}

@inlinable
Expand Down Expand Up @@ -1168,7 +1155,7 @@ internal final class _RandomAccessCollectionBox<S: RandomAccessCollection>
internal override func _index(
_ i: _AnyIndexBox, offsetBy n: Int
) -> _AnyIndexBox {
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: numericCast(n)))
return _IndexBox(_base: _base.index(_unbox(i), offsetBy: n))
}

@inlinable
Expand All @@ -1177,11 +1164,7 @@ internal final class _RandomAccessCollectionBox<S: RandomAccessCollection>
offsetBy n: Int,
limitedBy limit: _AnyIndexBox
) -> _AnyIndexBox? {
return _base.index(
_unbox(i),
offsetBy: numericCast(n),
limitedBy: _unbox(limit)
)
return _base.index(_unbox(i), offsetBy: n, limitedBy: _unbox(limit))
.map { _IndexBox(_base: $0) }
}

Expand All @@ -1190,7 +1173,7 @@ internal final class _RandomAccessCollectionBox<S: RandomAccessCollection>
_ i: inout _AnyIndexBox, offsetBy n: Int
) {
if let box = i as? _IndexBox<S.Index> {
return _base.formIndex(&box._base, offsetBy: numericCast(n))
return _base.formIndex(&box._base, offsetBy: n)
}
fatalError("Index type mismatch!")
}
Expand All @@ -1200,10 +1183,7 @@ internal final class _RandomAccessCollectionBox<S: RandomAccessCollection>
_ i: inout _AnyIndexBox, offsetBy n: Int, limitedBy limit: _AnyIndexBox
) -> Bool {
if let box = i as? _IndexBox<S.Index> {
return _base.formIndex(
&box._base,
offsetBy: numericCast(n),
limitedBy: _unbox(limit))
return _base.formIndex(&box._base, offsetBy: n, limitedBy: _unbox(limit))
}
fatalError("Index type mismatch!")
}
Expand All @@ -1213,12 +1193,12 @@ internal final class _RandomAccessCollectionBox<S: RandomAccessCollection>
from start: _AnyIndexBox,
to end: _AnyIndexBox
) -> Int {
return numericCast(_base.distance(from: _unbox(start), to: _unbox(end)))
return _base.distance(from: _unbox(start), to: _unbox(end))
}

@inlinable
internal override var _count: Int {
return numericCast(_base.count)
return _base.count
}

@inlinable
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/Filter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -231,7 +231,7 @@ extension LazyFilterCollection: Collection {
// _base at least once, to trigger a _precondition in forward only
// collections.
_ensureBidirectional(step: step)
for _ in 0 ..< abs(numericCast(n)) {
for _ in 0 ..< abs(n) {
_advanceIndex(&i, step: step)
}
return i
Expand All @@ -252,7 +252,7 @@ extension LazyFilterCollection: Collection {
// invoked on the _base at least once, to trigger a _precondition in
// forward only collections.
_ensureBidirectional(step: step)
for _ in 0 ..< abs(numericCast(n)) {
for _ in 0 ..< abs(n) {
if i == limit {
return nil
}
Expand Down
7 changes: 3 additions & 4 deletions stdlib/public/core/RangeReplaceableCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,7 @@ extension RangeReplaceableCollection {
public mutating func append<S: Sequence>(contentsOf newElements: __owned S)
where S.Element == Element {

let approximateCapacity = self.count +
numericCast(newElements.underestimatedCount)
let approximateCapacity = self.count + newElements.underestimatedCount
self.reserveCapacity(approximateCapacity)
for element in newElements {
append(element)
Expand Down Expand Up @@ -800,7 +799,7 @@ extension RangeReplaceableCollection

@inlinable
public mutating func _customRemoveLast(_ n: Int) -> Bool {
self = self[startIndex..<index(endIndex, offsetBy: numericCast(-n))]
self = self[startIndex..<index(endIndex, offsetBy: -n)]
return true
}
}
Expand Down Expand Up @@ -998,7 +997,7 @@ extension RangeReplaceableCollection {
>(lhs: Other, rhs: Self) -> Self
where Element == Other.Element {
var result = Self()
result.reserveCapacity(rhs.count + numericCast(lhs.underestimatedCount))
result.reserveCapacity(rhs.count + lhs.underestimatedCount)
result.append(contentsOf: lhs)
result.append(contentsOf: rhs)
return result
Expand Down
12 changes: 6 additions & 6 deletions stdlib/public/core/Slice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -337,7 +337,7 @@ extension Slice: RangeReplaceableCollection
let newSliceCount =
_base.distance(from: _startIndex, to: subRange.lowerBound)
+ _base.distance(from: subRange.upperBound, to: _endIndex)
+ (numericCast(newElements.count) as Int)
+ newElements.count
_base.replaceSubrange(subRange, with: newElements)
_startIndex = _base.index(_base.startIndex, offsetBy: sliceOffset)
_endIndex = _base.index(_startIndex, offsetBy: newSliceCount)
Expand Down Expand Up @@ -400,7 +400,7 @@ extension Slice
let newSliceCount =
_base.distance(from: _startIndex, to: subRange.lowerBound)
+ _base.distance(from: subRange.upperBound, to: _endIndex)
+ (numericCast(newElements.count) as Int)
+ newElements.count
_base.replaceSubrange(subRange, with: newElements)
_startIndex = _base.startIndex
_endIndex = _base.index(_startIndex, offsetBy: newSliceCount)
Expand All @@ -409,7 +409,7 @@ extension Slice
let lastValidIndex = _base.index(before: subRange.lowerBound)
let newEndIndexOffset =
_base.distance(from: subRange.upperBound, to: _endIndex)
+ (numericCast(newElements.count) as Int) + 1
+ newElements.count + 1
_base.replaceSubrange(subRange, with: newElements)
if shouldUpdateStartIndex {
_startIndex = _base.index(after: lastValidIndex)
Expand Down Expand Up @@ -443,7 +443,7 @@ extension Slice
where S: Collection, S.Element == Base.Element {
// FIXME: swift-3-indexing-model: range check.
if i == _base.startIndex {
let newSliceCount = count + numericCast(newElements.count)
let newSliceCount = count + newElements.count
_base.insert(contentsOf: newElements, at: i)
_startIndex = _base.startIndex
_endIndex = _base.index(_startIndex, offsetBy: newSliceCount)
Expand All @@ -452,7 +452,7 @@ extension Slice
let lastValidIndex = _base.index(before: i)
let newEndIndexOffset =
_base.distance(from: i, to: _endIndex)
+ numericCast(newElements.count) + 1
+ newElements.count + 1
_base.insert(contentsOf: newElements, at: i)
if shouldUpdateStartIndex {
_startIndex = _base.index(after: lastValidIndex)
Expand Down
7 changes: 3 additions & 4 deletions stdlib/public/core/SliceBuffer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
// Copyright (c) 2014 - 2020 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
Expand Down Expand Up @@ -116,8 +116,7 @@ internal struct _SliceBuffer<Element>
/// the given collection.
///
/// - Precondition: This buffer is backed by a uniquely-referenced
/// `_ContiguousArrayBuffer` and
/// `insertCount <= numericCast(newValues.count)`.
/// `_ContiguousArrayBuffer` and `insertCount <= newValues.count`.
@inlinable
internal mutating func replaceSubrange<C>(
_ subrange: Range<Int>,
Expand All @@ -126,7 +125,7 @@ internal struct _SliceBuffer<Element>
) where C: Collection, C.Element == Element {

_invariantCheck()
_internalInvariant(insertCount <= numericCast(newValues.count))
_internalInvariant(insertCount <= newValues.count)

_internalInvariant(_hasNativeBuffer)
_internalInvariant(isUniquelyReferenced())
Expand Down