Skip to content

Commit db24246

Browse files
authored
Merge pull request #2026 from dotty-staging/fix-#2001
Better error messages for missing type of recursive definitions
2 parents 2699715 + 4b494ab commit db24246

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,28 +31,21 @@ 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
}
4841
} else msg
4942

50-
if (cycleSym.is(Implicit, butNot = Method) && cycleSym.owner.isTerm)
51-
em"""cyclic reference involving implicit $cycleSym
43+
if (cycleSym.is(Implicit, butNot = Method) && cycleSym.owner.isTerm)
44+
em"""cyclic reference involving implicit $cycleSym
5245
|This happens when the right hand-side of $cycleSym's definition involves an implicit search.
5346
|To avoid the error, give $cycleSym an explicit type."""
54-
else
55-
errorMsg(ex.show, ctx)
47+
else
48+
errorMsg(ex.show, ctx)
5649
}
5750

5851
def wrongNumberOfTypeArgs(fntpe: Type, expectedArgs: List[TypeParamInfo], actual: List[untpd.Tree], pos: Position)(implicit ctx: Context) =

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)