Skip to content

Commit 03486df

Browse files
committed
ConsoleReporter: handling of non-sensical messages is now reusable
This is now handled by a separate trait HideNonSensicalMessages that can be mixed in, similar to UniqueMessagePositions. This way we'll be able to reuse this functionality for other kind of Reporters. This also means that we don't need `doReport` to return a Boolean anymore, so we change it to return a Unit as it did before 065a002
1 parent 9d24583 commit 03486df

File tree

5 files changed

+39
-27
lines changed

5 files changed

+39
-27
lines changed

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import scala.reflect.internal.util._
1616
class ConsoleReporter(
1717
reader: BufferedReader = Console.in,
1818
writer: PrintWriter = new PrintWriter(Console.err, true))
19-
extends Reporter with UniqueMessagePositions {
19+
extends Reporter with UniqueMessagePositions with HideNonSensicalMessages {
2020

2121
/** maximal number of error messages to be printed */
2222
protected def ErrorLimit = 100
@@ -40,24 +40,17 @@ class ConsoleReporter(
4040
}
4141
}
4242

43-
override def doReport(d: Diagnostic)(implicit ctx: Context): Boolean = {
44-
val issue =
45-
!d.isNonSensical ||
46-
!hasErrors || // if there are no errors yet, report even if diagnostic is non-sensical
47-
ctx.settings.YshowSuppressedErrors.value
48-
if (issue) d match {
49-
case d: Error =>
50-
printMessageAndPos(s"error: ${d.msg}", d.pos)
51-
if (ctx.settings.prompt.value) displayPrompt()
52-
case d: ConditionalWarning if !d.enablingOption.value =>
53-
case d: MigrationWarning =>
54-
printMessageAndPos(s"migration warning: ${d.msg}", d.pos)
55-
case d: Warning =>
56-
printMessageAndPos(s"warning: ${d.msg}", d.pos)
57-
case _ =>
58-
printMessageAndPos(d.msg, d.pos)
59-
}
60-
issue
43+
override def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match {
44+
case d: Error =>
45+
printMessageAndPos(s"error: ${d.msg}", d.pos)
46+
if (ctx.settings.prompt.value) displayPrompt()
47+
case d: ConditionalWarning if !d.enablingOption.value =>
48+
case d: MigrationWarning =>
49+
printMessageAndPos(s"migration warning: ${d.msg}", d.pos)
50+
case d: Warning =>
51+
printMessageAndPos(s"warning: ${d.msg}", d.pos)
52+
case _ =>
53+
printMessageAndPos(d.msg, d.pos)
6154
}
6255

6356
def displayPrompt(): Unit = {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package dotty.tools
2+
package dotc
3+
package reporting
4+
5+
import core.Contexts.Context
6+
7+
/**
8+
* This trait implements `isHidden` so that we avoid reporting non-sensical messages.
9+
*/
10+
trait HideNonSensicalMessages extends Reporter {
11+
/** Hides non-sensical messages, unless we haven't reported any error yet or
12+
* `-Yshow-suppressed-errors` is set.
13+
*/
14+
override def isHidden(d: Diagnostic)(implicit ctx: Context): Boolean =
15+
super.isHidden(d) || {
16+
d.isNonSensical &&
17+
hasErrors && // if there are no errors yet, report even if diagnostic is non-sensical
18+
!ctx.settings.YshowSuppressedErrors.value
19+
}
20+
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,8 @@ trait Reporting { this: Context =>
159159
*/
160160
abstract class Reporter {
161161

162-
/** Report a diagnostic, unless it is suppressed because it is nonsensical
163-
* @return a diagnostic was reported.
164-
*/
165-
def doReport(d: Diagnostic)(implicit ctx: Context): Boolean
162+
/** Report a diagnostic */
163+
def doReport(d: Diagnostic)(implicit ctx: Context): Unit
166164

167165
/** Whether very long lines can be truncated. This exists so important
168166
* debugging information (like printing the classpath) is not rendered
@@ -203,7 +201,8 @@ abstract class Reporter {
203201
}
204202

205203
def report(d: Diagnostic)(implicit ctx: Context): Unit =
206-
if (!isHidden(d) && doReport(d)(ctx.addMode(Mode.Printing)))
204+
if (!isHidden(d)) {
205+
doReport(d)(ctx.addMode(Mode.Printing))
207206
d match {
208207
case d: ConditionalWarning if !d.enablingOption.value => unreportedWarnings(d.enablingOption.name) += 1
209208
case d: Warning => warningCount += 1
@@ -213,6 +212,7 @@ abstract class Reporter {
213212
case d: Info => // nothing to do here
214213
// match error if d is something else
215214
}
215+
}
216216

217217
def incomplete(d: Diagnostic)(implicit ctx: Context): Unit =
218218
incompleteHandler(d)(ctx)

src/dotty/tools/dotc/reporting/StoreReporter.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ class StoreReporter(outer: Reporter) extends Reporter {
1414

1515
private var infos: mutable.ListBuffer[Diagnostic] = null
1616

17-
def doReport(d: Diagnostic)(implicit ctx: Context): Boolean = {
17+
def doReport(d: Diagnostic)(implicit ctx: Context): Unit = {
1818
typr.println(s">>>> StoredError: ${d.msg}") // !!! DEBUG
1919
if (infos == null) infos = new mutable.ListBuffer
2020
infos += d
21-
true
2221
}
2322

2423
override def hasPending: Boolean = infos != null && {

src/dotty/tools/dotc/reporting/ThrowingReporter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Reporter._
1111
* info to the underlying reporter.
1212
*/
1313
class ThrowingReporter(reportInfo: Reporter) extends Reporter {
14-
def doReport(d: Diagnostic)(implicit ctx: Context): Boolean = d match {
14+
def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match {
1515
case _: Error => throw d
1616
case _ => reportInfo.doReport(d)
1717
}

0 commit comments

Comments
 (0)