Skip to content

Added error message for non object phantom extensions #3779

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
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 @@ -125,6 +125,7 @@ public enum ErrorMessageID {
UnableToEmitSwitchID,
MissingCompanionForStaticID,
PolymorphicMethodMissingTypeInParentID,
NonObjectPhantomTypeID,
;

public int errorNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2074,4 +2074,11 @@ object messages {
|$rsym does not override any method in $parentSym. Structural refinement does not allow for
|polymorphic methods."""
}

case class NonObjectPhantomType()(implicit ctx: Context)
extends Message(NonObjectPhantomTypeID) {
val kind = "Syntax"
val msg = hl"only static objects can extend scala.Phantom"
val explanation = ""
}
}
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 @@ -1504,7 +1504,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(NonObjectPhantomType(), cdef.pos)

// check value class constraints
checkDerivedValueClass(cls, body1)
Expand Down
16 changes: 16 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1294,4 +1294,20 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals("method get", rsym.show)
assertEquals("class Object", parentSym.show)
}

@Test def nonObjectPhantomType =
checkMessagesAfter("frontend") {
"""
|class Boo1 extends Phantom
|trait Boo2 extends Phantom
|object Boo3 extends Phantom
""".stripMargin
}.expect { (ictx, messages) =>
implicit val ctx: Context = ictx

val expectedErr = NonObjectPhantomType()
assertMessageCount(2, messages)
assertEquals(expectedErr, messages(0))
assertEquals(expectedErr, messages(1))
}
}