Skip to content

Commit 7675e12

Browse files
authored
Merge pull request #14859 from moiseev/filter-filter-compat
[stdlib] Source compatibility fix for lazy filter-filter and map-map cases
2 parents 30b5453 + 661f56d commit 7675e12

File tree

5 files changed

+105
-67
lines changed

5 files changed

+105
-67
lines changed

stdlib/public/core/Filter.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ extension LazyCollectionProtocol {
385385
}
386386

387387
extension LazyFilterSequence {
388+
@available(swift, introduced: 5)
388389
public func filter(
389390
_ isIncluded: @escaping (Element) -> Bool
390391
) -> LazyFilterSequence<Base> {
@@ -395,6 +396,7 @@ extension LazyFilterSequence {
395396
}
396397

397398
extension LazyFilterCollection {
399+
@available(swift, introduced: 5)
398400
public func filter(
399401
_ isIncluded: @escaping (Element) -> Bool
400402
) -> LazyFilterCollection<Base> {

stdlib/public/core/Map.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ extension LazyMapCollection {
278278

279279
extension LazyMapSequence {
280280
@_inlineable
281+
@available(swift, introduced: 5)
281282
public func map<ElementOfResult>(
282283
_ transform: @escaping (Element) -> ElementOfResult
283284
) -> LazyMapSequence<Base, ElementOfResult> {
@@ -289,6 +290,7 @@ extension LazyMapSequence {
289290

290291
extension LazyMapCollection {
291292
@_inlineable
293+
@available(swift, introduced: 5)
292294
public func map<ElementOfResult>(
293295
_ transform: @escaping (Element) -> ElementOfResult
294296
) -> LazyMapCollection<Base, ElementOfResult> {

test/stdlib/Filter.swift

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -73,36 +73,4 @@ FilterTests.test("single-count") {
7373
expectEqual(30, count)
7474
}
7575

76-
FilterTests.test("Double filter type/Sequence") {
77-
func foldingLevels<S : Sequence>(_ xs: S) {
78-
var result = xs.lazy.filter { _ in true }.filter { _ in true }
79-
expectType(LazyFilterSequence<S>.self, &result)
80-
}
81-
foldingLevels(Array(0..<10))
82-
83-
func backwardCompatible<S : Sequence>(_ xs: S) {
84-
typealias ExpectedType = LazyFilterSequence<LazyFilterSequence<S>>
85-
var result: ExpectedType = xs.lazy
86-
.filter { _ in true }.filter { _ in true }
87-
expectType(ExpectedType.self, &result)
88-
}
89-
backwardCompatible(Array(0..<10))
90-
}
91-
92-
FilterTests.test("Double filter type/Collection") {
93-
func foldingLevels<C : Collection>(_ xs: C) {
94-
var result = xs.lazy.filter { _ in true }.filter { _ in true }
95-
expectType(LazyFilterCollection<C>.self, &result)
96-
}
97-
foldingLevels(Array(0..<10))
98-
99-
func backwardCompatible<C : Collection>(_ xs: C) {
100-
typealias ExpectedType = LazyFilterCollection<LazyFilterCollection<C>>
101-
var result: ExpectedType = xs.lazy
102-
.filter { _ in true }.filter { _ in true }
103-
expectType(ExpectedType.self, &result)
104-
}
105-
backwardCompatible(Array(0..<10))
106-
}
107-
10876
runAllTests()

test/stdlib/Map.swift

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -100,40 +100,5 @@ print(Array(m1))
100100
// CHECK-NEXT: [2, 4, 6, 8]
101101
print(Array(m1))
102102

103-
// lazy.map.map chain should fold two layers of LazyMapSequence
104-
func foldingLevelsSequence<S : Sequence>(_ xs: S) {
105-
let result = xs.lazy.map { $0 }.map { $0 }
106-
print(type(of: result))
107-
}
108-
// CHECK-NEXT: LazyMapSequence<Array<Int>, Int>
109-
foldingLevelsSequence(Array(0..<10))
110-
111-
// ... but the old way should also be available given explicit type context
112-
func backwardCompatibleSequence<S : Sequence>(_ xs: S) {
113-
typealias ExpectedType = LazyMapSequence<LazyMapSequence<S, S.Element>, S.Element>
114-
let result: ExpectedType = xs.lazy.map { $0 }.map { $0 }
115-
print(type(of: result))
116-
}
117-
// CHECK-NEXT: LazyMapSequence<LazyMapSequence<Array<Int>, Int>, Int>
118-
backwardCompatibleSequence(Array(0..<10))
119-
120-
// lazy.map.map chain should fold two layers of LazyMapCollection
121-
func foldingLevelsCollection<C : Collection>(_ xs: C) {
122-
let result = xs.lazy.map { $0 }.map { $0 }
123-
print(type(of: result))
124-
}
125-
// CHECK-NEXT: LazyMapCollection<Array<Int>, Int>
126-
foldingLevelsCollection(Array(0..<10))
127-
128-
// ... but the old way should also be available given explicit type context
129-
func backwardCompatibleCollection<C : Collection>(_ xs: C) {
130-
typealias ExpectedType =
131-
LazyMapCollection<LazyMapCollection<C, C.Element>, C.Element>
132-
let result: ExpectedType = xs.lazy.map { $0 }.map { $0 }
133-
print(type(of: result))
134-
}
135-
// CHECK-NEXT: LazyMapCollection<LazyMapCollection<Array<Int>, Int>, Int>
136-
backwardCompatibleCollection(Array(0..<10))
137-
138103
// CHECK-NEXT: all done.
139104
print("all done.")
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// RUN: rm -rf %t ; mkdir -p %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(>=5)
10+
let swiftVersion = ">=5"
11+
#else
12+
let swiftVersion = "<5"
13+
#endif
14+
15+
let tests = TestSuite("MapFilterLayerFoldingCompatibility")
16+
17+
tests.test("Double filter type/Sequence/\(swiftVersion)") {
18+
func foldingLevels<S : Sequence>(_ xs: S) {
19+
var result = xs.lazy.filter { _ in true }.filter { _ in true }
20+
#if swift(>=5)
21+
expectType(LazyFilterSequence<S>.self, &result)
22+
#else
23+
expectType(LazyFilterSequence<LazyFilterSequence<S>>.self, &result)
24+
#endif
25+
}
26+
foldingLevels(Array(0..<10))
27+
28+
func backwardCompatible<S : Sequence>(_ xs: S) {
29+
typealias ExpectedType = LazyFilterSequence<LazyFilterSequence<S>>
30+
var result: ExpectedType = xs.lazy
31+
.filter { _ in true }.filter { _ in true }
32+
expectType(ExpectedType.self, &result)
33+
}
34+
backwardCompatible(Array(0..<10))
35+
}
36+
37+
tests.test("Double filter type/Collection/\(swiftVersion)") {
38+
func foldingLevels<C : Collection>(_ xs: C) {
39+
var result = xs.lazy.filter { _ in true }.filter { _ in true }
40+
#if swift(>=5)
41+
expectType(LazyFilterCollection<C>.self, &result)
42+
#else
43+
expectType(LazyFilterCollection<LazyFilterCollection<C>>.self, &result)
44+
#endif
45+
}
46+
foldingLevels(Array(0..<10))
47+
48+
func backwardCompatible<C : Collection>(_ xs: C) {
49+
typealias ExpectedType = LazyFilterCollection<LazyFilterCollection<C>>
50+
var result: ExpectedType = xs.lazy
51+
.filter { _ in true }.filter { _ in true }
52+
expectType(ExpectedType.self, &result)
53+
}
54+
backwardCompatible(Array(0..<10))
55+
}
56+
57+
tests.test("Double map type/Sequence/\(swiftVersion)") {
58+
func foldingLevels<S : Sequence>(_ xs: S) {
59+
var result = xs.lazy.map { $0 }.map { $0 }
60+
#if swift(>=5)
61+
expectType(LazyMapSequence<S, S.Element>.self, &result)
62+
#else
63+
expectType(
64+
LazyMapSequence<LazyMapSequence<S, S.Element>, S.Element>.self,
65+
&result)
66+
#endif
67+
}
68+
foldingLevels(Array(0..<10))
69+
70+
func backwardCompatible<S : Sequence>(_ xs: S) {
71+
typealias ExpectedType =
72+
LazyMapSequence<LazyMapSequence<S, S.Element>, S.Element>
73+
var result: ExpectedType = xs.lazy.map { $0 }.map { $0 }
74+
expectType(ExpectedType.self, &result)
75+
}
76+
backwardCompatible(Array(0..<10))
77+
}
78+
79+
tests.test("Double map type/Collection/\(swiftVersion)") {
80+
func foldingLevels<C : Collection>(_ xs: C) {
81+
var result = xs.lazy.map { $0 }.map { $0 }
82+
#if swift(>=5)
83+
expectType(LazyMapCollection<C, C.Element>.self, &result)
84+
#else
85+
expectType(
86+
LazyMapCollection<LazyMapCollection<C, C.Element>, C.Element>.self,
87+
&result)
88+
#endif
89+
}
90+
foldingLevels(Array(0..<10))
91+
92+
func backwardCompatible<C : Collection>(_ xs: C) {
93+
typealias ExpectedType =
94+
LazyMapCollection<LazyMapCollection<C, C.Element>, C.Element>
95+
var result: ExpectedType = xs.lazy.map { $0 }.map { $0 }
96+
expectType(ExpectedType.self, &result)
97+
}
98+
backwardCompatible(Array(0..<10))
99+
}
100+
101+
runAllTests()

0 commit comments

Comments
 (0)