@@ -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,15 @@ 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
195
+ override def transformIf (tree : If )(implicit ctx : Context , info : TransformerInfo ) = {
196
+ tree.cond.tpe.widenTermRefExpr match {
197
+ case ConstantType (value) =>
198
+ val branch = if (value.booleanValue) tree.thenp else tree.elsep
199
+ if (tree.cond.isInstanceOf [Literal ]) branch
200
+ else Block (List (tree.cond), branch)
196
201
case _ => tree
197
202
}
203
+ }
198
204
199
205
// invariants: all modules have companion objects
200
206
// all types are TypeTrees
0 commit comments