Skip to content

Commit cf7e3f6

Browse files
committed
stdlib: Range.contains: fix coding style and make the function public
... so that it actually has a chance to get referenced in the witness table. But since the function is in a constrained extension, it can't be.
1 parent a5a70dd commit cf7e3f6

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

stdlib/public/core/Range.swift

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,15 +154,16 @@ extension Range : CustomReflectable {
154154
}
155155

156156
/// O(1) implementation of `contains()` for ranges of comparable elements.
157-
extension Range where Element: Comparable {
157+
extension Range where Element : Comparable {
158158
@warn_unused_result
159-
func _customContainsEquatableElement(element: Generator.Element) -> Bool? {
159+
public func _customContainsEquatableElement(element: Element) -> Bool? {
160160
return element >= self.startIndex && element < self.endIndex
161161
}
162-
163-
// FIXME: copied from SequenceAlgorithms as a workaround for https://bugs.swift.org/browse/SR-435
162+
163+
// FIXME: copied from SequenceAlgorithms as a workaround for
164+
// https://bugs.swift.org/browse/SR-435
164165
@warn_unused_result
165-
public func contains(element: Generator.Element) -> Bool {
166+
public func contains(element: Element) -> Bool {
166167
if let result = _customContainsEquatableElement(element) {
167168
return result
168169
}

validation-test/stdlib/SequenceType.swift.gyb

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -608,18 +608,40 @@ SequenceTypeTests.test("Set<T>.contains/CustomImplementation/${dispatch}") {
608608

609609
% end
610610

611-
SequenceTypeTests.test("Range<Element>.contains/WhereElementIsComparable/dispatch") {
612-
let start = 0
613-
let end = 10
614-
let range = Range(start: MinimalComparableIndexValue(start), end: MinimalComparableIndexValue(end))
615-
let count = 20
611+
SequenceTypeTests.test("Range<Element>.contains/WhereElementIsComparable/staticDispatch") {
612+
let start = 10
613+
let end = 20
614+
let range: Range<MinimalComparableIndexValue> =
615+
MinimalComparableIndexValue(start)..<MinimalComparableIndexValue(end)
616+
expectEqual(
617+
1, MinimalComparableIndexValue.timesLessWasCalled.value)
618+
let count = 30
616619
for test in 0..<count {
617620
expectEqual(
618621
test >= start && test < end,
619622
range.contains(MinimalComparableIndexValue(test)))
620623
}
621624
expectEqual(
622-
count * 2, MinimalComparableIndexValue.timesLessWasCalled.value)
625+
51, MinimalComparableIndexValue.timesLessWasCalled.value)
626+
}
627+
628+
SequenceTypeTests.test("Range<Element>.contains/WhereElementIsComparable/dispatch") {
629+
let start = 10
630+
let end = 20
631+
let range: Range<MinimalComparableIndexValue> =
632+
MinimalComparableIndexValue(start)..<MinimalComparableIndexValue(end)
633+
expectEqual(
634+
1, MinimalComparableIndexValue.timesLessWasCalled.value)
635+
let count = 30
636+
for test in 0..<count {
637+
expectEqual(
638+
test >= start && test < end,
639+
LoggingCollection(range).contains(MinimalComparableIndexValue(test)))
640+
}
641+
// Dispatch through a protocol does not get the optimization because the
642+
// custom contains method is not getting into the witness table.
643+
expectEqual(
644+
1, MinimalComparableIndexValue.timesLessWasCalled.value)
623645
}
624646

625647
SequenceTypeTests.test("contains/Predicate") {

0 commit comments

Comments
 (0)