Skip to content

Commit 5674a46

Browse files
authored
Merge pull request #3516 from esarbe/staticOnly
#1589: add dedicated error message for @static annotation not inside object
2 parents bb1fe9a + 5702a28 commit 5674a46

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ public enum ErrorMessageID {
115115
TraitRedefinedFinalMethodFromAnyRefID,
116116
PackageNameAlreadyDefinedID,
117117
UnapplyInvalidNumberOfArgumentsID,
118+
StaticFieldsOnlyAllowedInObjectsID,
118119
;
119120

120121
public int errorNumber() {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,4 +1942,11 @@ object messages {
19421942
|where subsequent arguments would have following types: ($argTypes%, %).
19431943
|""".stripMargin
19441944
}
1945+
1946+
case class StaticFieldsOnlyAllowedInObjects(member: Symbol)(implicit ctx: Context) extends Message(StaticFieldsOnlyAllowedInObjectsID) {
1947+
val msg = hl"${"@static"} $member in ${member.owner} must be defined inside an ${"object"}."
1948+
val kind = "Syntax"
1949+
val explanation =
1950+
hl"${"@static"} members are only allowed inside objects."
1951+
}
19451952
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Names.Name
2121
import NameOps._
2222
import Decorators._
2323
import TypeUtils._
24+
import reporting.diagnostic.messages.StaticFieldsOnlyAllowedInObjects
2425

2526
/** A transformer that check that requirements of Static fields\methods are implemented:
2627
* 1. Only objects can have members annotated with `@static`
@@ -45,7 +46,7 @@ class CheckStatic extends MiniPhase {
4546
for(defn <- defns) {
4647
if (defn.symbol.hasAnnotation(ctx.definitions.ScalaStaticAnnot)) {
4748
if(!ctx.owner.is(Module)) {
48-
ctx.error("@static fields are only allowed inside objects", defn.pos)
49+
ctx.error(StaticFieldsOnlyAllowedInObjects(defn.symbol), defn.pos)
4950
}
5051

5152
if (defn.isInstanceOf[ValDef] && hadNonStaticField) {

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,4 +1224,18 @@ class ErrorMessagesTests extends ErrorMessagesTest {
12241224
assertEquals("Boo", qual.show)
12251225
assertEquals("(class Int, class String)", argTypes.map(_.typeSymbol).mkString("(", ", ", ")"))
12261226
}
1227+
1228+
1229+
@Test def staticOnlyAllowedInsideObjects =
1230+
checkMessagesAfter("checkStatic") {
1231+
"""
1232+
|class Foo {
1233+
| @annotation.static def bar(): Unit = bar()
1234+
|}
1235+
""".stripMargin
1236+
}.expect { (ictx, messages) =>
1237+
implicit val ctx: Context = ictx
1238+
val StaticFieldsOnlyAllowedInObjects(field) = messages.head
1239+
assertEquals(field.show, "method bar")
1240+
}
12271241
}

0 commit comments

Comments
 (0)