Skip to content

Commit a82a1a6

Browse files
authored
Merge pull request #13459 from dwijnand/unpend-repl/errmsgs
Fix and reinstate repl/errmsgs
2 parents f7344ab + 624db95 commit a82a1a6

File tree

3 files changed

+32
-29
lines changed

3 files changed

+32
-29
lines changed

compiler/src/dotty/tools/dotc/reporting/ConsoleReporter.scala

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,27 +12,30 @@ import Diagnostic.{ Error, ConditionalWarning }
1212
class ConsoleReporter(
1313
reader: BufferedReader = Console.in,
1414
writer: PrintWriter = new PrintWriter(Console.err, true)
15-
) extends AbstractReporter {
15+
) extends ConsoleReporter.AbstractConsoleReporter {
16+
override def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
17+
override def flush()(using Context): Unit = writer.flush()
1618

17-
import Diagnostic._
18-
19-
/** Prints the message. */
20-
def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
21-
22-
/** Prints the message with the given position indication. */
23-
def doReport(dia: Diagnostic)(using Context): Unit = {
19+
override def doReport(dia: Diagnostic)(using Context): Unit = {
20+
super.doReport(dia)
2421
dia match
25-
case dia: Error =>
26-
printMessage(messageAndPos(dia))
27-
if (ctx.settings.Xprompt.value) Reporter.displayPrompt(reader, writer)
28-
case dia =>
29-
printMessage(messageAndPos(dia))
30-
31-
if shouldExplain(dia) then
32-
printMessage(explanation(dia.msg))
33-
else if dia.msg.canExplain then
34-
printMessage("\nlonger explanation available when compiling with `-explain`")
22+
case dia: Error if ctx.settings.Xprompt.value => Reporter.displayPrompt(reader, writer)
23+
case _ =>
3524
}
25+
}
26+
27+
object ConsoleReporter {
28+
abstract class AbstractConsoleReporter extends AbstractReporter {
29+
/** Prints the message. */
30+
def printMessage(msg: String): Unit
3631

37-
override def flush()(using Context): Unit = { writer.flush() }
32+
/** Prints the message with the given position indication. */
33+
def doReport(dia: Diagnostic)(using Context): Unit = {
34+
printMessage(messageAndPos(dia))
35+
if Diagnostic.shouldExplain(dia) then
36+
printMessage(explanation(dia.msg))
37+
else if dia.msg.canExplain then
38+
printMessage("\nlonger explanation available when compiling with `-explain`")
39+
}
40+
}
3841
}

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package dotty.tools.repl
22

3-
import java.io.{File => JFile, PrintStream, PrintWriter}
3+
import java.io.{File => JFile, PrintStream}
44
import java.nio.charset.StandardCharsets
55

66
import dotty.tools.dotc.ast.Trees._
@@ -425,12 +425,12 @@ class ReplDriver(settings: Array[String],
425425
state
426426
}
427427

428-
/** Like ConsoleReporter, but without file paths or real -Xprompt'ing */
429-
private object ReplConsoleReporter extends ConsoleReporter(
430-
reader = null, // this short-circuits the -Xprompt display from waiting for an input
431-
writer = new PrintWriter(out, /* autoFlush = */ true), // write to out, not Console.err
432-
) {
428+
/** Like ConsoleReporter, but without file paths, -Xprompt displaying,
429+
* and using a PrintStream rather than a PrintWriter so messages aren't re-encoded. */
430+
private object ReplConsoleReporter extends ConsoleReporter.AbstractConsoleReporter {
433431
override def posFileStr(pos: SourcePosition) = "" // omit file paths
432+
override def printMessage(msg: String): Unit = out.println(msg)
433+
override def flush()(using Context): Unit = out.flush()
434434
}
435435

436436
/** Print warnings & errors using ReplConsoleReporter, and info straight to out */

compiler/test-resources/pending/repl/errmsgs renamed to compiler/test-resources/repl/errmsgs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,19 +51,19 @@ scala> abstract class C { type T; val x: T; val s: Unit = { type T = String; var
5151
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() } }; }
5252
| ^
5353
|Found: (C.this.x : C.this.T)
54-
|Required: T?
54+
|Required: T²
5555
|
5656
|where: T is a type in class C
57-
| T? is a type in the initializer of value s which is an alias of String
57+
| T² is a type in the initializer of value s which is an alias of String
5858
longer explanation available when compiling with `-explain`
5959
-- [E007] Type Mismatch Error: -------------------------------------------------
6060
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() } }; }
6161
| ^
6262
|Found: (y : T)
63-
|Required: T?
63+
|Required: T²
6464
|
6565
|where: T is a type in the initializer of value s which is an alias of String
66-
| T? is a type in method f which is an alias of Int
66+
| T² is a type in method f which is an alias of Int
6767
longer explanation available when compiling with `-explain`
6868
2 errors found
6969
scala> class Foo() { def bar: Int = 1 }; val foo = new Foo(); foo.barr

0 commit comments

Comments
 (0)