Skip to content
New issue

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

[SR-14850] MutableCollection gives wrong Subsequence type to implementor #57197

Closed
glessard opened this issue Jun 29, 2021 · 2 comments
Closed

[SR-14850] MutableCollection gives wrong Subsequence type to implementor #57197

glessard opened this issue Jun 29, 2021 · 2 comments

Comments

@glessard
Copy link
Contributor

@glessard glessard commented Jun 29, 2021

Previous ID SR-14850
Radar rdar://problem/79898408
Original Reporter @glessard
Type Bug
Status Resolved
Resolution Done

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
Votes 1
Component/s Standard Library
Labels Bug
Assignee @glessard
Priority Medium

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>`.

@glessard
Copy link
Contributor Author

@glessard glessard commented Jun 29, 2021

@swift-ci create

@glessard
Copy link
Contributor Author

@glessard glessard commented Aug 2, 2021

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant