From a326b06088d7eadde03bbcd56883b62fcfd21011 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Thu, 8 Aug 2013 19:21:47 +0200 Subject: Typing of try and throw statements. Also issues an error on returns form methods missing a return type. --- src/dotty/tools/dotc/typer/Typer.scala | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) (limited to 'src/dotty/tools/dotc/typer/Typer.scala') diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index 554884382..1e5d0d331 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -491,8 +491,15 @@ class Typer extends Namer with Applications with Implicits { else cx.tree match { case ddef: DefDef => val meth = ddef.symbol - (Ident(TermRef.withSym(NoPrefix, meth.asTerm)), - if (meth.isConstructor) defn.UnitType else ddef.tpt.tpe) + val from = Ident(TermRef.withSym(NoPrefix, meth.asTerm)) + val proto = + if (meth.isConstructor) + defn.UnitType + else if (ddef.tpt.isEmpty) + errorType(s"method ${meth.show} has return statement; needs result type", tree.pos) + else + ddef.tpt.tpe + (from, proto) case _ => enclMethInfo(cx.outer) } @@ -501,6 +508,22 @@ class Typer extends Namer with Applications with Implicits { cpy.Return(tree, expr1, from) withType defn.NothingType } + def typedTry(tree: untpd.Try, pt: Type)(implicit ctx: Context): Try = { + val expr1 = typed(tree.expr, pt) + val handler1 = typed(tree.handler, defn.FunctionType(defn.ThrowableType :: Nil, pt)) + val finalizer1 = typed(tree.finalizer, defn.UnitType) + val handlerResultType = handler1.tpe match { + case defn.FunctionType(_, resultType) => resultType + case _ => defn.NothingType + } + cpy.Try(tree, expr1, handler1, finalizer1).withType(expr1.tpe | handlerResultType) + } + + def typedThrow(tree: untpd.Throw)(implicit ctx: Context): Throw = { + val expr1 = typed(tree.expr, defn.ThrowableType) + cpy.Throw(tree, expr1) withType defn.NothingType + } + def typedModifiers(mods: untpd.Modifiers)(implicit ctx: Context): Modifiers = { val annotations1 = mods.annotations mapconserve typedAnnotation if (annotations1 eq mods.annotations) mods.asInstanceOf[Modifiers] -- cgit v1.2.3