diff options
author | Martin Odersky <odersky@gmail.com> | 2017-03-13 22:02:56 +0100 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2017-03-13 22:03:06 +0100 |
commit | e33ad95fd8e31c80f3615e6feb11d7e583723dd8 (patch) | |
tree | 75cdc8ac314d1f7b24b2130a44f39b8fd436ed97 /compiler/src/dotty/tools/dotc | |
parent | 921f8bffc18b19449b2c1ad68c32725a7b7532e2 (diff) | |
download | dotty-e33ad95fd8e31c80f3615e6feb11d7e583723dd8.tar.gz dotty-e33ad95fd8e31c80f3615e6feb11d7e583723dd8.tar.bz2 dotty-e33ad95fd8e31c80f3615e6feb11d7e583723dd8.zip |
Fix #2077: Optimization of constant conditionals
Move fixed logic to FirstTransform, where the other constant
folding operations are also done.
Diffstat (limited to 'compiler/src/dotty/tools/dotc')
-rw-r--r-- | compiler/src/dotty/tools/dotc/transform/FirstTransform.scala | 9 | ||||
-rw-r--r-- | compiler/src/dotty/tools/dotc/typer/RefChecks.scala | 12 |
2 files changed, 10 insertions, 11 deletions
diff --git a/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala b/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala index 597146514..9e71fda5d 100644 --- a/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala +++ b/compiler/src/dotty/tools/dotc/transform/FirstTransform.scala @@ -31,6 +31,9 @@ import StdNames._ * - eliminates self tree in Template and self symbol in ClassInfo * - collapsess all type trees to trees of class TypeTree * - converts idempotent expressions with constant types + * - drops branches of ifs using the rules + * if (true) A else B --> A + * if (false) A else B --> B */ class FirstTransform extends MiniPhaseTransform with InfoTransformer with AnnotationTransformer { thisTransformer => import ast.tpd._ @@ -187,6 +190,12 @@ 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 + } + // invariants: all modules have companion objects // all types are TypeTrees // all this types are explicit diff --git a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala index e8ff7d572..d61f5fa68 100644 --- a/compiler/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/compiler/src/dotty/tools/dotc/typer/RefChecks.scala @@ -739,11 +739,7 @@ import RefChecks._ * * 2. It warns about references to symbols labeled deprecated or migration. - * 3. It performs the following transformations: - * - * - if (true) A else B --> A - * if (false) A else B --> B - * - macro definitions are eliminated. + * 3. It eliminates macro definitions. * * 4. It makes members not private where necessary. The following members * cannot be private in the Java model: @@ -836,12 +832,6 @@ class RefChecks extends MiniPhase { thisTransformer => tree } - override def transformIf(tree: If)(implicit ctx: Context, info: TransformerInfo) = - tree.cond.tpe match { - case ConstantType(value) => if (value.booleanValue) tree.thenp else tree.elsep - case _ => tree - } - override def transformNew(tree: New)(implicit ctx: Context, info: TransformerInfo) = { currentLevel.enterReference(tree.tpe.typeSymbol, tree.pos) tree |