Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ public enum ErrorMessageID {
ExpectedTypeBoundOrEqualsID,
ClassAndCompanionNameClashID,
TailrecNotApplicableID,
OnlyStaticObjectsCanExtendPhantomID,
;

public int errorNumber() {
Expand Down
18 changes: 18 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1826,4 +1826,22 @@ object messages {
val explanation =
hl"A method annotated ${"@tailrec"} must be declared ${"private"} or ${"final"} so it can't be overridden."
}

case class OnlyStaticObjectsCanExtendPhantom(symbol: Symbol)(implicit ctx: Context)
extends Message(OnlyStaticObjectsCanExtendPhantomID) {
val kind = "Syntax"
val msg = "only static objects can extend scala.Phantom"
val explanation = {
val codeExample = "object MyPhantoms extends Phantom"

hl"""
|Phantom types are definded by an object extending scala.Phantom.
|This object will represent a universe of phantom types that is completely separated from types in scala.Any or other phantom universes.
|We can define our phantom universe MyPhantoms:
|
|$codeExample
|
""".stripMargin
}
}
}
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1427,7 +1427,7 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit

// Check that phantom lattices are defined in a static object
if (cls.classParents.exists(_.typeSymbol eq defn.PhantomClass) && !cls.isStaticOwner)
ctx.error("only static objects can extend scala.Phantom", cdef.pos)
ctx.error(OnlyStaticObjectsCanExtendPhantom(cls), cdef.pos)

// check value class constraints
checkDerivedValueClass(cls, body1)
Expand Down
13 changes: 13 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1051,4 +1051,17 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals("trait G", other.show)

}

@Test def onlyStaticObjectsCanExtendPhantom =
checkMessagesAfter("frontend") {
"class T extends Phantom"
}.expect { (ictx, messages) =>
implicit val ctx: Context = ictx

assertMessageCount(1, messages)

val OnlyStaticObjectsCanExtendPhantom(cls) :: Nil = messages

assertEquals("class T", cls.show)
}
}