diff --git a/compiler/src/dotty/tools/dotc/parsing/Scanners.scala b/compiler/src/dotty/tools/dotc/parsing/Scanners.scala index 55d99b0bf0da..70cf1213c67d 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Scanners.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Scanners.scala @@ -197,7 +197,7 @@ object Scanners { val noindentSyntax = ctx.settings.noindent.value || ctx.settings.oldSyntax.value - || migrateTo3 + || (migrateTo3 && !ctx.settings.indent.value) val indentSyntax = ((if (Config.defaultIndent) !noindentSyntax else ctx.settings.indent.value) || rewriteNoIndent) diff --git a/compiler/test/dotty/tools/dotc/CompilationTests.scala b/compiler/test/dotty/tools/dotc/CompilationTests.scala index c88bfcf9a06d..aeaeda4c5745 100644 --- a/compiler/test/dotty/tools/dotc/CompilationTests.scala +++ b/compiler/test/dotty/tools/dotc/CompilationTests.scala @@ -37,7 +37,6 @@ class CompilationTests extends ParallelTesting { implicit val testGroup: TestGroup = TestGroup("compilePos") aggregateTests( compileFile("tests/pos/nullarify.scala", defaultOptions.and("-Ycheck:nullarify")), - compileFile("tests/pos-scala2/rewrites.scala", scala2CompatMode.and("-rewrite")).copyToTarget(), compileFile("tests/pos-special/utf8encoded.scala", explicitUTF8), compileFile("tests/pos-special/utf16encoded.scala", explicitUTF16), compileFilesInDir("tests/pos-special/sourcepath/outer", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")), @@ -68,6 +67,15 @@ class CompilationTests extends ParallelTesting { ).checkCompile() } + @Test def rewrites: Unit = { + implicit val testGroup: TestGroup = TestGroup("rewrites") + + aggregateTests( + compileFile("tests/rewrites/rewrites.scala", scala2CompatMode.and("-rewrite", "-indent")), + compileFile("tests/rewrites/i8982.scala", defaultOptions.and("-indent", "-rewrite")) + ).checkRewrites() + } + @Test def posTwice: Unit = { implicit val testGroup: TestGroup = TestGroup("posTwice") aggregateTests( diff --git a/compiler/test/dotty/tools/vulpix/ParallelTesting.scala b/compiler/test/dotty/tools/vulpix/ParallelTesting.scala index 875d6fe11f1f..30c0df405054 100644 --- a/compiler/test/dotty/tools/vulpix/ParallelTesting.scala +++ b/compiler/test/dotty/tools/vulpix/ParallelTesting.scala @@ -81,6 +81,13 @@ trait ParallelTesting extends RunnerOrchestration { self => else self } + def withoutFlags(flags1: String*): TestSource = self match { + case self: JointCompilationSource => + self.copy(flags = flags.without(flags1: _*)) + case self: SeparateCompilationSource => + self.copy(flags = flags.without(flags1: _*)) + } + /** Generate the instructions to redo the test from the command line */ def buildInstructions(errors: Int, warnings: Int): String = { val sb = new StringBuilder @@ -582,6 +589,24 @@ trait ParallelTesting extends RunnerOrchestration { self => private final class PosTest(testSources: List[TestSource], times: Int, threadLimit: Option[Int], suppressAllOutput: Boolean)(implicit summaryReport: SummaryReporting) extends Test(testSources, times, threadLimit, suppressAllOutput) + private final class RewriteTest(testSources: List[TestSource], checkFiles: Map[JFile, JFile], times: Int, threadLimit: Option[Int], suppressAllOutput: Boolean)(implicit summaryReport: SummaryReporting) + extends Test(testSources, times, threadLimit, suppressAllOutput) { + private def verifyOutput(testSource: TestSource, reporters: Seq[TestReporter], logger: LoggedRunnable) = { + testSource.sourceFiles.foreach { file => + if checkFiles.contains(file) then + val checkFile = checkFiles(file) + val actual = Source.fromFile(file, "UTF-8").getLines().toList + diffTest(testSource, checkFile, actual, reporters, logger) + } + + // check that the rewritten code compiles + new CompilationTest(testSource).checkCompile() + } + + override def onSuccess(testSource: TestSource, reporters: Seq[TestReporter], logger: LoggedRunnable) = + verifyOutput(testSource, reporters, logger) + } + private final class RunTest(testSources: List[TestSource], times: Int, threadLimit: Option[Int], suppressAllOutput: Boolean)(implicit summaryReport: SummaryReporting) extends Test(testSources, times, threadLimit, suppressAllOutput) { private var didAddNoRunWarning = false @@ -917,6 +942,33 @@ trait ParallelTesting extends RunnerOrchestration { self => this } + /** Tests `-rewrite`, which makes sure that the rewritten files still compile + * and agree with the expected result (if specified). + * + * Check files are only supported for joint compilation sources. + */ + def checkRewrites()(implicit summaryReport: SummaryReporting): this.type = { + // use the original check file, to simplify update of check files + var checkFileMap = Map.empty[JFile, JFile] + + // copy source file to targets, as they will be changed + val copiedTargets = targets.map { + case target @ JointCompilationSource(_, files, _, outDir, _, _) => + val files2 = files.map { f => + val dest = copyToDir(outDir, f) + val checkFile = new JFile(f.getPath.replaceFirst("\\.scala$", ".check")) + if (checkFile.exists) checkFileMap = checkFileMap.updated(dest, checkFile) + dest + } + target.copy(files = files2) + case target @ SeparateCompilationSource(_, dir, _, outDir) => + target.copy(dir = copyToDir(outDir, dir)) + } + + val test = new RewriteTest(copiedTargets, checkFileMap, times, threadLimit, shouldFail || shouldSuppressOutput).executeTestSuite() + this + } + /** Deletes output directories and files */ private def cleanup(): this.type = { if (shouldDelete) delete() @@ -947,20 +999,6 @@ trait ParallelTesting extends RunnerOrchestration { self => target.toFile } - /** Builds a new `CompilationTest` where we have copied the target files to - * the out directory. This is needed for tests that modify the original - * source, such as `-rewrite` tests - */ - def copyToTarget(): CompilationTest = new CompilationTest ( - targets.map { - case target @ JointCompilationSource(_, files, _, outDir, _, _) => - target.copy(files = files.map(copyToDir(outDir,_))) - case target @ SeparateCompilationSource(_, dir, _, outDir) => - target.copy(dir = copyToDir(outDir, dir)) - }, - times, shouldDelete, threadLimit, shouldFail, shouldSuppressOutput - ) - /** Builds a `CompilationTest` which performs the compilation `i` times on * each target */ diff --git a/tests/rewrites/i8982.check b/tests/rewrites/i8982.check new file mode 100644 index 000000000000..c78e77e66c2e --- /dev/null +++ b/tests/rewrites/i8982.check @@ -0,0 +1,10 @@ + +object Foo: + def bar(x: Int): Unit = + println(x) + +class Baz(n: Int): + def printRepeat(repeat: Int) = + for { + x <- 1 to repeat + } println(s"$x - ${n * x}") diff --git a/tests/pos/i8982.scala b/tests/rewrites/i8982.scala similarity index 100% rename from tests/pos/i8982.scala rename to tests/rewrites/i8982.scala diff --git a/tests/pos-scala2/rewrites.scala b/tests/rewrites/rewrites.scala similarity index 100% rename from tests/pos-scala2/rewrites.scala rename to tests/rewrites/rewrites.scala