File tree 4 files changed +25
-3
lines changed
test/dotty/tools/dotc/reporting
4 files changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,7 @@ public enum ErrorMessageID {
119
119
CyclicInheritanceID ,
120
120
UnableToExtendSealedClassID ,
121
121
UnableToEmitSwitchID ,
122
+ MissingCompanionForStaticID ,
122
123
;
123
124
124
125
public int errorNumber () {
Original file line number Diff line number Diff line change @@ -2001,4 +2001,11 @@ object messages {
2001
2001
|- there are less than three cases """
2002
2002
}
2003
2003
}
2004
+
2005
+ case class MissingCompanionForStatic (member : Symbol )(implicit ctx : Context ) extends Message (MissingCompanionForStaticID ) {
2006
+ val msg = hl " ${member.owner} does not have a companion class "
2007
+ val kind = " Syntax"
2008
+ val explanation =
2009
+ hl " An object that contains ${" @static" } members must have a companion class. "
2010
+ }
2004
2011
}
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,17 @@ 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 = ()
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
+ }
1252
1265
}
You can’t perform that action at this time.
0 commit comments