Skip to content

#1589: add dedicated error message for missing companion for static members #3530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ public enum ErrorMessageID {
CyclicInheritanceID,
UnableToExtendSealedClassID,
UnableToEmitSwitchID,
MissingCompanionForStaticID,
;

public int errorNumber() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2001,4 +2001,11 @@ object messages {
|- there are less than three cases"""
}
}

case class MissingCompanionForStatic(member: Symbol)(implicit ctx: Context) extends Message(MissingCompanionForStaticID) {
val msg = hl"${member.owner} does not have a companion class"
val kind = "Syntax"
val explanation =
hl"An object that contains ${"@static"} members must have a companion class."
}
}
7 changes: 4 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/CheckStatic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import Flags._
import Contexts.Context
import Symbols._
import Constants._
import Denotations._, SymDenotations._
import Denotations._
import SymDenotations._
import Decorators.StringInterpolators
import dotty.tools.dotc.ast.tpd
import dotty.tools.dotc.core.Annotations.ConcreteAnnotation
Expand All @@ -21,7 +22,7 @@ import Names.Name
import NameOps._
import Decorators._
import TypeUtils._
import reporting.diagnostic.messages.StaticFieldsOnlyAllowedInObjects
import reporting.diagnostic.messages.{MissingCompanionForStatic, StaticFieldsOnlyAllowedInObjects}

/** A transformer that check that requirements of Static fields\methods are implemented:
* 1. Only objects can have members annotated with `@static`
Expand Down Expand Up @@ -57,7 +58,7 @@ class CheckStatic extends MiniPhase {
def clashes = companion.asClass.membersNamed(defn.name)

if (!companion.exists) {
ctx.error("object that contains @static members should have companion class", defn.pos)
ctx.error(MissingCompanionForStatic(defn.symbol), defn.pos)
} else if (clashes.exists) {
ctx.error("companion classes cannot define members with same name as @static member", defn.pos)
} else if (defn.symbol.is(Flags.Mutable) && companion.is(Flags.Trait)) {
Expand Down
13 changes: 13 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1249,4 +1249,17 @@ class ErrorMessagesTests extends ErrorMessagesTest {
val CyclicInheritance(symbol, _) :: Nil = messages
assertEquals("class A", symbol.show)
}

@Test def missingCompanionForStatic =
checkMessagesAfter("checkStatic") {
"""
|object Foo {
| @annotation.static def bar(): Unit = ()
|}
""".stripMargin
}.expect { (itcx, messages) =>
implicit val ctx: Context = itcx
val MissingCompanionForStatic(member) = messages.head
assertEquals(member.show, "method bar")
}
}