Skip to content
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 @@ -134,7 +134,8 @@ public enum ErrorMessageID {
TermMemberNeedsNeedsResultTypeForImplicitSearchID,
CaseClassCannotExtendEnumID,
ValueClassParameterMayNotBeCallByNameID,
NotAnExtractorID
NotAnExtractorID,
MemberWithSameNameAsStaticID
;

public int errorNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2131,4 +2131,12 @@ object messages {
|For this reason, you can also define patterns through `unapplySeq` which returns `Option[Seq[T]]`.
|This mechanism is used for instance in pattern `case List(x1, ..., xn)`""".stripMargin
}

case class MemberWithSameNameAsStatic()(implicit val ctx: Context)
extends Message(MemberWithSameNameAsStaticID) {

override def msg: String = hl"Companion classes cannot define members with same name as a @static member"
override def kind: String = "Syntax"
override def explanation: String = ""
}
}
4 changes: 2 additions & 2 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.{MissingCompanionForStatic, StaticFieldsOnlyAllowedInObjects}
import reporting.diagnostic.messages.{MemberWithSameNameAsStatic, MissingCompanionForStatic, StaticFieldsOnlyAllowedInObjects}

/** 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 @@ -46,7 +46,7 @@ class CheckStatic extends MiniPhase {
if (!companion.exists) {
ctx.error(MissingCompanionForStatic(defn.symbol), defn.pos)
} else if (clashes.exists) {
ctx.error("companion classes cannot define members with same name as @static member", defn.pos)
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)
} else if (defn.symbol.is(Flags.Lazy)) {
Expand Down
18 changes: 18 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1527,4 +1527,22 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val NotAnExtractor(tree) = messages.head
assertEquals("Foo", tree.show)
}

@Test def memberWithSameNameAsStatic() =
checkMessagesAfter(CheckStatic.name) {
"""
|import scala.annotation.static
|class Camp {
| val name = ""
|}
|object Camp {
| @static val name = ""
|}
""".stripMargin
}.expect { (_, messages) =>
assertMessageCount(1, messages)
val message = messages.head
assertTrue(message.isInstanceOf[MemberWithSameNameAsStatic])
assertEquals(message.msg, "Companion classes cannot define members with same name as a @static member")
}
}