File tree Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Expand file tree Collapse file tree 2 files changed +31
-0
lines changed Original file line number Diff line number Diff line change @@ -1044,6 +1044,18 @@ extension Collection where SubSequence == Slice<Self> {
1044
1044
}
1045
1045
}
1046
1046
1047
+ extension Collection {
1048
+ // This unavailable default implementation of `subscript(bounds: Range<_>)`
1049
+ // prevents incomplete Collection implementations from satisfying the
1050
+ // protocol through the use of the generic convenience implementation
1051
+ // `subscript<R: RangeExpression>(r: R)`. If that were the case, at
1052
+ // runtime the generic implementation would call itself
1053
+ // in an infinite recursion because of the absence of a better option.
1054
+ @available ( * , unavailable)
1055
+ @_alwaysEmitIntoClient
1056
+ public subscript( bounds: Range < Index > ) -> SubSequence { fatalError ( ) }
1057
+ }
1058
+
1047
1059
extension Collection where SubSequence == Self {
1048
1060
/// Removes and returns the first element of the collection.
1049
1061
///
Original file line number Diff line number Diff line change @@ -135,6 +135,25 @@ struct RangeReplaceableCollection_SubSequence_IsDefaulted : RangeReplaceableColl
135
135
}
136
136
}
137
137
138
+ //
139
+ // A Collection that does not use `Slice<Self>` as its SubSequence should
140
+ // require its own implementation of the Range<Index> subscript getter.
141
+ // The only valid default implementation of that Collection requirement
142
+ // returns `Slice<Self>`.
143
+ //
144
+
145
+ // expected-error@+2 {{type 'CollectionWithNonDefaultSubSequence' does not conform to protocol 'Collection'}}
146
+ // expected-error@+1 {{unavailable subscript 'subscript(_:)' was used to satisfy a requirement of protocol 'Collection'}}
147
+ struct CollectionWithNonDefaultSubSequence : Collection {
148
+ public var startIndex : Int
149
+ public var endIndex : Int
150
+
151
+ public typealias SubSequence = Self
152
+
153
+ public func index( after i: Int ) -> Int { i+ 1 }
154
+ public subscript( position: Int ) -> Int { position }
155
+ }
156
+
138
157
// FIXME: Remove -verify-ignore-unknown.
139
158
// <unknown>:0: error: unexpected note produced: possibly intended match
140
159
// <unknown>:0: error: unexpected note produced: possibly intended match
You can’t perform that action at this time.
0 commit comments