diff options
author | Nicolas Stucki <nicolas.stucki@gmail.com> | 2017-04-17 17:22:48 +0200 |
---|---|---|
committer | Nicolas Stucki <nicolas.stucki@gmail.com> | 2017-04-17 17:39:02 +0200 |
commit | 983ce8da3ce321bed7f8100c00b4aa709528208e (patch) | |
tree | 02120350c1f7cdb37efffa53372ba4fcaed30580 /compiler | |
parent | ea9325965a82610083e75e0d111fe3d70c46b3d2 (diff) | |
download | dotty-983ce8da3ce321bed7f8100c00b4aa709528208e.tar.gz dotty-983ce8da3ce321bed7f8100c00b4aa709528208e.tar.bz2 dotty-983ce8da3ce321bed7f8100c00b4aa709528208e.zip |
Pruning branch of constant type condition.
As done in inliner.
Diffstat (limited to 'compiler')
-rw-r--r-- | compiler/src/dotty/tools/dotc/transform/FirstTransform.scala | 17 |
1 files changed, 12 insertions, 5 deletions
diff --git a/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala b/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala index a3cf71ef2..767ee0901 100644 --- a/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala +++ b/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala @@ -9,7 +9,7 @@ import dotty.tools.dotc.transform.TreeTransforms._ import ast.Trees._ import Flags._ import Types._ -import Constants.Constant +import Constants._ import Contexts.Context import Symbols._ import SymDenotations._ @@ -34,6 +34,8 @@ import StdNames._ * - drops branches of ifs using the rules * if (true) A else B --> A * if (false) A else B --> B + * if (C: true) A else B --> C; A + * if (C: false) A else B --> C; B */ class FirstTransform extends MiniPhaseTransform with InfoTransformer with AnnotationTransformer { thisTransformer => import ast.tpd._ @@ -190,11 +192,16 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota override def transformBlock(tree: Block)(implicit ctx: Context, info: TransformerInfo) = constToLiteral(tree) - override def transformIf(tree: If)(implicit ctx: Context, info: TransformerInfo) = - tree.cond match { - case Literal(Constant(c: Boolean)) => if (c) tree.thenp else tree.elsep - case _ => tree + override def transformIf(tree: If)(implicit ctx: Context, info: TransformerInfo) = { + tree.cond.tpe.widenTermRefExpr match { + case ConstantType(Constant(condVal: Boolean)) => + val selected = if (condVal) tree.thenp else tree.elsep + if (isPureExpr(tree.cond)) selected + else Block(tree.cond :: Nil, selected) + case _ => + tree } + } // invariants: all modules have companion objects // all types are TypeTrees |