Skip to content

Fix safe-init error in DesugarEnums #14571

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
Feb 26, 2022
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
21 changes: 10 additions & 11 deletions compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,20 @@ import scala.annotation.internal.sharable
object DesugarEnums {
import untpd._

@sharable object CaseKind extends Enumeration {
val Simple, Object, Class: Value = Value
}
enum CaseKind:
case Simple, Object, Class

final case class EnumConstraints(minKind: CaseKind.Value, maxKind: CaseKind.Value, enumCases: List[(Int, RefTree)]):
require(minKind <= maxKind && !(cached && enumCases.isEmpty))
final case class EnumConstraints(minKind: CaseKind, maxKind: CaseKind, enumCases: List[(Int, RefTree)]):
require(minKind.ordinal <= maxKind.ordinal && !(cached && enumCases.isEmpty))
def requiresCreator = minKind == CaseKind.Simple
def isEnumeration = maxKind < CaseKind.Class
def cached = minKind < CaseKind.Class
def isEnumeration = maxKind.ordinal < CaseKind.Class.ordinal
def cached = minKind.ordinal < CaseKind.Class.ordinal
end EnumConstraints

/** Attachment containing the number of enum cases, the smallest kind that was seen so far,
* and a list of all the value cases with their ordinals.
*/
val EnumCaseCount: Property.Key[(Int, CaseKind.Value, CaseKind.Value, List[(Int, TermName)])] = Property.Key()
val EnumCaseCount: Property.Key[(Int, CaseKind, CaseKind, List[(Int, TermName)])] = Property.Key()

/** Attachment signalling that when this definition is desugared, it should add any additional
* lookup methods for enums.
Expand Down Expand Up @@ -249,11 +248,11 @@ object DesugarEnums {
* - scaffolding containing the necessary definitions for singleton enum cases
* unless that scaffolding was already generated by a previous call to `nextEnumKind`.
*/
def nextOrdinal(name: Name, kind: CaseKind.Value, definesLookups: Boolean)(using Context): (Int, List[Tree]) = {
def nextOrdinal(name: Name, kind: CaseKind, definesLookups: Boolean)(using Context): (Int, List[Tree]) = {
val (ordinal, seenMinKind, seenMaxKind, seenCases) =
ctx.tree.removeAttachment(EnumCaseCount).getOrElse((0, CaseKind.Class, CaseKind.Simple, Nil))
val minKind = if kind < seenMinKind then kind else seenMinKind
val maxKind = if kind > seenMaxKind then kind else seenMaxKind
val minKind = if kind.ordinal < seenMinKind.ordinal then kind else seenMinKind
val maxKind = if kind.ordinal > seenMaxKind.ordinal then kind else seenMaxKind
val cases = name match
case name: TermName => (ordinal, name) :: seenCases
case _ => seenCases
Expand Down