From 34e3508cccbbcc49554a4c5b13419769cc3c7bf9 Mon Sep 17 00:00:00 2001 From: Enno Runne Date: Sat, 4 Mar 2017 16:32:33 +0100 Subject: Explanations for recursive/cyclic type requirements --- .../tools/dotc/reporting/diagnostic/messages.scala | 44 ++++++++-------- .../tools/dotc/reporting/ErrorMessagesTests.scala | 58 ++++++++++++++++++++++ 2 files changed, 81 insertions(+), 21 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index f2f69b6cc..9d899923b 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1146,46 +1146,48 @@ object messages { |""".stripMargin } + case class AnnotatedPrimaryConstructorRequiresModifierOrThis(cls: Name)(implicit ctx: Context) + extends Message(AnnotatedPrimaryConstructorRequiresModifierOrThisID) { + val kind = "Syntax" + val msg = hl"""${"private"}, ${"protected"}, or ${"this"} expected for annotated primary constructor""" + val explanation = + hl"""|When using annotations with a primary constructor of a class, + |the annotation must be followed by an access modifier + |(${"private"} or ${"protected"}) or ${"this"}. + | + |For example: + | ${"class Sample @deprecated this(param: Parameter) { ..."} + | ^^^^ + |""".stripMargin + } + case class OverloadedOrRecursiveMethodNeedsResultType(tree: Names.TermName)(implicit ctx: Context) extends Message(OverloadedOrRecursiveMethodNeedsResultTypeID) { val kind = "Syntax" val msg = hl"""overloaded or recursive method ${tree} needs result type""" val explanation = - hl""" - | - """.stripMargin + hl"""|${tree} is overloaded and at least one definition of it calls another. + |You need to specify the calling method's return type. + """.stripMargin } case class RecursiveValueNeedsResultType(tree: Names.TermName)(implicit ctx: Context) - extends Message(RecursiveValueNeedsResultTypeID) { + extends Message(RecursiveValueNeedsResultTypeID) { val kind = "Syntax" val msg = hl"""recursive value ${tree.name} needs type""" val explanation = - hl"""""".stripMargin + hl"""|The definition of `${tree.name}` is recursive and you need to specify its type. + """.stripMargin } case class CyclicReferenceInvolvingImplicit(cycleSym: Symbol)(implicit ctx: Context) - extends Message(CyclicReferenceInvolvingImplicitID) { + extends Message(CyclicReferenceInvolvingImplicitID) { val kind = "Syntax" val msg = hl"""cyclic reference involving implicit $cycleSym""" val explanation = hl"""|This happens when the right hand-side of $cycleSym's definition involves an implicit search. - |To avoid the error, give $cycleSym an explicit type. + |To avoid this error, give `${cycleSym.name}` an explicit type. |""".stripMargin } - case class AnnotatedPrimaryConstructorRequiresModifierOrThis(cls: Name)(implicit ctx: Context) - extends Message(AnnotatedPrimaryConstructorRequiresModifierOrThisID) { - val kind = "Syntax" - val msg = hl"""${"private"}, ${"protected"}, or ${"this"} expected for annotated primary constructor""" - val explanation = - hl"""|When using annotations with a primary constructor of a class, - |the annotation must be followed by an access modifier - |(${"private"} or ${"protected"}) or ${"this"}. - | - |For example: - | ${"class Sample @deprecated this(param: Parameter) { ..."} - | ^^^^ - |""".stripMargin - } } diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index d6e687a1e..257d40bb0 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -212,4 +212,62 @@ class ErrorMessagesTests extends ErrorMessagesTest { val AnnotatedPrimaryConstructorRequiresModifierOrThis(cls) :: Nil = messages assertEquals("AnotherClass", cls.show) } + + @Test def overloadedMethodNeedsReturnType = + checkMessagesAfter("frontend") { + """ + |class Scope() { + | def foo(i: Int) = foo(i.toString) + | def foo(s: String) = s + |} + """.stripMargin + } + .expect { (ictx, messages) => + implicit val ctx: Context = ictx + val defn = ictx.definitions + + assertMessageCount(1, messages) + val OverloadedOrRecursiveMethodNeedsResultType(tree) :: Nil = messages + assertEquals("foo", tree.show) + } + + @Test def recursiveValueNeedsReturnType = + checkMessagesAfter("frontend") { + """ + |class Scope() { + | lazy val i = i + 5 + |} + """.stripMargin + } + .expect { (ictx, messages) => + implicit val ctx: Context = ictx + val defn = ictx.definitions + + assertMessageCount(1, messages) + val RecursiveValueNeedsResultType(tree) :: Nil = messages + assertEquals("i", tree.show) + } + + @Test def cyclicReferenceInvolvingImplicit = + checkMessagesAfter("frontend") { + """ + |object implicitDefs { + | def foo(implicit x: String) = 1 + | def bar() = { + | implicit val x = foo + | x + | } + |} + """.stripMargin + } + .expect { (ictx, messages) => + implicit val ctx: Context = ictx + val defn = ictx.definitions + + assertMessageCount(1, messages) + val CyclicReferenceInvolvingImplicit(tree) :: Nil = messages + assertEquals("x", tree.name.show) + } + + } -- cgit v1.2.3