Skip to content

Commit af37d64

Browse files
authored
Merge pull request #9037 from dotty-staging/test-rewrites
Test rewrites
2 parents ec621f0 + 66c390f commit af37d64

File tree

6 files changed

+72
-16
lines changed

6 files changed

+72
-16
lines changed

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ object Scanners {
197197
val noindentSyntax =
198198
ctx.settings.noindent.value
199199
|| ctx.settings.oldSyntax.value
200-
|| migrateTo3
200+
|| (migrateTo3 && !ctx.settings.indent.value)
201201
val indentSyntax =
202202
((if (Config.defaultIndent) !noindentSyntax else ctx.settings.indent.value)
203203
|| rewriteNoIndent)

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ class CompilationTests extends ParallelTesting {
3737
implicit val testGroup: TestGroup = TestGroup("compilePos")
3838
aggregateTests(
3939
compileFile("tests/pos/nullarify.scala", defaultOptions.and("-Ycheck:nullarify")),
40-
compileFile("tests/pos-scala2/rewrites.scala", scala2CompatMode.and("-rewrite")).copyToTarget(),
4140
compileFile("tests/pos-special/utf8encoded.scala", explicitUTF8),
4241
compileFile("tests/pos-special/utf16encoded.scala", explicitUTF16),
4342
compileFilesInDir("tests/pos-special/sourcepath/outer", defaultOptions.and("-sourcepath", "tests/pos-special/sourcepath")),
@@ -68,6 +67,15 @@ class CompilationTests extends ParallelTesting {
6867
).checkCompile()
6968
}
7069

70+
@Test def rewrites: Unit = {
71+
implicit val testGroup: TestGroup = TestGroup("rewrites")
72+
73+
aggregateTests(
74+
compileFile("tests/rewrites/rewrites.scala", scala2CompatMode.and("-rewrite", "-indent")),
75+
compileFile("tests/rewrites/i8982.scala", defaultOptions.and("-indent", "-rewrite"))
76+
).checkRewrites()
77+
}
78+
7179
@Test def posTwice: Unit = {
7280
implicit val testGroup: TestGroup = TestGroup("posTwice")
7381
aggregateTests(

compiler/test/dotty/tools/vulpix/ParallelTesting.scala

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ trait ParallelTesting extends RunnerOrchestration { self =>
8181
else self
8282
}
8383

84+
def withoutFlags(flags1: String*): TestSource = self match {
85+
case self: JointCompilationSource =>
86+
self.copy(flags = flags.without(flags1: _*))
87+
case self: SeparateCompilationSource =>
88+
self.copy(flags = flags.without(flags1: _*))
89+
}
90+
8491
/** Generate the instructions to redo the test from the command line */
8592
def buildInstructions(errors: Int, warnings: Int): String = {
8693
val sb = new StringBuilder
@@ -582,6 +589,24 @@ trait ParallelTesting extends RunnerOrchestration { self =>
582589
private final class PosTest(testSources: List[TestSource], times: Int, threadLimit: Option[Int], suppressAllOutput: Boolean)(implicit summaryReport: SummaryReporting)
583590
extends Test(testSources, times, threadLimit, suppressAllOutput)
584591

592+
private final class RewriteTest(testSources: List[TestSource], checkFiles: Map[JFile, JFile], times: Int, threadLimit: Option[Int], suppressAllOutput: Boolean)(implicit summaryReport: SummaryReporting)
593+
extends Test(testSources, times, threadLimit, suppressAllOutput) {
594+
private def verifyOutput(testSource: TestSource, reporters: Seq[TestReporter], logger: LoggedRunnable) = {
595+
testSource.sourceFiles.foreach { file =>
596+
if checkFiles.contains(file) then
597+
val checkFile = checkFiles(file)
598+
val actual = Source.fromFile(file, "UTF-8").getLines().toList
599+
diffTest(testSource, checkFile, actual, reporters, logger)
600+
}
601+
602+
// check that the rewritten code compiles
603+
new CompilationTest(testSource).checkCompile()
604+
}
605+
606+
override def onSuccess(testSource: TestSource, reporters: Seq[TestReporter], logger: LoggedRunnable) =
607+
verifyOutput(testSource, reporters, logger)
608+
}
609+
585610
private final class RunTest(testSources: List[TestSource], times: Int, threadLimit: Option[Int], suppressAllOutput: Boolean)(implicit summaryReport: SummaryReporting)
586611
extends Test(testSources, times, threadLimit, suppressAllOutput) {
587612
private var didAddNoRunWarning = false
@@ -917,6 +942,33 @@ trait ParallelTesting extends RunnerOrchestration { self =>
917942
this
918943
}
919944

945+
/** Tests `-rewrite`, which makes sure that the rewritten files still compile
946+
* and agree with the expected result (if specified).
947+
*
948+
* Check files are only supported for joint compilation sources.
949+
*/
950+
def checkRewrites()(implicit summaryReport: SummaryReporting): this.type = {
951+
// use the original check file, to simplify update of check files
952+
var checkFileMap = Map.empty[JFile, JFile]
953+
954+
// copy source file to targets, as they will be changed
955+
val copiedTargets = targets.map {
956+
case target @ JointCompilationSource(_, files, _, outDir, _, _) =>
957+
val files2 = files.map { f =>
958+
val dest = copyToDir(outDir, f)
959+
val checkFile = new JFile(f.getPath.replaceFirst("\\.scala$", ".check"))
960+
if (checkFile.exists) checkFileMap = checkFileMap.updated(dest, checkFile)
961+
dest
962+
}
963+
target.copy(files = files2)
964+
case target @ SeparateCompilationSource(_, dir, _, outDir) =>
965+
target.copy(dir = copyToDir(outDir, dir))
966+
}
967+
968+
val test = new RewriteTest(copiedTargets, checkFileMap, times, threadLimit, shouldFail || shouldSuppressOutput).executeTestSuite()
969+
this
970+
}
971+
920972
/** Deletes output directories and files */
921973
private def cleanup(): this.type = {
922974
if (shouldDelete) delete()
@@ -947,20 +999,6 @@ trait ParallelTesting extends RunnerOrchestration { self =>
947999
target.toFile
9481000
}
9491001

950-
/** Builds a new `CompilationTest` where we have copied the target files to
951-
* the out directory. This is needed for tests that modify the original
952-
* source, such as `-rewrite` tests
953-
*/
954-
def copyToTarget(): CompilationTest = new CompilationTest (
955-
targets.map {
956-
case target @ JointCompilationSource(_, files, _, outDir, _, _) =>
957-
target.copy(files = files.map(copyToDir(outDir,_)))
958-
case target @ SeparateCompilationSource(_, dir, _, outDir) =>
959-
target.copy(dir = copyToDir(outDir, dir))
960-
},
961-
times, shouldDelete, threadLimit, shouldFail, shouldSuppressOutput
962-
)
963-
9641002
/** Builds a `CompilationTest` which performs the compilation `i` times on
9651003
* each target
9661004
*/

tests/rewrites/i8982.check

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
object Foo:
3+
def bar(x: Int): Unit =
4+
println(x)
5+
6+
class Baz(n: Int):
7+
def printRepeat(repeat: Int) =
8+
for {
9+
x <- 1 to repeat
10+
} println(s"$x - ${n * x}")
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)