From 4daf06d80d38560a2d3fdfbb5db3130751f7d356 Mon Sep 17 00:00:00 2001 From: Raphael Jolly Date: Wed, 7 Aug 2019 15:27:24 +0200 Subject: [PATCH] Introduce option not to display definitions in repl driver The reason for this change is that currently, statements in the JSR223 script engine are evaluated twice. Once when the "run" method is called, and once when the value of the result is extracted. The first one needs not be, it exists solely due to displayDefinitions being called. If we put it behind a flag, the problem is solved. --- .../src/dotty/tools/dotc/config/ScalaSettings.scala | 1 + compiler/src/dotty/tools/repl/ReplDriver.scala | 10 ++++++++-- compiler/src/dotty/tools/repl/ScriptEngine.scala | 3 ++- tests/run-with-compiler/scripting.check | 2 -- tests/run-with-compiler/scripting.scala | 1 - 5 files changed, 11 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala index c52f7a231896..874c22e7728b 100644 --- a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala +++ b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala @@ -81,6 +81,7 @@ class ScalaSettings extends Settings.SettingGroup { val Xprompt: Setting[Boolean] = BooleanSetting("-Xprompt", "Display a prompt after each error (debugging option).") val XnoValueClasses: Setting[Boolean] = BooleanSetting("-Xno-value-classes", "Do not use value classes. Helps debugging.") val XreplLineWidth: Setting[Int] = IntSetting("-Xrepl-line-width", "Maximal number of columns per line for REPL output", 390) + val XreplDisableDisplay: Setting[Boolean] = BooleanSetting("-Xrepl-disable-display", "Do not display definitions in REPL.") val XfatalWarnings: Setting[Boolean] = BooleanSetting("-Xfatal-warnings", "Fail the compilation if there are any warnings.") val XverifySignatures: Setting[Boolean] = BooleanSetting("-Xverify-signatures", "Verify generic signatures in generated bytecode.") val XignoreScala2Macros: Setting[Boolean] = BooleanSetting("-Xignore-scala2-macros", "Ignore errors when compiling code that calls Scala2 macros, these will fail at runtime.") diff --git a/compiler/src/dotty/tools/repl/ReplDriver.scala b/compiler/src/dotty/tools/repl/ReplDriver.scala index acfa33f02765..b3a9c56590bd 100644 --- a/compiler/src/dotty/tools/repl/ReplDriver.scala +++ b/compiler/src/dotty/tools/repl/ReplDriver.scala @@ -205,7 +205,9 @@ class ReplDriver(settings: Array[String], case _ => // new line, empty tree state } - out.println() + implicit val ctx: Context = newState.context + if (!ctx.settings.XreplDisableDisplay.value) + out.println() newState } @@ -238,7 +240,11 @@ class ReplDriver(settings: Array[String], val warnings = newState.context.reporter.removeBufferedMessages(newState.context) displayErrors(warnings)(newState) // display warnings - displayDefinitions(unit.tpdTree, newestWrapper)(newStateWithImports) + implicit val ctx = newState.context + if (!ctx.settings.XreplDisableDisplay.value) + displayDefinitions(unit.tpdTree, newestWrapper)(newStateWithImports) + else + newStateWithImports } ) } diff --git a/compiler/src/dotty/tools/repl/ScriptEngine.scala b/compiler/src/dotty/tools/repl/ScriptEngine.scala index a1b26412f2d3..7a03fa3eab8e 100644 --- a/compiler/src/dotty/tools/repl/ScriptEngine.scala +++ b/compiler/src/dotty/tools/repl/ScriptEngine.scala @@ -21,7 +21,8 @@ class ScriptEngine extends AbstractScriptEngine { Array( "-classpath", "", // Avoid the default "." "-usejavacp", - "-color:never" + "-color:never", + "-Xrepl-disable-display" ), Console.out, None) private val rendering = new Rendering private var state: State = driver.initialState diff --git a/tests/run-with-compiler/scripting.check b/tests/run-with-compiler/scripting.check index 2585ccd6c17d..d81cc0710eb6 100644 --- a/tests/run-with-compiler/scripting.check +++ b/tests/run-with-compiler/scripting.check @@ -1,3 +1 @@ -val res0: Int = 42 - 42 diff --git a/tests/run-with-compiler/scripting.scala b/tests/run-with-compiler/scripting.scala index 2eb209201a0c..4b5807b29385 100644 --- a/tests/run-with-compiler/scripting.scala +++ b/tests/run-with-compiler/scripting.scala @@ -5,4 +5,3 @@ object Test { println(e.eval("42")) } } -