mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +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:
@@ -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)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user