@@ -9,7 +9,7 @@ import dotty.tools.dotc.transform.TreeTransforms._
9
9
import ast .Trees ._
10
10
import Flags ._
11
11
import Types ._
12
- import Constants .Constant
12
+ import Constants ._
13
13
import Contexts .Context
14
14
import Symbols ._
15
15
import SymDenotations ._
@@ -34,6 +34,8 @@ import StdNames._
34
34
* - drops branches of ifs using the rules
35
35
* if (true) A else B --> A
36
36
* if (false) A else B --> B
37
+ * if (C: true) A else B --> C; A
38
+ * if (C: false) A else B --> C; B
37
39
*/
38
40
class FirstTransform extends MiniPhaseTransform with InfoTransformer with AnnotationTransformer { thisTransformer =>
39
41
import ast .tpd ._
@@ -190,11 +192,16 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota
190
192
override def transformBlock (tree : Block )(implicit ctx : Context , info : TransformerInfo ) =
191
193
constToLiteral(tree)
192
194
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
195
+ override def transformIf (tree : If )(implicit ctx : Context , info : TransformerInfo ) = {
196
+ tree.cond.tpe.widenTermRefExpr match {
197
+ case ConstantType (Constant (condVal : Boolean )) =>
198
+ val selected = if (condVal) tree.thenp else tree.elsep
199
+ if (isPureExpr(tree.cond)) selected
200
+ else Block (tree.cond :: Nil , selected)
201
+ case _ =>
202
+ tree
197
203
}
204
+ }
198
205
199
206
// invariants: all modules have companion objects
200
207
// all types are TypeTrees
0 commit comments