Skip to content

Commit ed70f30

Browse files
committed
[cxx-interop] Make Element a primary associated type of CxxSequence etc
1 parent b350f2b commit ed70f30

File tree

4 files changed

+14
-7
lines changed

4 files changed

+14
-7
lines changed

lib/ClangImporter/ClangDerivedConformances.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ void swift::conformToCxxSequenceIfNeeded(
500500
},
501501
LookUpConformanceInModule(module));
502502

503+
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Element"), pointeeTy);
503504
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Index"), indexTy);
504505
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Indices"), indicesTy);
505506
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("SubSequence"),
@@ -516,9 +517,11 @@ void swift::conformToCxxSequenceIfNeeded(
516517
// copy of the sequence's elements) by conforming the type to
517518
// CxxCollectionConvertible. This enables an overload of Array.init declared
518519
// in the Cxx module.
519-
if (!conformedToRAC && cxxConvertibleProto)
520+
if (!conformedToRAC && cxxConvertibleProto) {
521+
impl.addSynthesizedTypealias(decl, ctx.getIdentifier("Element"), pointeeTy);
520522
impl.addSynthesizedProtocolAttrs(
521523
decl, {KnownProtocolKind::CxxConvertibleToCollection});
524+
}
522525
}
523526

524527
void swift::conformToCxxSetIfNeeded(ClangImporter::Implementation &impl,

stdlib/public/Cxx/CxxConvertibleToCollection.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111
//===----------------------------------------------------------------------===//
1212

1313
/// A C++ type that can be converted to a Swift collection.
14-
public protocol CxxConvertibleToCollection {
14+
public protocol CxxConvertibleToCollection<Element> {
15+
associatedtype Element
1516
associatedtype RawIterator: UnsafeCxxInputIterator
17+
where RawIterator.Pointee == Element
1618

1719
/// Do not implement this function manually in Swift.
1820
mutating func __beginUnsafe() -> RawIterator

stdlib/public/Cxx/CxxRandomAccessCollection.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@ extension UnsafePointer: UnsafeCxxRandomAccessIterator {}
2828

2929
extension UnsafeMutablePointer: UnsafeCxxRandomAccessIterator {}
3030

31-
public protocol CxxRandomAccessCollection: CxxSequence, RandomAccessCollection {
31+
public protocol CxxRandomAccessCollection<Element>: CxxSequence, RandomAccessCollection {
32+
override associatedtype Element
3233
override associatedtype RawIterator: UnsafeCxxRandomAccessIterator
34+
where RawIterator.Pointee == Element
3335
override associatedtype Iterator = CxxIterator<Self>
34-
override associatedtype Element = RawIterator.Pointee
3536
override associatedtype Index = Int
3637
override associatedtype Indices = Range<Int>
3738
override associatedtype SubSequence = Slice<Self>
@@ -67,7 +68,7 @@ extension CxxRandomAccessCollection {
6768
// Not using CxxIterator here to avoid making a copy of the collection.
6869
var rawIterator = __beginUnsafe()
6970
rawIterator += RawIterator.Distance(index)
70-
yield rawIterator.pointee as! Element
71+
yield rawIterator.pointee
7172
}
7273
}
7374
}

stdlib/public/Cxx/CxxSequence.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,10 @@ extension Optional: UnsafeCxxInputIterator where Wrapped: UnsafeCxxInputIterator
6565
/// This requires the C++ sequence type to define const methods `begin()` and
6666
/// `end()` which return input iterators into the C++ sequence. The iterator
6767
/// types must conform to `UnsafeCxxInputIterator`.
68-
public protocol CxxSequence: Sequence {
68+
public protocol CxxSequence<Element>: Sequence {
69+
override associatedtype Element
6970
associatedtype RawIterator: UnsafeCxxInputIterator
70-
override associatedtype Element = RawIterator.Pointee
71+
where RawIterator.Pointee == Element
7172
override associatedtype Iterator = CxxIterator<Self>
7273

7374
// `begin()` and `end()` have to be mutating, otherwise calling

0 commit comments

Comments
 (0)