stdlib: Don't check for overflows when adding 1 to Array.count

That addition can not possibly overflow. If Array.count would be Int max, the allocation of the array buffer would have failed way before.
This commit is contained in:
Erik Eckstein
2021-11-23 18:26:29 +01:00
parent 2a9c148e7a
commit 97424b76f4
3 changed files with 16 additions and 16 deletions

View File

@@ -847,7 +847,7 @@ extension ArraySlice: RangeReplaceableCollection {
@inline(never)
@inlinable // @specializable
internal mutating func _copyToNewBuffer(oldCount: Int) {
let newCount = oldCount + 1
let newCount = oldCount &+ 1
var newBuffer = _buffer._forceCreateUniqueMutableBuffer(
countForNewBuffer: oldCount, minNewCapacity: newCount)
_buffer._arrayOutOfPlaceUpdate(
@@ -877,7 +877,7 @@ extension ArraySlice: RangeReplaceableCollection {
let capacity = _buffer.capacity
_internalInvariant(capacity == 0 || _buffer.isMutableAndUniquelyReferenced())
if _slowPath(oldCount + 1 > capacity) {
if _slowPath(oldCount &+ 1 > capacity) {
_copyToNewBuffer(oldCount: oldCount)
}
}
@@ -889,9 +889,9 @@ extension ArraySlice: RangeReplaceableCollection {
newElement: __owned Element
) {
_internalInvariant(_buffer.isMutableAndUniquelyReferenced())
_internalInvariant(_buffer.capacity >= _buffer.count + 1)
_internalInvariant(_buffer.capacity >= _buffer.count &+ 1)
_buffer.count = oldCount + 1
_buffer.count = oldCount &+ 1
(_buffer.firstElementAddress + oldCount).initialize(to: newElement)
}