Skip to content

Add error messages for CheckStatic #5239

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
Oct 12, 2018
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
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,10 @@ public enum ErrorMessageID {
ValueClassParameterMayNotBeCallByNameID,
NotAnExtractorID,
MemberWithSameNameAsStaticID,
PureExpressionInStatementPositionID
PureExpressionInStatementPositionID,
TraitCompanionWithMutableStaticID,
LazyStaticFieldID,
StaticOverridingNonStaticMembersID
;

public int errorNumber() {
Expand Down
21 changes: 21 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2149,4 +2149,25 @@ object messages {
hl"""The pure expression `$stat` doesn't have any side effect and its result is not assigned elsewhere.
|It can be removed without changing the semantics of the program. This may indicate an error.""".stripMargin
}

case class TraitCompanionWithMutableStatic()(implicit val ctx: Context)
extends Message(TraitCompanionWithMutableStaticID) {
override def msg: String = hl"Companion of traits cannot define mutable @static fields"
override def kind: String = "Syntax"
override def explanation: String = ""
}

case class LazyStaticField()(implicit val ctx: Context)
extends Message(LazyStaticFieldID) {
override def msg: String = hl"Lazy @static fields are not supported"
override def kind: String = "Syntax"
override def explanation: String = ""
}

case class StaticOverridingNonStaticMembers()(implicit val ctx: Context)
extends Message(StaticOverridingNonStaticMembersID) {
override def msg: String = hl"@static members cannot override or implement non-static ones"
override def kind: String = "Syntax"
override def explanation: String = ""
}
}
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/dotc/transform/CheckStatic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import Contexts.Context
import Symbols._
import dotty.tools.dotc.ast.tpd
import Decorators._
import reporting.diagnostic.messages.{MemberWithSameNameAsStatic, MissingCompanionForStatic, StaticFieldsOnlyAllowedInObjects}
import reporting.diagnostic.messages._

/** A transformer that check that requirements of Static fields\methods are implemented:
* 1. Only objects can have members annotated with `@static`
Expand Down Expand Up @@ -48,11 +48,11 @@ class CheckStatic extends MiniPhase {
} else if (clashes.exists) {
ctx.error(MemberWithSameNameAsStatic(), defn.pos)
} else if (defn.symbol.is(Flags.Mutable) && companion.is(Flags.Trait)) {
ctx.error("Companions of traits cannot define mutable @static fields", defn.pos)
ctx.error(TraitCompanionWithMutableStatic(), defn.pos)
} else if (defn.symbol.is(Flags.Lazy)) {
ctx.error("Lazy @static fields are not supported", defn.pos)
ctx.error(LazyStaticField(), defn.pos)
} else if (defn.symbol.allOverriddenSymbols.nonEmpty) {
ctx.error("@static members cannot override or implement non-static ones", defn.pos)
ctx.error(StaticOverridingNonStaticMembers(), defn.pos)
}
} else hadNonStaticField = hadNonStaticField || defn.isInstanceOf[ValDef]

Expand Down
60 changes: 60 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1545,4 +1545,64 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertTrue(message.isInstanceOf[MemberWithSameNameAsStatic])
assertEquals(message.msg, "Companion classes cannot define members with same name as a @static member")
}

@Test def companionOfTraitWithMutableStatic() =
checkMessagesAfter(CheckStatic.name) {
"""
| import scala.annotation.static
| trait Test
| object Test {
| @static var myStatic = ""
| }
""".stripMargin
}.expect { (_, messages) =>
assertMessageCount(1, messages)
val message = messages.head
assertTrue(message.isInstanceOf[TraitCompanionWithMutableStatic])
assertEquals(
"Companion of traits cannot define mutable @static fields",
message.msg
)
}

@Test def lazyStaticField() =
checkMessagesAfter(CheckStatic.name) {
"""
| import scala.annotation.static
| class Test
| object Test {
| @static lazy val myStatic = ""
| }
""".stripMargin
}.expect { (_, messages) =>
assertMessageCount(1, messages)
val message = messages.head
assertTrue(message.isInstanceOf[LazyStaticField])
assertEquals(
"Lazy @static fields are not supported",
message.msg
)
}

@Test def staticOverridingNonStatic() =
checkMessagesAfter(CheckStatic.name) {
"""
| import scala.annotation.static
| trait Foo {
| val foo = ""
| }
| class Test
| object Test extends Foo {
| @static val foo = ""
| }
""".stripMargin
}.expect { (_, messages) =>
assertMessageCount(1, messages)
val message = messages.head
assertTrue(message.isInstanceOf[StaticOverridingNonStaticMembers])
assertEquals(
"@static members cannot override or implement non-static ones",
message.msg
)
}
}