scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Exception immutable.HashMap.merged in 2.12.x #13024

Closed retronym closed 3 months ago

retronym commented 3 months ago

Reproduction steps

Scala version: 2.12.19

scala> import scala.collection.immutable.HashMap; val left = HashMap((1 to 100000).map(x => (x, x)): _*); left.merged(new HashMap[Int, Int])((x, y) => x)
java.lang.RuntimeException: section supposed to be unreachable.
  at scala.sys.package$.error(package.scala:30)
  at scala.collection.immutable.HashMap$HashTrieMap.merge0(HashMap.scala:955)
  at scala.collection.immutable.HashMap.merged(HashMap.scala:142)
  ... 28 elided

Problem

No exception should be raised

Discussion

Workaround is to use HashMap[Int, Int]() or HashMap.empty[Int, Int], which returns a singleton instance of HashMap.EmptyHashMap.

The bug is due to code paths that check for that eq EmptyHashMap instead of that.isEmpty. These seem limited to the implementation of merged.

This is fixed in 2.13.0 with the new implementation of HashMap. Raising this bug for historical record as I found it while reviewing the old implementation.

SethTisue commented 3 months ago

thanks Jason! closing as we don't keep 2.12-only tickets open, but perhaps someone on @scala/collections would be interested in attempting a 2.12-only fix