diff options
Diffstat (limited to 'compiler')
4 files changed, 17 insertions, 26 deletions
diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java index f6d0de352..a03e21d98 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java @@ -52,8 +52,7 @@ public enum ErrorMessageID { MixedLeftAndRightAssociativeOpsID, CantInstantiateAbstractClassOrTraitID, AnnotatedPrimaryConstructorRequiresModifierOrThisID, - OverloadedMethodNeedsResultTypeID, - RecursiveMethodNeedsResultTypeID, + OverloadedOrRecursiveMethodNeedsResultTypeID, RecursiveValueNeedsResultTypeID, CyclicReferenceInvolvingID, CyclicReferenceInvolvingImplicitID, diff --git a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala index 44ae98d82..a4db1b785 100644 --- a/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala +++ b/compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala @@ -1162,23 +1162,18 @@ object messages { |""".stripMargin } - case class OverloadedMethodNeedsResultType(tree: Names.TermName)(implicit ctx: Context) - extends Message(OverloadedMethodNeedsResultTypeID) { + case class OverloadedOrRecursiveMethodNeedsResultType(tree: Names.TermName)(implicit ctx: Context) + extends Message(OverloadedOrRecursiveMethodNeedsResultTypeID) { val kind = "Syntax" - val msg = hl"""overloaded method ${tree} needs result type""" + val msg = hl"""overloaded or recursive method ${tree} needs return type""" val explanation = - 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 RecursiveMethodNeedsResultType(tree: Names.TermName)(implicit ctx: Context) - extends Message(RecursiveMethodNeedsResultTypeID) { - val kind = "Syntax" - val msg = hl"""recursive method ${tree} needs result type""" - val explanation = - hl"""|The definition of `${tree.name}` is recursive and you need to specify its type. - """.stripMargin + hl"""Case 1: ${tree} is overloaded + |If there are multiple methods named `${tree.name}` and at least one definition of + |it calls another, you need to specify the calling method's return type. + | + |Case 2: ${tree} is recursive + |If `${tree.name}` calls itself on any path, you need to specify its return type. + |""".stripMargin } case class RecursiveValueNeedsResultType(tree: Names.TermName)(implicit ctx: Context) @@ -1186,8 +1181,8 @@ object messages { val kind = "Syntax" val msg = hl"""recursive value ${tree.name} needs type""" val explanation = - hl"""|The definition of `${tree.name}` is recursive and you need to specify its type. - """.stripMargin + hl"""The definition of `${tree.name}` is recursive and you need to specify its type. + |""".stripMargin } case class CyclicReferenceInvolving(denot: SymDenotation)(implicit ctx: Context) diff --git a/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala b/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala index 3a377bdc0..a1690955f 100644 --- a/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala +++ b/compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala @@ -32,10 +32,7 @@ object ErrorReporting { if (cx.mode is Mode.InferringReturnType) { cx.tree match { case tree: untpd.DefDef if !tree.tpt.typeOpt.exists => - // TODO: analysis if tree is an overloaded method (or directly recursive) - val overloaded = true - if (overloaded) OverloadedMethodNeedsResultType(tree.name) - else RecursiveMethodNeedsResultType(tree.name) + OverloadedOrRecursiveMethodNeedsResultType(tree.name) case tree: untpd.ValDef if !tree.tpt.typeOpt.exists => RecursiveValueNeedsResultType(tree.name) case _ => diff --git a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala index 0c40ec477..f11c6dd96 100644 --- a/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala +++ b/compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala @@ -227,11 +227,11 @@ class ErrorMessagesTests extends ErrorMessagesTest { val defn = ictx.definitions assertMessageCount(1, messages) - val OverloadedMethodNeedsResultType(tree) :: Nil = messages + val OverloadedOrRecursiveMethodNeedsResultType(tree) :: Nil = messages assertEquals("foo", tree.show) } - @Test @Ignore def recursiveMethodNeedsReturnType = + @Test def recursiveMethodNeedsReturnType = checkMessagesAfter("frontend") { """ |class Scope() { @@ -244,7 +244,7 @@ class ErrorMessagesTests extends ErrorMessagesTest { val defn = ictx.definitions assertMessageCount(1, messages) - val RecursiveMethodNeedsResultType(tree) :: Nil = messages + val OverloadedOrRecursiveMethodNeedsResultType(tree) :: Nil = messages assertEquals("i", tree.show) } |