File tree Expand file tree Collapse file tree 4 files changed +25
-3
lines changed
test/dotty/tools/dotc/reporting Expand file tree Collapse file tree 4 files changed +25
-3
lines changed Original file line number Diff line number Diff line change @@ -119,6 +119,7 @@ public enum ErrorMessageID {
119119 CyclicInheritanceID ,
120120 UnableToExtendSealedClassID ,
121121 UnableToEmitSwitchID ,
122+ MissingCompanionForStaticID ,
122123 ;
123124
124125 public int errorNumber () {
Original file line number Diff line number Diff line change @@ -2001,4 +2001,11 @@ object messages {
20012001 |- there are less than three cases """
20022002 }
20032003 }
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+ }
20042011}
Original file line number Diff line number Diff line change @@ -11,7 +11,8 @@ import Flags._
1111import Contexts .Context
1212import Symbols ._
1313import Constants ._
14- import Denotations ._ , SymDenotations ._
14+ import Denotations ._
15+ import SymDenotations ._
1516import Decorators .StringInterpolators
1617import dotty .tools .dotc .ast .tpd
1718import dotty .tools .dotc .core .Annotations .ConcreteAnnotation
@@ -21,7 +22,7 @@ import Names.Name
2122import NameOps ._
2223import Decorators ._
2324import TypeUtils ._
24- import reporting .diagnostic .messages .StaticFieldsOnlyAllowedInObjects
25+ import reporting .diagnostic .messages .{ MissingCompanionForStatic , StaticFieldsOnlyAllowedInObjects }
2526
2627/** A transformer that check that requirements of Static fields\methods are implemented:
2728 * 1. Only objects can have members annotated with `@static`
@@ -57,7 +58,7 @@ class CheckStatic extends MiniPhase {
5758 def clashes = companion.asClass.membersNamed(defn.name)
5859
5960 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)
6162 } else if (clashes.exists) {
6263 ctx.error(" companion classes cannot define members with same name as @static member" , defn.pos)
6364 } 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 {
12491249 val CyclicInheritance (symbol, _) :: Nil = messages
12501250 assertEquals(" class A" , symbol.show)
12511251 }
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+ }
12521265}
You can’t perform that action at this time.
0 commit comments