Skip to content

Commit 3351f8b

Browse files
Merge pull request #5239 from sleepiejohn/error-msg/check-static
Add error messages for CheckStatic
2 parents 7027670 + b195cb8 commit 3351f8b

File tree

4 files changed

+89
-5
lines changed

4 files changed

+89
-5
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,10 @@ public enum ErrorMessageID {
136136
ValueClassParameterMayNotBeCallByNameID,
137137
NotAnExtractorID,
138138
MemberWithSameNameAsStaticID,
139-
PureExpressionInStatementPositionID
139+
PureExpressionInStatementPositionID,
140+
TraitCompanionWithMutableStaticID,
141+
LazyStaticFieldID,
142+
StaticOverridingNonStaticMembersID
140143
;
141144

142145
public int errorNumber() {

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2149,4 +2149,25 @@ object messages {
21492149
hl"""The pure expression `$stat` doesn't have any side effect and its result is not assigned elsewhere.
21502150
|It can be removed without changing the semantics of the program. This may indicate an error.""".stripMargin
21512151
}
2152+
2153+
case class TraitCompanionWithMutableStatic()(implicit val ctx: Context)
2154+
extends Message(TraitCompanionWithMutableStaticID) {
2155+
override def msg: String = hl"Companion of traits cannot define mutable @static fields"
2156+
override def kind: String = "Syntax"
2157+
override def explanation: String = ""
2158+
}
2159+
2160+
case class LazyStaticField()(implicit val ctx: Context)
2161+
extends Message(LazyStaticFieldID) {
2162+
override def msg: String = hl"Lazy @static fields are not supported"
2163+
override def kind: String = "Syntax"
2164+
override def explanation: String = ""
2165+
}
2166+
2167+
case class StaticOverridingNonStaticMembers()(implicit val ctx: Context)
2168+
extends Message(StaticOverridingNonStaticMembersID) {
2169+
override def msg: String = hl"@static members cannot override or implement non-static ones"
2170+
override def kind: String = "Syntax"
2171+
override def explanation: String = ""
2172+
}
21522173
}

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

Lines changed: 4 additions & 4 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.{MemberWithSameNameAsStatic, MissingCompanionForStatic, StaticFieldsOnlyAllowedInObjects}
11+
import reporting.diagnostic.messages._
1212

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

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

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,4 +1545,64 @@ class ErrorMessagesTests extends ErrorMessagesTest {
15451545
assertTrue(message.isInstanceOf[MemberWithSameNameAsStatic])
15461546
assertEquals(message.msg, "Companion classes cannot define members with same name as a @static member")
15471547
}
1548+
1549+
@Test def companionOfTraitWithMutableStatic() =
1550+
checkMessagesAfter(CheckStatic.name) {
1551+
"""
1552+
| import scala.annotation.static
1553+
| trait Test
1554+
| object Test {
1555+
| @static var myStatic = ""
1556+
| }
1557+
""".stripMargin
1558+
}.expect { (_, messages) =>
1559+
assertMessageCount(1, messages)
1560+
val message = messages.head
1561+
assertTrue(message.isInstanceOf[TraitCompanionWithMutableStatic])
1562+
assertEquals(
1563+
"Companion of traits cannot define mutable @static fields",
1564+
message.msg
1565+
)
1566+
}
1567+
1568+
@Test def lazyStaticField() =
1569+
checkMessagesAfter(CheckStatic.name) {
1570+
"""
1571+
| import scala.annotation.static
1572+
| class Test
1573+
| object Test {
1574+
| @static lazy val myStatic = ""
1575+
| }
1576+
""".stripMargin
1577+
}.expect { (_, messages) =>
1578+
assertMessageCount(1, messages)
1579+
val message = messages.head
1580+
assertTrue(message.isInstanceOf[LazyStaticField])
1581+
assertEquals(
1582+
"Lazy @static fields are not supported",
1583+
message.msg
1584+
)
1585+
}
1586+
1587+
@Test def staticOverridingNonStatic() =
1588+
checkMessagesAfter(CheckStatic.name) {
1589+
"""
1590+
| import scala.annotation.static
1591+
| trait Foo {
1592+
| val foo = ""
1593+
| }
1594+
| class Test
1595+
| object Test extends Foo {
1596+
| @static val foo = ""
1597+
| }
1598+
""".stripMargin
1599+
}.expect { (_, messages) =>
1600+
assertMessageCount(1, messages)
1601+
val message = messages.head
1602+
assertTrue(message.isInstanceOf[StaticOverridingNonStaticMembers])
1603+
assertEquals(
1604+
"@static members cannot override or implement non-static ones",
1605+
message.msg
1606+
)
1607+
}
15481608
}

0 commit comments

Comments
 (0)