Skip to content

Commit e657ece

Browse files
authored
Merge pull request #3617 from natecook1000/nc-existenz-identity
2 parents 861c426 + 36e37ce commit e657ece

File tree

3 files changed

+41
-37
lines changed

3 files changed

+41
-37
lines changed

stdlib/public/core/ExistentialCollection.swift.gyb

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -720,30 +720,11 @@ extension AnyIndex : Comparable {
720720
//===--- Collections ------------------------------------------------------===//
721721
//===----------------------------------------------------------------------===//
722722

723-
/// A protocol for `AnyCollection<Element>`,
724-
/// `AnyBidirectionalCollection<Element>`, and
725-
/// `AnyRandomAccessCollection<Element>`.
726-
///
727-
/// This protocol can be considered an implementation detail of the
728-
/// `===` and `!==` implementations for these types.
729-
public protocol AnyCollectionProtocol : Collection {
723+
public // @testable
724+
protocol _AnyCollectionProtocol : Collection {
730725
/// Identifies the underlying collection stored by `self`. Instances
731-
/// copied from one another have the same `_underlyingCollectionID`.
732-
var _underlyingCollectionID: ObjectIdentifier { get }
733-
}
734-
735-
/// Returns `true` iff `lhs` and `rhs` store the same underlying collection.
736-
public func === <
737-
L : AnyCollectionProtocol, R : AnyCollectionProtocol
738-
>(lhs: L, rhs: R) -> Bool {
739-
return lhs._underlyingCollectionID == rhs._underlyingCollectionID
740-
}
741-
742-
/// Returns `false` iff `lhs` and `rhs` store the same underlying collection.
743-
public func !== <
744-
L : AnyCollectionProtocol, R : AnyCollectionProtocol
745-
>(lhs: L, rhs: R) -> Bool {
746-
return lhs._underlyingCollectionID != rhs._underlyingCollectionID
726+
/// copied or upgraded/downgraded from one another have the same `_boxID`.
727+
var _boxID: ObjectIdentifier { get }
747728
}
748729

749730
% for (ti, Traversal) in enumerate(TRAVERSALS):
@@ -758,7 +739,7 @@ public func !== <
758739
///
759740
/// - SeeAlso: ${', '.join('`Any%sCollection`' % t for t in (2 * TRAVERSALS)[ti + 1 : ti + 3]) }
760741
public struct ${Self}<Element>
761-
: AnyCollectionProtocol, ${SelfProtocol} {
742+
: _AnyCollectionProtocol, ${SelfProtocol} {
762743

763744
internal init(_box: _${Self}Box<Element>) {
764745
self._box = _box
@@ -955,7 +936,7 @@ public struct ${Self}<Element>
955936

956937
/// Uniquely identifies the stored underlying collection.
957938
public // Due to language limitations only
958-
var _underlyingCollectionID: ObjectIdentifier {
939+
var _boxID: ObjectIdentifier {
959940
return ObjectIdentifier(_box)
960941
}
961942

@@ -983,10 +964,13 @@ extension Any${Kind} {
983964
}
984965
%end
985966

986-
@available(*, unavailable, renamed: "AnyCollectionProtocol")
987-
public typealias AnyCollectionType = AnyCollectionProtocol
967+
@available(*, unavailable, renamed: "_AnyCollectionProtocol")
968+
public typealias AnyCollectionType = _AnyCollectionProtocol
969+
970+
@available(*, unavailable, renamed: "_AnyCollectionProtocol")
971+
public typealias AnyCollectionProtocol = _AnyCollectionProtocol
988972

989-
extension AnyCollectionProtocol {
973+
extension _AnyCollectionProtocol {
990974
@available(*, unavailable, renamed: "makeIterator()")
991975
public func generate() -> AnyIterator<Iterator.Element> {
992976
Builtin.unreachable()
@@ -1008,3 +992,17 @@ public func anyGenerator<G : IteratorProtocol>(_ base: G) -> AnyIterator<G.Eleme
1008992
public func anyGenerator<Element>(_ body: () -> Element?) -> AnyIterator<Element> {
1009993
Builtin.unreachable()
1010994
}
995+
996+
@available(*, unavailable)
997+
public func === <
998+
L : _AnyCollectionProtocol, R : _AnyCollectionProtocol
999+
>(lhs: L, rhs: R) -> Bool {
1000+
Builtin.unreachable()
1001+
}
1002+
1003+
@available(*, unavailable)
1004+
public func !== <
1005+
L : _AnyCollectionProtocol, R : _AnyCollectionProtocol
1006+
>(lhs: L, rhs: R) -> Bool {
1007+
Builtin.unreachable()
1008+
}

test/1_stdlib/Renames.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ func _ErrorType() {
110110

111111
func _ExistentialCollection<T>(i: AnyIterator<T>) {
112112
func fn1<T>(_: AnyGenerator<T>) {} // expected-error {{'AnyGenerator' has been renamed to 'AnyIterator'}} {{18-30=AnyIterator}} {{none}}
113-
func fn2<T : AnyCollectionType>(_: T) {} // expected-error {{'AnyCollectionType' has been renamed to 'AnyCollectionProtocol'}} {{16-33=AnyCollectionProtocol}} {{none}}
113+
func fn2<T : AnyCollectionType>(_: T) {} // expected-error {{'AnyCollectionType' has been renamed to '_AnyCollectionProtocol'}} {{16-33=_AnyCollectionProtocol}} {{none}}
114114
func fn3(_: AnyForwardIndex) {} // expected-error {{'AnyForwardIndex' has been renamed to 'AnyIndex'}} {{15-30=AnyIndex}} {{none}}
115115
func fn4(_: AnyBidirectionalIndex) {} // expected-error {{'AnyBidirectionalIndex' has been renamed to 'AnyIndex'}} {{15-36=AnyIndex}} {{none}}
116116
func fn5(_: AnyRandomAccessIndex) {} // expected-error {{'AnyRandomAccessIndex' has been renamed to 'AnyIndex'}} {{15-35=AnyIndex}} {{none}}
@@ -130,7 +130,7 @@ func _ExistentialCollection<T>(c: AnyBidirectionalCollection<T>) {
130130
func _ExistentialCollection<T>(c: AnyRandomAccessCollection<T>) {
131131
_ = c.underestimateCount() // expected-error {{'underestimateCount()' is unavailable: Please use underestimatedCount property instead.}} {{none}}
132132
}
133-
func _ExistentialCollection<C : AnyCollectionProtocol>(c: C) {
133+
func _ExistentialCollection<C : _AnyCollectionProtocol>(c: C) {
134134
_ = c.generate() // expected-error {{'generate()' has been renamed to 'makeIterator()'}} {{9-17=makeIterator}} {{none}}
135135
}
136136

validation-test/stdlib/ExistentialCollection.swift.gyb

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ extension AnyRandomAccessCollection where Element : TestProtocol1 {
5757
}
5858
}
5959

60+
func storesSameUnderlyingCollection<
61+
L: _AnyCollectionProtocol, R: _AnyCollectionProtocol
62+
>(_ lhs: L, _ rhs: R) -> Bool {
63+
return lhs._boxID == rhs._boxID
64+
}
65+
6066
var tests = TestSuite("ExistentialCollection")
6167

6268
tests.test("AnyIterator") {
@@ -357,13 +363,13 @@ tests.test("BidirectionalCollection") {
357363
let bc0_ = AnyBidirectionalCollection(fc0) // upgrade!
358364
expectNotEmpty(bc0_)
359365
let bc0 = bc0_!
360-
expectTrue(fc0 === bc0)
366+
expectTrue(storesSameUnderlyingCollection(fc0, bc0))
361367

362368
let fc1 = AnyCollection(a0.lazy.reversed()) // new collection
363-
expectFalse(fc1 === fc0)
369+
expectFalse(storesSameUnderlyingCollection(fc1, fc0))
364370

365371
let fc2 = AnyCollection(bc0) // downgrade
366-
expectTrue(fc2 === bc0)
372+
expectTrue(storesSameUnderlyingCollection(fc2, bc0))
367373

368374
let a1 = ContiguousArray(bc0.lazy.reversed())
369375
expectEqual(a0, a1)
@@ -381,7 +387,7 @@ tests.test("BidirectionalCollection") {
381387
let s0 = "Hello, Woyld".characters
382388
let bc1 = AnyBidirectionalCollection(s0)
383389
let fc3 = AnyCollection(bc1)
384-
expectTrue(fc3 === bc1)
390+
expectTrue(storesSameUnderlyingCollection(fc3, bc1))
385391
expectEmpty(AnyRandomAccessCollection(bc1))
386392
expectEmpty(AnyRandomAccessCollection(fc3))
387393
}
@@ -392,13 +398,13 @@ tests.test("RandomAccessCollection") {
392398
let rc0_ = AnyRandomAccessCollection(fc0) // upgrade!
393399
expectNotEmpty(rc0_)
394400
let rc0 = rc0_!
395-
expectTrue(rc0 === fc0)
401+
expectTrue(storesSameUnderlyingCollection(rc0, fc0))
396402

397403
let bc1 = AnyBidirectionalCollection(rc0) // downgrade
398-
expectTrue(bc1 === rc0)
404+
expectTrue(storesSameUnderlyingCollection(bc1, rc0))
399405

400406
let fc1 = AnyBidirectionalCollection(rc0) // downgrade
401-
expectTrue(fc1 === rc0)
407+
expectTrue(storesSameUnderlyingCollection(fc1, rc0))
402408

403409
let a1 = ContiguousArray(rc0.lazy.reversed())
404410
expectEqual(a0, a1)

0 commit comments

Comments
 (0)