diff options
author | Paul Phillips <paulp@improving.org> | 2011-04-24 19:16:10 +0000 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2011-04-24 19:16:10 +0000 |
commit | 60463a8721728545d7d626d96f77e8688084c71f (patch) | |
tree | 6cc639b7b4539cfdae45a0ab468c2d6202d1651a /src/compiler/scala/tools/nsc/typechecker/Typers.scala | |
parent | 870679585afc3fe8dc07b40fe032919ede414489 (diff) | |
download | scala-60463a8721728545d7d626d96f77e8688084c71f.tar.gz scala-60463a8721728545d7d626d96f77e8688084c71f.tar.bz2 scala-60463a8721728545d7d626d96f77e8688084c71f.zip |
Added warning when someone tries to return a no...
Added warning when someone tries to return a non-Unit value from a Unit
method, no review.
Diffstat (limited to 'src/compiler/scala/tools/nsc/typechecker/Typers.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Typers.scala | 20 |
1 files changed, 13 insertions, 7 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 8718d31520..add3bca6f2 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -3141,14 +3141,20 @@ trait Typers extends Modes { ) { errorTree(tree, "return outside method definition") } else { - val DefDef(_, _, _, _, restpt, _) = enclMethod.tree - var restpt0 = restpt - if (restpt0.tpe eq null) { - errorTree(tree, "" + enclMethod.owner + - " has return statement; needs result type") - } else { + val DefDef(_, name, _, _, restpt, _) = enclMethod.tree + if (restpt.tpe eq null) + errorTree(tree, enclMethod.owner + " has return statement; needs result type") + else { context.enclMethod.returnsSeen = true - val expr1: Tree = typed(expr, EXPRmode | BYVALmode, restpt0.tpe) + val expr1: Tree = typed(expr, EXPRmode | BYVALmode, restpt.tpe) + // Warn about returning a value if no value can be returned. + if (restpt.tpe.typeSymbol == UnitClass) { + // The typing in expr1 says expr is Unit (it has already been coerced if + // it is non-Unit) so we have to retype it. Fortunately it won't come up much + // unless the warning is legitimate. + if (typed(expr).tpe.typeSymbol != UnitClass) + unit.warning(tree.pos, "enclosing method " + name + " has result type Unit: return value discarded") + } treeCopy.Return(tree, checkDead(expr1)) setSymbol enclMethod.owner setType NothingClass.tpe } } |