Skip to content

Commit 13ed1b5

Browse files
Backport "Improve failure message of enum fromOrdinal/valueOf" to LTS (#20802)
Backports #19182 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 3972a02 + d39100d commit 13ed1b5

File tree

6 files changed

+24
-5
lines changed

6 files changed

+24
-5
lines changed

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

+3-2
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ object DesugarEnums {
126126

127127
val valuesOfBody: Tree =
128128
val defaultCase =
129-
val msg = Apply(Select(Literal(Constant("enum case not found: ")), nme.PLUS), Ident(nme.nameDollar))
129+
val msg = Apply(Select(Literal(Constant(s"enum ${enumClass.fullName} has no case with name: ")), nme.PLUS), Ident(nme.nameDollar))
130130
CaseDef(Ident(nme.WILDCARD), EmptyTree,
131131
Throw(New(TypeTree(defn.IllegalArgumentExceptionType), List(msg :: Nil))))
132132
val stringCases = enumValues.map(enumValue =>
@@ -148,7 +148,8 @@ object DesugarEnums {
148148
def valueCtor: List[Tree] = if constraints.requiresCreator then enumValueCreator :: Nil else Nil
149149
def fromOrdinal: Tree =
150150
def throwArg(ordinal: Tree) =
151-
Throw(New(TypeTree(defn.NoSuchElementExceptionType), List(Select(ordinal, nme.toString_) :: Nil)))
151+
val msg = Apply(Select(Literal(Constant(s"enum ${enumClass.fullName} has no case with ordinal: ")), nme.PLUS), Select(ordinal, nme.toString_))
152+
Throw(New(TypeTree(defn.NoSuchElementExceptionType), List(msg :: Nil)))
152153
if !constraints.cached then
153154
fromOrdinalMeth(throwArg)
154155
else

tests/run/enum-java.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ MONDAY : 0
2626
TUESDAY : 1
2727
SATURDAY : 2
2828
By-name value: MONDAY
29-
Correctly failed to retrieve illegal name, message: enum case not found: stuff
29+
Correctly failed to retrieve illegal name, message: enum A has no case with name: stuff
3030

3131
Collections Test
3232
Retrieving Monday: workday

tests/run/enum-values.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ enum ClassOnly: // this should still generate the `ordinal` and `fromOrdinal` co
6161
catch
6262
case e: java.lang.reflect.InvocationTargetException => // TODO: maybe reflect.Selectable should catch this?
6363
assert(e.getCause.isInstanceOf[java.util.NoSuchElementException]
64-
&& e.getCause.getMessage == ordinal.toString)
64+
&& e.getCause.getMessage == s"enum ${companion.getClass.getName.stripSuffix("$")} has no case with ordinal: $ordinal")
6565

6666
fetchFromOrdinal(companion = Color, compare = Red, Green, Blue)
6767
fetchFromOrdinal(companion = Suits, compare = Clubs, Spades, Diamonds, Hearts)

tests/run/enums-java-compat.check

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ TUESDAY : 1
66
SATURDAY : 2
77
Stuff : 3
88
By-name value: MONDAY
9-
Correctly failed to retrieve illegal name, message: enum case not found: stuff
9+
Correctly failed to retrieve illegal name, message: enum A has no case with name: stuff

tests/run/i19178.check

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Failure(java.util.NoSuchElementException: enum Foo has no case with ordinal: 3)
2+
Failure(java.lang.IllegalArgumentException: enum Foo has no case with name: Bar)
3+
Failure(java.util.NoSuchElementException: enum bar.Bar has no case with ordinal: 4)
4+
Failure(java.lang.IllegalArgumentException: enum bar.Bar has no case with name: Baz)

tests/run/i19178.scala

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
enum Foo:
2+
case A
3+
4+
package bar {
5+
enum Bar:
6+
case B
7+
}
8+
9+
@main def Test =
10+
import scala.util.Try
11+
println(Try(Foo.fromOrdinal(3)))
12+
println(Try(Foo.valueOf("Bar")))
13+
println(Try(bar.Bar.fromOrdinal(4)))
14+
println(Try(bar.Bar.valueOf("Baz")))

0 commit comments

Comments
 (0)