diff options
Diffstat (limited to 'src/compiler')
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Contexts.scala | 9 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Typers.scala | 6 |
2 files changed, 12 insertions, 3 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala index dc75fa3746..48df7cb261 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Contexts.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Contexts.scala @@ -243,6 +243,7 @@ trait Contexts { self: Analyzer => def retyping = this(ReTyping) def inSecondTry = this(SecondTry) def inSecondTry_=(value: Boolean) = this(SecondTry) = value + def inReturnExpr = this(ReturnExpr) /** These messages are printed when issuing an error */ var diagnostic: List[String] = Nil @@ -361,6 +362,11 @@ trait Contexts { self: Analyzer => def withSuperInit[T](op: => T): T = withMode(enabled = SuperInit)(op) def withSecondTry[T](op: => T): T = withMode(enabled = SecondTry)(op) + def withReturnExpr[T](op: => T): T = { + enclMethod.returnsSeen = true + withMode(enabled = ReturnExpr)(op) + } + /** @return true if the `expr` evaluates to true within a silent Context that incurs no errors */ @inline final def inSilentMode(expr: => Boolean): Boolean = { withMode() { // withMode with no arguments to restore the mode mutated by `setBufferErrors`. @@ -1355,6 +1361,9 @@ object ContextMode { */ final val SecondTry: ContextMode = 1 << 14 + /** Are we in return position? Formerly RETmode. */ + final val ReturnExpr: ContextMode = 1 << 15 + final val DefaultMode: ContextMode = MacrosEnabled private val contextModeNameMap = Map( diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 2066ab980d..3bcc670c89 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -4292,9 +4292,9 @@ trait Typers extends Adaptations with Tags { val DefDef(_, name, _, _, restpt, _) = enclMethod.tree if (restpt.tpe eq null) { ReturnWithoutTypeError(tree, enclMethod.owner) - } else { - context.enclMethod.returnsSeen = true - val expr1: Tree = typed(expr, EXPRmode | BYVALmode | RETmode, restpt.tpe) + } + else { + val expr1 = context withReturnExpr 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 |