Skip to content

Commit 01c1855

Browse files
committed
fix dead code, return Either instead of throwing error
1 parent 6d06661 commit 01c1855

File tree

4 files changed

+48
-48
lines changed

4 files changed

+48
-48
lines changed

compiler/src/dotty/tools/scripting/Main.scala

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,18 @@ object Main:
3636
def main(args: Array[String]): Unit =
3737
val (compilerArgs, scriptFile, scriptArgs, saveJar, invokeFlag) = distinguishArgs(args)
3838
val driver = ScriptingDriver(compilerArgs, scriptFile, scriptArgs)
39-
try driver.compileAndRun { (outDir:Path, classpathEntries:Seq[Path], mainClass: String) =>
39+
driver.compileAndRun { (outDir:Path, classpathEntries:Seq[Path], mainClass: String) =>
4040
// write expanded classpath to java.class.path property, so called script can see it
4141
sys.props("java.class.path") = classpathEntries.map(_.toString).mkString(pathsep)
4242
if saveJar then
4343
// write a standalone jar to the script parent directory
4444
writeJarfile(outDir, scriptFile, scriptArgs, classpathEntries, mainClass)
4545
invokeFlag
46-
}
47-
catch
48-
case ScriptingException(msg) =>
49-
println(s"Error: $msg")
46+
} match
47+
case Some(ex) =>
48+
println(ex.getMessage)
5049
sys.exit(1)
51-
52-
case e: java.lang.reflect.InvocationTargetException =>
53-
throw e.getCause
50+
case _ =>
5451

5552
private def writeJarfile(outDir: Path, scriptFile: File, scriptArgs:Array[String],
5653
classpathEntries:Seq[Path], mainClassName: String): Unit =

compiler/src/dotty/tools/scripting/ScriptingDriver.scala

Lines changed: 20 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import dotty.tools.io.{ PlainDirectory, Directory, ClassPath }
1111
import Util.*
1212

1313
class ScriptingDriver(compilerArgs: Array[String], scriptFile: File, scriptArgs: Array[String]) extends Driver:
14-
def compileAndRun(pack:(Path, Seq[Path], String) => Boolean = null): Unit =
14+
def compileAndRun(pack:(Path, Seq[Path], String) => Boolean = null): Option[Throwable] =
1515
val outDir = Files.createTempDirectory("scala3-scripting")
1616
outDir.toFile.deleteOnExit()
1717
setup(compilerArgs :+ scriptFile.getAbsolutePath, initCtx.fresh) match
@@ -20,26 +20,25 @@ class ScriptingDriver(compilerArgs: Array[String], scriptFile: File, scriptArgs:
2020
new PlainDirectory(Directory(outDir)))
2121

2222
if doCompile(newCompiler, toCompile).hasErrors then
23-
throw ScriptingException("Errors encountered during compilation")
24-
25-
try
26-
val classpath = s"${ctx.settings.classpath.value}${pathsep}${sys.props("java.class.path")}"
27-
val classpathEntries: Seq[Path] = ClassPath.expandPath(classpath, expandStar=true).map { Paths.get(_) }
28-
val (mainClass, mainMethod) = detectMainClassAndMethod(outDir, classpathEntries, scriptFile.toString)
29-
val invokeMain: Boolean =
30-
Option(pack) match
31-
case Some(func) =>
32-
func(outDir, classpathEntries, mainClass)
33-
case None =>
34-
true
35-
end match
36-
if invokeMain then mainMethod.invoke(null, scriptArgs)
37-
catch
38-
case e: java.lang.reflect.InvocationTargetException =>
39-
throw e.getCause
40-
finally
41-
deleteFile(outDir.toFile)
42-
case None =>
23+
Some(ScriptingException("Errors encountered during compilation"))
24+
else
25+
try
26+
val classpath = s"${ctx.settings.classpath.value}${pathsep}${sys.props("java.class.path")}"
27+
val classpathEntries: Seq[Path] = ClassPath.expandPath(classpath, expandStar=true).map { Paths.get(_) }
28+
detectMainClassAndMethod(outDir, classpathEntries, scriptFile.toString) match
29+
case Right((mainClass, mainMethod)) =>
30+
val invokeMain: Boolean = Option(pack).map { func =>
31+
func(outDir, classpathEntries, mainClass)
32+
}.getOrElse(true)
33+
if invokeMain then mainMethod.invoke(null, scriptArgs)
34+
None
35+
case Left(ex) => Some(ex)
36+
catch
37+
case e: java.lang.reflect.InvocationTargetException =>
38+
Some(e.getCause)
39+
finally
40+
deleteFile(outDir.toFile)
41+
case None => None
4342
end compileAndRun
4443

4544
end ScriptingDriver

compiler/src/dotty/tools/scripting/StringDriver.scala

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,21 @@ class StringDriver(compilerArgs: Array[String], scalaSource: String) extends Dri
2727
val output = ctx.settings.outputDir.value
2828
if ctx.reporter.hasErrors then
2929
Some(StringDriverException("Errors encountered during compilation"))
30-
31-
try
32-
val classpath = s"${ctx.settings.classpath.value}${pathsep}${sys.props("java.class.path")}"
33-
val classpathEntries: Seq[Path] = ClassPath.expandPath(classpath, expandStar=true).map { Paths.get(_) }
34-
sys.props("java.class.path") = classpathEntries.map(_.toString).mkString(pathsep)
35-
val (mainClass, mainMethod) = detectMainClassAndMethod(outDir, classpathEntries, scalaSource)
36-
mainMethod.invoke(null, Array.empty[String])
37-
None
38-
catch
39-
case e: java.lang.reflect.InvocationTargetException =>
40-
throw e.getCause
41-
finally
42-
deleteFile(outDir.toFile)
30+
else
31+
try
32+
val classpath = s"${ctx.settings.classpath.value}${pathsep}${sys.props("java.class.path")}"
33+
val classpathEntries: Seq[Path] = ClassPath.expandPath(classpath, expandStar=true).map { Paths.get(_) }
34+
sys.props("java.class.path") = classpathEntries.map(_.toString).mkString(pathsep)
35+
detectMainClassAndMethod(outDir, classpathEntries, scalaSource) match
36+
case Right((mainClass, mainMethod)) =>
37+
mainMethod.invoke(null, Array.empty[String])
38+
None
39+
case Left(ex) => Some(ex)
40+
catch
41+
case e: java.lang.reflect.InvocationTargetException =>
42+
throw e.getCause
43+
finally
44+
deleteFile(outDir.toFile)
4345
case None => None
4446
end compileAndRun
4547

compiler/src/dotty/tools/scripting/Util.scala

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ import java.net.{ URLClassLoader }
88
import java.lang.reflect.{ Modifier, Method }
99

1010
object Util:
11-
1211
def deleteFile(target: File): Unit =
1312
if target.isDirectory then
1413
for member <- target.listFiles.toList
1514
do deleteFile(member)
1615
target.delete()
1716
end deleteFile
1817

19-
def detectMainClassAndMethod(outDir: Path, classpathEntries: Seq[Path], srcFile: String): (String, Method) =
18+
def detectMainClassAndMethod(
19+
outDir: Path,
20+
classpathEntries: Seq[Path],
21+
srcFile: String
22+
): Either[Throwable, (String, Method)] =
2023
val classpathUrls = (classpathEntries :+ outDir).map { _.toUri.toURL }
2124
val cl = URLClassLoader(classpathUrls.toArray)
2225

@@ -48,11 +51,10 @@ object Util:
4851

4952
mains match
5053
case Nil =>
51-
throw StringDriverException(s"No main methods detected for [${srcFile}]")
54+
Left(StringDriverException(s"No main methods detected for [${srcFile}]"))
5255
case _ :: _ :: _ =>
53-
throw StringDriverException(
54-
s"internal error: Detected the following main methods:\n${mains.mkString("\n")}")
55-
case m :: Nil => m
56+
Left(StringDriverException(s"Internal error: Detected the following main methods:\n${mains.mkString("\n")}"))
57+
case mainMethod :: Nil => Right(mainMethod)
5658
end match
5759
end detectMainClassAndMethod
5860

0 commit comments

Comments
 (0)