Skip to content

[stdlib][WIP] Recursive where clause constraints #9370

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions benchmark/single-source/PopFrontGeneric.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ let arrayCount = 1024
// being really slow).
@_versioned
protocol MyArrayBufferProtocol : MutableCollection, RandomAccessCollection {
associatedtype Element

mutating func myReplace<C>(
_ subRange: Range<Int>,
with newValues: C
Expand Down
2 changes: 1 addition & 1 deletion lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6771,7 +6771,7 @@ bool FailureDiagnosis::visitArrayExpr(ArrayExpr *E) {
contextualElementType = ProtocolConformanceRef::getTypeWitnessByName(
contextualType,
*Conformance,
CS->getASTContext().Id_Element,
CS->getASTContext().getIdentifier("ArrayLiteralElement"),
&CS->TC)
->getDesugaredType();
elementTypePurpose = CTP_ArrayElement;
Expand Down
7 changes: 2 additions & 5 deletions stdlib/public/core/ArrayBufferProtocol.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ internal protocol _ArrayBufferProtocol

associatedtype Indices : RandomAccessCollection = CountableRange<Int>

/// The type of elements stored in the buffer.
associatedtype Element

/// Create an empty buffer.
init()

Expand Down Expand Up @@ -76,7 +73,7 @@ internal protocol _ArrayBufferProtocol
_ subrange: Range<Int>,
with newCount: Int,
elementsOf newValues: C
) where C : Collection, C.Iterator.Element == Element
) where C : Collection, C.Element == Element

/// Returns a `_SliceBuffer` containing the elements in `bounds`.
subscript(bounds: Range<Int>) -> _SliceBuffer<Element> { get }
Expand Down Expand Up @@ -141,7 +138,7 @@ extension _ArrayBufferProtocol {
_ subrange: Range<Int>,
with newCount: Int,
elementsOf newValues: C
) where C : Collection, C.Iterator.Element == Element {
) where C : Collection, C.Element == Element {
_sanityCheck(startIndex == 0, "_SliceBuffer should override this function.")
let oldCount = self.count
let eraseCount = subrange.count
Expand Down
6 changes: 3 additions & 3 deletions stdlib/public/core/ArrayType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ internal protocol _ArrayProtocol
/// element. Otherwise, `nil`.
var _baseAddressIfContiguous: UnsafeMutablePointer<Element>? { get }

subscript(index: Int) -> Iterator.Element { get set }
subscript(index: Int) -> Element { get set }

//===--- basic mutations ------------------------------------------------===//

Expand All @@ -50,7 +50,7 @@ internal protocol _ArrayProtocol
/// - Complexity: O(`self.count`).
///
/// - Precondition: `startIndex <= i`, `i <= endIndex`.
mutating func insert(_ newElement: Iterator.Element, at i: Int)
mutating func insert(_ newElement: Element, at i: Int)

/// Remove and return the element at the given index.
///
Expand All @@ -60,7 +60,7 @@ internal protocol _ArrayProtocol
///
/// - Precondition: `count > index`.
@discardableResult
mutating func remove(at index: Int) -> Iterator.Element
mutating func remove(at index: Int) -> Element

//===--- implementation detail -----------------------------------------===//

Expand Down
20 changes: 10 additions & 10 deletions stdlib/public/core/Arrays.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -1083,7 +1083,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
/// - Parameter s: The sequence of elements to turn into an array.
@_inlineable
public init<S : Sequence>(_ s: S)
where S.Iterator.Element == Element {
where S.Element == Element {

self = ${Self}(
_buffer: _Buffer(
Expand Down Expand Up @@ -1436,7 +1436,7 @@ extension ${Self} : RangeReplaceableCollection, _ArrayProtocol {
@_inlineable
@_semantics("array.append_contentsOf")
public mutating func append<S : Sequence>(contentsOf newElements: S)
where S.Iterator.Element == Element {
where S.Element == Element {

let newElementsCount = newElements.underestimatedCount
reserveCapacityForAppend(newElementsCount: newElementsCount)
Expand Down Expand Up @@ -1779,8 +1779,8 @@ extension ${Self} {

@_inlineable
public func _copyContents(
initializing buffer: UnsafeMutableBufferPointer<Iterator.Element>
) -> (Iterator,UnsafeMutableBufferPointer<Iterator.Element>.Index) {
initializing buffer: UnsafeMutableBufferPointer<Element>
) -> (Iterator,UnsafeMutableBufferPointer<Element>.Index) {

guard !self.isEmpty else { return (makeIterator(),buffer.startIndex) }

Expand Down Expand Up @@ -1817,7 +1817,7 @@ internal struct _InitializeMemoryFromCollection<
> : _PointerFunction {
@_inlineable
@_versioned
func call(_ rawMemory: UnsafeMutablePointer<C.Iterator.Element>, count: Int) {
func call(_ rawMemory: UnsafeMutablePointer<C.Element>, count: Int) {
var p = rawMemory
var q = newValues.startIndex
for _ in 0..<count {
Expand Down Expand Up @@ -1846,7 +1846,7 @@ extension _ArrayBufferProtocol {
_ bounds: Range<Int>,
with newValues: C,
count insertCount: Int
) where C.Iterator.Element == Element {
) where C.Element == Element {

let growth = insertCount - bounds.count
let newCount = self.count + growth
Expand Down Expand Up @@ -1918,7 +1918,7 @@ extension ${Self} {
public mutating func replaceSubrange<C>(
_ subrange: Range<Int>,
with newElements: C
) where C : Collection, C.Iterator.Element == Element {
) where C : Collection, C.Element == Element {
% if Self in ['Array', 'ContiguousArray']:
_precondition(subrange.lowerBound >= self._buffer.startIndex,
"${Self} replace: subrange start is negative")
Expand Down Expand Up @@ -2131,7 +2131,7 @@ extension _ArrayBufferProtocol {
@_versioned
internal mutating func _arrayAppendSequence<S : Sequence>(
_ newItems: S
) where S.Iterator.Element == Element {
) where S.Element == Element {

// this function is only ever called from append(contentsOf:)
// which should always have exhausted its capacity before calling
Expand Down Expand Up @@ -2410,13 +2410,13 @@ extension ${Self} {
public mutating func replaceRange<C>(
_ subRange: Range<Int>,
with newElements: C
) where C : Collection, C.Iterator.Element == Element {
) where C : Collection, C.Element == Element {
Builtin.unreachable()
}

@available(*, unavailable, renamed: "append(contentsOf:)")
public mutating func appendContentsOf<S : Sequence>(_ newElements: S)
where S.Iterator.Element == Element {
where S.Element == Element {
Builtin.unreachable()
}
}
Expand Down
15 changes: 6 additions & 9 deletions stdlib/public/core/BidirectionalCollection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ public protocol _BidirectionalIndexable : _Indexable {
/// `c.index(before: c.index(after: i)) == i`.
/// - If `i > c.startIndex && i <= c.endIndex`
/// `c.index(after: c.index(before: i)) == i`.
public protocol BidirectionalCollection
: _BidirectionalIndexable, Collection {
public protocol BidirectionalCollection : _BidirectionalIndexable, Collection
where SubSequence: BidirectionalCollection {

// TODO: swift-3-indexing-model - replaces functionality in BidirectionalIndex
/// Returns the position immediately before the given index.
Expand All @@ -84,10 +84,7 @@ public protocol BidirectionalCollection

/// A sequence that can represent a contiguous subrange of the collection's
/// elements.
associatedtype SubSequence : _BidirectionalIndexable, Collection
= BidirectionalSlice<Self>
// FIXME(ABI)#93 (Recursive Protocol Constraints):
// associatedtype SubSequence : BidirectionalCollection
associatedtype SubSequence = BidirectionalSlice<Self>

/// A type that represents the indices that are valid for subscripting the
/// collection, in ascending order.
Expand Down Expand Up @@ -127,7 +124,7 @@ public protocol BidirectionalCollection
/// // Prints "50"
///
/// - Complexity: O(1)
var last: Iterator.Element? { get }
var last: Element? { get }

/// Accesses a contiguous subrange of the collection's elements.
///
Expand Down Expand Up @@ -231,7 +228,7 @@ extension BidirectionalCollection where SubSequence == Self {
///
/// - Complexity: O(1).
/// - SeeAlso: `removeLast()`
public mutating func popLast() -> Iterator.Element? {
public mutating func popLast() -> Element? {
guard !isEmpty else { return nil }
let element = last!
self = self[startIndex..<index(before: endIndex)]
Expand All @@ -248,7 +245,7 @@ extension BidirectionalCollection where SubSequence == Self {
/// - Complexity: O(1)
/// - SeeAlso: `popLast()`
@discardableResult
public mutating func removeLast() -> Iterator.Element {
public mutating func removeLast() -> Element {
let element = last!
self = self[startIndex..<index(before: endIndex)]
return element
Expand Down
Loading