Skip to content

Commit 465f243

Browse files
committed
Move += operator for append(contentsOf:) up to RangeReplaceableCollection
1 parent fefc2e4 commit 465f243

File tree

5 files changed

+41
-61
lines changed

5 files changed

+41
-61
lines changed

stdlib/private/StdlibCollectionUnittest/CheckRangeReplaceableCollectionType.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -608,6 +608,19 @@ self.test("\(testNamePrefix).append(contentsOf:)/semantics") {
608608
}
609609
}
610610

611+
self.test("\(testNamePrefix).OperatorPlusEquals") {
612+
for test in appendContentsOfTests {
613+
var c = makeWrappedCollection(test.collection)
614+
let newElements =
615+
MinimalCollection(elements: test.newElements.map(wrapValue))
616+
c += newElements
617+
expectEqualSequence(
618+
test.expected,
619+
c.map { extractValue($0).value },
620+
stackTrace: SourceLocStack().with(test.loc))
621+
}
622+
}
623+
611624
//===----------------------------------------------------------------------===//
612625
// insert()
613626
//===----------------------------------------------------------------------===//

stdlib/public/core/ArrayType.swift

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,6 @@ internal protocol _ArrayProtocol
4343
/// - Complexity: O(`self.count`).
4444
mutating func reserveCapacity(_ minimumCapacity: Int)
4545

46-
/// Operator form of `append(contentsOf:)`.
47-
static func += <S : Sequence>(lhs: inout Self, rhs: S)
48-
where S.Iterator.Element == Iterator.Element
49-
5046
/// Insert `newElement` at index `i`.
5147
///
5248
/// Invalidates all indices with respect to `self`.

stdlib/public/core/Arrays.swift.gyb

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1837,30 +1837,6 @@ extension ${Self} {
18371837
}
18381838
}
18391839
}
1840-
1841-
// FIXME(ABI)#16 : remove this entrypoint. The functionality should be provided by
1842-
// a `+=` operator on `RangeReplaceableCollection`.
1843-
/// Appends the elements of a sequence to ${a_Self}.
1844-
///
1845-
/// Use this operator to append the elements of a sequence to the end of
1846-
/// ${a_Self} with same `Element` type. This example appends
1847-
/// the elements of a `Range<Int>` instance to an array of integers.
1848-
///
1849-
/// var numbers = [1, 2, 3, 4, 5]
1850-
/// numbers += 10...15
1851-
/// print(numbers)
1852-
/// // Prints "[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]"
1853-
///
1854-
/// - Parameters:
1855-
/// - lhs: The array to append to.
1856-
/// - rhs: A collection or finite sequence.
1857-
///
1858-
/// - Complexity: O(*n*), where *n* is the length of the resulting array.
1859-
public func += <
1860-
S : Sequence
1861-
>(lhs: inout ${Self}<S.Iterator.Element>, rhs: S) {
1862-
lhs.append(contentsOf: rhs)
1863-
}
18641840
% end
18651841

18661842
//===--- generic helpers --------------------------------------------------===//

stdlib/public/core/RangeReplaceableCollection.swift.gyb

Lines changed: 25 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -294,26 +294,6 @@ public protocol RangeReplaceableCollection
294294
with newElements: C
295295
) where C : Collection, C.Iterator.Element == Iterator.Element
296296

297-
/*
298-
We could have these operators with default implementations, but the compiler
299-
crashes:
300-
301-
<rdar://problem/16566712> Dependent type should have been substituted by Sema
302-
or SILGen
303-
304-
func + <S : Sequence>(_: Self, _: S) -> Self
305-
where S.Iterator.Element == Iterator.Element
306-
307-
func + <S : Sequence>(_: S, _: Self) -> Self
308-
where S.Iterator.Element == Iterator.Element
309-
310-
func + <S : Collection>(_: Self, _: S) -> Self
311-
where S.Iterator.Element == Iterator.Element
312-
313-
func + <RC : RangeReplaceableCollection>(_: Self, _: S) -> Self
314-
where RC.Iterator.Element == Iterator.Element
315-
*/
316-
317297
/// Prepares the collection to store the specified number of elements, when
318298
/// doing so is appropriate for the underlying type.
319299
///
@@ -370,16 +350,6 @@ public protocol RangeReplaceableCollection
370350
/// collection.
371351
mutating func append(_ newElement: Iterator.Element)
372352

373-
/*
374-
The 'append(contentsOf:)' requirement should be an operator, but the compiler crashes:
375-
376-
<rdar://problem/16566712> Dependent type should have been substituted by Sema
377-
or SILGen
378-
379-
func += <S : Sequence>(inout _: Self, _: S)
380-
where S.Iterator.Element == Iterator.Element
381-
*/
382-
383353
/// Adds the elements of a sequence or collection to the end of this
384354
/// collection.
385355
///
@@ -398,6 +368,8 @@ public protocol RangeReplaceableCollection
398368
///
399369
/// - Complexity: O(*n*), where *n* is the length of the resulting
400370
/// collection.
371+
// FIXME(ABI)#166 (Evolution): Consider replacing .append(contentsOf) with +=
372+
// suggestion in SE-91
401373
mutating func append<S : Sequence>(contentsOf newElements: S)
402374
where S.Iterator.Element == Iterator.Element
403375

@@ -1171,6 +1143,29 @@ public func +<
11711143
return lhs
11721144
}
11731145

1146+
/// Appends the elements of a sequence to a range-replaceable collection.
1147+
///
1148+
/// Use this operator to append the elements of a sequence to the end of
1149+
/// range-replaceable collection with same `Element` type. This example appends
1150+
/// the elements of a `Range<Int>` instance to an array of integers.
1151+
///
1152+
/// var numbers = [1, 2, 3, 4, 5]
1153+
/// numbers += 10...15
1154+
/// print(numbers)
1155+
/// // Prints "[1, 2, 3, 4, 5, 10, 11, 12, 13, 14, 15]"
1156+
///
1157+
/// - Parameters:
1158+
/// - lhs: The array to append to.
1159+
/// - rhs: A collection or finite sequence.
1160+
///
1161+
/// - Complexity: O(*n*), where *n* is the length of the resulting array.
1162+
public func += <
1163+
R : RangeReplaceableCollection, S : Sequence
1164+
>(lhs: inout R, rhs: S)
1165+
where R.Iterator.Element == S.Iterator.Element {
1166+
lhs.append(contentsOf: rhs)
1167+
}
1168+
11741169
@available(*, unavailable, renamed: "RangeReplaceableCollection")
11751170
public typealias RangeReplaceableCollectionType = RangeReplaceableCollection
11761171

test/Misc/misc_diagnostics.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,11 @@ func test17875634() {
100100
var col = 2
101101
var coord = (row, col)
102102

103-
match += (1, 2) // expected-error{{binary operator '+=' cannot be applied to operands of type '[(Int, Int)]' and '(Int, Int)'}} expected-note {{overloads for '+=' exist}}
103+
match += (1, 2) // expected-error{{argument type '(Int, Int)' does not conform to expected type 'Sequence'}}
104104

105-
match += (row, col) // expected-error{{binary operator '+=' cannot be applied to operands of type '[(Int, Int)]' and '(Int, Int)'}} expected-note {{overloads for '+=' exist}}
105+
match += (row, col) // expected-error{{argument type '(@lvalue Int, @lvalue Int)' does not conform to expected type 'Sequence'}}
106106

107-
match += coord // expected-error{{binary operator '+=' cannot be applied to operands of type '[(Int, Int)]' and '(Int, Int)'}} expected-note {{overloads for '+=' exist}}
107+
match += coord // expected-error{{argument type '@lvalue (Int, Int)' does not conform to expected type 'Sequence'}}
108108

109109
match.append(row, col) // expected-error{{extra argument in call}}
110110

0 commit comments

Comments
 (0)