Skip to content

Commit e85fa42

Browse files
authored
ConsoleReporter sends INFO to stdout (#20328)
Fixes #16701 Tested only manually. For example, with `tailrec` printer enabled, ``` scalac -Vprint:typer example.scala > x 2> y ``` puts tree output and tailrec trace in x, warnings & errors & summary count in y. x is the output I asked for. This makes it easier to see trace and trees correctly interleaved, since `> out 2>&1` does not guarantee it. Tests may depend on how output is captured. Scala 2 partest captures stderr to log, for example.
1 parent 4a47614 commit e85fa42

File tree

4 files changed

+20
-12
lines changed

4 files changed

+20
-12
lines changed

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

+14-8
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,18 @@ package reporting
55
import core.Contexts.*
66
import java.io.{ BufferedReader, PrintWriter }
77
import Diagnostic.*
8+
import dotty.tools.dotc.interfaces.Diagnostic.INFO
89

910
/**
1011
* This class implements a Reporter that displays messages on a text console
1112
*/
1213
class ConsoleReporter(
1314
reader: BufferedReader = Console.in,
14-
writer: PrintWriter = new PrintWriter(Console.err, true)
15+
writer: PrintWriter = new PrintWriter(Console.err, true),
16+
echoer: PrintWriter = new PrintWriter(Console.out, true)
1517
) extends ConsoleReporter.AbstractConsoleReporter {
16-
override def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
18+
override def printMessage(msg: String): Unit = { writer.println(msg); writer.flush() }
19+
override def echoMessage(msg: String): Unit = { echoer.println(msg); echoer.flush() }
1720
override def flush()(using Context): Unit = writer.flush()
1821

1922
override def doReport(dia: Diagnostic)(using Context): Unit = {
@@ -22,18 +25,21 @@ class ConsoleReporter(
2225
dia match
2326
case _: Error => Reporter.displayPrompt(reader, writer)
2427
case _: Warning if ctx.settings.XfatalWarnings.value => Reporter.displayPrompt(reader, writer)
25-
case _ =>
28+
case _ =>
2629
}
2730
}
2831

2932
object ConsoleReporter {
3033
abstract class AbstractConsoleReporter extends AbstractReporter {
31-
/** Prints the message. */
34+
/** Print the diagnostic message. */
3235
def printMessage(msg: String): Unit
3336

34-
/** Prints the message with the given position indication. */
35-
def doReport(dia: Diagnostic)(using Context): Unit = {
36-
printMessage(messageAndPos(dia))
37-
}
37+
/** Print the informative message. */
38+
def echoMessage(msg: String): Unit
39+
40+
/** Print the message with the given position indication. */
41+
def doReport(dia: Diagnostic)(using Context): Unit =
42+
if dia.level == INFO then echoMessage(messageAndPos(dia))
43+
else printMessage(messageAndPos(dia))
3844
}
3945
}

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

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import dotty.tools.dotc.util.NoSourcePosition
1414
import java.io.{BufferedReader, PrintWriter}
1515
import scala.annotation.internal.sharable
1616
import scala.collection.mutable
17-
import core.Decorators.em
17+
import core.Decorators.{em, toMessage}
1818
import core.handleRecursive
1919

2020
object Reporter {
@@ -236,10 +236,9 @@ abstract class Reporter extends interfaces.ReporterResult {
236236
report(Warning(msg, NoSourcePosition))
237237

238238
/** Print the summary of warnings and errors */
239-
def printSummary()(using Context): Unit = {
239+
def printSummary()(using Context): Unit =
240240
val s = summary
241-
if (s != "") report(new Info(s, NoSourcePosition))
242-
}
241+
if (s != "") doReport(Warning(s.toMessage, NoSourcePosition))
243242

244243
/** Returns a string meaning "n elements". */
245244
protected def countString(n: Int, elements: String): String = n match {

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

+1
Original file line numberDiff line numberDiff line change
@@ -545,6 +545,7 @@ class ReplDriver(settings: Array[String],
545545
private object ReplConsoleReporter extends ConsoleReporter.AbstractConsoleReporter {
546546
override def posFileStr(pos: SourcePosition) = "" // omit file paths
547547
override def printMessage(msg: String): Unit = out.println(msg)
548+
override def echoMessage(msg: String): Unit = printMessage(msg)
548549
override def flush()(using Context): Unit = out.flush()
549550
}
550551

compiler/test/dotty/tools/dotc/reporting/TestReporter.scala

+2
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ extends Reporter with UniqueMessagePositions with HideNonSensicalMessages with M
7575
_diagnosticBuf.append(dia)
7676
printMessageAndPos(dia, extra)
7777
}
78+
79+
override def printSummary()(using Context): Unit = ()
7880
}
7981

8082
object TestReporter {

0 commit comments

Comments
 (0)