Skip to content

Fix #5034: Add error on lazy enum #6674

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ object DesugarEnums {

/** Add implied flags to an enum class or an enum case */
def addEnumFlags(cdef: TypeDef)(implicit ctx: Context): TypeDef =
if (cdef.mods.isEnumClass) cdef.withMods(cdef.mods.withFlags(cdef.mods.flags | Abstract | Sealed))
else if (isEnumCase(cdef)) cdef.withMods(cdef.mods.withFlags(cdef.mods.flags | Final))
if (cdef.mods.isEnumClass) cdef.withMods(cdef.mods.withFlags(cdef.mods.flags.toTypeFlags | Abstract | Sealed))
else if (isEnumCase(cdef)) cdef.withMods(cdef.mods | Final)
else cdef

private def valuesDot(name: PreName)(implicit src: SourceFile) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2259,6 +2259,13 @@ object messages {
override def explanation: String = ""
}

case class LazyEnum()(implicit val ctx: Context)
extends Message(LazyStaticFieldID) {
override def msg: String = "modifier lazy is not allowed for enums"
override def kind: String = "Syntax"
override def explanation: String = ""
}

case class StaticOverridingNonStaticMembers()(implicit val ctx: Context)
extends Message(StaticOverridingNonStaticMembersID) {
override def msg: String = em"${hl("@static")} members cannot override or implement non-static ones"
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,9 @@ object Checking {
checkCombination(Lazy, Inline)
checkNoConflict(Lazy, ParamAccessor, s"parameter may not be `lazy`")
if (sym.is(Inline)) checkApplicable(Inline, sym.isTerm && !sym.is(Mutable | Module))
if (sym.is(Lazy)) checkApplicable(Lazy, !sym.is(Method | Mutable))
if(sym.is(Enum) && sym.flags.toTermFlags.is(Lazy)) {
fail(LazyEnum())
}
if (sym.isType && !sym.is(Deferred))
for (cls <- sym.allOverriddenSymbols.filter(_.isClass)) {
fail(CannotHaveSameNameAs(sym, cls, CannotHaveSameNameAs.CannotBeOverridden))
Expand Down
3 changes: 3 additions & 0 deletions tests/neg/i5034.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
lazy enum Color { // error
case Blue
}

This file was deleted.