File tree Expand file tree Collapse file tree 4 files changed +35
-3
lines changed
test/dotty/tools/dotc/reporting Expand file tree Collapse file tree 4 files changed +35
-3
lines changed Original file line number Diff line number Diff line change @@ -118,6 +118,7 @@ public enum ErrorMessageID {
118
118
StaticFieldsOnlyAllowedInObjectsID ,
119
119
CyclicInheritanceID ,
120
120
UnableToExtendSealedClassID ,
121
+ MissingCompanionForStaticID ,
121
122
;
122
123
123
124
public int errorNumber () {
Original file line number Diff line number Diff line change @@ -1972,4 +1972,11 @@ object messages {
1972
1972
val msg = hl " Cannot extend ${" sealed" } $pclazz in a different source file "
1973
1973
val explanation = " A sealed class or trait can only be extended in the same file as its declaration"
1974
1974
}
1975
+
1976
+ case class MissingCompanionForStatic (member : Symbol )(implicit ctx : Context ) extends Message (MissingCompanionForStaticID ) {
1977
+ val msg = hl " ${member.owner} does not have a companion class "
1978
+ val kind = " Syntax"
1979
+ val explanation =
1980
+ hl " object that contains @static members must have a companion class. "
1981
+ }
1975
1982
}
Original file line number Diff line number Diff line change @@ -11,7 +11,8 @@ import Flags._
11
11
import Contexts .Context
12
12
import Symbols ._
13
13
import Constants ._
14
- import Denotations ._ , SymDenotations ._
14
+ import Denotations ._
15
+ import SymDenotations ._
15
16
import Decorators .StringInterpolators
16
17
import dotty .tools .dotc .ast .tpd
17
18
import dotty .tools .dotc .core .Annotations .ConcreteAnnotation
@@ -21,7 +22,7 @@ import Names.Name
21
22
import NameOps ._
22
23
import Decorators ._
23
24
import TypeUtils ._
24
- import reporting .diagnostic .messages .StaticFieldsOnlyAllowedInObjects
25
+ import reporting .diagnostic .messages .{ MissingCompanionForStatic , StaticFieldsOnlyAllowedInObjects }
25
26
26
27
/** A transformer that check that requirements of Static fields\methods are implemented:
27
28
* 1. Only objects can have members annotated with `@static`
@@ -57,7 +58,7 @@ class CheckStatic extends MiniPhase {
57
58
def clashes = companion.asClass.membersNamed(defn.name)
58
59
59
60
if (! companion.exists) {
60
- ctx.error(" object that contains @static members should have companion class " , defn.pos)
61
+ ctx.error(MissingCompanionForStatic (defn.symbol) , defn.pos)
61
62
} else if (clashes.exists) {
62
63
ctx.error(" companion classes cannot define members with same name as @static member" , defn.pos)
63
64
} else if (defn.symbol.is(Flags .Mutable ) && companion.is(Flags .Trait )) {
Original file line number Diff line number Diff line change @@ -1249,4 +1249,27 @@ class ErrorMessagesTests extends ErrorMessagesTest {
1249
1249
val CyclicInheritance (symbol, _) :: Nil = messages
1250
1250
assertEquals(" class A" , symbol.show)
1251
1251
}
1252
+
1253
+ @ Test def missingCompanionForStatic =
1254
+ checkMessagesAfter(" checkStatic" ) {
1255
+ """
1256
+ |object Foo {
1257
+ | @annotation.static def bar(): Unit = bar()
1258
+ |}
1259
+ """ .stripMargin
1260
+ }.expect { (itcx, messages) =>
1261
+ implicit val ctx : Context = itcx
1262
+ val MissingCompanionForStatic (member) = messages.head
1263
+ assertEquals(member.show, " method bar" )
1264
+ }
1265
+
1266
+ @ Test def notMissingCompanionForStatic =
1267
+ checkMessagesAfter(" checkStatic" ) {
1268
+ """
1269
+ |object Foo {
1270
+ | @annotation.static def bar(): Unit = bar()
1271
+ |}
1272
+ |class Foo
1273
+ """ .stripMargin
1274
+ }.expectNoErrors
1252
1275
}
You can’t perform that action at this time.
0 commit comments