Skip to content

Commit f1e3fd4

Browse files
committed
add comment about unchecked annotation
1 parent 5f40270 commit f1e3fd4

File tree

1 file changed

+9
-3
lines changed

1 file changed

+9
-3
lines changed

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

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,12 @@ object DesugarEnums {
104104

105105
/** The following lists of definitions for an enum type E and known value cases e_0, ..., e_n:
106106
*
107-
* private val $values = Array[E](e_0,...,e_n)(ClassTag[E](classOf[E])): @unchecked
107+
* private val $values = Array[E](this.e_0,...,this.e_n)(ClassTag[E](classOf[E])): @unchecked
108108
* def values = $values.clone
109109
* def valueOf($name: String) = $name match {
110-
* case "e_0" => e_0
110+
* case "e_0" => this.e_0
111111
* ...
112-
* case "e_n" => e_n
112+
* case "e_n" => this.e_n
113113
* case _ => throw new IllegalArgumentException("case not found: " + $name)
114114
* }
115115
*/
@@ -119,6 +119,12 @@ object DesugarEnums {
119119

120120
val privateValuesDef =
121121
val uncheckedValues =
122+
// Here we use an unchecked annotation to silence warnings from the init checker. Without it, we get a warning
123+
// that simple enum cases are promoting this from warm to initialised. This is because we are populating the
124+
// array by selecting enum values from `this`, a value under construction.
125+
// Singleton enum values always construct a new anonymous class, which will not be checked by the init-checker,
126+
// so this warning will always persist even if the implementation of the anonymous class is safe.
127+
// TODO: We should remove the unchecked annotation when the init checker can look within anonymous classes.
122128
Annotated(ArrayLiteral(enumValues, rawEnumClassRef), New(ref(defn.UncheckedAnnot.typeRef)))
123129
ValDef(nme.DOLLAR_VALUES, TypeTree(), uncheckedValues)
124130
.withFlags(Private | Synthetic)

0 commit comments

Comments
 (0)