diff --git a/bench-run/src/main/scala/dotty/tools/benchmarks/Main.scala b/bench-run/src/main/scala/dotty/tools/benchmarks/Main.scala index cf85f1d99835..ac26e57c7381 100644 --- a/bench-run/src/main/scala/dotty/tools/benchmarks/Main.scala +++ b/bench-run/src/main/scala/dotty/tools/benchmarks/Main.scala @@ -8,6 +8,7 @@ import org.openjdk.jmh.runner.options._ import java.util.concurrent.TimeUnit import scala.io.Source +import scala.util.Using object Bench { def main(args: Array[String]): Unit = { @@ -57,7 +58,7 @@ object Bench { } def paramsFromFile(file: String): Array[(String, Array[String])] = { - Source.fromFile(file).getLines.toArray.map { l => + Using(Source.fromFile(file))(_.getLines.toArray).get.map { l => val Array(param, values) = l split ':' (param, values split ',') } diff --git a/bench/src/main/scala/Benchmarks.scala b/bench/src/main/scala/Benchmarks.scala index 779579cd1c6b..4c4ffca87810 100644 --- a/bench/src/main/scala/Benchmarks.scala +++ b/bench/src/main/scala/Benchmarks.scala @@ -15,6 +15,7 @@ import java.util.concurrent.TimeUnit import java.io.{File, FileOutputStream, BufferedWriter, FileWriter} import scala.collection.JavaConverters._ import scala.io.Source +import scala.util.Using object Bench { val COMPILE_OPTS_FILE = "compile.txt" @@ -77,7 +78,7 @@ object Bench { } def readCompileOptions: Seq[String] = - Source.fromFile(COMPILE_OPTS_FILE).getLines.toSeq + Using(Source.fromFile(COMPILE_OPTS_FILE))(_.getLines.toSeq).get } @State(Scope.Benchmark) diff --git a/bin/test/TestScripts.scala b/bin/test/TestScripts.scala index be0633450e7f..f3eab3607797 100644 --- a/bin/test/TestScripts.scala +++ b/bin/test/TestScripts.scala @@ -5,6 +5,7 @@ import org.junit.{Before, After, Test} import scala.io.Source import scala.sys.process.{Process, ProcessLogger} +import scala.util.Using import java.io.{File => JFile, FileNotFoundException} class TestScripts { @@ -29,7 +30,7 @@ class TestScripts { private def deletePackages: Unit = { try { - for (jar <- Source.fromFile("./.packages").getLines()) + for (jar <- Using(Source.fromFile("./.packages"))(_.getLines().toList).get) delete(jar) delete("./.packages") diff --git a/compiler/src/dotty/tools/repl/ReplDriver.scala b/compiler/src/dotty/tools/repl/ReplDriver.scala index cb6276775e8e..bdd4ddef6af5 100644 --- a/compiler/src/dotty/tools/repl/ReplDriver.scala +++ b/compiler/src/dotty/tools/repl/ReplDriver.scala @@ -25,6 +25,7 @@ import org.jline.reader._ import scala.annotation.tailrec import scala.collection.JavaConverters._ +import scala.util.Using /** The state of the REPL contains necessary bindings instead of having to have * mutation @@ -351,7 +352,7 @@ class ReplDriver(settings: Array[String], case Load(path) => val file = new JFile(path) if (file.exists) { - val contents = scala.io.Source.fromFile(file, "UTF-8").mkString + val contents = Using(scala.io.Source.fromFile(file, "UTF-8"))(_.mkString).get run(contents) } else { diff --git a/compiler/test/debug/Gen.scala b/compiler/test/debug/Gen.scala index 46a047d20d83..4818176275f5 100755 --- a/compiler/test/debug/Gen.scala +++ b/compiler/test/debug/Gen.scala @@ -1,5 +1,6 @@ import scala.io.Source import scala.collection.mutable.ListBuffer +import scala.util.Using /** Automate testing debuggability of generated code using JDB and expect * @@ -64,7 +65,7 @@ object Gen { } def parse(file: String): Program = { - val lines = Source.fromFile(file).getLines().toBuffer + val lines = Using(Source.fromFile(file))(_.getLines().toBuffer).get val breaks = new ListBuffer[Break]() val cmds = new ListBuffer[Command]() diff --git a/compiler/test/dotty/tools/repl/ReplTest.scala b/compiler/test/dotty/tools/repl/ReplTest.scala index eb2a19a9cfb6..de1e67861fac 100644 --- a/compiler/test/dotty/tools/repl/ReplTest.scala +++ b/compiler/test/dotty/tools/repl/ReplTest.scala @@ -6,6 +6,7 @@ import vulpix.TestConfiguration import java.lang.System.{lineSeparator => EOL} import java.io.{ByteArrayOutputStream, File => JFile, PrintStream} import scala.io.Source +import scala.util.Using import scala.collection.mutable.ArrayBuffer @@ -56,26 +57,8 @@ class ReplTest(withStaging: Boolean = false, out: ByteArrayOutputStream = new By def testFile(f: JFile): Unit = { val prompt = "scala>" - val lines = Source.fromFile(f, "UTF-8").getLines().buffered - assert(lines.head.startsWith(prompt), - s"""Each file has to start with the prompt: "$prompt"""") - - def extractInputs(prompt: String): List[String] = { - val input = lines.next() - - if (!input.startsWith(prompt)) extractInputs(prompt) - else if (lines.hasNext) { - // read lines and strip trailing whitespace: - while (lines.hasNext && !lines.head.startsWith(prompt)) - lines.next() - - input :: { if (lines.hasNext) extractInputs(prompt) else Nil } - } - else Nil - } - - def evaluate(state: State, input: String, prompt: String) = + def evaluate(state: State, input: String) = try { val nstate = run(input.drop(prompt.length))(state) val out = input + EOL + storedOutput() @@ -94,13 +77,18 @@ class ReplTest(withStaging: Boolean = false, out: ByteArrayOutputStream = new By } val expectedOutput = - Source.fromFile(f, "UTF-8").getLines().flatMap(filterEmpties).mkString(EOL) + Using(Source.fromFile(f, "UTF-8"))(_.getLines().flatMap(filterEmpties).mkString(EOL)).get val actualOutput = { resetToInitial() - val inputRes = extractInputs(prompt) + + val lines = Using(Source.fromFile(f, "UTF-8"))(_.getLines.toList).get + assert(lines.head.startsWith(prompt), + s"""Each file has to start with the prompt: "$prompt"""") + val inputRes = lines.filter(_.startsWith(prompt)) + val buf = new ArrayBuffer[String] inputRes.foldLeft(initialState) { (state, input) => - val (out, nstate) = evaluate(state, input, prompt) + val (out, nstate) = evaluate(state, input) buf.append(out) assert(out.endsWith("\n"), diff --git a/compiler/test/dotty/tools/vulpix/FileDiff.scala b/compiler/test/dotty/tools/vulpix/FileDiff.scala index 7eef57d4888b..bee9a28db965 100644 --- a/compiler/test/dotty/tools/vulpix/FileDiff.scala +++ b/compiler/test/dotty/tools/vulpix/FileDiff.scala @@ -1,6 +1,7 @@ package dotty.tools.vulpix import scala.io.Source +import scala.util.Using import java.io.File import java.lang.System.{lineSeparator => EOL} import java.nio.file.{Files, Paths} @@ -17,7 +18,7 @@ object FileDiff { def check(sourceTitle: String, outputLines: Seq[String], checkFile: String): Option[String] = { val checkLines = if (!(new File(checkFile)).exists) Nil - else Source.fromFile(checkFile, "UTF-8").getLines().toList + else Using(Source.fromFile(checkFile, "UTF-8"))(_.getLines().toList).get def linesMatch = outputLines.length == checkLines.length && diff --git a/compiler/test/dotty/tools/vulpix/ParallelTesting.scala b/compiler/test/dotty/tools/vulpix/ParallelTesting.scala index 995033b4c475..875d6fe11f1f 100644 --- a/compiler/test/dotty/tools/vulpix/ParallelTesting.scala +++ b/compiler/test/dotty/tools/vulpix/ParallelTesting.scala @@ -12,7 +12,7 @@ import java.util.concurrent.{TimeUnit, TimeoutException, Executors => JExecutors import scala.collection.mutable import scala.io.Source -import scala.util.{Random, Try, Failure => TryFailure, Success => TrySuccess} +import scala.util.{Random, Try, Failure => TryFailure, Success => TrySuccess, Using} import scala.util.control.NonFatal import scala.util.matching.Regex @@ -654,33 +654,35 @@ trait ParallelTesting extends RunnerOrchestration { self => val errorMap = new HashMap[String, Integer]() var expectedErrors = 0 files.filter(_.getName.endsWith(".scala")).foreach { file => - Source.fromFile(file, "UTF-8").getLines().zipWithIndex.foreach { case (line, lineNbr) => - val errors = line.toSeq.sliding("// error".length).count(_.unwrap == "// error") - if (errors > 0) - errorMap.put(s"${file.getPath}:$lineNbr", errors) - - val noposErrors = line.toSeq.sliding("// nopos-error".length).count(_.unwrap == "// nopos-error") - if (noposErrors > 0) { - val nopos = errorMap.get("nopos") - val existing: Integer = if (nopos eq null) 0 else nopos - errorMap.put("nopos", noposErrors + existing) - } + Using(Source.fromFile(file, "UTF-8")) { source => + source.getLines.zipWithIndex.foreach { case (line, lineNbr) => + val errors = line.toSeq.sliding("// error".length).count(_.unwrap == "// error") + if (errors > 0) + errorMap.put(s"${file.getPath}:$lineNbr", errors) + + val noposErrors = line.toSeq.sliding("// nopos-error".length).count(_.unwrap == "// nopos-error") + if (noposErrors > 0) { + val nopos = errorMap.get("nopos") + val existing: Integer = if (nopos eq null) 0 else nopos + errorMap.put("nopos", noposErrors + existing) + } - val anyposErrors = line.toSeq.sliding("// anypos-error".length).count(_.unwrap == "// anypos-error") - if (anyposErrors > 0) { - val anypos = errorMap.get("anypos") - val existing: Integer = if (anypos eq null) 0 else anypos - errorMap.put("anypos", anyposErrors + existing) - } + val anyposErrors = line.toSeq.sliding("// anypos-error".length).count(_.unwrap == "// anypos-error") + if (anyposErrors > 0) { + val anypos = errorMap.get("anypos") + val existing: Integer = if (anypos eq null) 0 else anypos + errorMap.put("anypos", anyposErrors + existing) + } - val possibleTypos = List("//error" -> "// error", "//nopos-error" -> "// nopos-error", "//anypos-error" -> "// anypos-error") - for ((possibleTypo, expected) <- possibleTypos) { - if (line.contains(possibleTypo)) - echo(s"Warning: Possible typo in error tag in file ${file.getCanonicalPath}:$lineNbr: found `$possibleTypo` but expected `$expected`") - } + val possibleTypos = List("//error" -> "// error", "//nopos-error" -> "// nopos-error", "//anypos-error" -> "// anypos-error") + for ((possibleTypo, expected) <- possibleTypos) { + if (line.contains(possibleTypo)) + echo(s"Warning: Possible typo in error tag in file ${file.getCanonicalPath}:$lineNbr: found `$possibleTypo` but expected `$expected`") + } - expectedErrors += anyposErrors + noposErrors + errors - } + expectedErrors += anyposErrors + noposErrors + errors + } + }.get } (errorMap, expectedErrors) diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala index 2d1e1783e99b..16cd22444787 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala @@ -25,6 +25,7 @@ import dotc.core.Contexts.Context import dotc.util.SourceFile import model.Package import scala.io.{ Codec, Source } +import scala.util.Using import io.{ AbstractFile, VirtualFile, File } import scala.collection.mutable.ArrayBuffer import util.syntax._ @@ -90,7 +91,7 @@ case class Site( root .listFiles .find(_.getName == "sidebar.yml") - .map("---\n" + Source.fromFile(_).mkString + "\n---") + .map(f => "---\n" + Using(Source.fromFile(f))(_.mkString).get + "\n---") .map(Yaml.apply) .flatMap(Sidebar.apply) .getOrElse(Sidebar.empty) @@ -412,7 +413,7 @@ case class Site( } private def toSourceFile(f: JFile): SourceFile = - new SourceFile(AbstractFile.getFile(new File(f.toPath)), Source.fromFile(f, "UTF-8").toArray) + new SourceFile(AbstractFile.getFile(new File(f.toPath)), Using(Source.fromFile(f, "UTF-8"))(_.toArray).get) private def collectFiles(dir: JFile, includes: String => Boolean): Array[JFile] = dir diff --git a/tests/run-custom-args/tasty-interpreter/Test.scala b/tests/run-custom-args/tasty-interpreter/Test.scala index 328cbe3a8958..b27293f9937b 100644 --- a/tests/run-custom-args/tasty-interpreter/Test.scala +++ b/tests/run-custom-args/tasty-interpreter/Test.scala @@ -7,6 +7,7 @@ import dotty.tools.dotc.util.DiffUtil import dotty.tools.io.Path import scala.io.Source +import scala.util.Using import scala.tasty.interpreter.TastyInterpreter object Test { @@ -89,7 +90,7 @@ object Test { val checkFile = java.nio.file.Paths.get("tests/run/" + testFileName.stripSuffix(".scala") + ".check") if (java.nio.file.Files.exists(checkFile)) { - val expectedOutput = Source.fromFile(checkFile.toFile).getLines().mkString("", "\n", "\n") + val expectedOutput = Using(Source.fromFile(checkFile.toFile))(_.getLines().mkString("", "\n", "\n")).get assert(expectedOutput == actualOutput, "\n>>>>>>>>>>>>>>>>>>\n" +