Skip to content

Commit 19cf4e9

Browse files
committed
add case to mix simple and value java enum cases
1 parent c064338 commit 19cf4e9

File tree

2 files changed

+55
-24
lines changed

2 files changed

+55
-24
lines changed

compiler/src/dotty/tools/dotc/transform/CompleteJavaEnums.scala

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,9 +155,6 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
155155
cpy.Template(templ)(
156156
parents = addEnumConstrArgs(defn.JavaEnumClass, templ.parents, addedSyms.map(ref)),
157157
body = params ++ addedDefs ++ addedForwarders ++ rest)
158-
else if cls.linkedClass.derivesFromJavaEnum then
159-
enumCaseOrdinals.clear() // remove simple cases // invariant: companion is visited after cases
160-
templ
161158
else if isJavaEnumValueImpl(cls) then
162159
def creatorParamRef(name: TermName) =
163160
ref(cls.owner.paramSymss.head.find(_.name == name).get)
@@ -169,6 +166,12 @@ class CompleteJavaEnums extends MiniPhase with InfoTransformer { thisPhase =>
169166
cpy.Template(templ)(
170167
parents = addEnumConstrArgs(cls.owner.owner.linkedClass, templ.parents, args),
171168
)
169+
else if cls.linkedClass.derivesFromJavaEnum then
170+
enumCaseOrdinals.clear() // remove simple cases // invariant: companion is visited after cases
171+
templ
172172
else templ
173173
}
174+
175+
override def checkPostCondition(tree: Tree)(using Context): Unit =
176+
assert(enumCaseOrdinals.isEmpty, "Java based enum ordinal cache was not cleared")
174177
}

tests/run/enum-values-order.scala

Lines changed: 49 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,35 +5,63 @@ enum LatinAlphabet2 extends java.lang.Enum[LatinAlphabet2] { case A, B, C, D, E,
55

66
enum LatinAlphabet3[+T] extends java.lang.Enum[LatinAlphabet3[_]] { 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 }
77

8+
object Color:
9+
trait Pretty
10+
enum Color extends java.lang.Enum[Color]:
11+
case Red, Green, Blue
12+
case Aqua extends Color with Color.Pretty
13+
case Grey, Black, White
14+
case Emerald extends Color with Color.Pretty
15+
case Brown
16+
817
@main def Test =
918

10-
val ordinals = Seq(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)
11-
val labels = 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")
1219

13-
def testLatin1() =
14-
import LatinAlphabet._
15-
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)
20+
def testLatin() =
1621

17-
assert(ordered sameElements LatinAlphabet.values)
18-
assert(ordinals == ordered.map(_.ordinal))
19-
assert(labels == ordered.map(_.productPrefix))
22+
val ordinals = Seq(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)
23+
val labels = 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")
2024

21-
def testLatin2() =
22-
import LatinAlphabet2._
23-
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)
25+
def testLatin1() =
26+
import LatinAlphabet._
27+
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)
2428

25-
assert(ordered sameElements LatinAlphabet2.values)
26-
assert(ordinals == ordered.map(_.ordinal))
27-
assert(labels == ordered.map(_.name))
29+
assert(ordered sameElements LatinAlphabet.values)
30+
assert(ordinals == ordered.map(_.ordinal))
31+
assert(labels == ordered.map(_.productPrefix))
32+
33+
def testLatin2() =
34+
import LatinAlphabet2._
35+
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)
36+
37+
assert(ordered sameElements LatinAlphabet2.values)
38+
assert(ordinals == ordered.map(_.ordinal))
39+
assert(labels == ordered.map(_.name))
40+
41+
def testLatin3() =
42+
import LatinAlphabet3._
43+
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)
44+
45+
assert(ordered sameElements LatinAlphabet3.values)
46+
assert(ordinals == ordered.map(_.ordinal))
47+
assert(labels == ordered.map(_.name))
48+
49+
testLatin1()
50+
testLatin2()
51+
testLatin3()
52+
53+
end testLatin
2854

29-
def testLatin3() =
30-
import LatinAlphabet3._
31-
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)
55+
def testColor() =
56+
import Color._
57+
val ordered = Seq(Red, Green, Blue, Aqua, Grey, Black, White, Emerald, Brown)
58+
val ordinals = Seq(0, 1, 2, 3, 4, 5, 6, 7, 8)
59+
val labels = Seq("Red", "Green", "Blue", "Aqua", "Grey", "Black", "White", "Emerald", "Brown")
3260

33-
assert(ordered sameElements LatinAlphabet3.values)
61+
assert(ordered sameElements Color.values)
3462
assert(ordinals == ordered.map(_.ordinal))
3563
assert(labels == ordered.map(_.name))
64+
end testColor
3665

37-
testLatin1()
38-
testLatin2()
39-
testLatin3()
66+
testLatin()
67+
testColor()

0 commit comments

Comments
 (0)