Skip to content

Add compilation order idempotency tests #3019

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 7 additions & 14 deletions compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,6 @@ class CompilationTests extends ParallelTesting {
compileDir("../library/src",
allowDeepSubtypes.and("-Ycheck-reentrant", "-strict", "-priorityclasspath", defaultOutputDir))

def sources(paths: JStream[Path], excludedFiles: List[String] = Nil): List[String] =
paths.iterator().asScala
.filter(path =>
(path.toString.endsWith(".scala") || path.toString.endsWith(".java"))
&& !excludedFiles.contains(path.getFileName.toString))
.map(_.toString).toList

val compilerDir = Paths.get("../compiler/src")
val compilerSources = sources(Files.walk(compilerDir))

Expand Down Expand Up @@ -293,13 +286,6 @@ class CompilationTests extends ParallelTesting {
}

private val (compilerSources, backendSources, backendJvmSources) = {
def sources(paths: JStream[Path], excludedFiles: List[String] = Nil): List[String] =
paths.iterator().asScala
.filter(path =>
(path.toString.endsWith(".scala") || path.toString.endsWith(".java"))
&& !excludedFiles.contains(path.getFileName.toString))
.map(_.toString).toList

val compilerDir = Paths.get("../compiler/src")
val compilerSources0 = sources(Files.walk(compilerDir))

Expand All @@ -324,4 +310,11 @@ class CompilationTests extends ParallelTesting {
object CompilationTests {
implicit val summaryReport: SummaryReporting = new SummaryReport
@AfterClass def cleanup(): Unit = summaryReport.echoSummary()

def sources(paths: JStream[Path], excludedFiles: List[String] = Nil): List[String] =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This definition is the same with the one under tastyBootstrap and both are non-capturing. Does it make sense to merge?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I will use the same function. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

paths.iterator().asScala
.filter(path =>
(path.toString.endsWith(".scala") || path.toString.endsWith(".java"))
&& !excludedFiles.contains(path.getFileName.toString))
.map(_.toString).toList
}
57 changes: 46 additions & 11 deletions compiler/test/dotty/tools/dotc/IdempotencyTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package dotty
package tools
package dotc

import java.io.{ File => JFile }
import java.nio.file.{ Files, Paths, Path }

import org.junit.{ Test, AfterClass }

import scala.concurrent.duration._
Expand All @@ -22,25 +25,57 @@ class IdempotencyTests extends ParallelTesting {
def testFilter = Properties.testsFilter

/* TODO: Only run them selectively? */
@Test def bytecodeIdempotency: Unit = {
@Test def idempotency: Unit = {
val opt = defaultOptions.and("-YemitTasty")

def idempotency1() = {
compileDir("../collection-strawman/src/main", opt) +
compileFilesInDir("../tests/pos", opt)
def sourcesFrom(dir: Path) = CompilationTests.sources(Files.walk(dir))

val strawmanSources = sourcesFrom(Paths.get("../collection-strawman/src/main"))
val strawmanSourcesSorted = strawmanSources.sorted
val strawmanSourcesRevSorted = strawmanSourcesSorted.reverse

val posIdempotency = {
def posIdempotency1 = compileFilesInDir("../tests/pos", opt)
def posIdempotency2 = compileFilesInDir("../tests/pos", opt)
posIdempotency1 + posIdempotency2
}
def idempotency2() = {
compileDir("../collection-strawman/src/main", opt) +
compileFilesInDir("../tests/pos", opt)

val orderIdempotency = {
(for {
testDir <- new JFile("../tests/order-idempotency").listFiles() if testDir.isDirectory
} yield {
val sources = sourcesFrom(testDir.toPath)
def orderIdempotency1 = compileList(testDir.getName, sources, opt)
def orderIdempotency2 = compileList(testDir.getName, sources.reverse, opt)
orderIdempotency1 + orderIdempotency2
}).reduce(_ + _)
}

val tests = (idempotency1() + idempotency2()).keepOutput.checkCompile()
val strawmanIdempotency = {
compileList("strawman0", strawmanSources, opt) +
compileList("strawman1", strawmanSources, opt) +
compileList("strawman2", strawmanSourcesSorted, opt) +
compileList("strawman3", strawmanSourcesRevSorted, opt)
}

assert(new java.io.File("../out/idempotency1/").exists)
assert(new java.io.File("../out/idempotency2/").exists)
def check(name: String) = {
val files = List(s"../tests/idempotency/$name.scala", "../tests/idempotency/IdempotencyCheck.scala")
compileList(name, files, defaultOptions)
}
val allChecks = {
check("CheckOrderIdempotency") +
check("CheckStrawmanIdempotency") +
check("CheckPosIdempotency")
}

compileList("idempotency", List("../tests/idempotency/Checker.scala", "../tests/idempotency/IdempotencyCheck.scala"), defaultOptions).checkRuns()
val allTests = {
strawmanIdempotency +
orderIdempotency +
posIdempotency
}

val tests = allTests.keepOutput.checkCompile()
allChecks.checkRuns()
tests.delete()
}

Expand Down
2 changes: 1 addition & 1 deletion tests/idempotency/BootstrapChecker.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

object Test {
def main(args: Array[String]): Unit =
IdempotencyCheck.checkIdempotency("../out/dotty")
IdempotencyCheck.checkIdempotency("../out/dotty1", "../out/dotty2")
}
5 changes: 5 additions & 0 deletions tests/idempotency/CheckOrderIdempotency.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

object Test {
def main(args: Array[String]): Unit =
IdempotencyCheck.checkIdempotency("../out/orderIdempotency1", "../out/orderIdempotency2")
}
5 changes: 5 additions & 0 deletions tests/idempotency/CheckPosIdempotency.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

object Test {
def main(args: Array[String]): Unit =
IdempotencyCheck.checkIdempotency("../out/posIdempotency1", "../out/posIdempotency2")
}
11 changes: 11 additions & 0 deletions tests/idempotency/CheckStrawmanIdempotency.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

object Test {
def main(args: Array[String]): Unit = {
IdempotencyCheck.checkIdempotency("../out/idempotency/strawman0", "../out/idempotency/strawman1")
// FIXME: #2964 and maybe more
/*
IdempotencyCheck.checkIdempotency("../out/idempotency/strawman1", "../out/idempotency/strawman2")
IdempotencyCheck.checkIdempotency("../out/idempotency/strawman1", "../out/idempotency/strawman3")
*/
}
}
5 changes: 0 additions & 5 deletions tests/idempotency/Checker.scala

This file was deleted.

31 changes: 16 additions & 15 deletions tests/idempotency/IdempotencyCheck.scala
Original file line number Diff line number Diff line change
Expand Up @@ -13,31 +13,32 @@ object IdempotencyCheck {
"pos/i2345/Whatever"
)

def checkIdempotency(dirPrefix: String): Unit = {
def checkIdempotency(dir1: String, dir2: String): Unit = {
var failed = 0
var total = 0

val groupedBytecodeFiles: List[(JPath, JPath, JPath, JPath)] = {
val bytecodeFiles = {
def bytecodeFiles(paths: JStream[JPath]): List[JPath] = {
def bytecodeFiles(paths: JStream[JPath], dir: String): List[(String, JPath)] = {
def isBytecode(file: String) = file.endsWith(".class") || file.endsWith(".tasty")
paths.iterator.asScala.filter(path => isBytecode(path.toString)).toList
def tupleWithName(f: JPath) = (f.toString.substring(dir.length + 1, f.toString.length - 6), f)
paths.iterator.asScala.filter(path => isBytecode(path.toString)).map(tupleWithName).toList
}
val compilerDir1 = JPaths.get(dirPrefix + 1)
val compilerDir2 = JPaths.get(dirPrefix + 2)
bytecodeFiles(JFiles.walk(compilerDir1)) ++ bytecodeFiles(JFiles.walk(compilerDir2))
val compilerDir1 = JPaths.get(dir1)
val compilerDir2 = JPaths.get(dir2)
bytecodeFiles(JFiles.walk(compilerDir1), dir1) ++ bytecodeFiles(JFiles.walk(compilerDir2), dir2)
}
val groups = bytecodeFiles.groupBy(f => f.toString.substring(dirPrefix.length + 1, f.toString.length - 6))
val groups = bytecodeFiles.groupBy(_._1).mapValues(_.map(_._2))

groups.filterNot(x => blacklisted(x._1)).valuesIterator.flatMap { g =>
def pred(f: JPath, i: Int, isTasty: Boolean) =
f.toString.contains(dirPrefix + i) && f.toString.endsWith(if (isTasty) ".tasty" else ".class")
val class1 = g.find(f => pred(f, 1, isTasty = false))
val class2 = g.find(f => pred(f, 2, isTasty = false))
val tasty1 = g.find(f => pred(f, 1, isTasty = true))
val tasty2 = g.find(f => pred(f, 2, isTasty = true))
assert(class1.isDefined, s"Could not find class in ${dirPrefix + 1} for $class2")
assert(class2.isDefined, s"Could not find class in ${dirPrefix + 2} for $class1")
def pred(f: JPath, dir: String, isTasty: Boolean) =
f.toString.contains(dir) && f.toString.endsWith(if (isTasty) ".tasty" else ".class")
val class1 = g.find(f => pred(f, dir1, isTasty = false))
val class2 = g.find(f => pred(f, dir2, isTasty = false))
val tasty1 = g.find(f => pred(f, dir1, isTasty = true))
val tasty2 = g.find(f => pred(f, dir2, isTasty = true))
assert(class1.isDefined, s"Could not find class in ${dir1} for $class2")
assert(class2.isDefined, s"Could not find class in ${dir2} for $class1")
if (tasty1.isEmpty || tasty2.isEmpty) Nil
else List(Tuple4(class1.get, tasty1.get, class2.get, tasty2.get))
}.toList
Expand Down
6 changes: 6 additions & 0 deletions tests/order-idempotency/listTest/App.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
object App {
val list1 = new C(2, N)
val list2 = new C(2, list1)
list2.head
list2.tail
}
1 change: 1 addition & 0 deletions tests/order-idempotency/listTest/C.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
class C[+T](val head: T, val tail: L[T]) extends L[T]
4 changes: 4 additions & 0 deletions tests/order-idempotency/listTest/L.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trait L[+T] {
def head: T
def tail: L[T]
}
4 changes: 4 additions & 0 deletions tests/order-idempotency/listTest/N.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
object N extends L[Nothing] {
def head = ???
def tail = ???
}