Skip to content

Commit c6a0982

Browse files
committed
Better error messages for missing type of recursive definitions
1 parent a0f47a0 commit c6a0982

File tree

2 files changed

+10
-11
lines changed

2 files changed

+10
-11
lines changed

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,17 +31,10 @@ object ErrorReporting {
3131
def errorMsg(msg: String, cx: Context): String =
3232
if (cx.mode is Mode.InferringReturnType) {
3333
cx.tree match {
34-
case tree: untpd.ValOrDefDef =>
35-
// Dotty deviation: Was Trees.ValOrDefDef[_], but this gives ValOrDefDef[Nothing] instead of
36-
// ValOrDefDel[Null]. Scala handles it, but it looks accidental because bounds propagation
37-
// fails if the parameter is invariant or cotravariant.
38-
// See test pending/pos/boundspropagation.scala
39-
val treeSym = ctx.symOfContextTree(tree)
40-
if (treeSym.exists && treeSym.name == cycleSym.name && treeSym.owner == cycleSym.owner) {
41-
val result = if (cycleSym is Method) " result" else ""
42-
em"overloaded or recursive $cycleSym needs$result type"
43-
}
44-
else errorMsg(msg, cx.outer)
34+
case tree: untpd.DefDef if !tree.tpt.typeOpt.exists =>
35+
em"overloaded or recursive method ${tree.name} needs result type"
36+
case tree: untpd.ValDef if !tree.tpt.typeOpt.exists =>
37+
em"recursive value ${tree.name} needs type"
4538
case _ =>
4639
errorMsg(msg, cx.outer)
4740
}

tests/neg/i2001.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class A {
2+
def odd(x: Int) = if (x == 0) false else !even(x-1)
3+
def even(x: Int) = if (x == 0) true else !odd(x-1) // error: overloaded or recursive method needs result type
4+
5+
lazy val x = x // error: recursive value needs type
6+
}

0 commit comments

Comments
 (0)