Skip to content

Commit

Permalink
[stdlib] Provisional c[...] syntax while we await language features
Browse files Browse the repository at this point in the history
  • Loading branch information
Dave Abrahams committed May 13, 2017
1 parent 5d034a2 commit 4dee6ea
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,7 @@ extension TestSuite {
self.test("\(testNamePrefix).removeFirst()/slice/semantics") {
for test in removeFirstTests.filter({ $0.numberToRemove == 1 }) {
let c = makeWrappedCollection(test.collection.map(OpaqueValue.init))
var slice = c[c.startIndex..<c.endIndex]
var slice = c[...]
let survivingIndices = _allIndices(
into: slice,
in: slice.index(after: slice.startIndex)..<slice.endIndex)
Expand Down Expand Up @@ -1081,7 +1081,7 @@ extension TestSuite {
self.test("\(testNamePrefix).removeFirst(n: Int)/slice/semantics") {
for test in removeFirstTests {
let c = makeWrappedCollection(test.collection.map(OpaqueValue.init))
var slice = c[c.startIndex..<c.endIndex]
var slice = c[...]
let survivingIndices = _allIndices(
into: slice,
in: slice.index(
Expand Down Expand Up @@ -1140,7 +1140,7 @@ extension TestSuite {
// This can just reuse the test data for removeFirst()
for test in removeFirstTests.filter({ $0.numberToRemove == 1 }) {
let c = makeWrappedCollection(test.collection.map(OpaqueValue.init))
var slice = c[c.startIndex..<c.endIndex]
var slice = c[...]
let survivingIndices = _allIndices(
into: slice,
in: slice.index(after: slice.startIndex)..<slice.endIndex)
Expand Down Expand Up @@ -1236,7 +1236,7 @@ extension TestSuite {
self.test("\(testNamePrefix).removeLast()/slice/semantics") {
for test in removeLastTests.filter({ $0.numberToRemove == 1 }) {
let c = makeWrappedCollection(test.collection)
var slice = c[c.startIndex..<c.endIndex]
var slice = c[...]
let survivingIndices = _allIndices(
into: slice,
in: slice.startIndex
Expand Down Expand Up @@ -1281,7 +1281,7 @@ extension TestSuite {
self.test("\(testNamePrefix).removeLast(n: Int)/slice/semantics") {
for test in removeLastTests {
let c = makeWrappedCollection(test.collection)
var slice = c[c.startIndex..<c.endIndex]
var slice = c[...]
let survivingIndices = _allIndices(
into: slice,
in: slice.startIndex
Expand Down Expand Up @@ -1342,7 +1342,7 @@ extension TestSuite {
// This can just reuse the test data for removeLast()
for test in removeLastTests.filter({ $0.numberToRemove == 1 }) {
let c = makeWrappedCollection(test.collection)
var slice = c[c.startIndex..<c.endIndex]
var slice = c[...]
let survivingIndices = _allIndices(
into: slice,
in: slice.startIndex
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,7 @@ self.test("\(testNamePrefix).subscript(_: Range)/Set/semantics") {

let newElements = makeWrappedCollection(
test.expected.map { OpaqueValue($0.value + 90000) })
c[test.bounds(in: c)] =
newElements[newElements.startIndex..<newElements.endIndex]
c[test.bounds(in: c)] = newElements[...]

// FIXME: improve checkForwardCollection to check the SubSequence type.
/*
Expand Down Expand Up @@ -303,17 +302,15 @@ if isFixedLengthCollection {
let newElements = makeWrappedCollection([ 91010, 92020, 93030, 94040 ].map(OpaqueValue.init))

expectCrashLater()
c[c.startIndex..<c.endIndex] =
newElements[newElements.startIndex..<newElements.endIndex]
c[...] = newElements[...]
}

self.test("\(testNamePrefix).subscript(_: Range)/Set/SmallerRange") {
var c = makeWrappedCollection([ 1010, 2020, 3030 ].map(OpaqueValue.init))
let newElements = makeWrappedCollection([ 91010, 92020 ].map(OpaqueValue.init))

expectCrashLater()
c[c.startIndex..<c.endIndex] =
newElements[newElements.startIndex..<newElements.endIndex]
c[...] = newElements[...]
}
} else {
self.test("\(testNamePrefix).subscript(_: Range)/Set/DifferentlySizedRange") {
Expand All @@ -322,8 +319,7 @@ if isFixedLengthCollection {
let newElements = makeWrappedCollection(test.newElements)
let rangeToReplace = test.rangeSelection.range(in: c)

c[rangeToReplace] =
newElements[newElements.startIndex..<newElements.endIndex]
c[rangeToReplace] = newElements[...]

expectEqualSequence(
test.expected,
Expand Down Expand Up @@ -367,8 +363,7 @@ if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior != .none {
let newValues = makeWrappedCollection(
elements[i..<j].map { OpaqueValue($0.value + 90000) }
)
slice[subSliceStartIndex..<subSliceEndIndex]
= newValues[newValues.startIndex..<newValues.endIndex]
slice[subSliceStartIndex..<subSliceEndIndex] = newValues[...]
let subSlice = slice[subSliceStartIndex..<subSliceEndIndex]
for (k, index) in subSlice.indices.enumerated() {
expectEqual(
Expand All @@ -382,8 +377,7 @@ if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior != .none {
extractValue(subSlice[index]).value)
}
let oldValues = makeWrappedCollection(Array(elements[i..<j]))
slice[subSliceStartIndex..<subSliceEndIndex]
= oldValues[oldValues.startIndex..<oldValues.endIndex]
slice[subSliceStartIndex..<subSliceEndIndex] = oldValues[...]
}
}
}
Expand Down Expand Up @@ -437,7 +431,7 @@ if resiliencyChecks.subscriptRangeOnOutOfBoundsRangesBehavior != .none {
let count: Int = numericCast(
base.distance(from: bounds.lowerBound, to: bounds.upperBound))
let newValues = makeWrappedCollection(Array(elements[0..<count]))
let newSlice = newValues[newValues.startIndex..<newValues.endIndex]
let newSlice = newValues[...]

expectCrashLater()
slice[bounds] = newSlice
Expand Down
23 changes: 23 additions & 0 deletions stdlib/public/core/Range.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -929,12 +929,26 @@ extension Strideable where Stride: SignedInteger {
}
}

// FIXME: replace this with a computed var named `...` when the language makes
// that possible.
public enum UnboundedRange_ {
public static postfix func ... (_: UnboundedRange_) -> () {
fatalError("uncallable")
}
}
public typealias UnboundedRange = (UnboundedRange_)->()

extension _Indexable {
@_inlineable
public subscript<R: RangeExpression>(r: R)
-> SubSequence where R.Bound == Index {
return self[r.relative(to: self)]
}

@_inlineable
public subscript(x: UnboundedRange) -> SubSequence {
return self[startIndex...]
}
}
extension _MutableIndexable {
@_inlineable
Expand All @@ -947,6 +961,15 @@ extension _MutableIndexable {
self[r.relative(to: self)] = newValue
}
}

public subscript(x: UnboundedRange) -> SubSequence {
get {
return self[startIndex...]
}
set {
self[startIndex...] = newValue
}
}
}

// swift-3-indexing-model: this is not really a proper rename
Expand Down
4 changes: 2 additions & 2 deletions test/stdlib/RangeDiagnostics.swift
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,8 @@ func disallowSubscriptingOnIntegers() {
r1[0...4] // expected-error {{ambiguous use of 'subscript'}}
r2[0...4] // expected-error {{ambiguous reference to member 'subscript'}}
r3[0...4] // expected-error {{ambiguous reference to member 'subscript'}}
(10...100)[0...4] // expected-error {{cannot subscript a value of type 'CountableClosedRange<_>' with an index of type 'CountableClosedRange<_>'}} expected-note {{overloads for 'subscript' exist with these partially matching parameter lists: (ClosedRangeIndex<Bound>), (Range<ClosedRangeIndex<Bound>>), (Range<Self.Index>), (R), (ClosedRange<Self.Index>), (CountableRange<Self.Index>), (CountableClosedRange<Self.Index>)}}
(UInt(10)...100)[0...4] // expected-error {{cannot subscript a value of type 'CountableClosedRange<_>' with an index of type 'CountableClosedRange<_>'}} expected-note {{overloads for 'subscript' exist with these partially matching parameter lists: (ClosedRangeIndex<Bound>), (Range<ClosedRangeIndex<Bound>>), (Range<Self.Index>), (R), (ClosedRange<Self.Index>), (CountableRange<Self.Index>), (CountableClosedRange<Self.Index>)}}
(10...100)[0...4] // expected-error {{cannot subscript a value of type 'CountableClosedRange<_>' with an index of type 'CountableClosedRange<_>'}} expected-note {{overloads for 'subscript' exist with these partially matching parameter lists: (ClosedRangeIndex<Bound>), (Range<ClosedRangeIndex<Bound>>), (Range<Self.Index>), (R), ((UnboundedRange_) -> ()), (ClosedRange<Self.Index>), (CountableRange<Self.Index>), (CountableClosedRange<Self.Index>)}}
(UInt(10)...100)[0...4] // expected-error {{cannot subscript a value of type 'CountableClosedRange<_>' with an index of type 'CountableClosedRange<_>'}} expected-note {{overloads for 'subscript' exist with these partially matching parameter lists: (ClosedRangeIndex<Bound>), (Range<ClosedRangeIndex<Bound>>), (Range<Self.Index>), (R), ((UnboundedRange_) -> ()), (ClosedRange<Self.Index>), (CountableRange<Self.Index>), (CountableClosedRange<Self.Index>)}}

r0[r0] // expected-error {{ambiguous use of 'subscript'}}
r0[r1] // expected-error {{ambiguous subscript with base type 'CountableRange<Int>' and index type 'CountableRange<UInt>'}}
Expand Down

0 comments on commit 4dee6ea

Please sign in to comment.