Skip to content

Commit 98465f9

Browse files
committed
Analysis of overloaded or recursive is harder than expected
Fall-back to reporting "overloaded or recursive needs type".
1 parent c3ec6df commit 98465f9

File tree

4 files changed

+17
-26
lines changed

4 files changed

+17
-26
lines changed

compiler/src/dotty/tools/dotc/reporting/diagnostic/ErrorMessageID.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,7 @@ public enum ErrorMessageID {
5252
MixedLeftAndRightAssociativeOpsID,
5353
CantInstantiateAbstractClassOrTraitID,
5454
AnnotatedPrimaryConstructorRequiresModifierOrThisID,
55-
OverloadedMethodNeedsResultTypeID,
56-
RecursiveMethodNeedsResultTypeID,
55+
OverloadedOrRecursiveMethodNeedsResultTypeID,
5756
RecursiveValueNeedsResultTypeID,
5857
CyclicReferenceInvolvingID,
5958
CyclicReferenceInvolvingImplicitID,

compiler/src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1162,32 +1162,27 @@ object messages {
11621162
|""".stripMargin
11631163
}
11641164

1165-
case class OverloadedMethodNeedsResultType(tree: Names.TermName)(implicit ctx: Context)
1166-
extends Message(OverloadedMethodNeedsResultTypeID) {
1165+
case class OverloadedOrRecursiveMethodNeedsResultType(tree: Names.TermName)(implicit ctx: Context)
1166+
extends Message(OverloadedOrRecursiveMethodNeedsResultTypeID) {
11671167
val kind = "Syntax"
1168-
val msg = hl"""overloaded method ${tree} needs result type"""
1168+
val msg = hl"""overloaded or recursive method ${tree} needs return type"""
11691169
val explanation =
1170-
hl"""|${tree} is overloaded and at least one definition of it calls another.
1171-
|You need to specify the calling method's return type.
1172-
""".stripMargin
1173-
}
1174-
1175-
case class RecursiveMethodNeedsResultType(tree: Names.TermName)(implicit ctx: Context)
1176-
extends Message(RecursiveMethodNeedsResultTypeID) {
1177-
val kind = "Syntax"
1178-
val msg = hl"""recursive method ${tree} needs result type"""
1179-
val explanation =
1180-
hl"""|The definition of `${tree.name}` is recursive and you need to specify its type.
1181-
""".stripMargin
1170+
hl"""Case 1: ${tree} is overloaded
1171+
|If there are multiple methods named `${tree.name}` and at least one definition of
1172+
|it calls another, you need to specify the calling method's return type.
1173+
|
1174+
|Case 2: ${tree} is recursive
1175+
|If `${tree.name}` calls itself on any path, you need to specify its return type.
1176+
|""".stripMargin
11821177
}
11831178

11841179
case class RecursiveValueNeedsResultType(tree: Names.TermName)(implicit ctx: Context)
11851180
extends Message(RecursiveValueNeedsResultTypeID) {
11861181
val kind = "Syntax"
11871182
val msg = hl"""recursive value ${tree.name} needs type"""
11881183
val explanation =
1189-
hl"""|The definition of `${tree.name}` is recursive and you need to specify its type.
1190-
""".stripMargin
1184+
hl"""The definition of `${tree.name}` is recursive and you need to specify its type.
1185+
|""".stripMargin
11911186
}
11921187

11931188
case class CyclicReferenceInvolving(denot: SymDenotation)(implicit ctx: Context)

compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@ object ErrorReporting {
3232
if (cx.mode is Mode.InferringReturnType) {
3333
cx.tree match {
3434
case tree: untpd.DefDef if !tree.tpt.typeOpt.exists =>
35-
// TODO: analysis if tree is an overloaded method (or directly recursive)
36-
val overloaded = true
37-
if (overloaded) OverloadedMethodNeedsResultType(tree.name)
38-
else RecursiveMethodNeedsResultType(tree.name)
35+
OverloadedOrRecursiveMethodNeedsResultType(tree.name)
3936
case tree: untpd.ValDef if !tree.tpt.typeOpt.exists =>
4037
RecursiveValueNeedsResultType(tree.name)
4138
case _ =>

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -227,11 +227,11 @@ class ErrorMessagesTests extends ErrorMessagesTest {
227227
val defn = ictx.definitions
228228

229229
assertMessageCount(1, messages)
230-
val OverloadedMethodNeedsResultType(tree) :: Nil = messages
230+
val OverloadedOrRecursiveMethodNeedsResultType(tree) :: Nil = messages
231231
assertEquals("foo", tree.show)
232232
}
233233

234-
@Test @Ignore def recursiveMethodNeedsReturnType =
234+
@Test def recursiveMethodNeedsReturnType =
235235
checkMessagesAfter("frontend") {
236236
"""
237237
|class Scope() {
@@ -244,7 +244,7 @@ class ErrorMessagesTests extends ErrorMessagesTest {
244244
val defn = ictx.definitions
245245

246246
assertMessageCount(1, messages)
247-
val RecursiveMethodNeedsResultType(tree) :: Nil = messages
247+
val OverloadedOrRecursiveMethodNeedsResultType(tree) :: Nil = messages
248248
assertEquals("i", tree.show)
249249
}
250250

0 commit comments

Comments
 (0)