Skip to content

Commit 6d50fef

Browse files
committed
refactor default case, test default cases
1 parent 6335cec commit 6d50fef

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ object DesugarEnums {
161161
fromOrdinalMeth(throwArg)
162162
else
163163
def default(ordinal: Tree) =
164-
CaseDef(Ident(nme.x_0), EmptyTree, throwArg(ordinal))
164+
CaseDef(Ident(nme.WILDCARD), EmptyTree, throwArg(ordinal))
165165
if constraints.isEnumeration then
166166
fromOrdinalMeth(ordinal =>
167167
Try(Apply(valuesDot(nme.apply), ordinal), default(ordinal) :: Nil, EmptyTree))

tests/run/enum-values.scala

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ import deriving.Mirror
44
enum Color:
55
case Red, Green, Blue
66

7+
enum Suits extends java.lang.Enum[Suits]:
8+
case Clubs, Spades, Diamonds, Hearts
9+
710
enum Tag[T]:
811
case Int extends Tag[Int]
912
case OfClass[T]()(using val tag: reflect.ClassTag[T]) extends Tag[T] // mix order of class and value
@@ -29,7 +32,7 @@ enum ClassOnly: // this should still generate the `ordinal` and `fromOrdinal` co
2932
case BranchProd(i: Int)
3033

3134
@main def Test: Unit =
32-
import Color._, Tag._, Expr._, ListLike._, TypeCtorsK._, MixedParams._, ClassOnly._
35+
import Color._, Suits._, Tag._, Expr._, ListLike._, TypeCtorsK._, MixedParams._, ClassOnly._
3336

3437
type FromOrdinal[T] = {
3538
def fromOrdinal(ordinal: Int): T
@@ -47,15 +50,19 @@ enum ClassOnly: // this should still generate the `ordinal` and `fromOrdinal` co
4750
s"$c does not `eq` companion.fromOrdinal(${c.ordinal}), got ${companion.fromOrdinal(c.ordinal)}")
4851

4952
def notFromOrdinal[T <: AnyRef & reflect.Enum](companion: FromOrdinal[T], compare: T): Unit =
53+
cantFind(companion, compare.ordinal)
54+
55+
def cantFind[T](companion: FromOrdinal[T], ordinal: Int): Unit =
5056
try
51-
companion.fromOrdinal(compare.ordinal)
52-
assertFail(s"$companion.fromOrdinal(${compare.ordinal}) did not fail")
57+
companion.fromOrdinal(ordinal)
58+
assertFail(s"$companion.fromOrdinal(${ordinal}) did not fail")
5359
catch
5460
case e: java.lang.reflect.InvocationTargetException => // TODO: maybe reflect.Selectable should catch this?
5561
assert(e.getCause.isInstanceOf[java.util.NoSuchElementException]
56-
&& e.getCause.getMessage == compare.ordinal.toString)
62+
&& e.getCause.getMessage == ordinal.toString)
5763

5864
fetchFromOrdinal(companion = Color, compare = Red, Green, Blue)
65+
fetchFromOrdinal(companion = Suits, compare = Clubs, Spades, Diamonds, Hearts)
5966
fetchFromOrdinal(companion = Tag, compare = Int, String)
6067
fetchFromOrdinal(companion = Expr, compare = EmptyTree, AnyTree)
6168
fetchFromOrdinal(companion = ListLike, compare = EmptyListLike)
@@ -66,6 +73,11 @@ enum ClassOnly: // this should still generate the `ordinal` and `fromOrdinal` co
6673
notFromOrdinal(companion = TypeCtorsK, compare = Const[String]())
6774
notFromOrdinal(companion = ClassOnly, compare = BranchProd(1)) // ClassOnly has the `fromOrdinal` method
6875

76+
cantFind(companion = Color, ordinal = 500) // test default case for enumeration
77+
cantFind(companion = Suits, ordinal = 500) // test default case for Java style enumeration
78+
cantFind(companion = Tag, ordinal = 500) // test default case for mixed adt with non-simple values
79+
cantFind(companion = ClassOnly, ordinal = 500) // should always throw
80+
6981
assert(summon[Mirror.SumOf[ClassOnly]].ordinal(BranchProd(1)) == 0)
7082

7183
val colors: Array[Color] = Color.values

0 commit comments

Comments
 (0)