diff options
author | Martin Odersky <odersky@gmail.com> | 2014-01-15 10:56:41 +0100 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2014-01-15 10:57:16 +0100 |
commit | 3d9a664e75307410b8845ecc1540a00924867912 (patch) | |
tree | d9ae348b660156897d97a0162d83aa424236ec6f /src/dotty/tools/dotc/typer | |
parent | a709a72cd2ff64e5fb81a388d95f85c62ede7db3 (diff) | |
download | dotty-3d9a664e75307410b8845ecc1540a00924867912.tar.gz dotty-3d9a664e75307410b8845ecc1540a00924867912.tar.bz2 dotty-3d9a664e75307410b8845ecc1540a00924867912.zip |
Fix for constant folding
Diffstat (limited to 'src/dotty/tools/dotc/typer')
-rw-r--r-- | src/dotty/tools/dotc/typer/Applications.scala | 2 | ||||
-rw-r--r-- | src/dotty/tools/dotc/typer/ConstFold.scala | 39 | ||||
-rw-r--r-- | src/dotty/tools/dotc/typer/Typer.scala | 2 |
3 files changed, 21 insertions, 22 deletions
diff --git a/src/dotty/tools/dotc/typer/Applications.scala b/src/dotty/tools/dotc/typer/Applications.scala index 3a8605570..644227878 100644 --- a/src/dotty/tools/dotc/typer/Applications.scala +++ b/src/dotty/tools/dotc/typer/Applications.scala @@ -442,7 +442,7 @@ trait Applications extends Compatibility { self: Typer => if (proto.argsAreTyped) new ApplyToTyped(tree, fun1, funRef, proto.typedArgs, pt) else new ApplyToUntyped(tree, fun1, funRef, proto, pt) val result = app.result - ConstFold(result) orElse result + ConstFold(result) } { (failedVal, failedState) => fun1 match { case Select(qual, name) => // try with prototype `[].name(args)`, this might succeed by inserting an diff --git a/src/dotty/tools/dotc/typer/ConstFold.scala b/src/dotty/tools/dotc/typer/ConstFold.scala index a66e8a9c8..7930b5d4a 100644 --- a/src/dotty/tools/dotc/typer/ConstFold.scala +++ b/src/dotty/tools/dotc/typer/ConstFold.scala @@ -17,26 +17,25 @@ object ConstFold { import tpd._ /** If tree is a constant operation, replace with result. */ - def apply(tree: Tree)(implicit ctx: Context): Tree = - finish(tree) { - tree match { - case Apply(Select(xt, op), yt :: Nil) => - xt.tpe match { - case ConstantType(x) => - yt.tpe match { - case ConstantType(y) => foldBinop(op, x, y) - case _ => null - } - case _ => null - } - case Select(xt, op) => - xt.tpe match { - case ConstantType(x) => foldUnop(op, x) - case _ => null - } - case _ => null - } + def apply(tree: Tree)(implicit ctx: Context): Tree = finish(tree) { + tree match { + case Apply(Select(xt, op), yt :: Nil) => + xt.tpe match { + case ConstantType(x) => + yt.tpe match { + case ConstantType(y) => foldBinop(op, x, y) + case _ => null + } + case _ => null + } + case Select(xt, op) => + xt.tpe match { + case ConstantType(x) => foldUnop(op, x) + case _ => null + } + case _ => null } + } /** If tree is a constant value that can be converted to type `pt`, perform * the conversion. @@ -53,7 +52,7 @@ object ConstFold { try { val x = compX if (x ne null) tree withType ConstantType(x) - else EmptyTree + else tree } catch { case _: ArithmeticException => tree // the code will crash at runtime, // but that is better than the diff --git a/src/dotty/tools/dotc/typer/Typer.scala b/src/dotty/tools/dotc/typer/Typer.scala index a40062127..61ab0ed56 100644 --- a/src/dotty/tools/dotc/typer/Typer.scala +++ b/src/dotty/tools/dotc/typer/Typer.scala @@ -1205,7 +1205,7 @@ class Typer extends Namer with Applications with Implicits { def adaptToSubType(wtp: Type): Tree = { // try converting a constant to the target type val folded = ConstFold(tree, pt) - if (folded ne EmptyTree) return folded + if (folded ne tree) return folded // drop type if prototype is Unit if (pt isRef defn.UnitClass) return tpd.Block(tree :: Nil, Literal(Constant(()))) |