Skip to content

Error message for enum case in non-enum class companion object #3197

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 8 commits into from
Oct 11, 2017
Merged
Show file tree
Hide file tree
Changes from 7 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,15 @@ 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"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You shouldn't backtick owner and enum

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be fixed

val explanation =
hl"""
Copy link
Contributor

@allanrenucci allanrenucci Oct 3, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Start your message here and remove the whitespaces after |

| `${"case"}` is only allowed at this place if the surrounding object is the companion object of an `${"enum"} ${"class"}`.
| If you wanted to create an enumeration make sure that the corresponding class has the `${"enum"}` keyword.
| Otherwise you might have forgotten `${"class"}` or `${"object"}` after this `${"case"}` here.
"""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer something like:

  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."""

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's take that.

}
}
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)
}
}