Skip to content

Commit dcd8bb8

Browse files
authored
Merge pull request #14941 from airspeedswift/a-a-a-ambiguity-breaker
2 parents d96306f + 7c99c68 commit dcd8bb8

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

stdlib/public/core/RangeReplaceableCollection.swift

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,7 @@ extension RangeReplaceableCollection where Self : BidirectionalCollection {
832832
public mutating func removeLast() -> Element {
833833
_precondition(!isEmpty, "Can't remove last element from an empty collection")
834834
// NOTE if you change this implementation, change popLast above as well
835+
// AND change the tie-breaker implementations in the next extension
835836
if let result = _customRemoveLast() { return result }
836837
return remove(at: index(before: endIndex))
837838
}
@@ -865,10 +866,27 @@ extension RangeReplaceableCollection where Self : BidirectionalCollection {
865866
}
866867
}
867868

868-
// FIXME: swift-3-indexing-model: file a bug for the compiler?
869869
/// Ambiguity breakers.
870870
extension RangeReplaceableCollection
871-
where Self : BidirectionalCollection, SubSequence == Self {
871+
where Self : BidirectionalCollection, SubSequence == Self {
872+
/// Removes and returns the last element of the collection.
873+
///
874+
/// Calling this method may invalidate all saved indices of this
875+
/// collection. Do not rely on a previously stored index value after
876+
/// altering a collection with any operation that can change its length.
877+
///
878+
/// - Returns: The last element of the collection if the collection is not
879+
/// empty; otherwise, `nil`.
880+
///
881+
/// - Complexity: O(1)
882+
@_inlineable
883+
public mutating func popLast() -> Element? {
884+
if isEmpty { return nil }
885+
// duplicate of removeLast logic below, to avoid redundant precondition
886+
if let result = _customRemoveLast() { return result }
887+
return remove(at: index(before: endIndex))
888+
}
889+
872890
/// Removes and returns the last element of the collection.
873891
///
874892
/// The collection must not be empty.
@@ -884,9 +902,8 @@ extension RangeReplaceableCollection
884902
@discardableResult
885903
public mutating func removeLast() -> Element {
886904
_precondition(!isEmpty, "Can't remove last element from an empty collection")
887-
if let result = _customRemoveLast() {
888-
return result
889-
}
905+
// NOTE if you change this implementation, change popLast above as well
906+
if let result = _customRemoveLast() { return result }
890907
return remove(at: index(before: endIndex))
891908
}
892909

validation-test/stdlib/Arrays.swift.gyb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ ArrayTestSuite.test("Native/isEmpty") {
15131513
expectFalse(a.isEmpty)
15141514
}
15151515

1516-
% for Kind in ['Array', 'ContiguousArray']:
1516+
% for Kind in ['Array', 'ContiguousArray', 'ArraySlice']:
15171517
ArrayTestSuite.test("${Kind}/popLast") {
15181518
// Empty
15191519
do {

0 commit comments

Comments
 (0)