Skip to content
Merged
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
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/ast/DesugarEnums.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import core._
import util.Positions._, Types._, Contexts._, Constants._, Names._, NameOps._, Flags._
import SymDenotations._, Symbols._, StdNames._, Annotations._, Trees._
import Decorators._
import reporting.diagnostic.messages.EnumCaseDefinitionInNonEnumOwner
import collection.mutable.ListBuffer
import util.Property
import typer.ErrorReporting._
Expand All @@ -32,7 +33,7 @@ object DesugarEnums {
/** Is enum case `tree` situated in a companion object of an enum class? */
def enumCaseIsLegal(tree: Tree)(implicit ctx: Context): Boolean = (
ctx.owner.is(ModuleClass) && enumClass.derivesFrom(defn.EnumClass)
|| { ctx.error(em"case not allowed here, since owner ${ctx.owner} is not an `enum' object", tree.pos)
|| { ctx.error(EnumCaseDefinitionInNonEnumOwner(ctx.owner), tree.pos)
false
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ public enum ErrorMessageID {
ReturnOutsideMethodDefinitionID,
UncheckedTypePatternID,
ExtendFinalClassID,
EnumCaseDefinitionInNonEnumOwnerID,
;

public int errorNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1777,5 +1777,13 @@ object messages {
val explanation =
hl"""A class marked with the ${"final"} keyword cannot be extended"""
}

case class EnumCaseDefinitionInNonEnumOwner(owner: Symbol)(implicit ctx: Context)
extends Message(EnumCaseDefinitionInNonEnumOwnerID) {
val kind = "Syntax"
val msg = em"case not allowed here, since owner ${owner} is not an ${"enum"} object"
val explanation =
hl"""${"enum"} cases are only allowed within the companion ${"object"} of an ${"enum class"}.
|If you want to create an ${"enum"} case, make sure the corresponding ${"enum class"} exists
|and has the ${"enum"} keyword."""
}
}
14 changes: 14 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -998,6 +998,7 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val ReturnOutsideMethodDefinition(owner) :: Nil = messages
assertEquals("object A", owner.show)
}

@Test def extendFinalClass = checkMessagesAfter("refchecks") {
"""final class A
|
Expand All @@ -1010,4 +1011,17 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals(extender.show, "class B")
assertEquals(parent.show, "class A")
}

@Test def enumCaseDefinitionInNonEnumOwner =
checkMessagesAfter("frontend") {
"""object Qux {
| case Foo
|}
""".stripMargin
}.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val EnumCaseDefinitionInNonEnumOwner(owner) :: Nil = messages
assertEquals("object Qux", owner.show)
}
}