pointfreeco / swift-identified-collections

A library of data structures for working with collections of identifiable elements in an ergonomic, performant way.
MIT License
531 stars 45 forks source link

Regression when using `sort(using: Comparator)` #69

Closed tgrapperon closed 4 months ago

tgrapperon commented 4 months ago

Description

Since the release 1.0.1, sorting an identified array using a KeyPathComparator fails when it internally accesses the subscript. It was previously working on v1.0.0

Checklist

Expected behavior

func testIdentifiedArraySort() {
  struct Item: Identifiable {
    let id: Int
  }
  var items: IdentifiedArrayOf<Item> = [Item(id: 3), Item(id: 2), Item(id: 1)]
  items.sort(using: KeyPathComparator(\.id))
  XCTAssertEqual([1, 2, 3], items.map(\.id))
}

Actual behavior

It crashes when it hits the precondition in the subscript (we removed an element, but its id can already be found elsewhere in the array we're working on).

Steps to reproduce

The following test should pass (and passes on v1.0.0):

func testIdentifiedArraySort() {
  struct Item: Identifiable {
    let id: Int
  }
  var items: IdentifiedArrayOf<Item> = [Item(id: 3), Item(id: 2), Item(id: 1)]
  items.sort(using: KeyPathComparator(\.id))
  XCTAssertEqual([1, 2, 3], items.map(\.id))
}

Note that sorting using the closure comparison works.

Identified Collections version information

1.0.1

Destination operating system

iOS 17

Xcode version information

Version 15.3 (15E5202a)

Swift Compiler version information

swift-driver version: 1.90.11.1 Apple Swift version 5.10 (swiftlang-5.10.0.13 clang-1500.3.9.4)
Target: arm64-apple-macosx14.0