Skip to content

Commit d31e012

Browse files
swift-cinatecook1000
authored andcommitted
Merge pull request swiftlang#32451 from stephencelis/patch-1
1 parent 5053380 commit d31e012

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

stdlib/public/core/Collection.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1664,8 +1664,10 @@ extension Collection where SubSequence == Self {
16641664
public mutating func removeFirst(_ k: Int) {
16651665
if k == 0 { return }
16661666
_precondition(k >= 0, "Number of elements to remove should be non-negative")
1667-
_precondition(count >= k,
1668-
"Can't remove more items from a collection than it contains")
1669-
self = self[index(startIndex, offsetBy: k)..<endIndex]
1667+
guard let idx = index(startIndex, offsetBy: k, limitedBy: endIndex) else {
1668+
_preconditionFailure(
1669+
"Can't remove more items from a collection than it contains")
1670+
}
1671+
self = self[idx..<endIndex]
16701672
}
16711673
}

stdlib/public/core/RangeReplaceableCollection.swift

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -591,9 +591,10 @@ extension RangeReplaceableCollection {
591591
public mutating func removeFirst(_ k: Int) {
592592
if k == 0 { return }
593593
_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)
594+
guard let end = index(startIndex, offsetBy: k, limitedBy: endIndex) else {
595+
_preconditionFailure(
596+
"Can't remove more items from a collection than it has")
597+
}
597598
removeSubrange(startIndex..<end)
598599
}
599600

@@ -699,9 +700,11 @@ extension RangeReplaceableCollection where SubSequence == Self {
699700
public mutating func removeFirst(_ k: Int) {
700701
if k == 0 { return }
701702
_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]
703+
guard let idx = index(startIndex, offsetBy: k, limitedBy: endIndex) else {
704+
_preconditionFailure(
705+
"Can't remove more items from a collection than it contains")
706+
}
707+
self = self[idx..<endIndex]
705708
}
706709
}
707710

0 commit comments

Comments
 (0)