Skip to content

Commit 30bebc3

Browse files
authored
Merge pull request #9036 from rethab/i8358/fix-repl-resource-leak
fix resoruce leaks in tests and REPL, fixes #8358
2 parents a3e69f1 + 8a58598 commit 30bebc3

File tree

10 files changed

+54
-56
lines changed

10 files changed

+54
-56
lines changed

bench-run/src/main/scala/dotty/tools/benchmarks/Main.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.openjdk.jmh.runner.options._
88
import java.util.concurrent.TimeUnit
99

1010
import scala.io.Source
11+
import scala.util.Using
1112

1213
object Bench {
1314
def main(args: Array[String]): Unit = {
@@ -57,7 +58,7 @@ object Bench {
5758
}
5859

5960
def paramsFromFile(file: String): Array[(String, Array[String])] = {
60-
Source.fromFile(file).getLines.toArray.map { l =>
61+
Using(Source.fromFile(file))(_.getLines.toArray).get.map { l =>
6162
val Array(param, values) = l split ':'
6263
(param, values split ',')
6364
}

bench/src/main/scala/Benchmarks.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import java.util.concurrent.TimeUnit
1515
import java.io.{File, FileOutputStream, BufferedWriter, FileWriter}
1616
import scala.collection.JavaConverters._
1717
import scala.io.Source
18+
import scala.util.Using
1819

1920
object Bench {
2021
val COMPILE_OPTS_FILE = "compile.txt"
@@ -77,7 +78,7 @@ object Bench {
7778
}
7879

7980
def readCompileOptions: Seq[String] =
80-
Source.fromFile(COMPILE_OPTS_FILE).getLines.toSeq
81+
Using(Source.fromFile(COMPILE_OPTS_FILE))(_.getLines.toSeq).get
8182
}
8283

8384
@State(Scope.Benchmark)

bin/test/TestScripts.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.junit.{Before, After, Test}
55

66
import scala.io.Source
77
import scala.sys.process.{Process, ProcessLogger}
8+
import scala.util.Using
89
import java.io.{File => JFile, FileNotFoundException}
910

1011
class TestScripts {
@@ -29,7 +30,7 @@ class TestScripts {
2930

3031
private def deletePackages: Unit = {
3132
try {
32-
for (jar <- Source.fromFile("./.packages").getLines())
33+
for (jar <- Using(Source.fromFile("./.packages"))(_.getLines().toList).get)
3334
delete(jar)
3435

3536
delete("./.packages")

compiler/src/dotty/tools/repl/ReplDriver.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import org.jline.reader._
2525

2626
import scala.annotation.tailrec
2727
import scala.collection.JavaConverters._
28+
import scala.util.Using
2829

2930
/** The state of the REPL contains necessary bindings instead of having to have
3031
* mutation
@@ -363,7 +364,7 @@ class ReplDriver(settings: Array[String],
363364
case Load(path) =>
364365
val file = new JFile(path)
365366
if (file.exists) {
366-
val contents = scala.io.Source.fromFile(file, "UTF-8").mkString
367+
val contents = Using(scala.io.Source.fromFile(file, "UTF-8"))(_.mkString).get
367368
run(contents)
368369
}
369370
else {

compiler/test/debug/Gen.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import scala.io.Source
22
import scala.collection.mutable.ListBuffer
3+
import scala.util.Using
34

45
/** Automate testing debuggability of generated code using JDB and expect
56
*
@@ -64,7 +65,7 @@ object Gen {
6465
}
6566

6667
def parse(file: String): Program = {
67-
val lines = Source.fromFile(file).getLines().toBuffer
68+
val lines = Using(Source.fromFile(file))(_.getLines().toBuffer).get
6869

6970
val breaks = new ListBuffer[Break]()
7071
val cmds = new ListBuffer[Command]()

compiler/test/dotty/tools/repl/ReplTest.scala

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import vulpix.TestConfiguration
66
import java.lang.System.{lineSeparator => EOL}
77
import java.io.{ByteArrayOutputStream, File => JFile, PrintStream}
88
import scala.io.Source
9+
import scala.util.Using
910

1011
import scala.collection.mutable.ArrayBuffer
1112

@@ -56,26 +57,8 @@ class ReplTest(withStaging: Boolean = false, out: ByteArrayOutputStream = new By
5657

5758
def testFile(f: JFile): Unit = {
5859
val prompt = "scala>"
59-
val lines = Source.fromFile(f, "UTF-8").getLines().buffered
6060

61-
assert(lines.head.startsWith(prompt),
62-
s"""Each file has to start with the prompt: "$prompt"""")
63-
64-
def extractInputs(prompt: String): List[String] = {
65-
val input = lines.next()
66-
67-
if (!input.startsWith(prompt)) extractInputs(prompt)
68-
else if (lines.hasNext) {
69-
// read lines and strip trailing whitespace:
70-
while (lines.hasNext && !lines.head.startsWith(prompt))
71-
lines.next()
72-
73-
input :: { if (lines.hasNext) extractInputs(prompt) else Nil }
74-
}
75-
else Nil
76-
}
77-
78-
def evaluate(state: State, input: String, prompt: String) =
61+
def evaluate(state: State, input: String) =
7962
try {
8063
val nstate = run(input.drop(prompt.length))(state)
8164
val out = input + EOL + storedOutput()
@@ -94,13 +77,18 @@ class ReplTest(withStaging: Boolean = false, out: ByteArrayOutputStream = new By
9477
}
9578

9679
val expectedOutput =
97-
Source.fromFile(f, "UTF-8").getLines().flatMap(filterEmpties).mkString(EOL)
80+
Using(Source.fromFile(f, "UTF-8"))(_.getLines().flatMap(filterEmpties).mkString(EOL)).get
9881
val actualOutput = {
9982
resetToInitial()
100-
val inputRes = extractInputs(prompt)
83+
84+
val lines = Using(Source.fromFile(f, "UTF-8"))(_.getLines.toList).get
85+
assert(lines.head.startsWith(prompt),
86+
s"""Each file has to start with the prompt: "$prompt"""")
87+
val inputRes = lines.filter(_.startsWith(prompt))
88+
10189
val buf = new ArrayBuffer[String]
10290
inputRes.foldLeft(initialState) { (state, input) =>
103-
val (out, nstate) = evaluate(state, input, prompt)
91+
val (out, nstate) = evaluate(state, input)
10492
buf.append(out)
10593

10694
assert(out.endsWith("\n"),

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package dotty.tools.vulpix
22

33
import scala.io.Source
4+
import scala.util.Using
45
import java.io.File
56
import java.lang.System.{lineSeparator => EOL}
67
import java.nio.file.{Files, Paths}
@@ -17,7 +18,7 @@ object FileDiff {
1718
def check(sourceTitle: String, outputLines: Seq[String], checkFile: String): Option[String] = {
1819
val checkLines =
1920
if (!(new File(checkFile)).exists) Nil
20-
else Source.fromFile(checkFile, "UTF-8").getLines().toList
21+
else Using(Source.fromFile(checkFile, "UTF-8"))(_.getLines().toList).get
2122

2223
def linesMatch =
2324
outputLines.length == checkLines.length &&

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

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import java.util.concurrent.{TimeUnit, TimeoutException, Executors => JExecutors
1212

1313
import scala.collection.mutable
1414
import scala.io.Source
15-
import scala.util.{Random, Try, Failure => TryFailure, Success => TrySuccess}
15+
import scala.util.{Random, Try, Failure => TryFailure, Success => TrySuccess, Using}
1616
import scala.util.control.NonFatal
1717
import scala.util.matching.Regex
1818

@@ -654,33 +654,35 @@ trait ParallelTesting extends RunnerOrchestration { self =>
654654
val errorMap = new HashMap[String, Integer]()
655655
var expectedErrors = 0
656656
files.filter(_.getName.endsWith(".scala")).foreach { file =>
657-
Source.fromFile(file, "UTF-8").getLines().zipWithIndex.foreach { case (line, lineNbr) =>
658-
val errors = line.toSeq.sliding("// error".length).count(_.unwrap == "// error")
659-
if (errors > 0)
660-
errorMap.put(s"${file.getPath}:$lineNbr", errors)
661-
662-
val noposErrors = line.toSeq.sliding("// nopos-error".length).count(_.unwrap == "// nopos-error")
663-
if (noposErrors > 0) {
664-
val nopos = errorMap.get("nopos")
665-
val existing: Integer = if (nopos eq null) 0 else nopos
666-
errorMap.put("nopos", noposErrors + existing)
667-
}
657+
Using(Source.fromFile(file, "UTF-8")) { source =>
658+
source.getLines.zipWithIndex.foreach { case (line, lineNbr) =>
659+
val errors = line.toSeq.sliding("// error".length).count(_.unwrap == "// error")
660+
if (errors > 0)
661+
errorMap.put(s"${file.getPath}:$lineNbr", errors)
662+
663+
val noposErrors = line.toSeq.sliding("// nopos-error".length).count(_.unwrap == "// nopos-error")
664+
if (noposErrors > 0) {
665+
val nopos = errorMap.get("nopos")
666+
val existing: Integer = if (nopos eq null) 0 else nopos
667+
errorMap.put("nopos", noposErrors + existing)
668+
}
668669

669-
val anyposErrors = line.toSeq.sliding("// anypos-error".length).count(_.unwrap == "// anypos-error")
670-
if (anyposErrors > 0) {
671-
val anypos = errorMap.get("anypos")
672-
val existing: Integer = if (anypos eq null) 0 else anypos
673-
errorMap.put("anypos", anyposErrors + existing)
674-
}
670+
val anyposErrors = line.toSeq.sliding("// anypos-error".length).count(_.unwrap == "// anypos-error")
671+
if (anyposErrors > 0) {
672+
val anypos = errorMap.get("anypos")
673+
val existing: Integer = if (anypos eq null) 0 else anypos
674+
errorMap.put("anypos", anyposErrors + existing)
675+
}
675676

676-
val possibleTypos = List("//error" -> "// error", "//nopos-error" -> "// nopos-error", "//anypos-error" -> "// anypos-error")
677-
for ((possibleTypo, expected) <- possibleTypos) {
678-
if (line.contains(possibleTypo))
679-
echo(s"Warning: Possible typo in error tag in file ${file.getCanonicalPath}:$lineNbr: found `$possibleTypo` but expected `$expected`")
680-
}
677+
val possibleTypos = List("//error" -> "// error", "//nopos-error" -> "// nopos-error", "//anypos-error" -> "// anypos-error")
678+
for ((possibleTypo, expected) <- possibleTypos) {
679+
if (line.contains(possibleTypo))
680+
echo(s"Warning: Possible typo in error tag in file ${file.getCanonicalPath}:$lineNbr: found `$possibleTypo` but expected `$expected`")
681+
}
681682

682-
expectedErrors += anyposErrors + noposErrors + errors
683-
}
683+
expectedErrors += anyposErrors + noposErrors + errors
684+
}
685+
}.get
684686
}
685687

686688
(errorMap, expectedErrors)

doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import dotc.core.Contexts.Context
2525
import dotc.util.SourceFile
2626
import model.Package
2727
import scala.io.{ Codec, Source }
28+
import scala.util.Using
2829
import io.{ AbstractFile, VirtualFile, File }
2930
import scala.collection.mutable.ArrayBuffer
3031
import util.syntax._
@@ -90,7 +91,7 @@ case class Site(
9091
root
9192
.listFiles
9293
.find(_.getName == "sidebar.yml")
93-
.map("---\n" + Source.fromFile(_).mkString + "\n---")
94+
.map(f => "---\n" + Using(Source.fromFile(f))(_.mkString).get + "\n---")
9495
.map(Yaml.apply)
9596
.flatMap(Sidebar.apply)
9697
.getOrElse(Sidebar.empty)
@@ -412,7 +413,7 @@ case class Site(
412413
}
413414

414415
private def toSourceFile(f: JFile): SourceFile =
415-
new SourceFile(AbstractFile.getFile(new File(f.toPath)), Source.fromFile(f, "UTF-8").toArray)
416+
new SourceFile(AbstractFile.getFile(new File(f.toPath)), Using(Source.fromFile(f, "UTF-8"))(_.toArray).get)
416417

417418
private def collectFiles(dir: JFile, includes: String => Boolean): Array[JFile] =
418419
dir

tests/run-custom-args/tasty-interpreter/Test.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import dotty.tools.dotc.util.DiffUtil
77
import dotty.tools.io.Path
88

99
import scala.io.Source
10+
import scala.util.Using
1011
import scala.tasty.interpreter.TastyInterpreter
1112

1213
object Test {
@@ -89,7 +90,7 @@ object Test {
8990

9091
val checkFile = java.nio.file.Paths.get("tests/run/" + testFileName.stripSuffix(".scala") + ".check")
9192
if (java.nio.file.Files.exists(checkFile)) {
92-
val expectedOutput = Source.fromFile(checkFile.toFile).getLines().mkString("", "\n", "\n")
93+
val expectedOutput = Using(Source.fromFile(checkFile.toFile))(_.getLines().mkString("", "\n", "\n")).get
9394

9495
assert(expectedOutput == actualOutput,
9596
"\n>>>>>>>>>>>>>>>>>>\n" +

0 commit comments

Comments
 (0)