diff --git a/stdlib/public/core/Collection.swift b/stdlib/public/core/Collection.swift index 1db572b65d06c..8badbf020b404 100644 --- a/stdlib/public/core/Collection.swift +++ b/stdlib/public/core/Collection.swift @@ -1664,8 +1664,10 @@ extension Collection where SubSequence == Self { public mutating func removeFirst(_ k: Int) { if k == 0 { return } _precondition(k >= 0, "Number of elements to remove should be non-negative") - _precondition(count >= k, - "Can't remove more items from a collection than it contains") - self = self[index(startIndex, offsetBy: k)..= 0, "Number of elements to remove should be non-negative") - _precondition(count >= k, - "Can't remove more items from a collection than it has") - let end = index(startIndex, offsetBy: k) + guard let end = index(startIndex, offsetBy: k, limitedBy: endIndex) else { + _preconditionFailure( + "Can't remove more items from a collection than it has") + } removeSubrange(startIndex..= 0, "Number of elements to remove should be non-negative") - _precondition(count >= k, - "Can't remove more items from a collection than it contains") - self = self[index(startIndex, offsetBy: k)..