Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Attachment: Download
swift-driver version: 1.26 Apple Swift version 5.5 (swiftlang-1300.0.20.104 clang-1300.0.21.1)
Target: arm64-apple-macosx11.0
md5: 6b025e063944499abb3a144e2e308b4a
Issue Description:
MutableCollection has its Range subscript requirement:
subscript(bounds: Range<Index>) -> SubSequence { get set }
It has an unconditional default implementation with this signature:
subscript(bounds: Range<Index>) -> Slice<Self> { get set }
However, if a type has a Subsequence different than that it can accidentally pick up this default implementation. The following compiles and runs:
struct MyMutableRange { public let start: Int public let count: Int public init(_ range: Range<Int>) { start = range.lowerBound count = range.count } } extension MyMutableRange: Sequence { public struct Iterator: IteratorProtocol { public mutating func next() -> Int? { nil } } public func makeIterator() -> Iterator { Iterator() } } extension MyMutableRange: Collection, MutableCollection { public typealias Index = Int public typealias SubSequence = Self public var startIndex: Index { start } public var endIndex: Index { start + count } public func index(after i: Index) -> Index { i.advanced(by: 1) } public subscript(position: Index) -> Int { get { precondition(position >= start) precondition(position < endIndex) return position } nonmutating set(newValue) { precondition(position >= start) precondition(position < endIndex) _ = newValue } } // public subscript(bounds: Range<Index>) -> SubSequence { // get { // precondition(bounds.lowerBound >= startIndex) // precondition(bounds.upperBound <= endIndex) // return SubSequence(start: bounds.lowerBound, count: bounds.count) // } // nonmutating set { // precondition(bounds.lowerBound >= startIndex) // precondition(bounds.upperBound <= endIndex) // precondition(bounds.count == newValue.count) // if !newValue.isEmpty { // bounds.lowerBound.copyMemory(from: newValue.baseAddress, // byteCount: newValue.count) // } // } // } } var rb: MyMutableRange rb = MyMutableRange(0..<48) // Not the correct SubSequence type let slice: Slice<MyMutableRange> = rb[16..<24] rb[32..<40] = slice
The default implementation on `MutableCollection` should have been defined under the condition where `SubSequence == Slice<Self>`.
The text was updated successfully, but these errors were encountered:
@swift-ci create
Sorry, something went wrong.
#38509
glessard
No branches or pull requests
Attachment: Download
Environment
swift-driver version: 1.26 Apple Swift version 5.5 (swiftlang-1300.0.20.104 clang-1300.0.21.1)
Target: arm64-apple-macosx11.0
Additional Detail from JIRA
md5: 6b025e063944499abb3a144e2e308b4a
Issue Description:
MutableCollection has its Range subscript requirement:
It has an unconditional default implementation with this signature:
However, if a type has a Subsequence different than that it can accidentally pick up this default implementation. The following compiles and runs:
The default implementation on `MutableCollection` should have been defined under the condition where `SubSequence == Slice<Self>`.
The text was updated successfully, but these errors were encountered: