scala / collection-strawman

Implementation of the new Scala 2.13 Collections
Apache License 2.0
200 stars 72 forks source link

Queue/ArrayDeque checks bounds too aggressively #506

Closed Ichoran closed 6 years ago

Ichoran commented 6 years ago

ArrayDeque throws exceptions in a variety of cases when empty when there is a very sensible behavior instead. For instance,

scala> val xs = strawman.collection.mutable.Queue.empty[Int]
xs: strawman.collection.mutable.Queue[Int] = ArrayDeque()

scala> xs.toArray
java.lang.IndexOutOfBoundsException: 0
  at strawman.collection.mutable.ArrayDeque.requireBounds(ArrayDeque.scala:487)
  at strawman.collection.mutable.ArrayDeque.copySliceToArray(ArrayDeque.scala:447)
  at strawman.collection.mutable.ArrayDeque.toArray(ArrayDeque.scala:434)
  ... 40 elided

scala> xs.insert(0, 0)
java.lang.IndexOutOfBoundsException: 0
  at strawman.collection.mutable.ArrayDeque.requireBounds(ArrayDeque.scala:487)
  at strawman.collection.mutable.ArrayDeque.insert(ArrayDeque.scala:132)
  ... 40 elided

There are also problems with copyToArray and patchInPlace. These issues make it impossible to test ArrayDeque (and thus mutable.Queue) properly with collections-laws, so there are likely other undiscovered bugs as well.