Skip to content

Commit 745fa3d

Browse files
committed
Add constant folding tests
In particular, this test the changes to constant folding done two commits ago.
1 parent 915e74d commit 745fa3d

File tree

3 files changed

+95
-25
lines changed

3 files changed

+95
-25
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ import StdNames._
3232
* - collapses all type trees to trees of class TypeTree
3333
* - converts idempotent expressions with constant types
3434
* - drops branches of ifs using the rules
35-
* if (true) A else B --> A
36-
* if (false) A else B --> B
35+
* if (true) A else B ==> A
36+
* if (false) A else B ==> B
3737
*/
3838
class FirstTransform extends MiniPhaseTransform with InfoTransformer with AnnotationTransformer { thisTransformer =>
3939
import ast.tpd._
@@ -217,6 +217,13 @@ class FirstTransform extends MiniPhaseTransform with InfoTransformer with Annota
217217
case _ => tree
218218
}
219219

220+
/** Perform one of the following simplification if applicable:
221+
*
222+
* true && y ==> y
223+
* false && y ==> false
224+
* true || y ==> true
225+
* false || y ==> y
226+
*/
220227
private def foldCondition(tree: Apply)(implicit ctx: Context) = tree.fun match {
221228
case Select(x @ Literal(Constant(c: Boolean)), op) =>
222229
tree.args match {
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package dotty.tools.dotc
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
import dotty.tools.backend.jvm._
6+
import dotty.tools.dotc.config.CompilerCommand
7+
import dotty.tools.dotc.core.Contexts.FreshContext
8+
import scala.tools.asm.tree.MethodNode
9+
10+
import scala.collection.JavaConverters._
11+
12+
class ConstantFoldingTests extends DottyBytecodeTest {
13+
14+
val conditionSrc = """class Test {
15+
def someCondition: Boolean = ???
16+
final val t = true
17+
final val f = false
18+
19+
def whenTrue =
20+
println("hi")
21+
def whenFalse =
22+
()
23+
def whenCond =
24+
if (someCondition)
25+
println("hi")
26+
27+
def reduceToTrue1 =
28+
if (t)
29+
println("hi")
30+
def reduceToTrue2 =
31+
if (t || someCondition)
32+
println("hi")
33+
def reduceToTrue3 =
34+
if (f || t || someCondition)
35+
println("hi")
36+
37+
def reduceToFalse1 =
38+
if (f)
39+
println("hi")
40+
def reduceToFalse2 =
41+
if (f && someCondition)
42+
println("hi")
43+
def reduceToFalse3 =
44+
if (t && f && someCondition)
45+
println("hi")
46+
47+
def reduceToCond1 =
48+
if (t && someCondition)
49+
println("hi")
50+
def reduceToCond2 =
51+
if (f || someCondition)
52+
println("hi")
53+
def reduceToCond3 =
54+
if ((t && f) || someCondition)
55+
println("hi")
56+
}
57+
"""
58+
59+
@Test def constantFoldConditions: Unit = {
60+
import ASMConverters._
61+
62+
checkBCode(conditionSrc) { dir =>
63+
val clsIn = dir.lookupName(s"Test.class", directory = false).input
64+
val methods = loadClassNode(clsIn).methods.asScala
65+
66+
val whenTrue = methods.find(_.name == "whenTrue").get
67+
val whenFalse = methods.find(_.name == "whenFalse").get
68+
val whenCond = methods.find(_.name == "whenCond").get
69+
70+
val reduceToTrue = methods.filter(_.name.startsWith("reduceToTrue"))
71+
val reduceToFalse = methods.filter(_.name.startsWith("reduceToFalse"))
72+
val reduceToCond = methods.filter(_.name.startsWith("reduceToCond"))
73+
74+
def compare(expected: MethodNode, actual: MethodNode) = {
75+
val expectedInstrs = instructionsFromMethod(expected)
76+
val actualInstrs = instructionsFromMethod(actual)
77+
val diff = diffInstructions(expectedInstrs, actualInstrs)
78+
assert(expectedInstrs == actualInstrs,
79+
s"Different bytecode between ${expected.name} and ${actual.name}\n$diff")
80+
}
81+
reduceToTrue.foreach(compare(whenTrue, _))
82+
reduceToFalse.foreach(compare(whenFalse, _))
83+
reduceToCond.foreach(compare(whenCond, _))
84+
}
85+
}
86+
}

tests/pos/constantConditions.scala

Lines changed: 0 additions & 23 deletions
This file was deleted.

0 commit comments

Comments
 (0)