8000 BitSet.last raises exception on non-empty set · Issue #10164 · scala/bug · GitHub
[go: up one dir, main page]

Skip to content
BitSet.last raises exception on non-empty set #10164
@scabug

Description

@scabug

A non-empty bitset incorrectly raises an "Empty BitSet" exception. The exception occurs several seconds after invoking the method.

scala> scala.collection.mutable.BitSet() ++ (0 to 128)
res9: scala.collection.mutable.BitSet = BitSet(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128)

scala> res9.last
java.util.NoSuchElementException: Empty BitSet
  at scala.collection.BitSetLike.last(BitSetLike.scala:225)
  at scala.collection.BitSetLike.last$(BitSetLike.scala:218)
  at scala.collection.mutable.BitSet.last(BitSet.scala:40)
  ... 30 elided

I think that the cause is this line from the source code (link to line 223 of BitSetLike.scala).
Based on the loop termination condition, the loop looks like it is meant to walk backwards through the array of words. And this is what you would expect for "last"
But the index variable "i" is being incremented.

I believe that what is happening in the example above is that "i" is being incremented until it overflows to MIN_INT (this takes several seconds on my machine) - and then the loop terminates and the exception is thrown.

(0 to 128) seems to be the smallest example on which this fails. (0 to 127) is fine.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    0