From eea702972bd805bce8189a51287c7ca8176618c9 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Fri, 12 Apr 2024 23:49:21 -0700 Subject: [PATCH] Move terminal test to properties --- project/MimaFilters.scala | 3 +++ src/library/scala/util/Properties.scala | 13 +++++++++++-- .../tools/nsc/interpreter/shell/ShellConfig.scala | 15 +++------------ test/files/neg/t6323a.scala | 2 +- 4 files changed, 18 insertions(+), 15 deletions(-) diff --git a/project/MimaFilters.scala b/project/MimaFilters.scala index 2c47296d3fac..f96a100ee5d5 100644 --- a/project/MimaFilters.scala +++ b/project/MimaFilters.scala @@ -39,6 +39,9 @@ object MimaFilters extends AutoPlugin { ProblemFilters.exclude[DirectMissingMethodProblem]("scala.concurrent.impl.FutureConvertersImpl#P.accept"), ProblemFilters.exclude[IncompatibleMethTypeProblem]("scala.concurrent.impl.FutureConvertersImpl#P.andThen"), + // private[scala] member used by Properties and by REPL + ProblemFilters.exclude[DirectMissingMethodProblem]("scala.util.Properties.consoleIsTerminal"), + ) override val buildSettings = Seq( diff --git a/src/library/scala/util/Properties.scala b/src/library/scala/util/Properties.scala index 668258c141a9..dc8968e38f85 100644 --- a/src/library/scala/util/Properties.scala +++ b/src/library/scala/util/Properties.scala @@ -142,8 +142,17 @@ private[scala] trait PropertiesTrait { private[scala] lazy val isAvian = javaVmName.contains("Avian") private[scala] def coloredOutputEnabled: Boolean = propOrElse("scala.color", "auto") match { - case "auto" => System.console() != null && !isWin - case s => s == "" || "true".equalsIgnoreCase(s) + case "auto" => !isWin && consoleIsTerminal + case s => "" == s || "true".equalsIgnoreCase(s) + } + + /** System.console.isTerminal, or just check for null console on JDK < 22 */ + private[scala] lazy val consoleIsTerminal: Boolean = { + val console = System.console + def isTerminal: Boolean = + try classOf[java.io.Console].getMethod("isTerminal", null).invoke(console).asInstanceOf[Boolean] + catch { case _: NoSuchMethodException => false } + console != null && (!isJavaAtLeast("22") || isTerminal) } // This is looking for javac, tools.jar, etc. diff --git a/src/repl-frontend/scala/tools/nsc/interpreter/shell/ShellConfig.scala b/src/repl-frontend/scala/tools/nsc/interpreter/shell/ShellConfig.scala index e522ced3490b..14ca609920db 100644 --- a/src/repl-frontend/scala/tools/nsc/interpreter/shell/ShellConfig.scala +++ b/src/repl-frontend/scala/tools/nsc/interpreter/shell/ShellConfig.scala @@ -21,11 +21,10 @@ import scala.sys.Prop._ import scala.tools.nsc.{GenericRunnerSettings, Settings} import scala.tools.nsc.Properties.{ - coloredOutputEnabled, envOrNone, javaVersion, javaVmName, + coloredOutputEnabled, consoleIsTerminal, envOrNone, javaVersion, javaVmName, shellBannerString, shellInterruptedString, shellPromptString, shellWelcomeString, userHome, versionString, versionNumberString, } -import scala.util.Properties.isJavaAtLeast object ShellConfig { val EDITOR = envOrNone("EDITOR") @@ -60,15 +59,7 @@ trait ShellConfig { def batchText: String def batchMode: Boolean def doCompletion: Boolean - def haveInteractiveConsole: Boolean = System.console != null && consoleIsTerminal - - // false if JDK 22 and the system console says !isTerminal - def consoleIsTerminal: Boolean = { - def isTerminal: Boolean = - try classOf[java.io.Console].getMethod("isTerminal", null).invoke(System.console).asInstanceOf[Boolean] - catch { case _: NoSuchMethodException => false } - !isJavaAtLeast(22) || isTerminal - } + def haveInteractiveConsole: Boolean = consoleIsTerminal // source compatibility, i.e., -Xsource def xsource: String @@ -77,7 +68,7 @@ trait ShellConfig { private def int(name: String) = Prop[Int](name) // This property is used in TypeDebugging. Let's recycle it. - val colorOk = coloredOutputEnabled && haveInteractiveConsole + val colorOk = coloredOutputEnabled val historyFile = s"$userHome/.scala_history_jline3" diff --git a/test/files/neg/t6323a.scala b/test/files/neg/t6323a.scala index 182c31c609a1..a0d6324f8bb8 100644 --- a/test/files/neg/t6323a.scala +++ b/test/files/neg/t6323a.scala @@ -1,4 +1,4 @@ -// scalac: -Vimplicits +//> using options -Vimplicits // import scala.reflect.runtime.universe._ import scala.reflect.runtime.{currentMirror => m}