Skip to content

Test rewrites #9037

New issue

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

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

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
May 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/parsing/Scanners.scala
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ object Scanners {
val noindentSyntax =
ctx.settings.noindent.value
|| ctx.settings.oldSyntax.value
|| migrateTo3
Copy link
Member

Choose a reason for hiding this comment

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

I think having -source 3.0-migrate imply -noindent is intentional: it should allow existing weirdly formatted scala 2 code to compile /cc @odersky.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The issue is that if a user run compiler with -rewrite, then it's impossible to compile the code again.

It's discovered when we actually compile the rewritten code in the following test:

compileFile("tests/rewrites/rewrites.scala", scala2CompatMode.and("-rewrite", "-indent"))

Copy link
Member

@smarter smarter May 26, 2020

Choose a reason for hiding this comment

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

In that example I think -indent needs to take precedence over migrateTo3 and force indentation to be on.

|| (migrateTo3 && !ctx.settings.indent.value)
val indentSyntax =
((if (Config.defaultIndent) !noindentSyntax else ctx.settings.indent.value)
|| rewriteNoIndent)
Expand Down
10 changes: 9 additions & 1 deletion compiler/test/dotty/tools/dotc/CompilationTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")),
Expand Down Expand Up @@ -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(
Expand Down
66 changes: 52 additions & 14 deletions compiler/test/dotty/tools/vulpix/ParallelTesting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
*/
Expand Down
10 changes: 10 additions & 0 deletions tests/rewrites/i8982.check
Original file line number Diff line number Diff line change
@@ -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}")
File renamed without changes.
File renamed without changes.