@@ -285,12 +285,17 @@ public protocol RangeReplaceableCollection: Collection
285285 /// Customization point for `removeLast()`. Implement this function if you
286286 /// want to replace the default implementation.
287287 ///
288+ /// The collection must not be empty.
289+ ///
288290 /// - Returns: A non-nil value if the operation was performed.
289291 mutating func _customRemoveLast( ) -> Element ?
290292
291293 /// Customization point for `removeLast(_:)`. Implement this function if you
292294 /// want to replace the default implementation.
293295 ///
296+ /// - Parameter n: The number of elements to remove from the collection.
297+ /// `n` must be greater than or equal to zero and must not exceed the
298+ /// number of elements in the collection.
294299 /// - Returns: `true` if the operation was performed.
295300 mutating func _customRemoveLast( _ n: Int ) -> Bool
296301
@@ -591,9 +596,10 @@ extension RangeReplaceableCollection {
591596 public mutating func removeFirst( _ k: Int ) {
592597 if k == 0 { return }
593598 _precondition ( k >= 0 , " Number of elements to remove should be non-negative " )
594- _precondition ( count >= k,
595- " Can't remove more items from a collection than it has " )
596- let end = index ( startIndex, offsetBy: k)
599+ guard let end = index ( startIndex, offsetBy: k, limitedBy: endIndex) else {
600+ _preconditionFailure (
601+ " Can't remove more items from a collection than it has " )
602+ }
597603 removeSubrange ( startIndex..< end)
598604 }
599605
@@ -699,9 +705,11 @@ extension RangeReplaceableCollection where SubSequence == Self {
699705 public mutating func removeFirst( _ k: Int ) {
700706 if k == 0 { return }
701707 _precondition ( k >= 0 , " Number of elements to remove should be non-negative " )
702- _precondition ( count >= k,
703- " Can't remove more items from a collection than it contains " )
704- self = self [ index ( startIndex, offsetBy: k) ..< endIndex]
708+ guard let idx = index ( startIndex, offsetBy: k, limitedBy: endIndex) else {
709+ _preconditionFailure (
710+ " Can't remove more items from a collection than it contains " )
711+ }
712+ self = self [ idx..< endIndex]
705713 }
706714}
707715
@@ -800,7 +808,12 @@ extension RangeReplaceableCollection
800808
801809 @inlinable
802810 public mutating func _customRemoveLast( _ n: Int ) -> Bool {
803- self = self [ startIndex..< index ( endIndex, offsetBy: numericCast ( - n) ) ]
811+ guard let end = index ( endIndex, offsetBy: - n, limitedBy: startIndex)
812+ else {
813+ _preconditionFailure (
814+ " Can't remove more items from a collection than it contains " )
815+ }
816+ self = self [ startIndex..< end]
804817 return true
805818 }
806819}
@@ -864,13 +877,17 @@ extension RangeReplaceableCollection where Self: BidirectionalCollection {
864877 public mutating func removeLast( _ k: Int ) {
865878 if k == 0 { return }
866879 _precondition ( k >= 0 , " Number of elements to remove should be non-negative " )
867- _precondition ( count >= k,
868- " Can't remove more items from a collection than it contains " )
869880 if _customRemoveLast ( k) {
870881 return
871882 }
872883 let end = endIndex
873- removeSubrange ( index ( end, offsetBy: - k) ..< end)
884+ guard let start = index ( end, offsetBy: - k, limitedBy: startIndex)
885+ else {
886+ _preconditionFailure (
887+ " Can't remove more items from a collection than it contains " )
888+ }
889+
890+ removeSubrange ( start..< end)
874891 }
875892}
876893
@@ -934,13 +951,16 @@ where Self: BidirectionalCollection, SubSequence == Self {
934951 public mutating func removeLast( _ k: Int ) {
935952 if k == 0 { return }
936953 _precondition ( k >= 0 , " Number of elements to remove should be non-negative " )
937- _precondition ( count >= k,
938- " Can't remove more items from a collection than it contains " )
939954 if _customRemoveLast ( k) {
940955 return
941956 }
942957 let end = endIndex
943- removeSubrange ( index ( end, offsetBy: - k) ..< end)
958+ guard let start = index ( end, offsetBy: - k, limitedBy: startIndex)
959+ else {
960+ _preconditionFailure (
961+ " Can't remove more items from a collection than it contains " )
962+ }
963+ removeSubrange ( start..< end)
944964 }
945965}
946966
0 commit comments