aboutsummaryrefslogtreecommitdiff
path: root/compiler/test/dotty/tools/dotc
diff options
context:
space:
mode:
authorFelix Mulder <felix.mulder@gmail.com>2016-11-02 11:08:28 +0100
committerGuillaume Martres <smarter@ubuntu.com>2016-11-22 01:35:07 +0100
commit8a61ff432543a29234193cd1f7c14abd3f3d31a0 (patch)
treea8147561d307af862c295cfc8100d271063bb0dd /compiler/test/dotty/tools/dotc
parent6a455fe6da5ff9c741d91279a2dc6fe2fb1b472f (diff)
downloaddotty-8a61ff432543a29234193cd1f7c14abd3f3d31a0.tar.gz
dotty-8a61ff432543a29234193cd1f7c14abd3f3d31a0.tar.bz2
dotty-8a61ff432543a29234193cd1f7c14abd3f3d31a0.zip
Move compiler and compiler tests to compiler dir
Diffstat (limited to 'compiler/test/dotty/tools/dotc')
-rw-r--r--compiler/test/dotty/tools/dotc/CompilerTest.scala623
-rw-r--r--compiler/test/dotty/tools/dotc/EntryPointsTest.scala72
-rw-r--r--compiler/test/dotty/tools/dotc/InterfaceEntryPointTest.scala72
-rw-r--r--compiler/test/dotty/tools/dotc/ast/TreeInfoTest.scala30
-rw-r--r--compiler/test/dotty/tools/dotc/parsing/DeSugarTest.scala96
-rw-r--r--compiler/test/dotty/tools/dotc/parsing/DocstringTest.scala34
-rw-r--r--compiler/test/dotty/tools/dotc/parsing/DocstringTests.scala491
-rw-r--r--compiler/test/dotty/tools/dotc/parsing/ModifiersParsingTest.scala164
-rw-r--r--compiler/test/dotty/tools/dotc/parsing/ParserTest.scala44
-rw-r--r--compiler/test/dotty/tools/dotc/parsing/ScannerTest.scala65
-rw-r--r--compiler/test/dotty/tools/dotc/parsing/desugarPackage.scala28
-rw-r--r--compiler/test/dotty/tools/dotc/parsing/parseFile.scala13
-rw-r--r--compiler/test/dotty/tools/dotc/parsing/parsePackage.scala83
-rw-r--r--compiler/test/dotty/tools/dotc/parsing/showTree.scala26
-rw-r--r--compiler/test/dotty/tools/dotc/repl/TestREPL.scala66
-rw-r--r--compiler/test/dotty/tools/dotc/reporting/TestMessageLaziness.scala37
-rw-r--r--compiler/test/dotty/tools/dotc/reporting/TestReporter.scala52
-rw-r--r--compiler/test/dotty/tools/dotc/transform/CreateCompanionObjectsTest.scala128
-rw-r--r--compiler/test/dotty/tools/dotc/transform/LazyValsTest.scala361
-rw-r--r--compiler/test/dotty/tools/dotc/transform/PostTyperTransformerTest.scala132
-rw-r--r--compiler/test/dotty/tools/dotc/transform/TreeTransformerTest.scala198
21 files changed, 2815 insertions, 0 deletions
diff --git a/compiler/test/dotty/tools/dotc/CompilerTest.scala b/compiler/test/dotty/tools/dotc/CompilerTest.scala
new file mode 100644
index 000000000..fbec9003a
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/CompilerTest.scala
@@ -0,0 +1,623 @@
+package dotty.tools.dotc
+
+import repl.TestREPL
+import core.Contexts._
+import dotty.partest.DPConfig
+import interfaces.Diagnostic.ERROR
+import reporting._
+import diagnostic.MessageContainer
+import util.SourcePosition
+import config.CompilerCommand
+import dotty.tools.io.PlainFile
+import scala.collection.mutable.ListBuffer
+import scala.reflect.io.{ Path, Directory, File => SFile, AbstractFile }
+import scala.tools.partest.nest.{ FileManager, NestUI }
+import scala.annotation.tailrec
+import java.io.{ RandomAccessFile, File => JFile }
+
+
+/** This class has two modes: it can directly run compiler tests, or it can
+ * generate the necessary file structure for partest in the directory
+ * DPConfig.testRoot. Both modes are regular JUnit tests. Which mode is used
+ * depends on the existence of the tests/locks/partest-ppid.lock file which is
+ * created by sbt to trigger partest generation. Sbt will then run partest on
+ * the generated sources.
+ *
+ * Through overriding the partestableXX methods, tests can always be run as
+ * JUnit compiler tests. Run tests cannot be run by JUnit, only by partest.
+ *
+ * A test can either be a file or a directory. Partest will generate a
+ * <test>-<kind>.log file with output of failed tests. Partest reads compiler
+ * flags and the number of errors expected from a neg test from <test>.flags
+ * and <test>.nerr files (also generated). The test is in a parent directory
+ * that determines the kind of test:
+ * - pos: checks that compilation succeeds
+ * - neg: checks that compilation fails with the given number of errors
+ * - run: compilation succeeds, partest: test run generates the output in
+ * <test>.check. Run tests always need to be:
+ * object Test { def main(args: Array[String]): Unit = ... }
+ * Classpath jars can be added to partestDeps in the sbt Build.scala.
+ */
+abstract class CompilerTest {
+
+ /** Override with output dir of test so it can be patched. Partest expects
+ * classes to be in partest-generated/[kind]/[testname]-[kind].obj/ */
+ val defaultOutputDir: String
+
+ /** Override to filter out tests that should not be run by partest. */
+ def partestableFile(prefix: String, fileName: String, extension: String, args: List[String]) = true
+ def partestableDir(prefix: String, dirName: String, args: List[String]) = true
+ def partestableList(testName: String, files: List[String], args: List[String]) = true
+
+ val generatePartestFiles = {
+ /* Because we fork in test, the JVM in which this JUnit test runs has a
+ * different pid from the one that started the partest. But the forked VM
+ * receives the pid of the parent as system property. If the lock file
+ * exists, the parent is requesting partest generation. This mechanism
+ * allows one sbt instance to run test (JUnit only) and another partest.
+ * We cannot run two instances of partest at the same time, because they're
+ * writing to the same directories. The sbt lock file generation prevents
+ * this.
+ */
+ val pid = System.getProperty("partestParentID")
+ if (pid == null)
+ false
+ else
+ new JFile(".." + JFile.separator + "tests" + JFile.separator + "locks" + JFile.separator + s"partest-$pid.lock").exists
+ }
+
+ // Delete generated files from previous run and create new log
+ val logFile = if (!generatePartestFiles) None else Some(CompilerTest.init)
+
+ /** Always run with JUnit. */
+ def compileLine(cmdLine: String)(implicit defaultOptions: List[String]): Unit = {
+ if (generatePartestFiles)
+ log("WARNING: compileLine will always run with JUnit, no partest files generated.")
+ compileArgs(cmdLine.split("\n"), Nil)
+ }
+
+ /** Compiles the given code file.
+ *
+ * @param prefix the parent directory (including separator at the end)
+ * @param fileName the filename, by default without extension
+ * @param args arguments to the compiler
+ * @param extension the file extension, .scala by default
+ * @param defaultOptions more arguments to the compiler
+ */
+ def compileFile(prefix: String, fileName: String, args: List[String] = Nil, extension: String = ".scala", runTest: Boolean = false)
+ (implicit defaultOptions: List[String]): Unit = {
+ val filePath = s"$prefix$fileName$extension"
+ val expErrors = expectedErrors(filePath)
+ if (!generatePartestFiles || !partestableFile(prefix, fileName, extension, args ++ defaultOptions)) {
+ if (runTest)
+ log(s"WARNING: run tests can only be run by partest, JUnit just verifies compilation: $prefix$fileName$extension")
+ if (args.contains("-rewrite")) {
+ val file = new PlainFile(filePath)
+ val data = file.toByteArray
+ // compile with rewrite
+ compileArgs((filePath :: args).toArray, expErrors)
+ // compile again, check that file now compiles without -language:Scala2
+ val plainArgs = args.filter(arg => arg != "-rewrite" && arg != "-language:Scala2")
+ compileFile(prefix, fileName, plainArgs, extension, runTest)
+ // restore original test file
+ val out = file.output
+ out.write(data)
+ out.close()
+ }
+ else compileArgs((filePath :: args).toArray, expErrors)
+ } else {
+ val kind = testKind(prefix, runTest)
+ log(s"generating partest files for test file: $prefix$fileName$extension of kind $kind")
+
+ val sourceFile = new JFile(prefix + fileName + extension)
+ if (sourceFile.exists) {
+ val firstDest = SFile(DPConfig.testRoot + JFile.separator + kind + JFile.separator + fileName + extension)
+ val xerrors = expErrors.map(_.totalErrors).sum
+ computeDestAndCopyFiles(sourceFile, firstDest, kind, args ++ defaultOptions, xerrors.toString)
+ } else {
+ throw new java.io.FileNotFoundException(s"Unable to locate test file $prefix$fileName")
+ }
+ }
+ }
+ def runFile(prefix: String, fileName: String, args: List[String] = Nil, extension: String = ".scala")
+ (implicit defaultOptions: List[String]): Unit = {
+ compileFile(prefix, fileName, args, extension, true)
+ }
+
+ def findJarFromRuntime(partialName: String): String = {
+ val urls = ClassLoader.getSystemClassLoader.asInstanceOf[java.net.URLClassLoader].getURLs.map(_.getFile.toString)
+ urls.find(_.contains(partialName)).getOrElse {
+ throw new java.io.FileNotFoundException(
+ s"""Unable to locate $partialName on classpath:\n${urls.toList.mkString("\n")}"""
+ )
+ }
+ }
+
+ private def compileWithJavac(
+ fs: Array[String],
+ args: Array[String]
+ )(implicit defaultOptions: List[String]): Boolean = {
+ val scalaLib = findJarFromRuntime("scala-library")
+ val fullArgs = Array(
+ "javac",
+ "-classpath",
+ s".:$scalaLib"
+ ) ++ args ++ defaultOptions.dropWhile("-d" != _).take(2) ++ fs
+
+ Runtime.getRuntime.exec(fullArgs).waitFor() == 0
+ }
+
+ /** Compiles the code files in the given directory together. If args starts
+ * with "-deep", all files in subdirectories (and so on) are included. */
+ def compileDir(prefix: String, dirName: String, args: List[String] = Nil, runTest: Boolean = false)
+ (implicit defaultOptions: List[String]): Unit = {
+ def computeFilePathsAndExpErrors = {
+ val dir = Directory(prefix + dirName)
+ val (files, normArgs) = args match {
+ case "-deep" :: args1 => (dir.deepFiles, args1)
+ case _ => (dir.files, args)
+ }
+ val (filePaths, javaFilePaths) = files
+ .toArray.map(_.toString)
+ .foldLeft((Array.empty[String], Array.empty[String])) { case (acc @ (fp, jfp), name) =>
+ if (name endsWith ".scala") (name +: fp, jfp)
+ else if (name endsWith ".java") (fp, name +: jfp)
+ else (fp, jfp)
+ }
+ val expErrors = expectedErrors(filePaths.toList)
+ (filePaths, javaFilePaths, normArgs, expErrors)
+ }
+ if (!generatePartestFiles || !partestableDir(prefix, dirName, args ++ defaultOptions)) {
+ if (runTest)
+ log(s"WARNING: run tests can only be run by partest, JUnit just verifies compilation: $prefix$dirName")
+ val (filePaths, javaFilePaths, normArgs, expErrors) = computeFilePathsAndExpErrors
+ compileWithJavac(javaFilePaths, Array.empty) // javac needs to run first on dotty-library
+ compileArgs(javaFilePaths ++ filePaths ++ normArgs, expErrors)
+ } else {
+ val (sourceDir, flags, deep) = args match {
+ case "-deep" :: args1 => (flattenDir(prefix, dirName), args1 ++ defaultOptions, "deep")
+ case _ => (new JFile(prefix + dirName), args ++ defaultOptions, "shallow")
+ }
+ val kind = testKind(prefix, runTest)
+ log(s"generating partest files for test directory ($deep): $prefix$dirName of kind $kind")
+
+ if (sourceDir.exists) {
+ val firstDest = Directory(DPConfig.testRoot + JFile.separator + kind + JFile.separator + dirName)
+ val xerrors = if (isNegTest(prefix)) {
+ val (_, _, _, expErrors) = computeFilePathsAndExpErrors
+ expErrors.map(_.totalErrors).sum
+ } else 0
+ computeDestAndCopyFiles(sourceDir, firstDest, kind, flags, xerrors.toString)
+ if (deep == "deep")
+ Directory(sourceDir).deleteRecursively
+ } else {
+ throw new java.io.FileNotFoundException(s"Unable to locate test dir $prefix$dirName")
+ }
+ }
+ }
+ def runDir(prefix: String, dirName: String, args: List[String] = Nil)
+ (implicit defaultOptions: List[String]): Unit =
+ compileDir(prefix, dirName, args, true)
+
+ /** Compiles each source in the directory path separately by calling
+ * compileFile resp. compileDir. */
+ def compileFiles(path: String, args: List[String] = Nil, verbose: Boolean = true, runTest: Boolean = false,
+ compileSubDirs: Boolean = true)(implicit defaultOptions: List[String]): Unit = {
+ val dir = Directory(path)
+ val fileNames = dir.files.toArray.map(_.jfile.getName).filter(name => (name endsWith ".scala") || (name endsWith ".java"))
+ for (name <- fileNames) {
+ if (verbose) log(s"testing $path$name")
+ compileFile(path, name, args, "", runTest)
+ }
+ if (compileSubDirs)
+ for (subdir <- dir.dirs) {
+ if (verbose) log(s"testing $subdir")
+ compileDir(path, subdir.jfile.getName, args, runTest)
+ }
+ }
+ def runFiles(path: String, args: List[String] = Nil, verbose: Boolean = true)
+ (implicit defaultOptions: List[String]): Unit =
+ compileFiles(path, args, verbose, true)
+
+ /** Compiles the given list of code files. */
+ def compileList(testName: String, files: List[String], args: List[String] = Nil)
+ (implicit defaultOptions: List[String]): Unit = {
+ if (!generatePartestFiles || !partestableList(testName, files, args ++ defaultOptions)) {
+ val expErrors = expectedErrors(files)
+ compileArgs((files ++ args).toArray, expErrors)
+ } else {
+ val destDir = Directory(DPConfig.testRoot + JFile.separator + testName)
+ files.foreach({ file =>
+ val jfile = new JFile(file)
+ recCopyFiles(jfile, destDir / jfile.getName)
+ })
+ compileDir(DPConfig.testRoot + JFile.separator, testName, args)
+ destDir.deleteRecursively
+ }
+ }
+
+ def replFile(prefix: String, fileName: String): Unit = {
+ val path = s"$prefix$fileName"
+ val f = new PlainFile(path)
+ val repl = new TestREPL(new String(f.toCharArray))
+ repl.process(Array[String]())
+ repl.check()
+ }
+
+ def replFiles(path: String): Unit = {
+ val dir = Directory(path)
+ val fileNames = dir.files.toArray.map(_.jfile.getName).filter(_ endsWith ".check")
+ for (name <- fileNames) {
+ log(s"testing $path$name")
+ replFile(path, name)
+ }
+ }
+
+ // ========== HELPERS =============
+
+ private def expectedErrors(filePaths: List[String]): List[ErrorsInFile] = if (filePaths.exists(isNegTest(_))) filePaths.map(getErrors(_)) else Nil
+
+ private def expectedErrors(filePath: String): List[ErrorsInFile] = expectedErrors(List(filePath))
+
+ private def isNegTest(testPath: String) = testPath.contains("/neg/")
+
+ private def compileArgs(args: Array[String], expectedErrorsPerFile: List[ErrorsInFile])
+ (implicit defaultOptions: List[String]): Unit = {
+ val allArgs = args ++ defaultOptions
+ //println(s"""all args: ${allArgs.mkString("\n")}""")
+ val processor = if (allArgs.exists(_.startsWith("#"))) Bench else Main
+ val storeReporter = new Reporter with UniqueMessagePositions with HideNonSensicalMessages {
+ private val consoleReporter = new ConsoleReporter()
+ private val innerStoreReporter = new StoreReporter(consoleReporter)
+ def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
+ if (m.level == ERROR) {
+ innerStoreReporter.flush()
+ consoleReporter.doReport(m)
+ }
+ else if (errorCount > 0) consoleReporter.doReport(m)
+ else innerStoreReporter.doReport(m)
+ }
+ }
+ val reporter = processor.process(allArgs, storeReporter)
+
+ val nerrors = reporter.errorCount
+ val xerrors = (expectedErrorsPerFile map {_.totalErrors}).sum
+ def expectedErrorFiles =
+ expectedErrorsPerFile.collect{
+ case er if er.totalErrors > 0 => er.fileName
+ }
+ assert(nerrors == xerrors,
+ s"""Wrong # of errors. Expected: $xerrors, found: $nerrors
+ |Files with expected errors: $expectedErrorFiles
+ |errors:
+ """.stripMargin)
+ // NEG TEST
+ if (xerrors > 0) {
+ val errorLines = reporter.allErrors.map(_.pos)
+ // reporter didn't record as many errors as its errorCount says
+ assert(errorLines.length == nerrors, s"Not enough errors recorded.")
+
+ // Some compiler errors have an associated source position. Each error
+ // needs to correspond to a "// error" marker on that line in the source
+ // file and vice versa.
+ // Other compiler errors don't have an associated source position. Their
+ // number should correspond to the total count of "// nopos-error"
+ // markers in all files
+ val (errorsByFile, errorsWithoutPos) = errorLines.groupBy(_.source.file).toList.partition(_._1.toString != "<no source>")
+
+ // check errors with source position
+ val foundErrorsPerFile = errorsByFile.map({ case (fileName, errorList) =>
+ val posErrorLinesToNr = errorList.groupBy(_.line).toList.map({ case (line, list) => (line, list.length) }).sortBy(_._1)
+ ErrorsInFile(fileName.toString, 0, posErrorLinesToNr)
+ })
+ val expectedErrorsPerFileZeroed = expectedErrorsPerFile.map({
+ case ErrorsInFile(fileName, _, posErrorLinesToNr) =>
+ ErrorsInFile(fileName.toString, 0, posErrorLinesToNr)
+ })
+ checkErrorsWithPosition(expectedErrorsPerFileZeroed, foundErrorsPerFile)
+
+ // check errors without source position
+ val expectedNoPos = expectedErrorsPerFile.map(_.noposErrorNr).sum
+ val foundNoPos = errorsWithoutPos.map(_._2.length).sum
+ assert(foundNoPos == expectedNoPos,
+ s"Wrong # of errors without source position. Expected (all files): $expectedNoPos, found (compiler): $foundNoPos")
+ }
+ }
+
+ // ========== NEG TEST HELPERS =============
+
+ /** Captures the number of nopos-errors in the given file and the number of
+ * errors with a position, represented as a tuple of source line and number
+ * of errors on that line. */
+ case class ErrorsInFile(fileName: String, noposErrorNr: Int, posErrorLinesToNr: List[(Int, Int)]) {
+ def totalErrors = noposErrorNr + posErrorLinesToNr.map(_._2).sum
+ }
+
+ /** Extracts the errors expected for the given neg test file. */
+ def getErrors(fileName: String): ErrorsInFile = {
+ val content = SFile(fileName).slurp
+ val (line, rest) = content.span(_ != '\n')
+
+ @tailrec
+ def checkLine(line: String, rest: String, index: Int, noposAcc: Int, posAcc: List[(Int, Int)]): ErrorsInFile = {
+ val posErrors = "// ?error".r.findAllIn(line).length
+ val newPosAcc = if (posErrors > 0) (index, posErrors) :: posAcc else posAcc
+ val newNoPosAcc = noposAcc + "// ?nopos-error".r.findAllIn(line).length
+ val (newLine, newRest) = rest.span(_ != '\n')
+ if (newRest.isEmpty)
+ ErrorsInFile(fileName.toString, newNoPosAcc, newPosAcc.reverse)
+ else
+ checkLine(newLine, newRest.tail, index + 1, newNoPosAcc, newPosAcc) // skip leading '\n'
+ }
+
+ checkLine(line, rest.tail, 0, 0, Nil) // skip leading '\n'
+ }
+
+ /** Asserts that the expected and found number of errors correspond, and
+ * otherwise throws an error with the filename, plus optionally a line
+ * number if available. */
+ def errorMsg(fileName: String, lineNumber: Option[Int], exp: Int, found: Int) = {
+ val i = lineNumber.map({ i => ":" + (i + 1) }).getOrElse("")
+ assert(found == exp, s"Wrong # of errors for $fileName$i. Expected (file): $exp, found (compiler): $found")
+ }
+
+ /** Compares the expected with the found errors and creates a nice error
+ * message if they don't agree. */
+ def checkErrorsWithPosition(expected: List[ErrorsInFile], found: List[ErrorsInFile]): Unit = {
+ // create nice error messages
+ expected.diff(found) match {
+ case Nil => // nothing missing
+ case ErrorsInFile(fileName, _, expectedLines) :: xs =>
+ found.find(_.fileName == fileName) match {
+ case None =>
+ // expected some errors, but none found for this file
+ errorMsg(fileName, None, expectedLines.map(_._2).sum, 0)
+ case Some(ErrorsInFile(_,_,foundLines)) =>
+ // found wrong number/location of markers for this file
+ compareLines(fileName, expectedLines, foundLines)
+ }
+ }
+
+ found.diff(expected) match {
+ case Nil => // nothing missing
+ case ErrorsInFile(fileName, _, foundLines) :: xs =>
+ expected.find(_.fileName == fileName) match {
+ case None =>
+ // found some errors, but none expected for this file
+ errorMsg(fileName, None, 0, foundLines.map(_._2).sum)
+ case Some(ErrorsInFile(_,_,expectedLines)) =>
+ // found wrong number/location of markers for this file
+ compareLines(fileName, expectedLines, foundLines)
+ }
+ }
+ }
+
+ /** Gives an error message for one line where the expected number of errors and
+ * the number of compiler errors differ. */
+ def compareLines(fileName: String, expectedLines: List[(Int, Int)], foundLines: List[(Int, Int)]) = {
+ expectedLines.foreach({ case (line, expNr) =>
+ foundLines.find(_._1 == line) match {
+ case Some((_, `expNr`)) => // this line is ok
+ case Some((_, foundNr)) => errorMsg(fileName, Some(line), expNr, foundNr)
+ case None => errorMsg(fileName, Some(line), expNr, 0)
+ }
+ })
+ foundLines.foreach({ case (line, foundNr) =>
+ expectedLines.find(_._1 == line) match {
+ case Some((_, `foundNr`)) => // this line is ok
+ case Some((_, expNr)) => errorMsg(fileName, Some(line), expNr, foundNr)
+ case None => errorMsg(fileName, Some(line), 0, foundNr)
+ }
+ })
+ }
+
+ // ========== PARTEST HELPERS =============
+
+ // In particular, don't copy flags from scalac tests
+ private val extensionsToCopy = scala.collection.immutable.HashSet("scala", "java")
+
+ /** Determines what kind of test to run. */
+ private def testKind(prefixDir: String, runTest: Boolean) = {
+ if (runTest) "run"
+ else if (isNegTest(prefixDir)) "neg"
+ else if (prefixDir.endsWith("run" + JFile.separator)) {
+ log("WARNING: test is being run as pos test despite being in a run directory. " +
+ "Use runFile/runDir instead of compileFile/compileDir to do a run test")
+ "pos"
+ } else "pos"
+ }
+
+ /** The three possibilities: no generated sources exist yet, the same sources
+ * exist already, different sources exist. */
+ object Difference extends Enumeration {
+ type Difference = Value
+ val NotExists, ExistsSame, ExistsDifferent = Value
+ }
+ import Difference._
+
+ /** The same source might be used for several partest test cases (e.g. with
+ * different flags). Detects existing versions and computes the path to be
+ * used for this version, e.g. testname_v1 for the first alternative. */
+ private def computeDestAndCopyFiles(source: JFile, dest: Path, kind: String, oldFlags: List[String], nerr: String,
+ nr: Int = 0, oldOutput: String = defaultOutputDir): Unit = {
+
+ val partestOutput = dest.jfile.getParentFile + JFile.separator + dest.stripExtension + "-" + kind + ".obj"
+
+ val altOutput =
+ source.getParentFile.getAbsolutePath.map(x => if (x == JFile.separatorChar) '_' else x)
+
+ val (beforeCp, remaining) = oldFlags
+ .map(f => if (f == oldOutput) partestOutput else f)
+ .span(_ != "-classpath")
+ val flags = beforeCp ++ List("-classpath", (partestOutput :: remaining.drop(1)).mkString(":"))
+
+ val difference = getExisting(dest).isDifferent(source, flags, nerr)
+ difference match {
+ case NotExists => copyFiles(source, dest, partestOutput, flags, nerr, kind)
+ case ExistsSame => // nothing else to do
+ case ExistsDifferent =>
+ val nextDest = dest.parent / (dest match {
+ case d: Directory =>
+ val newVersion = replaceVersion(d.name, nr).getOrElse(altOutput)
+ Directory(newVersion)
+ case f =>
+ val newVersion = replaceVersion(f.stripExtension, nr).getOrElse(altOutput)
+ SFile(newVersion).addExtension(f.extension)
+ })
+ computeDestAndCopyFiles(source, nextDest, kind, flags, nerr, nr + 1, partestOutput)
+ }
+ }
+
+ /** Copies the test sources. Creates flags, nerr, check and output files. */
+ private def copyFiles(sourceFile: Path, dest: Path, partestOutput: String, flags: List[String], nerr: String, kind: String) = {
+ recCopyFiles(sourceFile, dest)
+
+ new JFile(partestOutput).mkdirs
+
+ if (flags.nonEmpty)
+ dest.changeExtension("flags").createFile(true).writeAll(flags.mkString(" "))
+ if (nerr != "0")
+ dest.changeExtension("nerr").createFile(true).writeAll(nerr)
+ sourceFile.changeExtension("check").ifFile({ check =>
+ if (kind == "run") {
+ FileManager.copyFile(check.jfile, dest.changeExtension("check").jfile)
+ dest.changeExtension("checksrc").createFile(true).writeAll("check file generated from source:\n" + check.toString)
+ } else {
+ log(s"WARNING: ignoring $check for test kind $kind")
+ }
+ })
+
+ }
+
+ /** Recursively copy over source files and directories, excluding extensions
+ * that aren't in extensionsToCopy. */
+ private def recCopyFiles(sourceFile: Path, dest: Path): Unit = {
+
+ def copyfile(file: SFile, bytewise: Boolean): Unit = {
+ if (bytewise) {
+ val in = file.inputStream()
+ val out = SFile(dest).outputStream()
+ val buffer = new Array[Byte](1024)
+ def loop(available: Int):Unit = {
+ if (available < 0) {()}
+ else {
+ out.write(buffer, 0, available)
+ val read = in.read(buffer)
+ loop(read)
+ }
+ }
+ loop(0)
+ in.close()
+ out.close()
+ } else {
+ try {
+ SFile(dest)(scala.io.Codec.UTF8).writeAll((s"/* !!!!! WARNING: DO NOT MODIFY. Original is at: $file !!!!! */").replace("\\", "/"), file.slurp("UTF-8"))
+ } catch {
+ case unmappable: java.nio.charset.MalformedInputException =>
+ copyfile(file, true) //there are bytes that can't be mapped with UTF-8. Bail and just do a straight byte-wise copy without the warning header.
+ }
+ }
+ }
+
+ processFileDir(sourceFile, { sf =>
+ if (extensionsToCopy.contains(sf.extension)) {
+ dest.parent.jfile.mkdirs
+ copyfile(sf, false)
+ } else {
+ log(s"WARNING: ignoring $sf")
+ }
+ }, { sdir =>
+ dest.jfile.mkdirs
+ sdir.list.foreach(path => recCopyFiles(path, dest / path.name))
+ }, Some("DPCompilerTest.recCopyFiles: sourceFile not found: " + sourceFile))
+ }
+
+ /** Reads the existing files for the given test source if any. */
+ private def getExisting(dest: Path): ExistingFiles = {
+ val content: Option[Option[String]] = processFileDir(dest, f => try Some(f.slurp("UTF8")) catch {case io: java.io.IOException => Some(io.toString())}, d => Some(""))
+ if (content.isDefined && content.get.isDefined) {
+ val flags = (dest changeExtension "flags").toFile.safeSlurp
+ val nerr = (dest changeExtension "nerr").toFile.safeSlurp
+ ExistingFiles(content.get, flags, nerr)
+ } else ExistingFiles()
+ }
+
+ /** Encapsulates existing generated test files. */
+ case class ExistingFiles(genSrc: Option[String] = None, flags: Option[String] = None, nerr: Option[String] = None) {
+ def isDifferent(sourceFile: JFile, otherFlags: List[String], otherNerr: String): Difference = {
+ if (!genSrc.isDefined) {
+ NotExists
+ } else {
+ val source = processFileDir(sourceFile, { f => try Some(f.slurp("UTF8")) catch {case _: java.io.IOException => None} }, { d => Some("") },
+ Some("DPCompilerTest sourceFile doesn't exist: " + sourceFile)).get
+ if (source == genSrc) {
+ nerr match {
+ case Some(n) if (n != otherNerr) => ExistsDifferent
+ case None if (otherNerr != "0") => ExistsDifferent
+ case _ if (flags.map(_ == otherFlags.mkString(" ")).getOrElse(otherFlags.isEmpty)) => ExistsSame
+ case _ => ExistsDifferent
+ }
+ } else ExistsDifferent
+ }
+ }
+ }
+
+ import scala.util.matching.Regex
+ val nrFinder = """(.*_v)(\d+)""".r
+ /** Changes the version number suffix in the name (without extension). */
+ private def replaceVersion(name: String, nr: Int): Option[String] = {
+ val nrString = nr.toString
+ name match {
+ case nrFinder(prefix, `nrString`) => Some(prefix + (nr + 1))
+ case _ if nr != 0 => None
+ case _ => Some(name + "_v1")
+ }
+ }
+
+ /** Returns None if the given path doesn't exist, otherwise returns Some of
+ * applying either processFile or processDir, depending on what the path
+ * refers to in the file system. If failMsgOnNone is defined, this function
+ * asserts that the file exists using the provided message. */
+ private def processFileDir[T](input: Path, processFile: SFile => T, processDir: Directory => T, failMsgOnNone: Option[String] = None): Option[T] = {
+ val res = input.ifFile(f => processFile(f)).orElse(input.ifDirectory(d => processDir(d)))
+ (failMsgOnNone, res) match {
+ case (Some(msg), None) => assert(false, msg); None
+ case _ => res
+ }
+ }
+
+ /** Creates a temporary directory and copies all (deep) files over, thus
+ * flattening the directory structure. */
+ private def flattenDir(prefix: String, dirName: String): JFile = {
+ val destDir = Directory(DPConfig.testRoot + JFile.separator + "_temp")
+ Directory(prefix + dirName).deepFiles.foreach(source => recCopyFiles(source, destDir / source.name))
+ destDir.jfile
+ }
+
+ /** Write either to console (JUnit) or log file (partest). */
+ private def log(msg: String) = logFile.map(_.appendAll(msg + "\n")).getOrElse(println(msg))
+}
+
+object CompilerTest extends App {
+
+ /** Deletes generated partest sources from a previous run, recreates
+ * directory and returns the freshly created log file. */
+ lazy val init: SFile = {
+ scala.reflect.io.Directory(DPConfig.testRoot).deleteRecursively
+ new JFile(DPConfig.testRoot).mkdirs
+ val log = DPConfig.genLog.createFile(true)
+ println(s"CompilerTest is generating tests for partest, log: $log")
+ log
+ }
+
+// val dotcDir = "/Users/odersky/workspace/dotty/src/dotty/"
+
+// new CompilerTest().compileFile(dotcDir + "tools/dotc/", "CompilationUnit")
+// new CompilerTest().compileFile(dotcDir + "tools/dotc/", "Compiler")
+// new CompilerTest().compileFile(dotcDir + "tools/dotc/", "Driver")
+// new CompilerTest().compileFile(dotcDir + "tools/dotc/", "Main")
+// new CompilerTest().compileFile(dotcDir + "tools/dotc/", "Run")
+
+// new CompilerTest().compileDir(dotcDir + "tools/dotc")
+ // new CompilerTest().compileFile(dotcDir + "tools/dotc/", "Run")
+}
diff --git a/compiler/test/dotty/tools/dotc/EntryPointsTest.scala b/compiler/test/dotty/tools/dotc/EntryPointsTest.scala
new file mode 100644
index 000000000..4a87bbcb5
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/EntryPointsTest.scala
@@ -0,0 +1,72 @@
+package dotty.tools
+package dotc
+
+import org.junit.Test
+import org.junit.Assert._
+import dotty.tools.dotc.interfaces.{CompilerCallback, SourceFile}
+import reporting._
+import reporting.diagnostic.MessageContainer
+import core.Contexts._
+import java.io.File
+import scala.collection.mutable.ListBuffer
+
+/** Test the compiler entry points that depend on dotty
+ *
+ * This file also serve as an example for using [[dotty.tools.dotc.Driver#process]].
+ *
+ * @see [[InterfaceEntryPointTest]]
+ */
+class EntryPointsTest {
+ private val sources =
+ List("../tests/pos/HelloWorld.scala").map(p => new java.io.File(p).getPath())
+ private val dottyInterfaces =
+ new java.io.File("../interfaces/dotty-interfaces-0.1-SNAPSHOT.jar").getPath
+ private val dottyLibrary =
+ new java.io.File("../library/target/scala-2.11/dotty-library_2.11-0.1-SNAPSHOT.jar").getPath
+ private val args =
+ sources ++
+ List("-d", "../out/") ++
+ List("-classpath", dottyInterfaces + ":" + dottyLibrary)
+
+ @Test def runCompiler = {
+ val reporter = new CustomReporter
+ val callback = new CustomCompilerCallback
+
+ Main.process(args.toArray, reporter, callback)
+
+ assertEquals("Number of errors", false, reporter.hasErrors)
+ assertEquals("Number of warnings", false, reporter.hasWarnings)
+ assertEquals("Compiled sources", sources, callback.paths)
+ }
+
+ @Test def runCompilerWithContext = {
+ val reporter = new CustomReporter
+ val callback = new CustomCompilerCallback
+ val context = (new ContextBase).initialCtx.fresh
+ .setReporter(reporter)
+ .setCompilerCallback(callback)
+
+ Main.process(args.toArray, context)
+
+ assertEquals("Number of errors", false, reporter.hasErrors)
+ assertEquals("Number of warnings", false, reporter.hasWarnings)
+ assertEquals("Compiled sources", sources, callback.paths)
+ }
+
+ private class CustomReporter extends Reporter
+ with UniqueMessagePositions
+ with HideNonSensicalMessages {
+ def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
+ }
+ }
+
+ private class CustomCompilerCallback extends CompilerCallback {
+ private val pathsBuffer = new ListBuffer[String]
+ def paths = pathsBuffer.toList
+
+ override def onSourceCompiled(source: SourceFile): Unit = {
+ if (source.jfile.isPresent)
+ pathsBuffer += source.jfile.get.getPath
+ }
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/InterfaceEntryPointTest.scala b/compiler/test/dotty/tools/dotc/InterfaceEntryPointTest.scala
new file mode 100644
index 000000000..b36ea2955
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/InterfaceEntryPointTest.scala
@@ -0,0 +1,72 @@
+package dotty.tools.dotc
+
+import org.junit.Test
+import org.junit.Assert._
+import interfaces._
+import scala.collection.mutable.ListBuffer
+
+/** Test that demonstrates how to use dotty-interfaces
+ *
+ * This test requires:
+ * - dotty-interfaces to be present at compile-time
+ * - dotty-interfaces and dotty to be present at run-time
+ *
+ * Since the ABI of dotty-interfaces is stable, this means that users can write
+ * code that works with multiple versions of dotty without recompilation.
+ *
+ * @see [[OtherEntryPointsTest]]
+ */
+class InterfaceEntryPointTest {
+ @Test def runCompilerFromInterface = {
+ val sources =
+ List("../tests/pos/HelloWorld.scala").map(p => new java.io.File(p).getPath())
+ val dottyInterfaces =
+ new java.io.File("../interfaces/dotty-interfaces-0.1-SNAPSHOT.jar").getPath
+ val dottyLibrary =
+ new java.io.File("../library/target/scala-2.11/dotty-library_2.11-0.1-SNAPSHOT.jar").getPath
+
+ val args =
+ sources ++
+ List("-d", "../out/") ++
+ List("-classpath", dottyInterfaces + ":" + dottyLibrary)
+
+ val mainClass = Class.forName("dotty.tools.dotc.Main")
+ val process = mainClass.getMethod("process",
+ classOf[Array[String]], classOf[SimpleReporter], classOf[CompilerCallback])
+
+ val reporter = new CustomSimpleReporter
+ val callback = new CustomCompilerCallback
+
+ // Run the compiler by calling dotty.tools.dotc.Main.process
+ process.invoke(null, args.toArray, reporter, callback)
+
+ assertEquals("Number of errors", 0, reporter.errorCount)
+ assertEquals("Number of warnings", 0, reporter.warningCount)
+ assertEquals("Compiled sources", sources, callback.paths)
+ }
+
+ private class CustomSimpleReporter extends SimpleReporter {
+ var errorCount = 0
+ var warningCount = 0
+
+ def report(diag: Diagnostic): Unit = {
+ if (diag.level == Diagnostic.ERROR)
+ errorCount += 1
+ if (diag.level == Diagnostic.WARNING)
+ warningCount += 1
+
+ println(diag.message)
+ }
+ }
+
+ private class CustomCompilerCallback extends CompilerCallback {
+ private val pathsBuffer = new ListBuffer[String]
+ def paths = pathsBuffer.toList
+
+ override def onSourceCompiled(source: SourceFile): Unit = {
+ if (source.jfile.isPresent)
+ pathsBuffer += source.jfile.get.getPath
+ }
+ }
+}
+
diff --git a/compiler/test/dotty/tools/dotc/ast/TreeInfoTest.scala b/compiler/test/dotty/tools/dotc/ast/TreeInfoTest.scala
new file mode 100644
index 000000000..a55973c43
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/ast/TreeInfoTest.scala
@@ -0,0 +1,30 @@
+package dotty.tools
+package dotc
+package ast
+
+import org.junit.Test
+import core.Names._
+import core.Types._
+import core.Symbols._
+import org.junit.Assert._
+
+class TreeInfoTest extends DottyTest {
+
+ import tpd._
+
+ @Test
+ def testDefPath = checkCompile("frontend", "class A { def bar = { val x = { val z = 0; 0} }} ") {
+ (tree, context) =>
+ implicit val ctx = context
+ val xTree = tree.find(tree => tree.symbol.name == termName("x")).get
+ val path = defPath(xTree.symbol, tree)
+ assertEquals(List(
+ ("PackageDef", EMPTY_PACKAGE),
+ ("TypeDef", typeName("A")),
+ ("Template", termName("<local A>")),
+ ("DefDef", termName("bar")),
+ ("Block", NoSymbol.name),
+ ("ValDef", termName("x"))
+ ), path.map(x => (x.productPrefix, x.symbol.name)))
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/parsing/DeSugarTest.scala b/compiler/test/dotty/tools/dotc/parsing/DeSugarTest.scala
new file mode 100644
index 000000000..1f79c2cf6
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/parsing/DeSugarTest.scala
@@ -0,0 +1,96 @@
+package dotty.tools
+package dotc
+package parsing
+
+import Tokens._, Parsers._
+import scala.reflect.io._
+import util._
+import core._
+import ast.Trees._
+import ast.desugar
+import ast.desugar._
+import core.Mode
+import Contexts.Context
+
+import scala.collection.mutable.ListBuffer
+
+class DeSugarTest extends ParserTest {
+
+ import dotty.tools.dotc.ast.untpd._
+
+ import Mode._
+
+ val Expr = Mode(0)
+
+ object DeSugar extends UntypedTreeMap {
+ var curMode: Mode = Expr
+ def withMode[T](mode: Mode)(op: => T) = {
+ val saved = curMode
+ curMode = mode
+ try op
+ finally curMode = saved
+ }
+
+ def transform(tree: Tree, mode: Mode)(implicit ctx: Context): Tree = withMode(mode) { transform(tree) }
+ def transform(trees: List[Tree], mode: Mode)(implicit ctx: Context): List[Tree] = withMode(mode) { transform(trees) }
+
+ override def transform(tree: Tree)(implicit ctx: Context): Tree = {
+ val tree1 = desugar(tree)(ctx.withModeBits(curMode))
+ tree1 match {
+ case TypedSplice(t) =>
+ tree1
+ case PostfixOp(od, op) =>
+ PostfixOp(transform(od), op)
+ case Select(qual, name) =>
+ cpy.Select(tree1)(transform(qual, Expr), name)
+ case Apply(fn, args) =>
+ cpy.Apply(tree1)(transform(fn, Expr), transform(args))
+ case TypeApply(fn, args) =>
+ cpy.TypeApply(tree1)(transform(fn, Expr), transform(args, Type))
+ case New(tpt) =>
+ cpy.New(tree1)(transform(tpt, Type))
+ case Typed(expr, tpt) =>
+ cpy.Typed(tree1)(transform(expr), transform(tpt, Type))
+ case CaseDef(pat, guard, body) =>
+ cpy.CaseDef(tree1)(transform(pat, Pattern), transform(guard), transform(body))
+ case SeqLiteral(elems, elemtpt) =>
+ cpy.SeqLiteral(tree1)(transform(elems), transform(elemtpt))
+ case UnApply(fun, implicits, patterns) =>
+ cpy.UnApply(tree1)(transform(fun, Expr), transform(implicits), transform(patterns))
+ case tree1 @ ValDef(name, tpt, _) =>
+ cpy.ValDef(tree1)(name, transform(tpt, Type), transform(tree1.rhs))
+ case tree1 @ DefDef(name, tparams, vparamss, tpt, _) =>
+ cpy.DefDef(tree1)(name, transformSub(tparams), vparamss mapConserve (transformSub(_)), transform(tpt, Type), transform(tree1.rhs))
+ case tree1 @ TypeDef(name, rhs) =>
+ cpy.TypeDef(tree1)(name, transform(rhs, Type))
+ case impl @ Template(constr, parents, self, _) =>
+ cpy.Template(tree1)(transformSub(constr), transform(parents), transformSub(self), transform(impl.body, Expr))
+ case Thicket(trees) =>
+ Thicket(flatten(trees mapConserve super.transform))
+ case tree1 =>
+ super.transform(tree1)
+ }
+ }
+ }
+
+ def firstClass(stats: List[Tree]): String = stats match {
+ case Nil => "<empty>"
+ case TypeDef(name, _) :: _ => name.toString
+ case ModuleDef(name, _) :: _ => name.toString
+ case (pdef: PackageDef) :: _ => firstClass(pdef)
+ case stat :: stats => firstClass(stats)
+ }
+
+ def firstClass(tree: Tree): String = tree match {
+ case PackageDef(pid, stats) =>
+ pid.show + "." + firstClass(stats)
+ case _ => "??? " + tree.getClass
+ }
+
+ def desugarTree(tree: Tree): Tree = {
+ //println("***** desugaring " + firstClass(tree))
+ DeSugar.transform(tree)
+ }
+
+ def desugarAll() = parsedTrees foreach (desugarTree(_).show)
+}
diff --git a/compiler/test/dotty/tools/dotc/parsing/DocstringTest.scala b/compiler/test/dotty/tools/dotc/parsing/DocstringTest.scala
new file mode 100644
index 000000000..30e885f70
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/parsing/DocstringTest.scala
@@ -0,0 +1,34 @@
+package dotty.tools
+package dotc
+package parsing
+
+import ast.Trees._
+import core.Contexts.Context
+
+trait DocstringTest extends DottyTest {
+ ctx = ctx.fresh.setSetting(ctx.settings.YkeepComments, true)
+
+ def checkDocString(actual: Option[String], expected: String): Unit = actual match {
+ case Some(str) =>
+ assert(str == expected, s"""Docstring: "$str" didn't match expected "$expected"""")
+ case None =>
+ assert(false, s"""No docstring found, expected: "$expected"""")
+ }
+
+ def expectNoDocString(doc: Option[String]): Unit =
+ doc.fold(()) { d => assert(false, s"""Expected not to find a docstring, but found: "$d"""") }
+
+ def defaultAssertion: PartialFunction[Any, Unit] = {
+ case t: Tree[Untyped] =>
+ assert(false, s"Couldn't match resulting AST to expected AST in: ${t.show}")
+ case x =>
+ assert(false, s"Couldn't match resulting AST to expected AST in: $x")
+ }
+
+ def checkFrontend(source: String)(docAssert: PartialFunction[Tree[Untyped], Unit]) = {
+ checkCompile("frontend", source) { (_, ctx) =>
+ implicit val c = ctx
+ (docAssert orElse defaultAssertion)(ctx.compilationUnit.untpdTree)
+ }
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/parsing/DocstringTests.scala b/compiler/test/dotty/tools/dotc/parsing/DocstringTests.scala
new file mode 100644
index 000000000..930ec117a
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/parsing/DocstringTests.scala
@@ -0,0 +1,491 @@
+package dotty.tools
+package dotc
+package parsing
+
+import dotty.tools.dotc.core.Contexts.Context
+import dotty.tools.dotc.ast.Trees._
+
+import org.junit.Assert._
+import org.junit.Test
+
+class DocstringTests extends DocstringTest {
+
+ @Test def noComment = {
+ import dotty.tools.dotc.ast.untpd._
+ val source = "class Class"
+
+ checkFrontend(source) {
+ case PackageDef(_, Seq(c: TypeDef)) =>
+ assert(c.rawComment.map(_.raw) == None, "Should not have a comment, mainly used for exhaustive tests")
+ }
+ }
+
+ @Test def singleClassInPackage = {
+ val source =
+ """
+ |package a
+ |
+ |/** Hello world! */
+ |class Class(val x: String)
+ """.stripMargin
+
+ checkFrontend(source) {
+ case PackageDef(_, Seq(t @ TypeDef(name, _))) if name.toString == "Class" =>
+ checkDocString(t.rawComment.map(_.raw), "/** Hello world! */")
+ }
+ }
+
+ @Test def multipleOpenedOnSingleClassInPackage = {
+ val source =
+ """
+ |package a
+ |
+ |/** Hello /* multiple open */ world! */
+ |class Class(val x: String)
+ """.stripMargin
+
+ checkFrontend(source) {
+ case PackageDef(_, Seq(t @ TypeDef(name, _))) if name.toString == "Class" =>
+ checkDocString(t.rawComment.map(_.raw), "/** Hello /* multiple open */ world! */")
+ }
+ }
+ @Test def multipleClassesInPackage = {
+ val source =
+ """
+ |package a
+ |
+ |/** Class1 docstring */
+ |class Class1(val x: String)
+ |
+ |/** Class2 docstring */
+ |class Class2(val x: String)
+ """.stripMargin
+
+ checkCompile("frontend", source) { (_, ctx) =>
+ ctx.compilationUnit.untpdTree match {
+ case PackageDef(_, Seq(c1 @ TypeDef(_,_), c2 @ TypeDef(_,_))) => {
+ checkDocString(c1.rawComment.map(_.raw), "/** Class1 docstring */")
+ checkDocString(c2.rawComment.map(_.raw), "/** Class2 docstring */")
+ }
+ }
+ }
+ }
+
+ @Test def singleCaseClassWithoutPackage = {
+ val source =
+ """
+ |/** Class without package */
+ |case class Class(val x: Int)
+ """.stripMargin
+
+ checkFrontend(source) {
+ case PackageDef(_, Seq(t @ TypeDef(_,_))) => checkDocString(t.rawComment.map(_.raw), "/** Class without package */")
+ }
+ }
+
+ @Test def SingleTraitWihoutPackage = {
+ val source = "/** Trait docstring */\ntrait Trait"
+
+ checkFrontend(source) {
+ case PackageDef(_, Seq(t @ TypeDef(_,_))) => checkDocString(t.rawComment.map(_.raw), "/** Trait docstring */")
+ }
+ }
+
+ @Test def multipleTraitsWithoutPackage = {
+ val source =
+ """
+ |/** Trait1 docstring */
+ |trait Trait1
+ |
+ |/** Trait2 docstring */
+ |trait Trait2
+ """.stripMargin
+
+ checkFrontend(source) {
+ case PackageDef(_, Seq(t1 @ TypeDef(_,_), t2 @ TypeDef(_,_))) => {
+ checkDocString(t1.rawComment.map(_.raw), "/** Trait1 docstring */")
+ checkDocString(t2.rawComment.map(_.raw), "/** Trait2 docstring */")
+ }
+ }
+ }
+
+ @Test def multipleMixedEntitiesWithPackage = {
+ val source =
+ """
+ |/** Trait1 docstring */
+ |trait Trait1
+ |
+ |/** Class2 docstring */
+ |class Class2(val x: Int)
+ |
+ |/** CaseClass3 docstring */
+ |case class CaseClass3()
+ |
+ |case class NoComment()
+ |
+ |/** AbstractClass4 docstring */
+ |abstract class AbstractClass4(val x: Int)
+ """.stripMargin
+
+ checkFrontend(source) {
+ case PackageDef(_, Seq(t1 @ TypeDef(_,_), c2 @ TypeDef(_,_), cc3 @ TypeDef(_,_), _, ac4 @ TypeDef(_,_))) => {
+ checkDocString(t1.rawComment.map(_.raw), "/** Trait1 docstring */")
+ checkDocString(c2.rawComment.map(_.raw), "/** Class2 docstring */")
+ checkDocString(cc3.rawComment.map(_.raw), "/** CaseClass3 docstring */")
+ checkDocString(ac4.rawComment.map(_.raw), "/** AbstractClass4 docstring */")
+ }
+ }
+ }
+
+ @Test def nestedClass = {
+ val source =
+ """
+ |/** Outer docstring */
+ |class Outer {
+ | /** Inner docstring */
+ | class Inner(val x: Int)
+ |}
+ """.stripMargin
+
+ checkFrontend(source) {
+ case PackageDef(_, Seq(outer @ TypeDef(_, tpl @ Template(_,_,_,_)))) => {
+ checkDocString(outer.rawComment.map(_.raw), "/** Outer docstring */")
+ tpl.body match {
+ case (inner @ TypeDef(_,_)) :: _ => checkDocString(inner.rawComment.map(_.raw), "/** Inner docstring */")
+ case _ => assert(false, "Couldn't find inner class")
+ }
+ }
+ }
+ }
+
+ @Test def nestedClassThenOuter = {
+ val source =
+ """
+ |/** Outer1 docstring */
+ |class Outer1 {
+ | /** Inner docstring */
+ | class Inner(val x: Int)
+ |}
+ |
+ |/** Outer2 docstring */
+ |class Outer2
+ """.stripMargin
+
+ checkFrontend(source) {
+ case PackageDef(_, Seq(o1 @ TypeDef(_, tpl @ Template(_,_,_,_)), o2 @ TypeDef(_,_))) => {
+ checkDocString(o1.rawComment.map(_.raw), "/** Outer1 docstring */")
+ checkDocString(o2.rawComment.map(_.raw), "/** Outer2 docstring */")
+ tpl.body match {
+ case (inner @ TypeDef(_,_)) :: _ => checkDocString(inner.rawComment.map(_.raw), "/** Inner docstring */")
+ case _ => assert(false, "Couldn't find inner class")
+ }
+ }
+ }
+ }
+
+ @Test def objects = {
+ val source =
+ """
+ |package p
+ |
+ |/** Object1 docstring */
+ |object Object1
+ |
+ |/** Object2 docstring */
+ |object Object2
+ """.stripMargin
+
+ checkFrontend(source) {
+ case p @ PackageDef(_, Seq(o1: MemberDef[Untyped], o2: MemberDef[Untyped])) => {
+ assertEquals(o1.name.toString, "Object1")
+ checkDocString(o1.rawComment.map(_.raw), "/** Object1 docstring */")
+ assertEquals(o2.name.toString, "Object2")
+ checkDocString(o2.rawComment.map(_.raw), "/** Object2 docstring */")
+ }
+ }
+ }
+
+ @Test def objectsNestedClass = {
+ val source =
+ """
+ |package p
+ |
+ |/** Object1 docstring */
+ |object Object1
+ |
+ |/** Object2 docstring */
+ |object Object2 {
+ | class A1
+ | /** Inner docstring */
+ | class Inner
+ |}
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case p @ PackageDef(_, Seq(o1: ModuleDef, o2: ModuleDef)) => {
+ assert(o1.name.toString == "Object1")
+ checkDocString(o1.rawComment.map(_.raw), "/** Object1 docstring */")
+ assert(o2.name.toString == "Object2")
+ checkDocString(o2.rawComment.map(_.raw), "/** Object2 docstring */")
+
+ o2.impl.body match {
+ case _ :: (inner @ TypeDef(_,_)) :: _ => checkDocString(inner.rawComment.map(_.raw), "/** Inner docstring */")
+ case _ => assert(false, "Couldn't find inner class")
+ }
+ }
+ }
+ }
+
+ @Test def packageObject = {
+ val source =
+ """
+ |/** Package object docstring */
+ |package object foo {
+ | /** Boo docstring */
+ | case class Boo()
+ |
+ | /** Trait docstring */
+ | trait Trait
+ |
+ | /** InnerObject docstring */
+ | object InnerObject {
+ | /** InnerClass docstring */
+ | class InnerClass
+ | }
+ |}
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case PackageDef(_, Seq(p: ModuleDef)) => {
+ checkDocString(p.rawComment.map(_.raw), "/** Package object docstring */")
+
+ p.impl.body match {
+ case (b: TypeDef) :: (t: TypeDef) :: (o: ModuleDef) :: Nil => {
+ checkDocString(b.rawComment.map(_.raw), "/** Boo docstring */")
+ checkDocString(t.rawComment.map(_.raw), "/** Trait docstring */")
+ checkDocString(o.rawComment.map(_.raw), "/** InnerObject docstring */")
+ checkDocString(o.impl.body.head.asInstanceOf[TypeDef].rawComment.map(_.raw), "/** InnerClass docstring */")
+ }
+ case _ => assert(false, "Incorrect structure inside package object")
+ }
+ }
+ }
+ }
+
+ @Test def multipleDocStringsBeforeEntity = {
+ val source =
+ """
+ |/** First comment */
+ |/** Second comment */
+ |/** Real comment */
+ |class Class
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case PackageDef(_, Seq(c: TypeDef)) =>
+ checkDocString(c.rawComment.map(_.raw), "/** Real comment */")
+ }
+ }
+
+ @Test def multipleDocStringsBeforeAndAfter = {
+ val source =
+ """
+ |/** First comment */
+ |/** Second comment */
+ |/** Real comment */
+ |class Class
+ |/** Following comment 1 */
+ |/** Following comment 2 */
+ |/** Following comment 3 */
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case PackageDef(_, Seq(c: TypeDef)) =>
+ checkDocString(c.rawComment.map(_.raw), "/** Real comment */")
+ }
+ }
+
+ @Test def valuesWithDocString = {
+ val source =
+ """
+ |object Object {
+ | /** val1 */
+ | val val1 = 1
+ |
+ | /** val2 */
+ | val val2: Int = 2
+ | /** bogus docstring */
+ |
+ | /** bogus docstring */
+ | /** val3 */
+ | val val3: List[Int] = 1 :: 2 :: 3 :: Nil
+ |}
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case PackageDef(_, Seq(o: ModuleDef)) => {
+ o.impl.body match {
+ case (v1: MemberDef) :: (v2: MemberDef) :: (v3: MemberDef) :: Nil => {
+ checkDocString(v1.rawComment.map(_.raw), "/** val1 */")
+ checkDocString(v2.rawComment.map(_.raw), "/** val2 */")
+ checkDocString(v3.rawComment.map(_.raw), "/** val3 */")
+ }
+ case _ => assert(false, "Incorrect structure inside object")
+ }
+ }
+ }
+ }
+
+ @Test def varsWithDocString = {
+ val source =
+ """
+ |object Object {
+ | /** var1 */
+ | var var1 = 1
+ |
+ | /** var2 */
+ | var var2: Int = 2
+ | /** bogus docstring */
+ |
+ | /** bogus docstring */
+ | /** var3 */
+ | var var3: List[Int] = 1 :: 2 :: 3 :: Nil
+ |}
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case PackageDef(_, Seq(o: ModuleDef)) => {
+ o.impl.body match {
+ case (v1: MemberDef) :: (v2: MemberDef) :: (v3: MemberDef) :: Nil => {
+ checkDocString(v1.rawComment.map(_.raw), "/** var1 */")
+ checkDocString(v2.rawComment.map(_.raw), "/** var2 */")
+ checkDocString(v3.rawComment.map(_.raw), "/** var3 */")
+ }
+ case _ => assert(false, "Incorrect structure inside object")
+ }
+ }
+ }
+ }
+
+ @Test def defsWithDocString = {
+ val source =
+ """
+ |object Object {
+ | /** def1 */
+ | def def1 = 1
+ |
+ | /** def2 */
+ | def def2: Int = 2
+ | /** bogus docstring */
+ |
+ | /** bogus docstring */
+ | /** def3 */
+ | def def3: List[Int] = 1 :: 2 :: 3 :: Nil
+ |}
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case PackageDef(_, Seq(o: ModuleDef)) => {
+ o.impl.body match {
+ case (v1: MemberDef) :: (v2: MemberDef) :: (v3: MemberDef) :: Nil => {
+ checkDocString(v1.rawComment.map(_.raw), "/** def1 */")
+ checkDocString(v2.rawComment.map(_.raw), "/** def2 */")
+ checkDocString(v3.rawComment.map(_.raw), "/** def3 */")
+ }
+ case _ => assert(false, "Incorrect structure inside object")
+ }
+ }
+ }
+ }
+
+ @Test def typesWithDocString = {
+ val source =
+ """
+ |object Object {
+ | /** type1 */
+ | type T1 = Int
+ |
+ | /** type2 */
+ | type T2 = String
+ | /** bogus docstring */
+ |
+ | /** bogus docstring */
+ | /** type3 */
+ | type T3 = T2
+ |}
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case PackageDef(_, Seq(o: ModuleDef)) => {
+ o.impl.body match {
+ case (v1: MemberDef) :: (v2: MemberDef) :: (v3: MemberDef) :: Nil => {
+ checkDocString(v1.rawComment.map(_.raw), "/** type1 */")
+ checkDocString(v2.rawComment.map(_.raw), "/** type2 */")
+ checkDocString(v3.rawComment.map(_.raw), "/** type3 */")
+ }
+ case _ => assert(false, "Incorrect structure inside object")
+ }
+ }
+ }
+ }
+
+ @Test def defInnerClass = {
+ val source =
+ """
+ |object Foo {
+ | def foo() = {
+ | /** Innermost */
+ | class Innermost
+ | }
+ |}
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case PackageDef(_, Seq(o: ModuleDef)) =>
+ o.impl.body match {
+ case (foo: MemberDef) :: Nil =>
+ expectNoDocString(foo.rawComment.map(_.raw))
+ case _ => assert(false, "Incorrect structure inside object")
+ }
+ }
+ }
+
+ @Test def withExtends = {
+ val source =
+ """
+ |trait Trait1
+ |/** Class1 */
+ |class Class1 extends Trait1
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case p @ PackageDef(_, Seq(_, c: TypeDef)) =>
+ checkDocString(c.rawComment.map(_.raw), "/** Class1 */")
+ }
+ }
+
+ @Test def withAnnotation = {
+ val source =
+ """
+ |/** Class1 */
+ |@SerialVersionUID(1)
+ |class Class1
+ """.stripMargin
+
+ import dotty.tools.dotc.ast.untpd._
+ checkFrontend(source) {
+ case p @ PackageDef(_, Seq(c: TypeDef)) =>
+ checkDocString(c.rawComment.map(_.raw), "/** Class1 */")
+ }
+ }
+} /* End class */
diff --git a/compiler/test/dotty/tools/dotc/parsing/ModifiersParsingTest.scala b/compiler/test/dotty/tools/dotc/parsing/ModifiersParsingTest.scala
new file mode 100644
index 000000000..e31ef2160
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/parsing/ModifiersParsingTest.scala
@@ -0,0 +1,164 @@
+package dotty.tools
+package dotc
+package parsing
+
+import org.junit.Test
+import org.junit.Assert._
+
+import ast.untpd.modsDeco
+import ast.untpd._
+import ast.{ Trees => d }
+import Parsers.Parser
+import util.SourceFile
+import core.Contexts.ContextBase
+import core.Flags
+
+object ModifiersParsingTest {
+ implicit val ctx = (new ContextBase).initialCtx
+
+ implicit def parse(code: String): Tree = {
+ val (_, stats) = new Parser(new SourceFile("<meta>", code.toCharArray)).templateStatSeq()
+ stats match { case List(stat) => stat; case stats => Thicket(stats) }
+ }
+
+ implicit class TreeDeco(val code: Tree) extends AnyVal {
+ def firstConstrValDef: ValDef = code match {
+ case d.TypeDef(_, d.Template(constr, _, _, _)) =>
+ constr.vparamss.head.head
+ }
+
+ def firstTypeParam: TypeDef = code match {
+ case d.TypeDef(_, d.Template(constr, _, _, _)) =>
+ constr.tparams.head
+ }
+
+ def defParam(i: Int): ValDef = code match {
+ case d.DefDef(_, _, vparamss, _, _) =>
+ vparamss.head.toArray.apply(i)
+ }
+
+ def defParam(i: Int, j: Int): ValDef = code match {
+ case d.DefDef(_, _, vparamss, _, _) =>
+ vparamss.toArray.apply(i).toArray.apply(j)
+ }
+
+ def funParam(i: Int): Tree = code match {
+ case Function(params, _) =>
+ params.toArray.apply(i)
+ }
+
+ def field(i: Int): Tree = code match {
+ case d.TypeDef(_, t: Template) =>
+ t.body.toArray.apply(i)
+ }
+
+ def field(name: String): Tree = code match {
+ case d.TypeDef(_, t: Template) =>
+ t.body.find({
+ case m: MemberDef => m.name.show == name
+ case _ => false
+ }).get
+ }
+
+ def stat(i: Int): Tree = code match {
+ case d.Block(stats, expr) =>
+ if (i < stats.length) stats.toArray.apply(i)
+ else expr
+ }
+
+ def modifiers: List[Mod] = code match {
+ case t: MemberDef => t.mods.mods
+ }
+ }
+}
+
+
+class ModifiersParsingTest {
+ import ModifiersParsingTest._
+
+
+ @Test def valDef = {
+ var source: Tree = "class A(var a: Int)"
+ assert(source.firstConstrValDef.modifiers == List(Mod.Var()))
+
+ source = "class A(val a: Int)"
+ assert(source.firstConstrValDef.modifiers == List(Mod.Val()))
+
+ source = "class A(private val a: Int)"
+ assert(source.firstConstrValDef.modifiers == List(Mod.Private(), Mod.Val()))
+
+ source = "class A(protected var a: Int)"
+ assert(source.firstConstrValDef.modifiers == List(Mod.Protected(), Mod.Var()))
+
+ source = "class A(protected implicit val a: Int)"
+ assert(source.firstConstrValDef.modifiers == List(Mod.Protected(), Mod.Implicit(), Mod.Val()))
+
+ source = "class A[T]"
+ assert(source.firstTypeParam.modifiers == List())
+
+ source = "class A[type T]"
+ assert(source.firstTypeParam.modifiers == List(Mod.Type()))
+ }
+
+ @Test def typeDef = {
+ var source: Tree = "class A"
+ assert(source.modifiers == List())
+
+ source = "sealed class A"
+ assert(source.modifiers == List(Mod.Sealed()))
+
+ source = "implicit class A"
+ assert(source.modifiers == List(Mod.Implicit()))
+
+ source = "abstract sealed class A"
+ assert(source.modifiers == List(Mod.Abstract(), Mod.Sealed()))
+ }
+
+ @Test def fieldDef = {
+ val source: Tree =
+ """
+ | class A {
+ | lazy var a = ???
+ | lazy private val b = ???
+ | final val c = ???
+ |
+ | abstract override def f: Boolean
+ | inline def g(n: Int) = ???
+ | }
+ """.stripMargin
+
+ assert(source.field("a").modifiers == List(Mod.Lazy(), Mod.Var()))
+ assert(source.field("b").modifiers == List(Mod.Lazy(), Mod.Private(), Mod.Val()))
+ assert(source.field("c").modifiers == List(Mod.Final(), Mod.Val()))
+ assert(source.field("f").modifiers == List(Mod.Abstract(), Mod.Override()))
+ assert(source.field("g").modifiers == List(Mod.Inline()))
+ }
+
+ @Test def paramDef = {
+ var source: Tree = "def f(inline a: Int) = ???"
+ assert(source.defParam(0).modifiers == List(Mod.Inline()))
+
+ source = "def f(implicit a: Int, b: Int) = ???"
+ println(source.defParam(0).modifiers)
+ assert(source.defParam(0).modifiers == List(Mod.Implicit(Flags.Implicit)))
+ assert(source.defParam(1).modifiers == List(Mod.Implicit(Flags.Implicit)))
+
+ source = "def f(x: Int, y: Int)(implicit a: Int, b: Int) = ???"
+ assert(source.defParam(0, 0).modifiers == List())
+ assert(source.defParam(1, 0).modifiers == List(Mod.Implicit(Flags.Implicit)))
+ }
+
+ @Test def blockDef = {
+ var source: Tree = "implicit val x : A = ???"
+ assert(source.modifiers == List(Mod.Implicit(), Mod.Val()))
+
+ source = "implicit var x : A = ???"
+ assert(source.modifiers == List(Mod.Implicit(), Mod.Var()))
+
+ source = "{ implicit var x : A = ??? }"
+ assert(source.stat(0).modifiers == List(Mod.Implicit(), Mod.Var()))
+
+ source = "{ implicit x => x * x }"
+ assert(source.stat(0).funParam(0).modifiers == List(Mod.Implicit()))
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/parsing/ParserTest.scala b/compiler/test/dotty/tools/dotc/parsing/ParserTest.scala
new file mode 100644
index 000000000..a89b34512
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/parsing/ParserTest.scala
@@ -0,0 +1,44 @@
+package dotty.tools
+package dotc
+package parsing
+
+import scala.reflect.io._
+import util._
+import core._
+import scala.io.Codec
+import Tokens._, Parsers._
+import ast.untpd._
+import org.junit.Test
+import scala.collection.mutable.ListBuffer
+
+class ParserTest extends DottyTest {
+
+ def parse(name: String): Tree = parse(new PlainFile(name))
+
+ var parsed = 0
+ val parsedTrees = new ListBuffer[Tree]
+
+ def reset() = {
+ parsed = 0
+ parsedTrees.clear()
+ }
+
+ def parse(file: PlainFile): Tree = {
+ //println("***** parsing " + file)
+ val source = new SourceFile(file, Codec.UTF8)
+ val parser = new Parser(source)
+ val tree = parser.parse()
+ parsed += 1
+ parsedTrees += tree
+ tree
+ }
+
+ def parseDir(path: String): Unit = parseDir(Directory(path))
+
+ def parseDir(dir: Directory): Unit = {
+ for (f <- dir.files)
+ if (f.name.endsWith(".scala")) parse(new PlainFile(f))
+ for (d <- dir.dirs)
+ parseDir(d.path)
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/parsing/ScannerTest.scala b/compiler/test/dotty/tools/dotc/parsing/ScannerTest.scala
new file mode 100644
index 000000000..b024a63db
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/parsing/ScannerTest.scala
@@ -0,0 +1,65 @@
+package dotty.tools
+package dotc
+package parsing
+
+import scala.reflect.io._
+import scala.io.Codec
+import util._
+import Tokens._, Scanners._
+import org.junit.Test
+
+class ScannerTest extends DottyTest {
+
+ val blackList = List(
+ "/scaladoc/scala/tools/nsc/doc/html/page/Index.scala",
+ "/scaladoc/scala/tools/nsc/doc/html/page/Template.scala"
+ )
+
+ def scan(name: String): Unit = scan(new PlainFile(name))
+
+ def scan(file: PlainFile): Unit = {
+ //println("***** scanning " + file)
+ val source = new SourceFile(file, Codec.UTF8)
+ val scanner = new Scanner(source)
+ var i = 0
+ while (scanner.token != EOF) {
+// print("[" + scanner.token.show +"]")
+ scanner.nextToken
+// i += 1
+// if (i % 10 == 0) println()
+ }
+ }
+
+ def scanDir(path: String): Unit = scanDir(Directory(path))
+
+ def scanDir(dir: Directory): Unit = {
+ if (blackList exists (dir.jfile.toString endsWith _))
+ println(s"blacklisted package: ${dir.jfile.getAbsolutePath}")
+ else
+ for (f <- dir.files)
+ if (f.name.endsWith(".scala"))
+ if (blackList exists (f.jfile.toString endsWith _))
+ println(s"blacklisted file: ${f.jfile.getAbsolutePath}")
+ else
+ scan(new PlainFile(f))
+ for (d <- dir.dirs)
+ scanDir(d.path)
+ }
+
+ @Test
+ def scanList() = {
+ println(System.getProperty("user.dir"))
+ scan("./src/dotty/tools/dotc/core/Symbols.scala")
+ scan("./src/dotty/tools/dotc/core/Symbols.scala")
+ }
+
+ @Test
+ def scanDotty() = {
+ scanDir("src")
+ }
+
+ @Test
+ def scanScala() = {
+ scanDir("../scala-scala/src")
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/parsing/desugarPackage.scala b/compiler/test/dotty/tools/dotc/parsing/desugarPackage.scala
new file mode 100644
index 000000000..84bf7a2d1
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/parsing/desugarPackage.scala
@@ -0,0 +1,28 @@
+package dotty.tools
+package dotc
+package parsing
+
+import core._, ast._
+import Trees._
+
+object desugarPackage extends DeSugarTest {
+
+ def test() = {
+ reset()
+ val start = System.nanoTime()
+ val startNodes = Trees.ntrees
+ parseDir("./src")
+ parseDir("./scala-scala/src")
+ val ms1 = (System.nanoTime() - start)/1000000
+ val nodes = Trees.ntrees
+ val buf = parsedTrees map desugarTree
+ val ms2 = (System.nanoTime() - start)/1000000
+ println(s"$parsed files parsed in ${ms1}ms, ${nodes - startNodes} nodes desugared in ${ms2-ms1}ms, total trees created = ${Trees.ntrees - startNodes}")
+ ctx.reporter.printSummary(ctx)
+ }
+
+ def main(args: Array[String]): Unit = {
+// parse("/Users/odersky/workspace/scala/src/compiler/scala/tools/nsc/doc/model/ModelFactoryTypeSupport.scala")
+ for (i <- 0 until 10) test()
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/parsing/parseFile.scala b/compiler/test/dotty/tools/dotc/parsing/parseFile.scala
new file mode 100644
index 000000000..c41a286cb
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/parsing/parseFile.scala
@@ -0,0 +1,13 @@
+package dotty.tools.dotc.parsing
+
+object parseFile extends ParserTest {
+
+ def main(args: Array[String]): Unit = {
+ if (args.isEmpty) println("usage: scala test.parseFile file1.scala ... fileN.scala")
+ for (arg <- args) {
+ val tree = parse(arg)
+ println("parsed: " + arg)
+ println(tree.show)
+ }
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/parsing/parsePackage.scala b/compiler/test/dotty/tools/dotc/parsing/parsePackage.scala
new file mode 100644
index 000000000..df5368ffe
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/parsing/parsePackage.scala
@@ -0,0 +1,83 @@
+package dotty.tools
+package dotc
+package parsing
+
+import dotty.tools.dotc._
+import core._, ast._
+import Trees._
+import Contexts.Context
+
+object parsePackage extends ParserTest {
+
+ import ast.untpd._
+
+ var nodes = 0
+
+ val transformer = new UntypedTreeMap {
+ override def transform(tree: Tree)(implicit ctx: Context): Tree = {
+ nodes += 1
+ tree match {
+ case Ident(name) =>
+ Ident(name)
+ case This(name) =>
+ This(name)
+ case TypedSplice(t) =>
+ TypedSplice(t)
+ case SymbolLit(str) =>
+ tree
+ case InterpolatedString(id, segments) =>
+ InterpolatedString(id, segments map transform)
+ case mdef @ ModuleDef(name, impl) =>
+ ModuleDef(name, transformSub(impl)).withMods(mdef.mods)
+ case Function(params, body) =>
+ Function(params map transform, body)
+ case InfixOp(l, o, r) =>
+ InfixOp(transform(l), o, transform(r))
+ case PostfixOp(l, o) =>
+ PostfixOp(transform(l), o)
+ case PrefixOp(o, t) =>
+ PrefixOp(o, transform(t))
+ case Parens(t) =>
+ Parens(transform(t))
+ case Tuple(ts) =>
+ Tuple(ts map transform)
+ case WhileDo(cond, body) =>
+ WhileDo(transform(cond), transform(body))
+ case DoWhile(body, cond) =>
+ DoWhile(transform(body), transform(cond))
+ case ForYield(enums, expr) =>
+ ForYield(enums map transform, transform(expr))
+ case ForDo(enums, expr) =>
+ ForDo(enums map transform, transform(expr))
+ case GenFrom(pat, expr) =>
+ GenFrom(transform(pat), transform(expr))
+ case GenAlias(pat, expr) =>
+ GenAlias(transform(pat), transform(expr))
+ case PatDef(mods, pats, tpt, expr) =>
+ PatDef(mods, pats map transform, transform(tpt), transform(expr))
+ case ContextBounds(bounds, cxBounds) =>
+ ContextBounds(transformSub(bounds), cxBounds map transform)
+ case _ =>
+ super.transform(tree)
+ }
+ }
+ }
+
+ def test() = {
+ reset()
+ nodes = 0
+ val start = System.nanoTime()
+ parseDir("./src")
+ parseDir("./scala-scala/src")
+ val ms1 = (System.nanoTime() - start)/1000000
+ val buf = parsedTrees map transformer.transform
+ val ms2 = (System.nanoTime() - start)/1000000
+ println(s"$parsed files parsed in ${ms1}ms, $nodes nodes transformed in ${ms2-ms1}ms, total trees created = ${Trees.ntrees}")
+ ctx.reporter.printSummary(ctx)
+ }
+
+ def main(args: Array[String]): Unit = {
+// parse("/Users/odersky/workspace/scala/src/compiler/scala/tools/nsc/doc/model/ModelFactoryTypeSupport.scala")
+ for (i <- 0 until 10) test()
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/parsing/showTree.scala b/compiler/test/dotty/tools/dotc/parsing/showTree.scala
new file mode 100644
index 000000000..18b2203d5
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/parsing/showTree.scala
@@ -0,0 +1,26 @@
+package dotty.tools
+package dotc
+package parsing
+
+import ast.Trees._
+import ast.desugar
+import ast.desugar._
+import core.Mode
+
+object showTree extends DeSugarTest {
+
+ import dotty.tools.dotc.ast.untpd._
+
+ import Mode._
+
+ def test(arg: String) = {
+ val tree: Tree = parse(arg)
+ println("result = " + tree.show)
+ println("desugared = " + DeSugar.transform(tree).show)
+ }
+
+ def main(args: Array[String]): Unit = {
+ test("src/dotty/tools/dotc/core/Types.scala")
+ for (arg <- args) test(arg)
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/repl/TestREPL.scala b/compiler/test/dotty/tools/dotc/repl/TestREPL.scala
new file mode 100644
index 000000000..2263e85a0
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/repl/TestREPL.scala
@@ -0,0 +1,66 @@
+package dotty.tools.dotc
+package repl
+
+import core.Contexts.Context
+import collection.mutable
+import java.io.StringWriter
+
+/** A subclass of REPL used for testing.
+ * It takes a transcript of a REPL session in `script`. The transcript
+ * starts with the first input prompt `scala> ` and ends with `scala> :quit` and a newline.
+ * Invoking `process()` on the `TestREPL` runs all input lines and
+ * collects then interleaved with REPL output in a string writer `out`.
+ * Invoking `check()` checks that the collected output matches the original
+ * `script`.
+ */
+class TestREPL(script: String) extends REPL {
+
+ private val out = new StringWriter()
+
+ override lazy val config = new REPL.Config {
+ override val output = new NewLinePrintWriter(out)
+
+ override def context(ctx: Context) = {
+ val fresh = ctx.fresh
+ fresh.setSetting(ctx.settings.color, "never")
+ fresh.setSetting(
+ ctx.settings.classpath,
+ "../library/target/scala-2.11/dotty-library_2.11-0.1-SNAPSHOT.jar"
+ )
+ fresh.initialize()(fresh)
+ fresh
+ }
+
+ override def input(in: Interpreter)(implicit ctx: Context) = new InteractiveReader {
+ val lines = script.lines.buffered
+ def readLine(prompt: String): String = {
+ val line = lines.next
+ val buf = new StringBuilder
+ if (line.startsWith(prompt)) {
+ output.println(line)
+ buf append line.drop(prompt.length)
+ while (lines.hasNext && lines.head.startsWith(continuationPrompt)) {
+ val continued = lines.next
+ output.println(continued)
+ buf append System.lineSeparator()
+ buf append continued.drop(continuationPrompt.length)
+ }
+ buf.toString
+ }
+ else readLine(prompt)
+ }
+ val interactive = false
+ }
+ }
+
+ def check() = {
+ out.close()
+ val printed = out.toString
+ val transcript = printed.drop(printed.indexOf(config.prompt))
+ if (transcript.toString.lines.toList != script.lines.toList) {
+ println("input differs from transcript:")
+ println(transcript)
+ assert(false)
+ }
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/reporting/TestMessageLaziness.scala b/compiler/test/dotty/tools/dotc/reporting/TestMessageLaziness.scala
new file mode 100644
index 000000000..6892739e8
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/reporting/TestMessageLaziness.scala
@@ -0,0 +1,37 @@
+package dotty.tools
+package dotc
+package reporting
+
+import org.junit.Assert._
+import org.junit.Test
+
+import core.Contexts._
+
+import test.DottyTest
+
+import diagnostic.{ Message, MessageContainer, ExtendMessage }
+
+class TestMessageLaziness extends DottyTest {
+ ctx = ctx.fresh.setReporter(new NonchalantReporter)
+
+ class NonchalantReporter(implicit ctx: Context) extends Reporter
+ with UniqueMessagePositions with HideNonSensicalMessages {
+ def doReport(m: MessageContainer)(implicit ctx: Context) = ???
+
+ override def report(m: MessageContainer)(implicit ctx: Context) = ()
+ }
+
+ case class LazyError() extends Message(1000) {
+ throw new Error("Didn't stay lazy.")
+
+ val kind = "Test"
+ val msg = "Please don't blow up"
+ val explanation = ""
+ }
+
+ @Test def assureLazy =
+ ctx.error(LazyError())
+
+ @Test def assureLazyExtendMessage =
+ ctx.strictWarning(LazyError())
+}
diff --git a/compiler/test/dotty/tools/dotc/reporting/TestReporter.scala b/compiler/test/dotty/tools/dotc/reporting/TestReporter.scala
new file mode 100644
index 000000000..70d18d031
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/reporting/TestReporter.scala
@@ -0,0 +1,52 @@
+package dotty.tools
+package dotc
+package reporting
+
+import scala.collection.mutable
+import util.SourcePosition
+import core.Contexts._
+import Reporter._
+import java.io.PrintWriter
+import scala.reflect.internal.util._
+import diagnostic.{ Message, MessageContainer, NoExplanation }
+import diagnostic.messages._
+
+class TestReporter(writer: PrintWriter) extends Reporter
+with UniqueMessagePositions with HideNonSensicalMessages {
+
+ import MessageContainer._
+
+ /** maximal number of error messages to be printed */
+ protected def ErrorLimit = 100
+
+ def printPos(pos: SourcePosition): Unit =
+ if (pos.exists) {
+ if (pos.outer.exists) {
+ writer.println(s"\ninlined at ${pos.outer}:\n")
+ printPos(pos.outer)
+ }
+ }
+
+ /** Prints the message with the given position indication. */
+ def printMessageAndPos(msg: String, pos: SourcePosition)(implicit ctx: Context): Unit = {
+ val posStr = s"${pos.line + 1}: "
+ writer.println(posStr + msg)
+ printPos(pos)
+ }
+
+ override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
+ // Here we add extra information that we should know about the error message
+ val extra = m.contained match {
+ case pm: PatternMatchExhaustivity => s": ${pm.uncovered}"
+ case _ => ""
+ }
+
+ m match {
+ case m: Error =>
+ printMessageAndPos(m.contained.kind + extra, m.pos)
+ case w: Warning =>
+ printMessageAndPos(w.contained.kind + extra, w.pos)
+ case _ =>
+ }
+ }
+}
diff --git a/compiler/test/dotty/tools/dotc/transform/CreateCompanionObjectsTest.scala b/compiler/test/dotty/tools/dotc/transform/CreateCompanionObjectsTest.scala
new file mode 100644
index 000000000..18acb2105
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/transform/CreateCompanionObjectsTest.scala
@@ -0,0 +1,128 @@
+package dotty.tools
+package dotc
+package transform
+
+import org.junit.{Assert, Test}
+import core._
+import ast.{tpd, Trees}
+import Contexts._
+import Flags._
+import Denotations._
+import NameOps._
+import Symbols._
+import Types._
+import Decorators._
+import Trees._
+import TreeTransforms.{TreeTransform, TreeTransformer}
+
+
+class CreateCompanionObjectsTest extends DottyTest {
+ /* FIXME: re-enable after adapting to new scheme
+
+ import tpd._
+
+ type PostTyperTransformer = TreeTransformer // FIXME do without
+
+ @Test
+ def shouldCreateNonExistingObjectsInPackage = checkCompile("frontend", "class A{} ") {
+ (tree, context) =>
+ implicit val ctx = context
+
+ val transformer = new PostTyperTransformer {
+ override def transformations = Array(new CreateCompanionObjects {
+
+ override def name: String = "create all companion objects"
+ override def predicate(cts: TypeDef)(implicit ctx:Context): Boolean = true
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ })
+
+ override def name: String = "test"
+ }
+ val transformed = transformer.transform(tree).toString
+ val classPattern = "TypeDef(Modifiers(,,List()),A,"
+ val classPos = transformed.indexOf(classPattern)
+ val moduleClassPattern = "TypeDef(Modifiers(final module <synthetic>,,List()),A$"
+ val modulePos = transformed.indexOf(moduleClassPattern)
+
+ Assert.assertTrue("should create non-existing objects in package",
+ classPos < modulePos
+ )
+ }
+
+ @Test
+ def shouldCreateNonExistingObjectsInBlock = checkCompile("frontend", "class D {def p = {class A{}; 1}} ") {
+ (tree, context) =>
+ implicit val ctx = context
+ val transformer = new PostTyperTransformer {
+ override def transformations = Array(new CreateCompanionObjects {
+
+ override def name: String = "create all companion modules"
+ override def predicate(cts: TypeDef)(implicit ctx:Context): Boolean = true
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ })
+
+ override def name: String = "test"
+ }
+ val transformed = transformer.transform(tree).toString
+ val classPattern = "TypeDef(Modifiers(,,List()),A,"
+ val classPos = transformed.indexOf(classPattern)
+ val moduleClassPattern = "TypeDef(Modifiers(final module <synthetic>,,List()),A$"
+ val modulePos = transformed.indexOf(moduleClassPattern)
+
+ Assert.assertTrue("should create non-existing objects in block",
+ classPos < modulePos
+ )
+ }
+
+ @Test
+ def shouldCreateNonExistingObjectsInTemplate = checkCompile("frontend", "class D {class A{}; } ") {
+ (tree, context) =>
+ implicit val ctx = context
+ val transformer = new PostTyperTransformer {
+ override def transformations = Array(new CreateCompanionObjects {
+ override def name: String = "create all companion modules"
+ override def predicate(cts: TypeDef)(implicit ctx:Context): Boolean = true
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ })
+
+ override def name: String = "test"
+ }
+ val transformed = transformer.transform(tree).toString
+ val classPattern = "TypeDef(Modifiers(,,List()),A,"
+ val classPos = transformed.indexOf(classPattern)
+ val moduleClassPattern = "TypeDef(Modifiers(final module <synthetic>,,List()),A$"
+ val modulePos = transformed.indexOf(moduleClassPattern)
+
+ Assert.assertTrue("should create non-existing objects in template",
+ classPos < modulePos
+ )
+ }
+
+ @Test
+ def shouldCreateOnlyIfAskedFor = checkCompile("frontend", "class DONT {class CREATE{}; } ") {
+ (tree, context) =>
+ implicit val ctx = context
+ val transformer = new PostTyperTransformer {
+ override def transformations = Array(new CreateCompanionObjects {
+ override def name: String = "create all companion modules"
+ override def predicate(cts: TypeDef)(implicit ctx:Context): Boolean = cts.name.toString.contains("CREATE")
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ })
+
+ override def name: String = "test"
+ }
+ val transformed = transformer.transform(tree).toString
+ val classPattern = "TypeDef(Modifiers(,,List()),A,"
+ val classPos = transformed.indexOf(classPattern)
+ val moduleClassPattern = "TypeDef(Modifiers(final module <synthetic>,,List()),CREATE$"
+ val modulePos = transformed.indexOf(moduleClassPattern)
+
+ val notCreatedModulePattern = "TypeDef(Modifiers(final module <synthetic>,,List()),DONT"
+ val notCreatedPos = transformed.indexOf(notCreatedModulePattern)
+
+ Assert.assertTrue("should create non-existing objects in template",
+ classPos < modulePos && (notCreatedPos < 0)
+ )
+ }
+ */
+}
diff --git a/compiler/test/dotty/tools/dotc/transform/LazyValsTest.scala b/compiler/test/dotty/tools/dotc/transform/LazyValsTest.scala
new file mode 100644
index 000000000..96298c571
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/transform/LazyValsTest.scala
@@ -0,0 +1,361 @@
+package dotty.tools
+package dotc
+package transform
+
+import org.junit.Test
+import org.junit.Assert
+
+class LazyValsTest extends DottyTest {
+ /* FIXME: re-enable after adapting to new scheme
+ @Test
+ def doNotRewriteObjects = {
+ checkCompile("LazyVals", "object O"){ (tree, ctx) =>
+ Assert.assertTrue("local lazy shouldn't rewrite module instance definitions", tree.toString.contains(
+ "ValDef(Modifiers(final module <stable>,,List()),O,"
+ ))
+ }
+ }
+
+ @Test
+ def localInt = {
+ checkCompile("LazyVals", "class LocalLV { def m = { lazy val s = 1; s }}"){ (tree, ctx) =>
+ Assert.assertTrue("local lazy int rewritten to class creation", tree.toString.contains(
+ "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class runtime),LazyInt)],Apply(Select(New(TypeTree[TypeRef(ThisType(module class runtime),LazyInt)]),<init>),List(Literal(Constant(1)))))"
+ ))
+ }
+ }
+
+ @Test
+ def localLong = {
+ checkCompile("LazyVals", "class LocalLV { def m = { lazy val s = 1L; s }}"){ (tree, ctx) =>
+ Assert.assertTrue("local lazy long rewritten to class creation", tree.toString.contains(
+ "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class runtime),LazyLong)],Apply(Select(New(TypeTree[TypeRef(ThisType(module class runtime),LazyLong)]),<init>),List(Literal(Constant(1)))))"
+ ))
+ }
+ }
+
+ @Test
+ def localFloat = {
+ checkCompile("LazyVals", "class LocalLV { def m = { lazy val s = 1.0f; s }}"){ (tree, ctx) =>
+ Assert.assertTrue("local lazy float rewritten to class creation", tree.toString.contains(
+ "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class runtime),LazyFloat)],Apply(Select(New(TypeTree[TypeRef(ThisType(module class runtime),LazyFloat)]),<init>),List(Literal(Constant(1.0)))))"
+ ))
+ }
+ }
+
+ @Test
+ def localDouble = {
+ checkCompile("LazyVals", "class LocalLV { def m = { lazy val s = 1.0; s }}"){ (tree, ctx) =>
+ Assert.assertTrue("local lazy double rewritten to class creation", tree.toString.contains(
+ "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class runtime),LazyDouble)],Apply(Select(New(TypeTree[TypeRef(ThisType(module class runtime),LazyDouble)]),<init>),List(Literal(Constant(1.0)))))"
+ ))
+ }
+ }
+
+ @Test
+ def localBoolean = {
+ checkCompile("LazyVals", "class LocalLV { def m = { lazy val s = true; s }}"){ (tree, ctx) =>
+ Assert.assertTrue("local lazy boolean rewritten to class creation", tree.toString.contains(
+ "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class runtime),LazyBoolean)],Apply(Select(New(TypeTree[TypeRef(ThisType(module class runtime),LazyBoolean)]),<init>),List(Literal(Constant(true)))))"
+ ))
+ }
+ }
+
+ @Test
+ def localChar = {
+ checkCompile("LazyVals", "class LocalLV { def m = { lazy val s = 'a'; s }}"){ (tree, ctx) =>
+ Assert.assertTrue("local lazy char rewritten to class creation", tree.toString.contains(
+ "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class runtime),LazyChar)],Apply(Select(New(TypeTree[TypeRef(ThisType(module class runtime),LazyChar)]),<init>),List(Literal(Constant(a)))))"
+ ))
+ }
+ }
+
+ @Test
+ def localByte = {
+ checkCompile("LazyVals", "class LocalLV { def m = { lazy val s:Byte = 1; s }}"){ (tree, ctx) =>
+ Assert.assertTrue("local lazy byte rewritten to class creation", tree.toString.contains(
+ "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class runtime),LazyByte)],Apply(Select(New(TypeTree[TypeRef(ThisType(module class runtime),LazyByte)]),<init>),List(Literal(Constant(1)))))"
+ ))
+ }
+ }
+
+ @Test
+ def localShort = {
+ checkCompile("LazyVals", "class LocalLV { def m = { lazy val s:Short = 1; s }}"){ (tree, ctx) =>
+ Assert.assertTrue("local lazy short rewritten to class creation", tree.toString.contains(
+ "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class runtime),LazyShort)],Apply(Select(New(TypeTree[TypeRef(ThisType(module class runtime),LazyShort)]),<init>),List(Literal(Constant(1)))))"
+ ))
+ }
+ }
+
+ @Test
+ def localRef = {
+ checkCompile("LazyVals", "class LocalLV { def m = { lazy val s = \"string\"; s }}"){ (tree, ctx) =>
+ Assert.assertTrue("local lazy ref rewritten to class creation", tree.toString.contains(
+ "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class runtime),LazyRef)],Apply(Select(New(TypeTree[TypeRef(ThisType(module class runtime),LazyRef)]),<init>),List(Literal(Constant(string)))))"
+ ))
+ }
+ }
+
+ @Test
+ def fieldRef = {
+ checkCompile("LazyVals", "class LV { lazy val s = \"string\" }"){ (tree, ctx) =>
+ Assert.assertTrue("field lazy int rewritten to class creation", tree.toString.contains(
+ "DefDef(Modifiers(,,List()),s,List(),List(),TypeTree[TypeRef(ThisType(module class lang),String)],If(Ident(sbitmap$1),Ident(s$lzy1),Block(List(Assign(Ident(sbitmap$1),Literal(Constant(true))), Assign(Ident(s$lzy1),Literal(Constant(string)))),Ident(s$lzy1))))"
+ ))
+ }
+ }
+
+ @Test
+ def fieldInt = {
+ checkCompile("LazyVals", "class LV { lazy val s = 1 }"){ (tree, ctx) =>
+ Assert.assertTrue("field lazy int rewritten", tree.toString.contains(
+ "DefDef(Modifiers(,,List()),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Int)],If(Ident(sbitmap$1),Ident(s$lzy1),Block(List(Assign(Ident(sbitmap$1),Literal(Constant(true))), Assign(Ident(s$lzy1),Literal(Constant(1)))),Ident(s$lzy1))))"
+ ))
+ }
+ }
+
+ @Test
+ def fieldLong = {
+ checkCompile("LazyVals", "class LV { lazy val s = 1L }"){ (tree, ctx) =>
+ Assert.assertTrue("field lazy long rewritten", tree.toString.contains(
+ "DefDef(Modifiers(,,List()),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Long)],If(Ident(sbitmap$1),Ident(s$lzy1),Block(List(Assign(Ident(sbitmap$1),Literal(Constant(true))), Assign(Ident(s$lzy1),Literal(Constant(1)))),Ident(s$lzy1))))"
+ ))
+ }
+ }
+
+ @Test
+ def fieldShort = {
+ checkCompile("LazyVals", "class LV { lazy val s:Short = 1 }"){ (tree, ctx) =>
+ Assert.assertTrue("field lazy short rewritten", tree.toString.contains(
+ "DefDef(Modifiers(,,List()),s,List(),List(),TypeTree[TypeRef(TermRef(ThisType(module class <root>),scala),Short)],If(Ident(sbitmap$1),Ident(s$lzy1),Block(List(Assign(Ident(sbitmap$1),Literal(Constant(true))), Assign(Ident(s$lzy1),Literal(Constant(1)))),Ident(s$lzy1))))"
+ ))
+ }
+ }
+
+ @Test
+ def fieldByte = {
+ checkCompile("LazyVals", "class LV { lazy val s:Byte = 1 }"){ (tree, ctx) =>
+ Assert.assertTrue("field lazy byte rewritten", tree.toString.contains(
+ "DefDef(Modifiers(,,List()),s,List(),List(),TypeTree[TypeRef(TermRef(ThisType(module class <root>),scala),Byte)],If(Ident(sbitmap$1),Ident(s$lzy1),Block(List(Assign(Ident(sbitmap$1),Literal(Constant(true))), Assign(Ident(s$lzy1),Literal(Constant(1)))),Ident(s$lzy1))))"
+ ))
+ }
+ }
+
+ @Test
+ def fieldBoolean = {
+ checkCompile("LazyVals", "class LV { lazy val s = true }"){ (tree, ctx) =>
+ Assert.assertTrue("field lazy boolean rewritten", tree.toString.contains(
+ "DefDef(Modifiers(,,List()),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Boolean)],If(Ident(sbitmap$1),Ident(s$lzy1),Block(List(Assign(Ident(sbitmap$1),Literal(Constant(true))), Assign(Ident(s$lzy1),Literal(Constant(true)))),Ident(s$lzy1))))"
+ ))
+ }
+ }
+
+ @Test
+ def fieldDouble = {
+ checkCompile("LazyVals", "class LV { lazy val s = 1.0 }"){ (tree, ctx) =>
+ Assert.assertTrue("field lazy double rewritten", tree.toString.contains(
+ "DefDef(Modifiers(,,List()),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Double)],If(Ident(sbitmap$1),Ident(s$lzy1),Block(List(Assign(Ident(sbitmap$1),Literal(Constant(true))), Assign(Ident(s$lzy1),Literal(Constant(1.0)))),Ident(s$lzy1))))"
+ ))
+ }
+ }
+
+ @Test
+ def fieldFloat = {
+ checkCompile("LazyVals", "class LV { lazy val s = 1.0f }"){ (tree, ctx) =>
+ Assert.assertTrue("field lazy float rewritten", tree.toString.contains(
+ "DefDef(Modifiers(,,List()),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Float)],If(Ident(sbitmap$1),Ident(s$lzy1),Block(List(Assign(Ident(sbitmap$1),Literal(Constant(true))), Assign(Ident(s$lzy1),Literal(Constant(1.0)))),Ident(s$lzy1))))"
+ ))
+ }
+ }
+
+ @Test
+ def fieldChar = {
+ checkCompile("LazyVals", "class LV { lazy val s = 'a' }"){ (tree, ctx) =>
+ Assert.assertTrue("field lazy char rewritten", tree.toString.contains(
+ "DefDef(Modifiers(,,List()),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Char)],If(Ident(sbitmap$1),Ident(s$lzy1),Block(List(Assign(Ident(sbitmap$1),Literal(Constant(true))), Assign(Ident(s$lzy1),Literal(Constant(a)))),Ident(s$lzy1))))"
+ ))
+ }
+ }
+
+ @Test
+ def volatileFieldRef = {
+ checkCompile("LazyVals", "class LV { @volatile lazy val s = \"a\" }") {
+ (tree, ctx) =>
+ val accessor = "DefDef(Modifiers(,,List(Apply(Select(New(Ident(volatile)),<init>),List()))),s,List(),List(),TypeTree[TypeRef(ThisType(module class lang),String)],Block(List(ValDef(Modifiers(,,List()),result,TypeTree[TypeRef(ThisType(module class lang),String)],Literal(Constant(null))), ValDef(Modifiers(,,List()),retry,TypeTree[TypeRef(ThisType(module class scala),Boolean)],Literal(Constant(true))), ValDef(Modifiers(,,List()),flag,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0))), WhileDo(Ident(retry),Block(List(Assign(Ident(flag),Apply(Select(Ident(LazyVals),get),List(This(LV), Select(Ident(LV),OFFSET$0))))),Match(Apply(Select(Ident(LazyVals),STATE),List(Ident(flag), Literal(Constant(0)))),List(CaseDef(Literal(Constant(0)),EmptyTree,If(Apply(Select(Ident(LazyVals),CAS),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(1)), Literal(Constant(0)))),Block(List(Try(Assign(Ident(result),Literal(Constant(a))),Block(List(DefDef(Modifiers(,,List()),$anonfun,List(),List(List(ValDef(Modifiers(,,List()),x$1,TypeTree[TypeRef(ThisType(module class lang),Throwable)],EmptyTree))),TypeTree[TypeRef(ThisType(module class scala),Int)],Block(List(Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(0)), Literal(Constant(0))))),Throw(Ident(x$1))))),Closure(List(),Ident($anonfun),EmptyTree)),EmptyTree), Assign(Ident(s$lzy1),Ident(result)), Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(3)), Literal(Constant(0)))), Assign(Ident(retry),Literal(Constant(false)))),Literal(Constant(()))),Literal(Constant(())))), CaseDef(Literal(Constant(1)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(2)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(3)),EmptyTree,Block(List(Assign(Ident(retry),Literal(Constant(false))), Assign(Ident(result),Ident(s$lzy1))),Literal(Constant(()))))))))),Ident(result)))"
+ val fields = "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class lang),String)],Literal(Constant(null))), ValDef(Modifiers(,,List()),bitmap$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0)))"
+ val moduleField = "TypeDef(Modifiers(final module <synthetic>,,List()),LV$,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class <empty>),LV$)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(,,List()),_,TypeTree[TermRef(ThisType(module class <empty>),LV)],EmptyTree),List(ValDef(Modifiers(,,List()),OFFSET$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$0))))))))"
+ val treeS = tree.toString
+ //println(treeS)
+ Assert.assertTrue("volatile field lazy ref rewritten to class creation\n" + treeS,
+ treeS.contains(accessor) && treeS.contains(fields) && treeS.contains(moduleField))
+ }
+ }
+
+ @Test
+ def volatileFieldInt = {
+ checkCompile("LazyVals", "class LV { @volatile lazy val s = 1 }") {
+ (tree, ctx) =>
+ val accessor = "DefDef(Modifiers(,,List(Apply(Select(New(Ident(volatile)),<init>),List()))),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Int)],Block(List(ValDef(Modifiers(,,List()),result,TypeTree[TypeRef(ThisType(module class scala),Int)],Literal(Constant(0))), ValDef(Modifiers(,,List()),retry,TypeTree[TypeRef(ThisType(module class scala),Boolean)],Literal(Constant(true))), ValDef(Modifiers(,,List()),flag,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0))), WhileDo(Ident(retry),Block(List(Assign(Ident(flag),Apply(Select(Ident(LazyVals),get),List(This(LV), Select(Ident(LV),OFFSET$0))))),Match(Apply(Select(Ident(LazyVals),STATE),List(Ident(flag), Literal(Constant(0)))),List(CaseDef(Literal(Constant(0)),EmptyTree,If(Apply(Select(Ident(LazyVals),CAS),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(1)), Literal(Constant(0)))),Block(List(Try(Assign(Ident(result),Literal(Constant(1))),Block(List(DefDef(Modifiers(,,List()),$anonfun,List(),List(List(ValDef(Modifiers(,,List()),x$1,TypeTree[TypeRef(ThisType(module class lang),Throwable)],EmptyTree))),TypeTree[TypeRef(ThisType(module class scala),Int)],Block(List(Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(0)), Literal(Constant(0))))),Throw(Ident(x$1))))),Closure(List(),Ident($anonfun),EmptyTree)),EmptyTree), Assign(Ident(s$lzy1),Ident(result)), Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(3)), Literal(Constant(0)))), Assign(Ident(retry),Literal(Constant(false)))),Literal(Constant(()))),Literal(Constant(())))), CaseDef(Literal(Constant(1)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(2)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(3)),EmptyTree,Block(List(Assign(Ident(retry),Literal(Constant(false))), Assign(Ident(result),Ident(s$lzy1))),Literal(Constant(()))))))))),Ident(result)))"
+ val fields = "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class scala),Int)],Literal(Constant(0))), ValDef(Modifiers(,,List()),bitmap$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0)))"
+ val moduleField = "TypeDef(Modifiers(final module <synthetic>,,List()),LV$,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class <empty>),LV$)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(,,List()),_,TypeTree[TermRef(ThisType(module class <empty>),LV)],EmptyTree),List(ValDef(Modifiers(,,List()),OFFSET$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$0))))))))"
+ val treeS = tree.toString
+ Assert.assertTrue("volatile field lazy ref rewritten to class creation\n" + treeS,
+ treeS.contains(accessor) && treeS.contains(fields) && treeS.contains(moduleField))
+ }
+ }
+
+ @Test
+ def volatileFieldLong = {
+ checkCompile("LazyVals", "class LV { @volatile lazy val s = 1L }") {
+ (tree, ctx) =>
+ val accessor = "DefDef(Modifiers(,,List(Apply(Select(New(Ident(volatile)),<init>),List()))),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Long)],Block(List(ValDef(Modifiers(,,List()),result,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0))), ValDef(Modifiers(,,List()),retry,TypeTree[TypeRef(ThisType(module class scala),Boolean)],Literal(Constant(true))), ValDef(Modifiers(,,List()),flag,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0))), WhileDo(Ident(retry),Block(List(Assign(Ident(flag),Apply(Select(Ident(LazyVals),get),List(This(LV), Select(Ident(LV),OFFSET$0))))),Match(Apply(Select(Ident(LazyVals),STATE),List(Ident(flag), Literal(Constant(0)))),List(CaseDef(Literal(Constant(0)),EmptyTree,If(Apply(Select(Ident(LazyVals),CAS),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(1)), Literal(Constant(0)))),Block(List(Try(Assign(Ident(result),Literal(Constant(1))),Block(List(DefDef(Modifiers(,,List()),$anonfun,List(),List(List(ValDef(Modifiers(,,List()),x$1,TypeTree[TypeRef(ThisType(module class lang),Throwable)],EmptyTree))),TypeTree[TypeRef(ThisType(module class scala),Int)],Block(List(Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(0)), Literal(Constant(0))))),Throw(Ident(x$1))))),Closure(List(),Ident($anonfun),EmptyTree)),EmptyTree), Assign(Ident(s$lzy1),Ident(result)), Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(3)), Literal(Constant(0)))), Assign(Ident(retry),Literal(Constant(false)))),Literal(Constant(()))),Literal(Constant(())))), CaseDef(Literal(Constant(1)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(2)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(3)),EmptyTree,Block(List(Assign(Ident(retry),Literal(Constant(false))), Assign(Ident(result),Ident(s$lzy1))),Literal(Constant(()))))))))),Ident(result)))"
+ val fields = "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0))), ValDef(Modifiers(,,List()),bitmap$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0)))"
+ val moduleField = "TypeDef(Modifiers(final module <synthetic>,,List()),LV$,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class <empty>),LV$)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(,,List()),_,TypeTree[TermRef(ThisType(module class <empty>),LV)],EmptyTree),List(ValDef(Modifiers(,,List()),OFFSET$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$0))))))))"
+ val treeS = tree.toString
+ Assert.assertTrue("volatile field lazy ref rewritten to class creation\n" + treeS,
+ treeS.contains(accessor) && treeS.contains(fields) && treeS.contains(moduleField))
+ }
+ }
+
+ @Test
+ def volatileFieldFloat = {
+ checkCompile("LazyVals", "class LV { @volatile lazy val s = 1.0f }") {
+ (tree, ctx) =>
+ val accessor = "DefDef(Modifiers(,,List(Apply(Select(New(Ident(volatile)),<init>),List()))),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Float)],Block(List(ValDef(Modifiers(,,List()),result,TypeTree[TypeRef(ThisType(module class scala),Float)],Literal(Constant(0.0))), ValDef(Modifiers(,,List()),retry,TypeTree[TypeRef(ThisType(module class scala),Boolean)],Literal(Constant(true))), ValDef(Modifiers(,,List()),flag,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0))), WhileDo(Ident(retry),Block(List(Assign(Ident(flag),Apply(Select(Ident(LazyVals),get),List(This(LV), Select(Ident(LV),OFFSET$0))))),Match(Apply(Select(Ident(LazyVals),STATE),List(Ident(flag), Literal(Constant(0)))),List(CaseDef(Literal(Constant(0)),EmptyTree,If(Apply(Select(Ident(LazyVals),CAS),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(1)), Literal(Constant(0)))),Block(List(Try(Assign(Ident(result),Literal(Constant(1.0))),Block(List(DefDef(Modifiers(,,List()),$anonfun,List(),List(List(ValDef(Modifiers(,,List()),x$1,TypeTree[TypeRef(ThisType(module class lang),Throwable)],EmptyTree))),TypeTree[TypeRef(ThisType(module class scala),Int)],Block(List(Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(0)), Literal(Constant(0))))),Throw(Ident(x$1))))),Closure(List(),Ident($anonfun),EmptyTree)),EmptyTree), Assign(Ident(s$lzy1),Ident(result)), Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(3)), Literal(Constant(0)))), Assign(Ident(retry),Literal(Constant(false)))),Literal(Constant(()))),Literal(Constant(())))), CaseDef(Literal(Constant(1)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(2)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(3)),EmptyTree,Block(List(Assign(Ident(retry),Literal(Constant(false))), Assign(Ident(result),Ident(s$lzy1))),Literal(Constant(()))))))))),Ident(result)))"
+ val fields = "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class scala),Float)],Literal(Constant(0.0))), ValDef(Modifiers(,,List()),bitmap$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0)))"
+ val moduleField = "TypeDef(Modifiers(final module <synthetic>,,List()),LV$,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class <empty>),LV$)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(,,List()),_,TypeTree[TermRef(ThisType(module class <empty>),LV)],EmptyTree),List(ValDef(Modifiers(,,List()),OFFSET$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$0))))))))"
+ val treeS = tree.toString
+ Assert.assertTrue("volatile field lazy ref rewritten to class creation\n" + treeS,
+ treeS.contains(accessor) && treeS.contains(fields) && treeS.contains(moduleField))
+ }
+ }
+
+ @Test
+ def volatileFieldDouble = {
+ checkCompile("LazyVals", "class LV { @volatile lazy val s = 1.0 }") {
+ (tree, ctx) =>
+ val accessor = "DefDef(Modifiers(,,List(Apply(Select(New(Ident(volatile)),<init>),List()))),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Double)],Block(List(ValDef(Modifiers(,,List()),result,TypeTree[TypeRef(ThisType(module class scala),Double)],Literal(Constant(0.0))), ValDef(Modifiers(,,List()),retry,TypeTree[TypeRef(ThisType(module class scala),Boolean)],Literal(Constant(true))), ValDef(Modifiers(,,List()),flag,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0))), WhileDo(Ident(retry),Block(List(Assign(Ident(flag),Apply(Select(Ident(LazyVals),get),List(This(LV), Select(Ident(LV),OFFSET$0))))),Match(Apply(Select(Ident(LazyVals),STATE),List(Ident(flag), Literal(Constant(0)))),List(CaseDef(Literal(Constant(0)),EmptyTree,If(Apply(Select(Ident(LazyVals),CAS),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(1)), Literal(Constant(0)))),Block(List(Try(Assign(Ident(result),Literal(Constant(1.0))),Block(List(DefDef(Modifiers(,,List()),$anonfun,List(),List(List(ValDef(Modifiers(,,List()),x$1,TypeTree[TypeRef(ThisType(module class lang),Throwable)],EmptyTree))),TypeTree[TypeRef(ThisType(module class scala),Int)],Block(List(Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(0)), Literal(Constant(0))))),Throw(Ident(x$1))))),Closure(List(),Ident($anonfun),EmptyTree)),EmptyTree), Assign(Ident(s$lzy1),Ident(result)), Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(3)), Literal(Constant(0)))), Assign(Ident(retry),Literal(Constant(false)))),Literal(Constant(()))),Literal(Constant(())))), CaseDef(Literal(Constant(1)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(2)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(3)),EmptyTree,Block(List(Assign(Ident(retry),Literal(Constant(false))), Assign(Ident(result),Ident(s$lzy1))),Literal(Constant(()))))))))),Ident(result)))"
+ val fields = "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class scala),Double)],Literal(Constant(0.0))), ValDef(Modifiers(,,List()),bitmap$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0)))"
+ val moduleField = "TypeDef(Modifiers(final module <synthetic>,,List()),LV$,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class <empty>),LV$)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(,,List()),_,TypeTree[TermRef(ThisType(module class <empty>),LV)],EmptyTree),List(ValDef(Modifiers(,,List()),OFFSET$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$0))))))))"
+ val treeS = tree.toString
+ Assert.assertTrue("volatile field lazy ref rewritten to class creation\n" + treeS,
+ treeS.contains(accessor) && treeS.contains(fields) && treeS.contains(moduleField))
+ }
+ }
+
+ @Test
+ def volatileFieldBoolean = {
+ checkCompile("LazyVals", "class LV { @volatile lazy val s = true }") {
+ (tree, ctx) =>
+ val accessor = "DefDef(Modifiers(,,List(Apply(Select(New(Ident(volatile)),<init>),List()))),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Boolean)],Block(List(ValDef(Modifiers(,,List()),result,TypeTree[TypeRef(ThisType(module class scala),Boolean)],Literal(Constant(false))), ValDef(Modifiers(,,List()),retry,TypeTree[TypeRef(ThisType(module class scala),Boolean)],Literal(Constant(true))), ValDef(Modifiers(,,List()),flag,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0))), WhileDo(Ident(retry),Block(List(Assign(Ident(flag),Apply(Select(Ident(LazyVals),get),List(This(LV), Select(Ident(LV),OFFSET$0))))),Match(Apply(Select(Ident(LazyVals),STATE),List(Ident(flag), Literal(Constant(0)))),List(CaseDef(Literal(Constant(0)),EmptyTree,If(Apply(Select(Ident(LazyVals),CAS),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(1)), Literal(Constant(0)))),Block(List(Try(Assign(Ident(result),Literal(Constant(true))),Block(List(DefDef(Modifiers(,,List()),$anonfun,List(),List(List(ValDef(Modifiers(,,List()),x$1,TypeTree[TypeRef(ThisType(module class lang),Throwable)],EmptyTree))),TypeTree[TypeRef(ThisType(module class scala),Int)],Block(List(Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(0)), Literal(Constant(0))))),Throw(Ident(x$1))))),Closure(List(),Ident($anonfun),EmptyTree)),EmptyTree), Assign(Ident(s$lzy1),Ident(result)), Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(3)), Literal(Constant(0)))), Assign(Ident(retry),Literal(Constant(false)))),Literal(Constant(()))),Literal(Constant(())))), CaseDef(Literal(Constant(1)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(2)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(3)),EmptyTree,Block(List(Assign(Ident(retry),Literal(Constant(false))), Assign(Ident(result),Ident(s$lzy1))),Literal(Constant(()))))))))),Ident(result)))"
+ val fields = "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class scala),Boolean)],Literal(Constant(false))), ValDef(Modifiers(,,List()),bitmap$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0)))"
+ val moduleField = "TypeDef(Modifiers(final module <synthetic>,,List()),LV$,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class <empty>),LV$)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(,,List()),_,TypeTree[TermRef(ThisType(module class <empty>),LV)],EmptyTree),List(ValDef(Modifiers(,,List()),OFFSET$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$0))))))))"
+ val treeS = tree.toString
+ Assert.assertTrue("volatile field lazy ref rewritten to class creation\n" + treeS,
+ treeS.contains(accessor) && treeS.contains(fields) && treeS.contains(moduleField))
+ }
+ }
+
+ @Test
+ def volatileFieldByte = {
+ checkCompile("LazyVals", "class LV { @volatile lazy val s:Byte = 1 }") {
+ (tree, ctx) =>
+ val accessor = "DefDef(Modifiers(,,List(Apply(Select(New(Ident(volatile)),<init>),List()))),s,List(),List(),TypeTree[TypeRef(TermRef(ThisType(module class <root>),scala),Byte)],Block(List(ValDef(Modifiers(,,List()),result,TypeTree[TypeRef(TermRef(ThisType(module class <root>),scala),Byte)],Literal(Constant(0))), ValDef(Modifiers(,,List()),retry,TypeTree[TypeRef(ThisType(module class scala),Boolean)],Literal(Constant(true))), ValDef(Modifiers(,,List()),flag,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0))), WhileDo(Ident(retry),Block(List(Assign(Ident(flag),Apply(Select(Ident(LazyVals),get),List(This(LV), Select(Ident(LV),OFFSET$0))))),Match(Apply(Select(Ident(LazyVals),STATE),List(Ident(flag), Literal(Constant(0)))),List(CaseDef(Literal(Constant(0)),EmptyTree,If(Apply(Select(Ident(LazyVals),CAS),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(1)), Literal(Constant(0)))),Block(List(Try(Assign(Ident(result),Literal(Constant(1))),Block(List(DefDef(Modifiers(,,List()),$anonfun,List(),List(List(ValDef(Modifiers(,,List()),x$1,TypeTree[TypeRef(ThisType(module class lang),Throwable)],EmptyTree))),TypeTree[TypeRef(ThisType(module class scala),Int)],Block(List(Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(0)), Literal(Constant(0))))),Throw(Ident(x$1))))),Closure(List(),Ident($anonfun),EmptyTree)),EmptyTree), Assign(Ident(s$lzy1),Ident(result)), Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(3)), Literal(Constant(0)))), Assign(Ident(retry),Literal(Constant(false)))),Literal(Constant(()))),Literal(Constant(())))), CaseDef(Literal(Constant(1)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(2)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(3)),EmptyTree,Block(List(Assign(Ident(retry),Literal(Constant(false))), Assign(Ident(result),Ident(s$lzy1))),Literal(Constant(()))))))))),Ident(result)))"
+ val fields = "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(TermRef(ThisType(module class <root>),scala),Byte)],Literal(Constant(0))), ValDef(Modifiers(,,List()),bitmap$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0)))"
+ val moduleField = "TypeDef(Modifiers(final module <synthetic>,,List()),LV$,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class <empty>),LV$)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(,,List()),_,TypeTree[TermRef(ThisType(module class <empty>),LV)],EmptyTree),List(ValDef(Modifiers(,,List()),OFFSET$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$0))))))))"
+ val treeS = tree.toString
+ Assert.assertTrue("volatile field lazy ref rewritten to class creation\n" + treeS,
+ treeS.contains(accessor) && treeS.contains(fields) && treeS.contains(moduleField))
+ }
+ }
+
+ @Test
+ def volatileFieldShort = {
+ checkCompile("LazyVals", "class LV { @volatile lazy val s:Short = 1 }") {
+ (tree, ctx) =>
+ val accessor = "DefDef(Modifiers(,,List(Apply(Select(New(Ident(volatile)),<init>),List()))),s,List(),List(),TypeTree[TypeRef(TermRef(ThisType(module class <root>),scala),Short)],Block(List(ValDef(Modifiers(,,List()),result,TypeTree[TypeRef(TermRef(ThisType(module class <root>),scala),Short)],Literal(Constant(0))), ValDef(Modifiers(,,List()),retry,TypeTree[TypeRef(ThisType(module class scala),Boolean)],Literal(Constant(true))), ValDef(Modifiers(,,List()),flag,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0))), WhileDo(Ident(retry),Block(List(Assign(Ident(flag),Apply(Select(Ident(LazyVals),get),List(This(LV), Select(Ident(LV),OFFSET$0))))),Match(Apply(Select(Ident(LazyVals),STATE),List(Ident(flag), Literal(Constant(0)))),List(CaseDef(Literal(Constant(0)),EmptyTree,If(Apply(Select(Ident(LazyVals),CAS),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(1)), Literal(Constant(0)))),Block(List(Try(Assign(Ident(result),Literal(Constant(1))),Block(List(DefDef(Modifiers(,,List()),$anonfun,List(),List(List(ValDef(Modifiers(,,List()),x$1,TypeTree[TypeRef(ThisType(module class lang),Throwable)],EmptyTree))),TypeTree[TypeRef(ThisType(module class scala),Int)],Block(List(Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(0)), Literal(Constant(0))))),Throw(Ident(x$1))))),Closure(List(),Ident($anonfun),EmptyTree)),EmptyTree), Assign(Ident(s$lzy1),Ident(result)), Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(3)), Literal(Constant(0)))), Assign(Ident(retry),Literal(Constant(false)))),Literal(Constant(()))),Literal(Constant(())))), CaseDef(Literal(Constant(1)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(2)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(3)),EmptyTree,Block(List(Assign(Ident(retry),Literal(Constant(false))), Assign(Ident(result),Ident(s$lzy1))),Literal(Constant(()))))))))),Ident(result)))"
+ val fields = "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(TermRef(ThisType(module class <root>),scala),Short)],Literal(Constant(0))), ValDef(Modifiers(,,List()),bitmap$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0)))"
+ val moduleField = "TypeDef(Modifiers(final module <synthetic>,,List()),LV$,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class <empty>),LV$)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(,,List()),_,TypeTree[TermRef(ThisType(module class <empty>),LV)],EmptyTree),List(ValDef(Modifiers(,,List()),OFFSET$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$0))))))))"
+ val treeS = tree.toString
+ Assert.assertTrue("volatile field lazy ref rewritten to class creation\n" + treeS,
+ treeS.contains(accessor) && treeS.contains(fields) && treeS.contains(moduleField))
+ }
+ }
+
+ @Test
+ def volatileFieldChar = {
+ checkCompile("LazyVals", "class LV { @volatile lazy val s = 'a' }") {
+ (tree, ctx) =>
+ val accessor = "DefDef(Modifiers(,,List(Apply(Select(New(Ident(volatile)),<init>),List()))),s,List(),List(),TypeTree[TypeRef(ThisType(module class scala),Char)],Block(List(ValDef(Modifiers(,,List()),result,TypeTree[TypeRef(ThisType(module class scala),Char)],Literal(Constant(\u0000))), ValDef(Modifiers(,,List()),retry,TypeTree[TypeRef(ThisType(module class scala),Boolean)],Literal(Constant(true))), ValDef(Modifiers(,,List()),flag,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0))), WhileDo(Ident(retry),Block(List(Assign(Ident(flag),Apply(Select(Ident(LazyVals),get),List(This(LV), Select(Ident(LV),OFFSET$0))))),Match(Apply(Select(Ident(LazyVals),STATE),List(Ident(flag), Literal(Constant(0)))),List(CaseDef(Literal(Constant(0)),EmptyTree,If(Apply(Select(Ident(LazyVals),CAS),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(1)), Literal(Constant(0)))),Block(List(Try(Assign(Ident(result),Literal(Constant(a))),Block(List(DefDef(Modifiers(,,List()),$anonfun,List(),List(List(ValDef(Modifiers(,,List()),x$1,TypeTree[TypeRef(ThisType(module class lang),Throwable)],EmptyTree))),TypeTree[TypeRef(ThisType(module class scala),Int)],Block(List(Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(0)), Literal(Constant(0))))),Throw(Ident(x$1))))),Closure(List(),Ident($anonfun),EmptyTree)),EmptyTree), Assign(Ident(s$lzy1),Ident(result)), Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(3)), Literal(Constant(0)))), Assign(Ident(retry),Literal(Constant(false)))),Literal(Constant(()))),Literal(Constant(())))), CaseDef(Literal(Constant(1)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(2)),EmptyTree,Apply(Select(Ident(LazyVals),wait4Notification),List(This(LV), Select(Ident(LV),OFFSET$0), Ident(flag), Literal(Constant(0))))), CaseDef(Literal(Constant(3)),EmptyTree,Block(List(Assign(Ident(retry),Literal(Constant(false))), Assign(Ident(result),Ident(s$lzy1))),Literal(Constant(()))))))))),Ident(result)))"
+ val fields = "ValDef(Modifiers(,,List()),s$lzy1,TypeTree[TypeRef(ThisType(module class scala),Char)],Literal(Constant(\u0000))), ValDef(Modifiers(,,List()),bitmap$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Literal(Constant(0)))"
+ val moduleField = "TypeDef(Modifiers(final module <synthetic>,,List()),LV$,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class <empty>),LV$)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(,,List()),_,TypeTree[TermRef(ThisType(module class <empty>),LV)],EmptyTree),List(ValDef(Modifiers(,,List()),OFFSET$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$0))))))))"
+
+ val treeS = tree.toString
+ Assert.assertTrue("volatile field lazy ref rewritten to class creation\n" + treeS,
+ treeS.contains(accessor) && treeS.contains(fields) && treeS.contains(moduleField))
+ }
+ }
+
+ @Test
+ def volatilesReuseBitmaps = {
+ checkCompile("LazyVals", "class LV { @volatile lazy val a = 'a'; @volatile lazy val b = 'b'; }") {
+ (tree, ctx) =>
+ val moduleField = "TypeDef(Modifiers(final module <synthetic>,,List()),LV$,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class <empty>),LV$)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(,,List()),_,TypeTree[TermRef(ThisType(module class <empty>),LV)],EmptyTree),List(ValDef(Modifiers(,,List()),OFFSET$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$0))))))))"
+ val reuseFieldPattern = "Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$0), Literal(Constant(3)), Literal(Constant(1))))"
+ val treeS = tree.toString
+ Assert.assertTrue("volatile field lazy ref rewritten to class creation\n" + treeS,
+ treeS.contains(moduleField) && treeS.contains(reuseFieldPattern))
+ }
+ }
+
+ @Test
+ def volatilesCreateNewBitmaps = {
+ checkCompile("LazyVals",
+ """
+ | class LV {
+ | @volatile lazy val a1 = '1';
+ | @volatile lazy val a2 = '1';
+ | @volatile lazy val a3 = '1';
+ | @volatile lazy val a4 = '1';
+ | @volatile lazy val a5 = '1';
+ | @volatile lazy val a6 = '1';
+ | @volatile lazy val a7 = '1';
+ | @volatile lazy val a8 = '1';
+ | @volatile lazy val a9 = '1';
+ | @volatile lazy val a10 = '1';
+ | @volatile lazy val a11 = '1';
+ | @volatile lazy val a12 = '1';
+ | @volatile lazy val a13 = '1';
+ | @volatile lazy val a14 = '1';
+ | @volatile lazy val a15 = '1';
+ | @volatile lazy val a16 = '1';
+ | @volatile lazy val a17 = '1';
+ | @volatile lazy val a18 = '1';
+ | @volatile lazy val a19 = '1';
+ | @volatile lazy val a20 = '1';
+ | @volatile lazy val a21 = '1';
+ | @volatile lazy val a22 = '1';
+ | @volatile lazy val a23 = '1';
+ | @volatile lazy val a24 = '1';
+ | @volatile lazy val a25 = '1';
+ | @volatile lazy val a26 = '1';
+ | @volatile lazy val a27 = '1';
+ | @volatile lazy val a28 = '1';
+ | @volatile lazy val a29 = '1';
+ | @volatile lazy val a30 = '1';
+ | @volatile lazy val a31 = '1';
+ | @volatile lazy val a32 = '1';
+ | @volatile lazy val a33 = '1';
+ | @volatile lazy val a34 = '1';
+ | }
+ """.stripMargin ){
+ (tree, ctx) =>
+ val moduleField = "TypeDef(Modifiers(final module <synthetic>,,List()),LV$,Template(DefDef(Modifiers(,,List()),<init>,List(),List(List()),TypeTree[TypeRef(ThisType(module class <empty>),LV$)],EmptyTree),List(Apply(Select(New(TypeTree[TypeRef(ThisType(module class lang),Object)]),<init>),List())),ValDef(Modifiers(,,List()),_,TypeTree[TermRef(ThisType(module class <empty>),LV)],EmptyTree),List(ValDef(Modifiers(,,List()),OFFSET$1,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$1))))), ValDef(Modifiers(,,List()),OFFSET$0,TypeTree[TypeRef(ThisType(module class scala),Long)],Apply(Select(Ident(LazyVals),getOffset),List(This(LV), Literal(Constant(bitmap$0))))))))"
+ val reuseFieldPattern = "Apply(Select(Ident(LazyVals),setFlag),List(This(LV), Select(Ident(LV),OFFSET$1), Literal(Constant(3)), Literal(Constant(1))))"
+ val treeS = tree.toString
+ Assert.assertTrue("volatile field lazy ref rewritten to class creation",
+ treeS.contains(moduleField) && treeS.contains(reuseFieldPattern))
+ }
+ }*/
+}
diff --git a/compiler/test/dotty/tools/dotc/transform/PostTyperTransformerTest.scala b/compiler/test/dotty/tools/dotc/transform/PostTyperTransformerTest.scala
new file mode 100644
index 000000000..03d6d9b36
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/transform/PostTyperTransformerTest.scala
@@ -0,0 +1,132 @@
+package dotty.tools
+package dotc
+package transform
+
+import org.junit.{Assert, Test}
+import core._
+import ast.Trees
+import Contexts._
+import Flags._
+import Denotations._
+import NameOps._
+import Symbols._
+import Types._
+import Decorators._
+import Trees._
+import TreeTransforms.{TreeTransform, TreeTransformer}
+
+class PostTyperTransformerTest extends DottyTest {
+ /* FIXME: re-enable after adapting to new scheme
+
+ @Test
+ def shouldStripImports = checkCompile("frontend", "class A{ import scala.collection.mutable._; val d = 1}") {
+ (tree, context) =>
+ implicit val ctx = context
+ class EmptyTransform extends TreeTransform {
+ override def name: String = "empty"
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ val transformer = new PostTyperTransformer {
+ override def transformations = Array(new EmptyTransform)
+
+ override def name: String = "test"
+ }
+ val transformed = transformer.transform(tree)
+
+ Assert.assertTrue("should strip imports",
+ !transformed.toString.toLowerCase.contains("import")
+ )
+ }
+
+ @Test
+ def shouldStripNamedArgs = checkCompile("frontend", "class A{ def p(x:Int, y:Int= 2) = 1; p(1, y = 2)}") {
+ (tree, context) =>
+ implicit val ctx = context
+ class EmptyTransform extends TreeTransform {
+ override def name: String = "empty"
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ val transformer = new PostTyperTransformer {
+ override def transformations = Array(new EmptyTransform)
+
+ override def name: String = "test"
+ }
+ val transformed = transformer.transform(tree)
+
+ Assert.assertTrue("should string named arguments",
+ !transformed.toString.contains("NamedArg")
+ )
+ }
+
+ @Test
+ def shouldReorderExistingObjectsInPackage = checkCompile("frontend", "object A{}; class A{} ") {
+ (tree, context) =>
+ implicit val ctx = context
+ class EmptyTransform extends TreeTransform {
+ override def name: String = "empty"
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ val transformer = new PostTyperTransformer {
+ override def transformations = Array(new EmptyTransform)
+
+ override def name: String = "test"
+ }
+ val transformed = transformer.transform(tree).toString
+ val classPattern = "TypeDef(Modifiers(,,List()),A,"
+ val classPos = transformed.indexOf(classPattern)
+ val moduleClassPattern = "TypeDef(Modifiers(final module,,List()),A$,"
+ val modulePos = transformed.indexOf(moduleClassPattern)
+
+ Assert.assertTrue("should reorder existing objects in package",
+ classPos < modulePos
+ )
+ }
+
+ @Test
+ def shouldReorderExistingObjectsInBlock = checkCompile("frontend", "class D {def p = {object A{}; class A{}; 1}} ") {
+ (tree, context) =>
+ implicit val ctx = context
+ class EmptyTransform extends TreeTransform {
+ override def name: String = "empty"
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ val transformer = new PostTyperTransformer {
+ override def transformations = Array(new EmptyTransform)
+
+ override def name: String = "test"
+ }
+ val transformed = transformer.transform(tree).toString
+ val classPattern = "TypeDef(Modifiers(,,List()),A,"
+ val classPos = transformed.indexOf(classPattern)
+ val moduleClassPattern = "TypeDef(Modifiers(final module,,List()),A$,"
+ val modulePos = transformed.indexOf(moduleClassPattern)
+
+ Assert.assertTrue("should reorder existing objects in block",
+ classPos < modulePos
+ )
+ }
+
+ @Test
+ def shouldReorderExistingObjectsInTemplate = checkCompile("frontend", "class D {object A{}; class A{}; } ") {
+ (tree, context) =>
+ implicit val ctx = context
+ class EmptyTransform extends TreeTransform {
+ override def name: String = "empty"
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ val transformer = new PostTyperTransformer {
+ override def transformations = Array(new EmptyTransform)
+
+ override def name: String = "test"
+ }
+ val transformed = transformer.transform(tree).toString
+ val classPattern = "TypeDef(Modifiers(,,List()),A,"
+ val classPos = transformed.indexOf(classPattern)
+ val moduleClassPattern = "TypeDef(Modifiers(final module,,List()),A$,"
+ val modulePos = transformed.indexOf(moduleClassPattern)
+
+ Assert.assertTrue("should reorder existing objects in template",
+ classPos < modulePos
+ )
+ }*/
+}
diff --git a/compiler/test/dotty/tools/dotc/transform/TreeTransformerTest.scala b/compiler/test/dotty/tools/dotc/transform/TreeTransformerTest.scala
new file mode 100644
index 000000000..d72980d80
--- /dev/null
+++ b/compiler/test/dotty/tools/dotc/transform/TreeTransformerTest.scala
@@ -0,0 +1,198 @@
+package dotty.tools
+package dotc
+package transform
+
+import org.junit.{Assert, Test}
+import TreeTransforms.{TransformerInfo, TreeTransformer, MiniPhaseTransform}
+import ast.tpd
+import core.Constants.Constant
+import core.Contexts.Context
+
+class TreeTransformerTest extends DottyTest {
+
+ @Test
+ def shouldReturnSameTreeIfUnchanged = checkCompile("frontend", "class A{ val d = 1}") {
+ (tree, context) =>
+ implicit val ctx = context
+ class EmptyTransform extends MiniPhaseTransform {
+ override def phaseName: String = "empty"
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ val transformer = new TreeTransformer {
+ override def miniPhases = Array(new EmptyTransform)
+
+ override def phaseName: String = "test"
+ }
+ val transformed = transformer.macroTransform(tree)
+
+ Assert.assertTrue("returns same tree if unmodified",
+ tree eq transformed
+ )
+ }
+
+ // Disabled, awaiting resolution. @Test
+ def canReplaceConstant = checkCompile("frontend", "class A{ val d = 1}") {
+ (tree, context) =>
+ implicit val ctx = context
+ class ConstantTransform extends MiniPhaseTransform {
+
+ override def transformLiteral(tree: tpd.Literal)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = tpd.Literal(Constant(2))
+ override def phaseName: String = "canReplaceConstant"
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ val transformer = new TreeTransformer {
+ override def miniPhases = Array(new ConstantTransform)
+
+ override def phaseName: String = "test"
+ }
+ val transformed = transformer.macroTransform(tree)
+
+ Assert.assertTrue("returns same tree if unmodified",
+ transformed.toString.contains("List(ValDef(Modifiers(,,List()),d,TypeTree[TypeRef(ThisType(module class scala),Int)],Literal(Constant(2)))")
+ )
+ }
+
+ @Test
+ def canOverwrite = checkCompile("frontend", "class A{ val d = 1}") {
+ (tree, context) =>
+ implicit val ctx = context
+ class Transformation extends MiniPhaseTransform {
+
+ override def transformLiteral(tree: tpd.Literal)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = tpd.Literal(Constant(-1))
+ override def phaseName: String = "canOverwrite"
+
+ override def transformValDef(tree: tpd.ValDef)(implicit ctx: Context, info: TransformerInfo): tpd.ValDef = {
+ Assert.assertTrue("transformation of children succeeded",
+ tree.rhs.toString == "Literal(Constant(-1))"
+ )
+ tpd.cpy.ValDef(tree)(rhs = tpd.Literal(Constant(2)))
+ }
+
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ val transformer = new TreeTransformer {
+ override def miniPhases = Array(new Transformation)
+
+ override def phaseName: String = "test"
+
+ }
+ val tr = transformer.macroTransform(tree).toString
+
+ Assert.assertTrue("node can rewrite children",
+ tr.contains("Literal(Constant(2))") && !tr.contains("Literal(Constant(-1))")
+ )
+ }
+
+ @Test
+ def transformationOrder = checkCompile("frontend", "class A{ val d = 1}") {
+ (tree, context) =>
+ implicit val ctx = context
+ class Transformation1 extends MiniPhaseTransform {
+ override def phaseName: String = "transformationOrder1"
+
+ override def transformLiteral(tree: tpd.Literal)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
+ Assert.assertTrue("correct constant",
+ tree.const.toString == "Constant(1)"
+ )
+ tpd.cpy.Literal(tree)(Constant(-1))
+ }
+
+ override def transformValDef(tree: tpd.ValDef)(implicit ctx: Context, info: TransformerInfo): tpd.ValDef = {
+ Assert.assertTrue("transformation of children succeeded",
+ tree.rhs.toString == "Literal(Constant(-1))"
+ )
+ tpd.cpy.ValDef(tree)(rhs = tpd.Literal(Constant(2)))
+ }
+
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ class Transformation2 extends MiniPhaseTransform {
+ override def phaseName: String = "transformationOrder2"
+ override def transformValDef(tree: tpd.ValDef)(implicit ctx: Context, info: TransformerInfo): tpd.ValDef = {
+ Assert.assertTrue("transformation of children succeeded",
+ tree.rhs.toString == "Literal(Constant(2))"
+ )
+ tpd.cpy.ValDef(tree)(rhs = tpd.Literal(Constant(3)))
+ }
+
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ val transformer = new TreeTransformer {
+ override def miniPhases = Array(new Transformation1, new Transformation2)
+
+ override def phaseName: String = "test"
+ }
+ val tr = transformer.macroTransform(tree).toString
+
+ Assert.assertTrue("node can rewrite children",
+ tr.contains("Literal(Constant(3))")
+ )
+ }
+
+ @Test
+ def invocationCount = checkCompile("frontend", "class A{ val d = 1}") {
+ (tree, context) =>
+ implicit val ctx = context
+ var transformed1 = 0
+ class Transformation1 extends MiniPhaseTransform {
+ override def phaseName: String = "invocationCount1"
+ override def transformLiteral(tree: tpd.Literal)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
+ transformed1 += 1
+ Assert.assertTrue("correct constant",
+ tree.const.toString == "Constant(1)"
+ )
+ tpd.cpy.Literal(tree)(Constant(-1))
+ }
+
+ override def transformValDef(tree: tpd.ValDef)(implicit ctx: Context, info: TransformerInfo) = {
+ transformed1 += 1
+ Assert.assertTrue("transformation of children succeeded",
+ tree.rhs.toString == "Literal(Constant(-3))"
+ )
+ tpd.cpy.ValDef(tree)(rhs = transformFollowing(tpd.Literal(Constant(2))))
+ }
+
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ var transformed2 = 0
+ class Transformation2 extends MiniPhaseTransform {
+ var constantsSeen = 0
+ override def phaseName: String = "invocationCount2"
+ override def transformLiteral(tree: tpd.Literal)(implicit ctx: Context, info: TransformerInfo): tpd.Tree = {
+ transformed2 += 1
+ constantsSeen match {
+ case 0 =>
+ Assert.assertTrue("correct constant",
+ tree.const.toString == "Constant(-1)"
+ )
+ case 1 =>
+ Assert.assertTrue("correct constant",
+ tree.const.toString == "Constant(2)"
+ )
+ case _ => Assert.fail("to many constants seen")
+ }
+ constantsSeen += 1
+ tpd.cpy.Literal(tree)(Constant(-3))
+ }
+
+ override def transformValDef(tree: tpd.ValDef)(implicit ctx: Context, info: TransformerInfo) = {
+ transformed2 += 1
+ Assert.assertTrue("transformation of children succeeded",
+ tree.rhs.toString == "Literal(Constant(-3))"
+ )
+ transformFollowing(tpd.cpy.ValDef(tree)(rhs = tpd.Literal(Constant(3))))
+ }
+
+ init(ctx, ctx.period.firstPhaseId, ctx.period.lastPhaseId)
+ }
+ val transformer = new TreeTransformer {
+ override def miniPhases = Array(new Transformation1, new Transformation2)
+
+ override def phaseName: String = "test"
+ }
+ val tr = transformer.macroTransform(tree).toString
+ Assert.assertTrue("transformations aren't invoked multiple times",
+ transformed1 == 2 && transformed2 == 3
+ )
+ }
+}