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

accessing values of the Enumeration leads to unexpected behaviour #6094

Open
scabug opened this issue Jul 17, 2012 · 3 comments
Open

accessing values of the Enumeration leads to unexpected behaviour #6094

scabug opened this issue Jul 17, 2012 · 3 comments
Labels
Milestone

Comments

@scabug
Copy link

scabug commented Jul 17, 2012

A simple example of enumeration

object Test {
  object MyEnum extends Enumeration {
    val Z = Value(12)
  }

  import MyEnum._
  def main(args: Array[String]) {
    println(MyEnum.values)
  }
}

produces (as expected):

MyEnum.ValueSet(Z)

Add another value to the enumeration:

  val Foo = Value(2000000000)

produces:

MyEnum.ValueSet()

But this gets even better when you add

  val Bar = Value(-2000000000)

because it gives you

java.lang.IllegalArgumentException: requirement failed
	at scala.Predef$.require(Predef.scala:204)
	at scala.collection.mutable.BitSet.add(BitSet.scala:74)
	at scala.collection.mutable.BitSet.$plus$eq(BitSet.scala:92)
	at scala.Enumeration$ValueSet$$anon$1.$plus$eq(Enumeration.scala:283)
	at scala.Enumeration$ValueSet$$anon$1.$plus$eq(Enumeration.scala:281)
	at scala.collection.generic.Growable$$anonfun$$plus$plus$eq$1.apply(Growable.scala:48)
	at scala.collection.generic.Growable$$anonfun$$plus$plus$eq$1.apply(Growable.scala:48)
	at scala.collection.mutable.HashMap$$anon$2$$anonfun$foreach$3.apply(HashMap.scala:107)
	at scala.collection.mutable.HashMap$$anon$2$$anonfun$foreach$3.apply(HashMap.scala:107)
	at scala.collection.mutable.HashTable$class.foreachEntry(HashTable.scala:204)
	at scala.collection.mutable.HashMap.foreachEntry(HashMap.scala:39)
	at scala.collection.mutable.HashMap$$anon$2.foreach(HashMap.scala:107)
	at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48)
	at scala.Enumeration$ValueSet$$anon$1.$plus$plus$eq(Enumeration.scala:281)
	at scala.Enumeration.values(Enumeration.scala:93)
	at Test$.main(t5588.scala:10)
	at Test.main(t5588.scala)
@scabug
Copy link
Author

scabug commented Jul 17, 2012

Imported From: https://issues.scala-lang.org/browse/SI-6094?orig=1
Reporter: @hubertp
Affected Versions: 2.10.0-M5

@scabug
Copy link
Author

scabug commented Aug 4, 2012

@adriaanm said:
Hubert, I thought you last worked on Enum -- could you look into this or assign to someone else? Thanks

@scabug
Copy link
Author

scabug commented Jan 30, 2013

@ViniciusMiana said:
This is actually a BitSet problem. The example works on 2.9, because it did not use BitSet internally.
If you try:
scala> val b = new mutable.BitSet
scala> b+= 2000000000
you will get either:
res0: b.type = BitSet()
or
java.lang.OutOfMemoryError: Java heap space
Now when you use number like 2000000000 and -2000000000, since they are converted to a positive integer to store in the BitSet, you get an int overflow.

@scabug scabug added the enum label Apr 7, 2017
@scabug scabug added this to the Backlog milestone Apr 7, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant