mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
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:
@@ -724,7 +724,7 @@ extension ContiguousArray: 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(
|
||||
@@ -736,7 +736,7 @@ extension ContiguousArray: RangeReplaceableCollection {
|
||||
internal mutating func _makeUniqueAndReserveCapacityIfNotUnique() {
|
||||
if _slowPath(!_buffer.beginCOWMutation()) {
|
||||
_createNewBuffer(bufferIsUnique: false,
|
||||
minimumCapacity: count + 1,
|
||||
minimumCapacity: count &+ 1,
|
||||
growForAppend: true)
|
||||
}
|
||||
}
|
||||
@@ -756,9 +756,9 @@ extension ContiguousArray: RangeReplaceableCollection {
|
||||
let capacity = _buffer.mutableCapacity
|
||||
_internalInvariant(capacity == 0 || _buffer.isMutableAndUniquelyReferenced())
|
||||
|
||||
if _slowPath(oldCount + 1 > capacity) {
|
||||
if _slowPath(oldCount &+ 1 > capacity) {
|
||||
_createNewBuffer(bufferIsUnique: capacity > 0,
|
||||
minimumCapacity: oldCount + 1,
|
||||
minimumCapacity: oldCount &+ 1,
|
||||
growForAppend: true)
|
||||
}
|
||||
}
|
||||
@@ -770,9 +770,9 @@ extension ContiguousArray: RangeReplaceableCollection {
|
||||
newElement: __owned Element
|
||||
) {
|
||||
_internalInvariant(_buffer.isMutableAndUniquelyReferenced())
|
||||
_internalInvariant(_buffer.mutableCapacity >= _buffer.mutableCount + 1)
|
||||
_internalInvariant(_buffer.mutableCapacity >= _buffer.mutableCount &+ 1)
|
||||
|
||||
_buffer.mutableCount = oldCount + 1
|
||||
_buffer.mutableCount = oldCount &+ 1
|
||||
(_buffer.mutableFirstElementAddress + oldCount).initialize(to: newElement)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user