Skip to content

Fix #2077: Optimization of constant conditionals #2091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 14, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/FirstTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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._
Expand Down Expand Up @@ -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
Expand Down
12 changes: 1 addition & 11 deletions compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions tests/run/i2077.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hi
10 changes: 10 additions & 0 deletions tests/run/i2077.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
object Test {
inline val x = true
val y = if (x) 1 else 2 // reduced to val y = 1

def main(args: Array[String]): Unit =
if ({ println("hi"); true }) // cannot be reduced
1
else
2
}