From ca8147d01390d5ccb7895434c35c63322cd41a38 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Thu, 2 Sep 2021 21:42:54 +0100 Subject: [PATCH 1/3] Fix and reinstate repl/errmsgs --- compiler/src/dotty/tools/repl/ReplDriver.scala | 4 ++-- compiler/test-resources/{pending => }/repl/errmsgs | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) rename compiler/test-resources/{pending => }/repl/errmsgs (96%) diff --git a/compiler/src/dotty/tools/repl/ReplDriver.scala b/compiler/src/dotty/tools/repl/ReplDriver.scala index b2a8f9afb88e..2869b1501ea3 100644 --- a/compiler/src/dotty/tools/repl/ReplDriver.scala +++ b/compiler/src/dotty/tools/repl/ReplDriver.scala @@ -1,6 +1,6 @@ package dotty.tools.repl -import java.io.{File => JFile, PrintStream, PrintWriter} +import java.io.{BufferedWriter, File => JFile, OutputStreamWriter, PrintStream, PrintWriter} import java.nio.charset.StandardCharsets import dotty.tools.dotc.ast.Trees._ @@ -428,7 +428,7 @@ class ReplDriver(settings: Array[String], /** Like ConsoleReporter, but without file paths or real -Xprompt'ing */ private object ReplConsoleReporter extends ConsoleReporter( reader = null, // this short-circuits the -Xprompt display from waiting for an input - writer = new PrintWriter(out, /* autoFlush = */ true), // write to out, not Console.err + writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)), /* autoFlush = */ true), // write to out, not Console.err ) { override def posFileStr(pos: SourcePosition) = "" // omit file paths } diff --git a/compiler/test-resources/pending/repl/errmsgs b/compiler/test-resources/repl/errmsgs similarity index 96% rename from compiler/test-resources/pending/repl/errmsgs rename to compiler/test-resources/repl/errmsgs index f1207276df6d..14e9d44fa0b8 100644 --- a/compiler/test-resources/pending/repl/errmsgs +++ b/compiler/test-resources/repl/errmsgs @@ -51,19 +51,19 @@ scala> abstract class C { type T; val x: T; val s: Unit = { type T = String; var 1 | abstract class C { type T; val x: T; val s: Unit = { type T = String; var y: T = x; locally { def f() = { type T = Int; val z: T = y }; f() } }; } | ^ |Found: (C.this.x : C.this.T) - |Required: T? + |Required: T² | |where: T is a type in class C - | T? is a type in the initializer of value s which is an alias of String + | T² is a type in the initializer of value s which is an alias of String longer explanation available when compiling with `-explain` -- [E007] Type Mismatch Error: ------------------------------------------------- 1 | abstract class C { type T; val x: T; val s: Unit = { type T = String; var y: T = x; locally { def f() = { type T = Int; val z: T = y }; f() } }; } | ^ |Found: (y : T) - |Required: T? + |Required: T² | |where: T is a type in the initializer of value s which is an alias of String - | T? is a type in method f which is an alias of Int + | T² is a type in method f which is an alias of Int longer explanation available when compiling with `-explain` 2 errors found scala> class Foo() { def bar: Int = 1 }; val foo = new Foo(); foo.barr From 1632cb9e7e5af5e4df3e151d32176060298f026c Mon Sep 17 00:00:00 2001 From: Tom Grigg Date: Thu, 2 Sep 2021 18:05:12 -0700 Subject: [PATCH 2/3] Implement ReplConsoleReporter in terms of the ReplDriver `out` PrintStream Based on the implementation of ConsoleReporter. Sidesteps issues on Windows where the default console encoding may be something other than UTF8 (such as Cp437), which PrintStream is aware of, but apparently there is no API to query. --- .../src/dotty/tools/repl/ReplDriver.scala | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/compiler/src/dotty/tools/repl/ReplDriver.scala b/compiler/src/dotty/tools/repl/ReplDriver.scala index 2869b1501ea3..f4a2a9c6742b 100644 --- a/compiler/src/dotty/tools/repl/ReplDriver.scala +++ b/compiler/src/dotty/tools/repl/ReplDriver.scala @@ -1,6 +1,6 @@ package dotty.tools.repl -import java.io.{BufferedWriter, File => JFile, OutputStreamWriter, PrintStream, PrintWriter} +import java.io.{File => JFile, PrintStream} import java.nio.charset.StandardCharsets import dotty.tools.dotc.ast.Trees._ @@ -20,7 +20,7 @@ import dotty.tools.dotc.core.Symbols.{Symbol, defn} import dotty.tools.dotc.interfaces import dotty.tools.dotc.interactive.Completion import dotty.tools.dotc.printing.SyntaxHighlighting -import dotty.tools.dotc.reporting.{ConsoleReporter, MessageRendering, StoreReporter} +import dotty.tools.dotc.reporting.{AbstractReporter, MessageRendering, StoreReporter} import dotty.tools.dotc.reporting.{Message, Diagnostic} import dotty.tools.dotc.util.Spans.Span import dotty.tools.dotc.util.{SourceFile, SourcePosition} @@ -426,10 +426,18 @@ class ReplDriver(settings: Array[String], } /** Like ConsoleReporter, but without file paths or real -Xprompt'ing */ - private object ReplConsoleReporter extends ConsoleReporter( - reader = null, // this short-circuits the -Xprompt display from waiting for an input - writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8)), /* autoFlush = */ true), // write to out, not Console.err - ) { + private object ReplConsoleReporter extends AbstractReporter { + def printMessage(msg: String): Unit = out.println(msg) + + def doReport(dia: Diagnostic)(using Context): Unit = { + printMessage(messageAndPos(dia)) + + if Diagnostic.shouldExplain(dia) then + printMessage(explanation(dia.msg)) + else if dia.msg.canExplain then + printMessage("\nlonger explanation available when compiling with `-explain`") + } + override def posFileStr(pos: SourcePosition) = "" // omit file paths } From 624db9556bb7ea30433307c3f03277f355adae37 Mon Sep 17 00:00:00 2001 From: Dale Wijnand Date: Fri, 3 Sep 2021 08:40:31 +0100 Subject: [PATCH 3/3] Dedupe ConsoleReporter & ReplConsoleReporter --- .../dotc/reporting/ConsoleReporter.scala | 41 ++++++++++--------- .../src/dotty/tools/repl/ReplDriver.scala | 20 +++------ 2 files changed, 28 insertions(+), 33 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/reporting/ConsoleReporter.scala b/compiler/src/dotty/tools/dotc/reporting/ConsoleReporter.scala index 0609dd8c643c..f872968006b0 100644 --- a/compiler/src/dotty/tools/dotc/reporting/ConsoleReporter.scala +++ b/compiler/src/dotty/tools/dotc/reporting/ConsoleReporter.scala @@ -12,27 +12,30 @@ import Diagnostic.{ Error, ConditionalWarning } class ConsoleReporter( reader: BufferedReader = Console.in, writer: PrintWriter = new PrintWriter(Console.err, true) -) extends AbstractReporter { +) extends ConsoleReporter.AbstractConsoleReporter { + override def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() } + override def flush()(using Context): Unit = writer.flush() - import Diagnostic._ - - /** Prints the message. */ - def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() } - - /** Prints the message with the given position indication. */ - def doReport(dia: Diagnostic)(using Context): Unit = { + override def doReport(dia: Diagnostic)(using Context): Unit = { + super.doReport(dia) dia match - case dia: Error => - printMessage(messageAndPos(dia)) - if (ctx.settings.Xprompt.value) Reporter.displayPrompt(reader, writer) - case dia => - printMessage(messageAndPos(dia)) - - if shouldExplain(dia) then - printMessage(explanation(dia.msg)) - else if dia.msg.canExplain then - printMessage("\nlonger explanation available when compiling with `-explain`") + case dia: Error if ctx.settings.Xprompt.value => Reporter.displayPrompt(reader, writer) + case _ => } +} + +object ConsoleReporter { + abstract class AbstractConsoleReporter extends AbstractReporter { + /** Prints the message. */ + def printMessage(msg: String): Unit - override def flush()(using Context): Unit = { writer.flush() } + /** Prints the message with the given position indication. */ + def doReport(dia: Diagnostic)(using Context): Unit = { + printMessage(messageAndPos(dia)) + if Diagnostic.shouldExplain(dia) then + printMessage(explanation(dia.msg)) + else if dia.msg.canExplain then + printMessage("\nlonger explanation available when compiling with `-explain`") + } + } } diff --git a/compiler/src/dotty/tools/repl/ReplDriver.scala b/compiler/src/dotty/tools/repl/ReplDriver.scala index f4a2a9c6742b..40876b260394 100644 --- a/compiler/src/dotty/tools/repl/ReplDriver.scala +++ b/compiler/src/dotty/tools/repl/ReplDriver.scala @@ -20,7 +20,7 @@ import dotty.tools.dotc.core.Symbols.{Symbol, defn} import dotty.tools.dotc.interfaces import dotty.tools.dotc.interactive.Completion import dotty.tools.dotc.printing.SyntaxHighlighting -import dotty.tools.dotc.reporting.{AbstractReporter, MessageRendering, StoreReporter} +import dotty.tools.dotc.reporting.{ConsoleReporter, MessageRendering, StoreReporter} import dotty.tools.dotc.reporting.{Message, Diagnostic} import dotty.tools.dotc.util.Spans.Span import dotty.tools.dotc.util.{SourceFile, SourcePosition} @@ -425,20 +425,12 @@ class ReplDriver(settings: Array[String], state } - /** Like ConsoleReporter, but without file paths or real -Xprompt'ing */ - private object ReplConsoleReporter extends AbstractReporter { - def printMessage(msg: String): Unit = out.println(msg) - - def doReport(dia: Diagnostic)(using Context): Unit = { - printMessage(messageAndPos(dia)) - - if Diagnostic.shouldExplain(dia) then - printMessage(explanation(dia.msg)) - else if dia.msg.canExplain then - printMessage("\nlonger explanation available when compiling with `-explain`") - } - + /** Like ConsoleReporter, but without file paths, -Xprompt displaying, + * and using a PrintStream rather than a PrintWriter so messages aren't re-encoded. */ + private object ReplConsoleReporter extends ConsoleReporter.AbstractConsoleReporter { override def posFileStr(pos: SourcePosition) = "" // omit file paths + override def printMessage(msg: String): Unit = out.println(msg) + override def flush()(using Context): Unit = out.flush() } /** Print warnings & errors using ReplConsoleReporter, and info straight to out */