File tree Expand file tree Collapse file tree 4 files changed +21
-11
lines changed
compiler/src/dotty/tools/dotc Expand file tree Collapse file tree 4 files changed +21
-11
lines changed Original file line number Diff line number Diff line change @@ -31,6 +31,9 @@ import StdNames._
31
31
* - eliminates self tree in Template and self symbol in ClassInfo
32
32
* - collapsess all type trees to trees of class TypeTree
33
33
* - 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
34
37
*/
35
38
class FirstTransform extends MiniPhaseTransform with InfoTransformer with AnnotationTransformer { thisTransformer =>
36
39
import ast .tpd ._
@@ -187,6 +190,12 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota
187
190
override def transformBlock (tree : Block )(implicit ctx : Context , info : TransformerInfo ) =
188
191
constToLiteral(tree)
189
192
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
+
190
199
// invariants: all modules have companion objects
191
200
// all types are TypeTrees
192
201
// all this types are explicit
Original file line number Diff line number Diff line change @@ -739,11 +739,7 @@ import RefChecks._
739
739
*
740
740
* 2. It warns about references to symbols labeled deprecated or migration.
741
741
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.
747
743
*
748
744
* 4. It makes members not private where necessary. The following members
749
745
* cannot be private in the Java model:
@@ -836,12 +832,6 @@ class RefChecks extends MiniPhase { thisTransformer =>
836
832
tree
837
833
}
838
834
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
-
845
835
override def transformNew (tree : New )(implicit ctx : Context , info : TransformerInfo ) = {
846
836
currentLevel.enterReference(tree.tpe.typeSymbol, tree.pos)
847
837
tree
Original file line number Diff line number Diff line change
1
+ hi
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments