Skip to content

Commit e33ad95

Browse files
committed
Fix #2077: Optimization of constant conditionals
Move fixed logic to FirstTransform, where the other constant folding operations are also done.
1 parent 921f8bf commit e33ad95

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

compiler/src/dotty/tools/dotc/transform/FirstTransform.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ import StdNames._
3131
* - eliminates self tree in Template and self symbol in ClassInfo
3232
* - collapsess all type trees to trees of class TypeTree
3333
* - converts idempotent expressions with constant types
34+
* - drops branches of ifs using the rules
35+
* if (true) A else B --> A
36+
* if (false) A else B --> B
3437
*/
3538
class FirstTransform extends MiniPhaseTransform with InfoTransformer with AnnotationTransformer { thisTransformer =>
3639
import ast.tpd._
@@ -187,6 +190,12 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota
187190
override def transformBlock(tree: Block)(implicit ctx: Context, info: TransformerInfo) =
188191
constToLiteral(tree)
189192

193+
override def transformIf(tree: If)(implicit ctx: Context, info: TransformerInfo) =
194+
tree.cond match {
195+
case Literal(Constant(c: Boolean)) => if (c) tree.thenp else tree.elsep
196+
case _ => tree
197+
}
198+
190199
// invariants: all modules have companion objects
191200
// all types are TypeTrees
192201
// all this types are explicit

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -739,11 +739,7 @@ import RefChecks._
739739
*
740740
* 2. It warns about references to symbols labeled deprecated or migration.
741741
742-
* 3. It performs the following transformations:
743-
*
744-
* - if (true) A else B --> A
745-
* if (false) A else B --> B
746-
* - macro definitions are eliminated.
742+
* 3. It eliminates macro definitions.
747743
*
748744
* 4. It makes members not private where necessary. The following members
749745
* cannot be private in the Java model:
@@ -836,12 +832,6 @@ class RefChecks extends MiniPhase { thisTransformer =>
836832
tree
837833
}
838834

839-
override def transformIf(tree: If)(implicit ctx: Context, info: TransformerInfo) =
840-
tree.cond.tpe match {
841-
case ConstantType(value) => if (value.booleanValue) tree.thenp else tree.elsep
842-
case _ => tree
843-
}
844-
845835
override def transformNew(tree: New)(implicit ctx: Context, info: TransformerInfo) = {
846836
currentLevel.enterReference(tree.tpe.typeSymbol, tree.pos)
847837
tree

tests/run/i2077.check

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
hi

tests/run/i2077.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Test {
2+
inline val x = true
3+
val y = if (x) 1 else 2 // reduced to val y = 1
4+
5+
def main(args: Array[String]): Unit =
6+
if ({ println("hi"); true }) // cannot be reduced
7+
1
8+
else
9+
2
10+
}

0 commit comments

Comments
 (0)