Skip to content

Commit 02aef12

Browse files
authored
Merge pull request #15815 from Moximillian/SR-7266-avoid-nesting-reversed
SR-7266: Avoid nesting of ReversedCollection
2 parents 3b45ce6 + 4714952 commit 02aef12

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

stdlib/public/core/Reverse.swift

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ extension MutableCollection where Self: BidirectionalCollection {
4949
/// * `c.reversed()` does not create new storage
5050
/// * `c.reversed().map(f)` maps eagerly and returns a new array
5151
/// * `c.lazy.reversed().map(f)` maps lazily and returns a `LazyMapCollection`
52-
///
53-
/// - See also: `ReversedRandomAccessCollection`
5452
@_fixed_layout
5553
public struct ReversedCollection<Base: BidirectionalCollection> {
5654
public let _base: Base
@@ -84,7 +82,7 @@ extension ReversedCollection {
8482
}
8583
}
8684
}
87-
85+
8886
extension ReversedCollection.Iterator: IteratorProtocol, Sequence {
8987
public typealias Element = Base.Element
9088

@@ -253,6 +251,17 @@ extension ReversedCollection: BidirectionalCollection {
253251

254252
extension ReversedCollection: RandomAccessCollection where Base: RandomAccessCollection { }
255253

254+
extension ReversedCollection {
255+
/// Reversing a reversed collection returns the original collection.
256+
///
257+
/// - Complexity: O(1)
258+
@inlinable
259+
@available(swift, introduced: 4.2)
260+
public func reversed() -> Base {
261+
return _base
262+
}
263+
}
264+
256265
extension BidirectionalCollection {
257266
/// Returns a view presenting the elements of the collection in reverse
258267
/// order.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift %s -o %t/a.out3 -swift-version 3 && %target-run %t/a.out3
3+
// RUN: %target-build-swift %s -o %t/a.out4 -swift-version 4 && %target-run %t/a.out4
4+
// RUN: %target-build-swift %s -o %t/a.out5 -swift-version 5 && %target-run %t/a.out5
5+
// REQUIRES: executable_test
6+
7+
import StdlibUnittest
8+
9+
#if swift(>=4.2)
10+
let swiftVersion = ">=4.2"
11+
#else
12+
let swiftVersion = "<4.2"
13+
#endif
14+
15+
let tests = TestSuite("ReverseCompatibility")
16+
17+
tests.test("Double reverse type/Collection/\(swiftVersion)") {
18+
func reverse<C : BidirectionalCollection>(_ xs: C) {
19+
var result = xs.reversed().reversed()
20+
#if swift(>=4.2)
21+
expectType(C.self, &result)
22+
#else
23+
expectType(ReversedCollection<ReversedCollection<C>>.self, &result)
24+
#endif
25+
}
26+
reverse(Array(0..<10))
27+
28+
func backwardCompatible<C : BidirectionalCollection>(_ xs: C) {
29+
typealias ExpectedType = ReversedCollection<ReversedCollection<C>>
30+
var result: ExpectedType = xs.reversed().reversed()
31+
expectType(ExpectedType.self, &result)
32+
}
33+
backwardCompatible(Array(0..<10))
34+
}
35+
36+
runAllTests()

0 commit comments

Comments
 (0)