From daf769b87bf9b83df7df51149b5ef5eb1561f304 Mon Sep 17 00:00:00 2001 From: Jamie Thompson Date: Mon, 10 Aug 2020 11:06:25 +0200 Subject: [PATCH] fix #6663: sort EnumValues by ordinal --- library/src/scala/runtime/EnumValues.scala | 4 ++-- tests/run/enum-values-order.scala | 8 ++++++++ tests/run/planets.check | 6 +++--- 3 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 tests/run/enum-values-order.scala diff --git a/library/src/scala/runtime/EnumValues.scala b/library/src/scala/runtime/EnumValues.scala index 3f59faed5d56..b3f960b7f604 100644 --- a/library/src/scala/runtime/EnumValues.scala +++ b/library/src/scala/runtime/EnumValues.scala @@ -1,9 +1,9 @@ package scala.runtime -import scala.collection.immutable.Map +import scala.collection.immutable.TreeMap class EnumValues[E <: Enum] { - private[this] var myMap: Map[Int, E] = Map() + private[this] var myMap: Map[Int, E] = TreeMap.empty private[this] var fromNameCache: Map[String, E] = null def register(v: E) = { diff --git a/tests/run/enum-values-order.scala b/tests/run/enum-values-order.scala new file mode 100644 index 000000000000..be2b602f158c --- /dev/null +++ b/tests/run/enum-values-order.scala @@ -0,0 +1,8 @@ +/** immutable hashmaps (as of 2.13 collections) only store up to 4 entries in insertion order */ +enum LatinAlphabet { case A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z } + +@main def Test = + import LatinAlphabet._ + val ordered = Seq(A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, Z) + + assert(ordered sameElements LatinAlphabet.values) diff --git a/tests/run/planets.check b/tests/run/planets.check index feb6f737db78..2785b93d3008 100644 --- a/tests/run/planets.check +++ b/tests/run/planets.check @@ -1,8 +1,8 @@ Your weight on MERCURY is 37.775761520093525 -Your weight on SATURN is 106.60155388115666 Your weight on VENUS is 90.49990998410455 -Your weight on URANUS is 90.51271993894251 Your weight on EARTH is 100.0 -Your weight on NEPTUNE is 113.83280724696579 Your weight on MARS is 37.873718403712886 Your weight on JUPITER is 253.05575254957407 +Your weight on SATURN is 106.60155388115666 +Your weight on URANUS is 90.51271993894251 +Your weight on NEPTUNE is 113.83280724696579