Skip to content

Commit 7463afe

Browse files
authored
Merge pull request #5197 from sleepiejohn/error-msg/same-name-static
Add message CheckStatic for member with same name as @static one
2 parents 719f5e2 + 7c1eb58 commit 7463afe

File tree

4 files changed

+30
-3
lines changed

4 files changed

+30
-3
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,8 @@ public enum ErrorMessageID {
134134
TermMemberNeedsNeedsResultTypeForImplicitSearchID,
135135
CaseClassCannotExtendEnumID,
136136
ValueClassParameterMayNotBeCallByNameID,
137-
NotAnExtractorID
137+
NotAnExtractorID,
138+
MemberWithSameNameAsStaticID
138139
;
139140

140141
public int errorNumber() {

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2131,4 +2131,12 @@ object messages {
21312131
|For this reason, you can also define patterns through `unapplySeq` which returns `Option[Seq[T]]`.
21322132
|This mechanism is used for instance in pattern `case List(x1, ..., xn)`""".stripMargin
21332133
}
2134+
2135+
case class MemberWithSameNameAsStatic()(implicit val ctx: Context)
2136+
extends Message(MemberWithSameNameAsStaticID) {
2137+
2138+
override def msg: String = hl"Companion classes cannot define members with same name as a @static member"
2139+
override def kind: String = "Syntax"
2140+
override def explanation: String = ""
2141+
}
21342142
}

compiler/src/dotty/tools/dotc/transform/CheckStatic.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import Contexts.Context
88
import Symbols._
99
import dotty.tools.dotc.ast.tpd
1010
import Decorators._
11-
import reporting.diagnostic.messages.{MissingCompanionForStatic, StaticFieldsOnlyAllowedInObjects}
11+
import reporting.diagnostic.messages.{MemberWithSameNameAsStatic, MissingCompanionForStatic, StaticFieldsOnlyAllowedInObjects}
1212

1313
/** A transformer that check that requirements of Static fields\methods are implemented:
1414
* 1. Only objects can have members annotated with `@static`
@@ -46,7 +46,7 @@ class CheckStatic extends MiniPhase {
4646
if (!companion.exists) {
4747
ctx.error(MissingCompanionForStatic(defn.symbol), defn.pos)
4848
} else if (clashes.exists) {
49-
ctx.error("companion classes cannot define members with same name as @static member", defn.pos)
49+
ctx.error(MemberWithSameNameAsStatic(), defn.pos)
5050
} else if (defn.symbol.is(Flags.Mutable) && companion.is(Flags.Trait)) {
5151
ctx.error("Companions of traits cannot define mutable @static fields", defn.pos)
5252
} else if (defn.symbol.is(Flags.Lazy)) {

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1527,4 +1527,22 @@ class ErrorMessagesTests extends ErrorMessagesTest {
15271527
val NotAnExtractor(tree) = messages.head
15281528
assertEquals("Foo", tree.show)
15291529
}
1530+
1531+
@Test def memberWithSameNameAsStatic() =
1532+
checkMessagesAfter(CheckStatic.name) {
1533+
"""
1534+
|import scala.annotation.static
1535+
|class Camp {
1536+
| val name = ""
1537+
|}
1538+
|object Camp {
1539+
| @static val name = ""
1540+
|}
1541+
""".stripMargin
1542+
}.expect { (_, messages) =>
1543+
assertMessageCount(1, messages)
1544+
val message = messages.head
1545+
assertTrue(message.isInstanceOf[MemberWithSameNameAsStatic])
1546+
assertEquals(message.msg, "Companion classes cannot define members with same name as a @static member")
1547+
}
15301548
}

0 commit comments

Comments
 (0)