Hey all, I had this problem show up when using Xcode 13 with the code below. Basically, the code worked fine with Xcode 12.5, but in Xcode 13 the structs and classes no longer conform to the protocols for some reason. I get the following error message (many times): Unavailable subscript 'subscript(_:)' was used to satisfy a requirement of protocol 'RangeReplaceableCollection'
.
I'm not aware of any intentional source-breaking changes made to Swift 5.5, is this maybe due to some change in the implementation of the conformance-checking algorithm? In any case, I can't figure out what I need to do to make this compile again, so any help would be appreciated.
Here's a reduced version of the problematic file. I didn't try to reduce it more because I wasn't sure which methods I could remove without screwing up the protocol conformances. The original file can be found here.
public struct _ListSlice<Element>: Collection,
BidirectionalCollection,
RandomAccessCollection,
MutableCollection,
RangeReplaceableCollection
{
public typealias Index = Int
public typealias SubSequence = _ListSlice<Element>
let list: List<Element>
let range: Range<Int>
public var startIndex: Int {
return range.startIndex
}
public var endIndex: Int {
return range.endIndex
}
public subscript(position: Int) -> Element {
get {
return list[position]
}
// MutableCollection
set {
list._setElement(newValue, atIndex: position)
}
}
public func index(after i: Int) -> Int {
return list.index(after: i)
}
// BidirectionalCollection
public func index(before i: Int) -> Int {
return list.index(before: i)
}
// RangeReplaceableCollection
public init() {
self.list = []
self.range = 0..<0
}
}
public class List<Element>: CustomStringConvertible,
CustomDebugStringConvertible,
ExpressibleByArrayLiteral,
Sequence,
Collection,
BidirectionalCollection,
RandomAccessCollection
{
public typealias Buffer = [Element]
public typealias ArrayLiteralElement = Element
public typealias Index = Int
public typealias SubSequence = _ListSlice<Element>
public var array: Buffer
public init(_ array: Buffer) {
self.array = array
}
// Custom (Debug) String Convertible
public var description: String {
return array.description
}
public var debugDescription: String {
return array.debugDescription
}
// Expressible By Array Literal
public required init(arrayLiteral elements: Element...) {
self.array = elements
}
// Sequence
public func makeIterator() -> IndexingIterator<List<Element>> {
return IndexingIterator(_elements: self)
}
// Collection
public var startIndex: Int {
return array.startIndex
}
public var endIndex: Int {
return array.endIndex
}
public subscript(position: Int) -> Element {
return array[position]
}
public func index(after i: Int) -> Int {
return array.index(after: i)
}
// BidirectionalCollection
public func index(before i: Int) -> Int {
return array.index(before: i)
}
// Used for _ListSlice to conform to MutableCollection
fileprivate func _setElement(_ element: Element, atIndex index: Int) {
array[index] = element
}
// Other methods
public init<S>(_ sequence: S) where Element == S.Element, S: Sequence {
self.array = Array(sequence)
}
public init() {
self.array = []
}
}
public class MutableList<Element>: List<Element>,
MutableCollection,
RangeReplaceableCollection
{
// MutableCollection
public override subscript(position: Int) -> Element {
get {
return array[position]
}
set {
array[position] = newValue
}
}
// RangeReplaceableCollection
override public required init() {
super.init([])
}
public required init(arrayLiteral elements: Element...) {
super.init(elements)
}
// Other methods
public func append(_ newElement: Element) {
array.append(newElement)
}
public func append<S>(contentsOf newElements: S) where S: Sequence, Element == S.Element {
self.array.append(contentsOf: newElements)
}
public func insert(_ newElement: Element, at i: Index) {
array.insert(newElement, at: i)
}
@discardableResult
public func removeFirst() -> Element {
return array.removeFirst()
}
@discardableResult
public func removeLast() -> Element {
return array.removeLast()
}
public func removeAll(keepingCapacity keepCapacity: Bool = false) {
array.removeAll(keepingCapacity: keepCapacity)
}
@discardableResult
public func remove(at index: Int) -> Element {
return array.remove(at: index)
}
public func reverse() {
self.array = self.array.reversed()
}
}