Skip to content

Better error messages for missing type of recursive definitions #2026

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,10 @@ object ErrorReporting {
def errorMsg(msg: String, cx: Context): String =
if (cx.mode is Mode.InferringReturnType) {
cx.tree match {
case tree: untpd.ValOrDefDef =>
// Dotty deviation: Was Trees.ValOrDefDef[_], but this gives ValOrDefDef[Nothing] instead of
// ValOrDefDel[Null]. Scala handles it, but it looks accidental because bounds propagation
// fails if the parameter is invariant or cotravariant.
// See test pending/pos/boundspropagation.scala
val treeSym = ctx.symOfContextTree(tree)
if (treeSym.exists && treeSym.name == cycleSym.name && treeSym.owner == cycleSym.owner) {
val result = if (cycleSym is Method) " result" else ""
em"overloaded or recursive $cycleSym needs$result type"
}
else errorMsg(msg, cx.outer)
case tree: untpd.DefDef if !tree.tpt.typeOpt.exists =>
em"overloaded or recursive method ${tree.name} needs result type"
case tree: untpd.ValDef if !tree.tpt.typeOpt.exists =>
em"recursive value ${tree.name} needs type"
case _ =>
errorMsg(msg, cx.outer)
}
Expand Down
6 changes: 6 additions & 0 deletions tests/neg/i2001.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class A {
def odd(x: Int) = if (x == 0) false else !even(x-1)
def even(x: Int) = if (x == 0) true else !odd(x-1) // error: overloaded or recursive method needs result type

lazy val x = x // error: recursive value needs type
}