Skip to content

remove unchecked annotation from enum $values array #12293

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 30, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 2 additions & 10 deletions compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ object DesugarEnums {

/** The following lists of definitions for an enum type E and known value cases e_0, ..., e_n:
*
* private val $values = Array[E](this.e_0,...,this.e_n)(ClassTag[E](classOf[E])): @unchecked
* private val $values = Array[E](this.e_0,...,this.e_n)(ClassTag[E](classOf[E]))
* def values = $values.clone
* def valueOf($name: String) = $name match {
* case "e_0" => this.e_0
Expand All @@ -118,15 +118,7 @@ object DesugarEnums {
extension (tpe: NamedType) def ofRawEnum = AppliedTypeTree(ref(tpe), rawEnumClassRef)

val privateValuesDef =
val uncheckedValues =
// Here we use an unchecked annotation to silence warnings from the init checker. Without it, we get a warning
// that simple enum cases are promoting this from warm to initialised. This is because we are populating the
// array by selecting enum values from `this`, a value under construction.
// Singleton enum values always construct a new anonymous class, which will not be checked by the init-checker,
// so this warning will always persist even if the implementation of the anonymous class is safe.
// TODO: remove @unchecked after https://github.com/lampepfl/dotty-feature-requests/issues/135 is resolved.
Annotated(ArrayLiteral(enumValues, rawEnumClassRef), New(ref(defn.UncheckedAnnot.typeRef), Nil))
ValDef(nme.DOLLAR_VALUES, TypeTree(), uncheckedValues)
ValDef(nme.DOLLAR_VALUES, TypeTree(), ArrayLiteral(enumValues, rawEnumClassRef))
.withFlags(Private | Synthetic)

val valuesDef =
Expand Down