Skip to content

Commit 34b9b98

Browse files
committed
[stdlib] Convert existential collection === operator to method
1 parent 32db8f7 commit 34b9b98

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -718,27 +718,25 @@ public func < (lhs: AnyIndex, rhs: AnyIndex) -> Bool {
718718
/// A protocol for `AnyCollection<Element>`,
719719
/// `AnyBidirectionalCollection<Element>`, and
720720
/// `AnyRandomAccessCollection<Element>`.
721-
///
722-
/// This protocol can be considered an implementation detail of the
723-
/// `===` and `!==` implementations for these types.
724721
public protocol AnyCollectionProtocol : Collection {
725722
/// Identifies the underlying collection stored by `self`. Instances
726723
/// copied from one another have the same `_underlyingCollectionID`.
727724
var _underlyingCollectionID: ObjectIdentifier { get }
728-
}
729725

730-
/// Returns `true` iff `lhs` and `rhs` store the same underlying collection.
731-
public func === <
732-
L : AnyCollectionProtocol, R : AnyCollectionProtocol
733-
>(lhs: L, rhs: R) -> Bool {
734-
return lhs._underlyingCollectionID == rhs._underlyingCollectionID
726+
/// Returns `true` iff `self` and `other` use the same `_box` to store their
727+
/// underlying collections.
728+
func _storesSameUnderlyingCollection<
729+
C: AnyCollectionProtocol
730+
>(_ other: C) -> Bool
735731
}
736732

737-
/// Returns `false` iff `lhs` and `rhs` store the same underlying collection.
738-
public func !== <
739-
L : AnyCollectionProtocol, R : AnyCollectionProtocol
740-
>(lhs: L, rhs: R) -> Bool {
741-
return lhs._underlyingCollectionID != rhs._underlyingCollectionID
733+
extension AnyCollectionProtocol {
734+
public // @testable
735+
func _storesSameUnderlyingCollection<
736+
C: AnyCollectionProtocol
737+
>(_ other: C) -> Bool {
738+
return _underlyingCollectionID == other._underlyingCollectionID
739+
}
742740
}
743741

744742
% for (ti, Traversal) in enumerate(TRAVERSALS):
@@ -1003,3 +1001,17 @@ public func anyGenerator<G : IteratorProtocol>(_ base: G) -> AnyIterator<G.Eleme
10031001
public func anyGenerator<Element>(_ body: () -> Element?) -> AnyIterator<Element> {
10041002
Builtin.unreachable()
10051003
}
1004+
1005+
@available(*, unavailable)
1006+
public func === <
1007+
L : AnyCollectionProtocol, R : AnyCollectionProtocol
1008+
>(lhs: L, rhs: R) -> Bool {
1009+
Builtin.unreachable()
1010+
}
1011+
1012+
@available(*, unavailable)
1013+
public func !== <
1014+
L : AnyCollectionProtocol, R : AnyCollectionProtocol
1015+
>(lhs: L, rhs: R) -> Bool {
1016+
Builtin.unreachable()
1017+
}

validation-test/stdlib/ExistentialCollection.swift.gyb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,13 @@ tests.test("BidirectionalCollection") {
357357
let bc0_ = AnyBidirectionalCollection(fc0) // upgrade!
358358
expectNotEmpty(bc0_)
359359
let bc0 = bc0_!
360-
expectTrue(fc0 === bc0)
360+
expectTrue(fc0._storesSameUnderlyingCollection(bc0))
361361

362362
let fc1 = AnyCollection(a0.lazy.reversed()) // new collection
363-
expectFalse(fc1 === fc0)
363+
expectFalse(fc1._storesSameUnderlyingCollection(fc0))
364364

365365
let fc2 = AnyCollection(bc0) // downgrade
366-
expectTrue(fc2 === bc0)
366+
expectTrue(fc2._storesSameUnderlyingCollection(bc0))
367367

368368
let a1 = ContiguousArray(bc0.lazy.reversed())
369369
expectEqual(a0, a1)
@@ -381,7 +381,7 @@ tests.test("BidirectionalCollection") {
381381
let s0 = "Hello, Woyld".characters
382382
let bc1 = AnyBidirectionalCollection(s0)
383383
let fc3 = AnyCollection(bc1)
384-
expectTrue(fc3 === bc1)
384+
expectTrue(fc3._storesSameUnderlyingCollection(bc1))
385385
expectEmpty(AnyRandomAccessCollection(bc1))
386386
expectEmpty(AnyRandomAccessCollection(fc3))
387387
}
@@ -392,13 +392,13 @@ tests.test("RandomAccessCollection") {
392392
let rc0_ = AnyRandomAccessCollection(fc0) // upgrade!
393393
expectNotEmpty(rc0_)
394394
let rc0 = rc0_!
395-
expectTrue(rc0 === fc0)
395+
expectTrue(rc0._storesSameUnderlyingCollection(fc0))
396396

397397
let bc1 = AnyBidirectionalCollection(rc0) // downgrade
398-
expectTrue(bc1 === rc0)
398+
expectTrue(bc1._storesSameUnderlyingCollection(rc0))
399399

400400
let fc1 = AnyBidirectionalCollection(rc0) // downgrade
401-
expectTrue(fc1 === rc0)
401+
expectTrue(fc1._storesSameUnderlyingCollection(rc0))
402402

403403
let a1 = ContiguousArray(rc0.lazy.reversed())
404404
expectEqual(a0, a1)

0 commit comments

Comments
 (0)