Skip to content

Port Desugar error messages to the new scheme #8243

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 7, 2020
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
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ object desugar {
if (isEnum) {
val (enumCases, enumStats) = stats.partition(DesugarEnums.isEnumCase)
if (enumCases.isEmpty)
ctx.error("Enumerations must contain at least one case", namePos)
ctx.error(EnumerationsShouldNotBeEmpty(cdef), namePos)
val enumCompanionRef = TermRefTree()
val enumImport =
Import(enumCompanionRef, enumCases.flatMap(caseIds).map(ImportSelector(_)))
Expand Down Expand Up @@ -883,12 +883,12 @@ object desugar {
def flagSourcePos(flag: FlagSet) = mods.mods.find(_.flags == flag).fold(mdef.sourcePos)(_.sourcePos)

if (mods.is(Abstract))
ctx.error(em"${hl("abstract")} modifier cannot be used for objects", flagSourcePos(Abstract))
ctx.error(AbstractCannotBeUsedForObjects(mdef), flagSourcePos(Abstract))
if (mods.is(Sealed))
ctx.error(em"${hl("sealed")} modifier is redundant for objects", flagSourcePos(Sealed))
ctx.error(ModifierRedundantForObjects(mdef, "sealed"), flagSourcePos(Sealed))
// Maybe this should be an error; see https://github.com/scala/bug/issues/11094.
if (mods.is(Final) && !mods.is(Synthetic))
ctx.warning(em"${hl("final")} modifier is redundant for objects", flagSourcePos(Final))
ctx.warning(ModifierRedundantForObjects(mdef, "final"), flagSourcePos(Final))

if (mods.is(Package))
packageModuleDef(mdef)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ enum ErrorMessageID extends java.lang.Enum[ErrorMessageID] {
MissingTypeParameterInTypeAppID,
ImplicitTypesCanOnlyBeFunctionTypesID,
ErasedTypesCanOnlyBeFunctionTypesID,
CaseClassMissingNonImplicitParamListID
CaseClassMissingNonImplicitParamListID,
EnumerationsShouldNotBeEmptyID,
AbstractCannotBeUsedForObjectsID,
ModifierRedundantForObjectsID

def errorNumber = ordinal - 2
}
42 changes: 42 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2408,4 +2408,46 @@ object messages {
| if you're aiming to have a case class parametrized only by implicit ones, you should
| add an explicit ${hl("()")} as a parameter list to ${cdef.name}.""".stripMargin
}

case class EnumerationsShouldNotBeEmpty(cdef: untpd.TypeDef)(implicit ctx: Context)
extends Message(EnumerationsShouldNotBeEmptyID) {
val kind: String = "Syntax"
val msg: String = "Enumerations must contain at least one case"

val explanation: String =
em"""|Enumeration ${cdef.name} must contain at least one case
|Example Usage:
| ${hl("enum")} ${cdef.name} {
| ${hl("case")} Option1, Option2
| }
|""".stripMargin
}

case class AbstractCannotBeUsedForObjects(mdef: untpd.ModuleDef)(implicit ctx: Context)
extends Message(AbstractCannotBeUsedForObjectsID) {
val kind: String = "Syntax"
val msg: String = em"${hl("abstract")} modifier cannot be used for objects"

val explanation: String =
em"""|Objects are final and cannot be extended, thus cannot have the ${hl("abstract")} modifier
|
|You may want to define an abstract class:
| ${hl("abstract")} ${hl("class")} Abstract${mdef.name} { }
|
|And extend it in an object:
| ${hl("object")} ${mdef.name} ${hl("extends")} Abstract${mdef.name} { }
|""".stripMargin
}

case class ModifierRedundantForObjects(mdef: untpd.ModuleDef, modifier: String)(implicit ctx: Context)
extends Message(ModifierRedundantForObjectsID) {
val kind: String = "Syntax"
val msg: String = em"${hl(modifier)} modifier is redundant for objects"

val explanation: String =
em"""|Objects cannot be extended making the ${hl(modifier)} modifier redundant.
|You may want to define the object without it:
| ${hl("object")} ${mdef.name} { }
|""".stripMargin
}
}
45 changes: 45 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1738,4 +1738,49 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val CaseClassMissingNonImplicitParamList(tpe) :: Nil = messages
assertEquals("A case class must have at least one non-implicit parameter list", messages.head.msg)
}

@Test def enumMustContainOneCase =
checkMessagesAfter(RefChecks.name) {
"""
|enum Foo { }
""".stripMargin
}
.expect { (ictx, messages) ⇒
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val errorMsg = messages.head.msg
val EnumerationsShouldNotBeEmpty(typeDef) :: Nil = messages
assertEquals("Enumerations must contain at least one case", errorMsg)
assertEquals("Foo", typeDef.name.toString)
}

@Test def objectsCannotBeAbstract =
checkMessagesAfter(RefChecks.name) {
"""
|abstract object Foo { }
""".stripMargin
}
.expect { (ictx, messages) ⇒
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val errorMsg = messages.head.msg
val AbstractCannotBeUsedForObjects(mdef) :: Nil = messages
assertEquals("abstract modifier cannot be used for objects", errorMsg)
assertEquals("Foo", mdef.name.toString)
}

@Test def sealedOnObjectsIsRedundant =
checkMessagesAfter(RefChecks.name) {
"""
|sealed object Foo { }
""".stripMargin
}
.expect { (ictx, messages) ⇒
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val errorMsg = messages.head.msg
val ModifierRedundantForObjects(mdef, "sealed") :: Nil = messages
assertEquals("sealed modifier is redundant for objects", errorMsg)
assertEquals("Foo", mdef.name.toString)
}
}
6 changes: 6 additions & 0 deletions tests/neg/AbstractObject.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- [E146] Syntax Error: tests/neg/AbstractObject.scala:1:0 -------------------------------------------------------------
1 |abstract object A {} // error
|^^^^^^^^
|abstract modifier cannot be used for objects

longer explanation available when compiling with `-explain`
1 change: 1 addition & 0 deletions tests/neg/AbstractObject.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
abstract object A {} // error
6 changes: 6 additions & 0 deletions tests/neg/EmptyEnum.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- [E145] Syntax Error: tests/neg/EmptyEnum.scala:1:5 ------------------------------------------------------------------
1 |enum EmptyEnum {} // error
| ^^^^^^^^^
| Enumerations must contain at least one case

longer explanation available when compiling with `-explain`
1 change: 1 addition & 0 deletions tests/neg/EmptyEnum.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
enum EmptyEnum {} // error
6 changes: 6 additions & 0 deletions tests/neg/SealedObject.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- [E147] Syntax Error: tests/neg/SealedObject.scala:1:0 ---------------------------------------------------------------
1 |sealed object A {} // error
|^^^^^^
|sealed modifier is redundant for objects

longer explanation available when compiling with `-explain`
1 change: 1 addition & 0 deletions tests/neg/SealedObject.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sealed object A {} // error