Skip to content

Commit dec2766

Browse files
OlivierBlanvillainDarkDimius
authored andcommitted
Add byte code SimplifyTests
1 parent a2bead9 commit dec2766

File tree

3 files changed

+124
-7
lines changed

3 files changed

+124
-7
lines changed

compiler/test/dotty/tools/DottyTest.scala

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,11 @@ trait DottyTest extends ContextEscapeDetection {
1818

1919
dotc.parsing.Scanners // initialize keywords
2020

21-
implicit var ctx: Contexts.Context = {
21+
implicit var ctx: Context = {
2222
val base = new ContextBase {}
2323
import base.settings._
2424
val ctx = base.initialCtx.fresh
25-
ctx.setSetting(ctx.settings.encoding, "UTF8")
26-
ctx.setSetting(ctx.settings.classpath, Jars.dottyLib)
25+
initializeCtx(ctx)
2726
// when classpath is changed in ctx, we need to re-initialize to get the
2827
// correct classpath from PathResolver
2928
base.initialize()(ctx)
@@ -35,7 +34,12 @@ trait DottyTest extends ContextEscapeDetection {
3534
ctx = null
3635
}
3736

38-
private def compilerWithChecker(phase: String)(assertion:(tpd.Tree, Context) => Unit) = new Compiler {
37+
protected def initializeCtx(fc: FreshContext): Unit = {
38+
fc.setSetting(fc.settings.encoding, "UTF8")
39+
fc.setSetting(fc.settings.classpath, Jars.dottyLib)
40+
}
41+
42+
private def compilerWithChecker(phase: String)(assertion: (tpd.Tree, Context) => Unit) = new Compiler {
3943
override def phases = {
4044
val allPhases = super.phases
4145
val targetPhase = allPhases.flatten.find(p => p.phaseName == phase).get
@@ -58,7 +62,7 @@ trait DottyTest extends ContextEscapeDetection {
5862
run.compile(source)
5963
}
6064

61-
def checkCompile(checkAfterPhase: String, sources:List[String])(assertion:(tpd.Tree, Context) => Unit): Unit = {
65+
def checkCompile(checkAfterPhase: String, sources: List[String])(assertion: (tpd.Tree, Context) => Unit): Unit = {
6266
val c = compilerWithChecker(checkAfterPhase)(assertion)
6367
c.rootContext(ctx)
6468
val run = c.newRun

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ class CompilationTests extends ParallelTesting {
3232
compileDir("../collection-strawman/src/main", defaultOptions) +
3333
compileDir("../compiler/src/dotty/tools/dotc/ast", defaultOptions) +
3434
compileDir("../compiler/src/dotty/tools/dotc/config", defaultOptions) +
35-
compileDir("../compiler/src/dotty/tools/dotc/core", allowDeepSubtypes)
35+
compileDir("../compiler/src/dotty/tools/dotc/core", allowDeepSubtypes) +
3636
compileDir("../compiler/src/dotty/tools/dotc/transform", allowDeepSubtypes) +
3737
compileDir("../compiler/src/dotty/tools/dotc/parsing", defaultOptions) +
3838
compileDir("../compiler/src/dotty/tools/dotc/printing", defaultOptions) +
3939
compileDir("../compiler/src/dotty/tools/dotc/reporting", defaultOptions) +
4040
compileDir("../compiler/src/dotty/tools/dotc/typer", defaultOptions) +
4141
compileDir("../compiler/src/dotty/tools/dotc/util", defaultOptions) +
4242
compileDir("../compiler/src/dotty/tools/io", defaultOptions) +
43-
compileDir("../compiler/src/dotty/tools/dotc/core", noCheckOptions ++ classPath)
43+
compileDir("../compiler/src/dotty/tools/dotc/core", noCheckOptions ++ classPath) +
4444
compileFile("../tests/pos/nullarify.scala", defaultOptions.and("-Ycheck:nullarify")) +
4545
compileFile("../tests/pos-scala2/rewrites.scala", scala2Mode.and("-rewrite")).copyToTarget() +
4646
compileFile("../tests/pos-special/t8146a.scala", allowDeepSubtypes) +
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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+
class SimplifyPosTests extends DottyBytecodeOptimisedTest with SimplifyEquivalences
11+
class SimplifyNegTests extends DottyBytecodeTest with SimplifyEquivalences
12+
13+
class DottyBytecodeOptimisedTest extends DottyBytecodeTest {
14+
override protected def initializeCtx(c: FreshContext): Unit = {
15+
super.initializeCtx(c)
16+
val flags = Array("-optimise") // :+ "-Xprint:simplify"
17+
val summary = CompilerCommand.distill(flags)(c)
18+
c.setSettings(summary.sstate)
19+
}
20+
}
21+
22+
trait SimplifyEquivalences { self: DottyBytecodeTest =>
23+
def check(expr1: String, expr2: String, shared: String = ""): Unit = {
24+
import ASMConverters._
25+
val source =
26+
s"""
27+
$shared
28+
|class A {
29+
| def main(): Unit = {
30+
$expr1
31+
| }
32+
|}
33+
|class B {
34+
| def main(): Unit = {
35+
$expr2
36+
| }
37+
|}
38+
""".stripMargin
39+
checkBCode(source) { dir =>
40+
def instructions(clazz: String): List[Instruction] = {
41+
val clsIn = dir.lookupName(s"$clazz.class", directory = false).input
42+
val clsNode = loadClassNode(clsIn)
43+
instructionsFromMethod(getMethod(clsNode, "main"))
44+
}
45+
val A = instructions("A")
46+
val B = instructions("B")
47+
val diff = diffInstructions(A, B)
48+
if (this.isInstanceOf[DottyBytecodeOptimisedTest])
49+
assert(A == B, s"Bytecode wasn't same:\n$diff")
50+
else
51+
assert(A != B, s"Bytecode was the same:\n$diff")
52+
}
53+
}
54+
55+
@Test def inlineVals =
56+
check("println(1)",
57+
"""
58+
|val one = 1
59+
|val anotherone = one
60+
|println(anotherone)
61+
""")
62+
63+
@Test def inlineCaseIntrinsicsDottyApply =
64+
check(
65+
expr1 = "CC.apply(1, 2)",
66+
expr2 = "new CC(1, 2)",
67+
shared = "case class CC(i: Int, j: Int)")
68+
69+
@Test def inlineCaseIntrinsicsScalacApply =
70+
check("::.apply(1, Nil)", "new ::(1, Nil)")
71+
72+
@Test def inlineCaseIntrinsicsScalacUnapply =
73+
check(
74+
"""
75+
|val t = Tuple2(1, "s")
76+
|print(Tuple2.unapply(t))
77+
""",
78+
"""
79+
|val t = Tuple2(1, "s")
80+
|print(new Some(new Tuple2(t._1, t._2)))
81+
""")
82+
83+
@Test def constantFold =
84+
check(
85+
"""
86+
|val t = true // val needed, or typer takes care of this
87+
|if (t) print(1)
88+
|else print(2)
89+
""",
90+
"""
91+
|print(1)
92+
""")
93+
94+
@Test def dropNoEffects =
95+
check(
96+
"""
97+
|"wow"
98+
|print(1)
99+
""",
100+
"""
101+
|print(1)
102+
""")
103+
104+
// @Test def inlineOptions =
105+
// check(
106+
// """
107+
// |val sum = Some("s")
108+
// |println(sum.isDefined)
109+
// """,
110+
// """
111+
// |println(true)
112+
// """)
113+
}

0 commit comments

Comments
 (0)