mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[stdlib] Remove now-redundant slicing operations
This commit is contained in:
@@ -466,106 +466,6 @@ ${orderingExplanation}
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
% for Self in '_Indexable', '_MutableIndexable':
|
|
||||||
%{
|
|
||||||
|
|
||||||
subscriptCommentPre = """\
|
|
||||||
/// Accesses a contiguous subrange of the collection's elements.
|
|
||||||
///
|
|
||||||
/// The accessed slice uses the same indices for the same elements as the
|
|
||||||
/// original collection. Always use the slice's `startIndex` property
|
|
||||||
/// instead of assuming that its indices start at a particular value.
|
|
||||||
///
|
|
||||||
/// This example demonstrates getting a slice of an array of strings, finding
|
|
||||||
/// the index of one of the strings in the slice, and then using that index
|
|
||||||
/// in the original array.
|
|
||||||
///
|
|
||||||
/// let streets = ["Adams", "Bryant", "Channing", "Douglas", "Evarts"]
|
|
||||||
/// let streetsSlice = streets[2 ..< streets.endIndex]
|
|
||||||
/// print(streetsSlice)
|
|
||||||
/// // Prints "["Channing", "Douglas", "Evarts"]"
|
|
||||||
///
|
|
||||||
/// let index = streetsSlice.index(of: "Evarts") // 4"""
|
|
||||||
|
|
||||||
if 'Mutable' in Self:
|
|
||||||
subscriptCommentMid = """\
|
|
||||||
/// streets[index!] = "Eustace"
|
|
||||||
/// print(streets[index!])
|
|
||||||
/// // Prints "Eustace\""""
|
|
||||||
else:
|
|
||||||
subscriptCommentMid = """\
|
|
||||||
/// print(streets[index!])
|
|
||||||
/// // Prints "Evarts\""""
|
|
||||||
|
|
||||||
subscriptCommentPost = """\
|
|
||||||
///
|
|
||||||
/// - Parameter bounds: A range of the collection's indices. The bounds of
|
|
||||||
/// the range must be valid indices of the collection."""
|
|
||||||
}%
|
|
||||||
// FIXME(ABI)#180 (Type checker)
|
|
||||||
// WORKAROUND rdar://25214066 - should be on Collection
|
|
||||||
extension ${Self} {
|
|
||||||
${subscriptCommentPre}
|
|
||||||
${subscriptCommentMid}
|
|
||||||
${subscriptCommentPost}
|
|
||||||
@_inlineable
|
|
||||||
public subscript(bounds: ClosedRange<Index>) -> SubSequence {
|
|
||||||
get {
|
|
||||||
return self[
|
|
||||||
Range(
|
|
||||||
uncheckedBounds: (
|
|
||||||
lower: bounds.lowerBound,
|
|
||||||
upper: index(after: bounds.upperBound)))
|
|
||||||
]
|
|
||||||
}
|
|
||||||
% if 'Mutable' in Self:
|
|
||||||
set {
|
|
||||||
self[
|
|
||||||
Range(
|
|
||||||
uncheckedBounds: (
|
|
||||||
lower: bounds.lowerBound,
|
|
||||||
upper: index(after: bounds.upperBound)))
|
|
||||||
] = newValue
|
|
||||||
}
|
|
||||||
% end
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// FIXME(ABI)#180 (Type checker)
|
|
||||||
// WORKAROUND rdar://25214066 - should be on Collection
|
|
||||||
extension ${Self} where Index : Strideable, Index.Stride : SignedInteger {
|
|
||||||
${subscriptCommentPre}
|
|
||||||
${subscriptCommentMid}
|
|
||||||
${subscriptCommentPost}
|
|
||||||
@_inlineable
|
|
||||||
public subscript(bounds: CountableRange<Index>) -> SubSequence {
|
|
||||||
get {
|
|
||||||
return self[Range(bounds)]
|
|
||||||
}
|
|
||||||
% if 'Mutable' in Self:
|
|
||||||
set {
|
|
||||||
self[Range(bounds)] = newValue
|
|
||||||
}
|
|
||||||
% end
|
|
||||||
}
|
|
||||||
|
|
||||||
${subscriptCommentPre}
|
|
||||||
${subscriptCommentMid}
|
|
||||||
${subscriptCommentPost}
|
|
||||||
@_inlineable
|
|
||||||
public subscript(bounds: CountableClosedRange<Index>) -> SubSequence {
|
|
||||||
get {
|
|
||||||
return self[ClosedRange(bounds)]
|
|
||||||
}
|
|
||||||
% if 'Mutable' in Self:
|
|
||||||
set {
|
|
||||||
self[ClosedRange(bounds)] = newValue
|
|
||||||
}
|
|
||||||
% end
|
|
||||||
}
|
|
||||||
}
|
|
||||||
% end
|
|
||||||
|
|
||||||
//===--- Unavailable stuff ------------------------------------------------===//
|
//===--- Unavailable stuff ------------------------------------------------===//
|
||||||
|
|
||||||
extension MutableCollection where Self : RandomAccessCollection {
|
extension MutableCollection where Self : RandomAccessCollection {
|
||||||
|
|||||||
@@ -302,8 +302,8 @@ func disallowSubscriptingOnIntegers() {
|
|||||||
r1[0...4] // expected-error {{ambiguous use of 'subscript'}}
|
r1[0...4] // expected-error {{ambiguous use of 'subscript'}}
|
||||||
r2[0...4] // expected-error {{ambiguous reference to member 'subscript'}}
|
r2[0...4] // expected-error {{ambiguous reference to member 'subscript'}}
|
||||||
r3[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), ((UnboundedRange_) -> ()), (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)}}
|
||||||
(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>)}}
|
(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)}}
|
||||||
|
|
||||||
r0[r0] // expected-error {{ambiguous use of 'subscript'}}
|
r0[r0] // expected-error {{ambiguous use of 'subscript'}}
|
||||||
r0[r1] // expected-error {{ambiguous subscript with base type 'CountableRange<Int>' and index type 'CountableRange<UInt>'}}
|
r0[r1] // expected-error {{ambiguous subscript with base type 'CountableRange<Int>' and index type 'CountableRange<UInt>'}}
|
||||||
|
|||||||
Reference in New Issue
Block a user