Skip to content

Commit 9d24583

Browse files
oderskysmarter
authored andcommitted
Refactor Diagnostic
Break it out from Reporter and eliminate all dependencies to Context. This is done so that Diagnostics can be part of a public and minimal compiler API.
1 parent 11bd355 commit 9d24583

File tree

7 files changed

+56
-49
lines changed

7 files changed

+56
-49
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ class ConsoleReporter(
4141
}
4242

4343
override def doReport(d: Diagnostic)(implicit ctx: Context): Boolean = {
44-
val issue = !(d.isSuppressed && hasErrors)
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
4548
if (issue) d match {
4649
case d: Error =>
4750
printMessageAndPos(s"error: ${d.msg}", d.pos)
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package dotty.tools
2+
package dotc
3+
package reporting
4+
5+
import util.SourcePosition
6+
7+
object Diagnostic {
8+
9+
// Error levels
10+
val ERROR = 2
11+
val WARNING = 1
12+
val INFO = 0
13+
14+
val nonSensicalStartTag = "<nonsensical>"
15+
val nonSensicalEndTag = "</nonsensical>"
16+
}
17+
18+
class Diagnostic(msgFn: => String, val pos: SourcePosition, val level: Int) extends Exception {
19+
import Diagnostic._
20+
private var myMsg: String = null
21+
private var myIsNonSensical: Boolean = false
22+
23+
/** The message to report */
24+
def msg: String = {
25+
if (myMsg == null) {
26+
myMsg = msgFn
27+
if (myMsg.contains(nonSensicalStartTag)) {
28+
myIsNonSensical = true
29+
// myMsg might be composed of several d"..." invocations -> nested nonsensical tags possible
30+
myMsg = myMsg.replaceAllLiterally(nonSensicalStartTag, "").replaceAllLiterally(nonSensicalEndTag, "")
31+
}
32+
}
33+
myMsg
34+
}
35+
36+
/** A message is non-sensical if it contains references to <nonsensical> tags.
37+
* Such tags are inserted by the error diagnostic framework if a message
38+
* contains references to internally generated error types. Normally we
39+
* want to suppress error messages referring to types like this because
40+
* they look weird and are normally follow-up errors to something that
41+
* was diagnosed before.
42+
*/
43+
def isNonSensical = { msg; myIsNonSensical }
44+
45+
override def toString = s"$getClass at $pos: $msg"
46+
override def getMessage() = msg
47+
}

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

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,10 @@ import collection.mutable
1010
import config.Settings.Setting
1111
import config.Printers
1212
import java.lang.System.currentTimeMillis
13-
import typer.ErrorReporting.DiagnosticString
1413
import typer.Mode
14+
import Diagnostic.{ERROR, WARNING, INFO}
1515

1616
object Reporter {
17-
18-
private val ERROR = 2
19-
private val WARNING = 1
20-
private val INFO = 0
21-
22-
class Diagnostic(msgFn: => String, val pos: SourcePosition, val level: Int) extends Exception {
23-
import DiagnosticString._
24-
25-
private var myMsg: String = null
26-
private var myIsNonSensical: Boolean = false
27-
28-
/** The message to report */
29-
def msg: String = {
30-
if (myMsg == null) {
31-
myMsg = msgFn
32-
if (myMsg.contains(nonSensicalStartTag)) {
33-
myIsNonSensical = true
34-
// myMsg might be composed of several d"..." invocations -> nested nonsensical tags possible
35-
myMsg = myMsg.replaceAllLiterally(nonSensicalStartTag, "").replaceAllLiterally(nonSensicalEndTag, "")
36-
}
37-
}
38-
myMsg
39-
}
40-
41-
/** Report in current reporter */
42-
def report(implicit ctx: Context) = ctx.reporter.report(this)
43-
44-
def isNonSensical = { msg; myIsNonSensical }
45-
def isSuppressed(implicit ctx: Context): Boolean = !ctx.settings.YshowSuppressedErrors.value && isNonSensical
46-
47-
override def toString = s"$getClass at $pos: $msg"
48-
override def getMessage() = msg
49-
50-
def checkingStr: String = msgFn
51-
}
52-
5317
class Error(msgFn: => String, pos: SourcePosition) extends Diagnostic(msgFn, pos, ERROR)
5418
class Warning(msgFn: => String, pos: SourcePosition) extends Diagnostic(msgFn, pos, WARNING)
5519
class Info(msgFn: => String, pos: SourcePosition) extends Diagnostic(msgFn, pos, INFO)

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package reporting
44

55
import core.Contexts.Context
66
import collection.mutable
7-
import Reporter.{Diagnostic, Error, Warning}
7+
import Reporter.{Error, Warning}
88
import config.Printers._
99

1010
/**

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package reporting
44

55
import scala.collection.mutable
66
import util.{SourcePosition, SourceFile}
7-
import Reporter.Diagnostic
87
import core.Contexts.Context
98

109
/**

src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Trees._
88
import Types._, ProtoTypes._, Contexts._, Decorators._, Denotations._, Symbols._
99
import Applications._, Implicits._, Flags._
1010
import util.Positions._
11+
import reporting.Diagnostic
1112
import printing.Showable
1213
import printing.Disambiguation.disambiguated
1314

@@ -127,7 +128,6 @@ object ErrorReporting {
127128
* message composition methods, this is crucial.
128129
*/
129130
implicit class DiagnosticString(val sc: StringContext) extends AnyVal {
130-
import DiagnosticString._
131131
def d(args: Any*)(implicit ctx: Context): String = {
132132
def isSensical(arg: Any): Boolean = arg match {
133133
case l: Seq[_] => l.forall(isSensical(_))
@@ -139,13 +139,8 @@ object ErrorReporting {
139139
}
140140

141141
val s = new StringInterpolators(sc).i(args : _*)
142-
if (args.forall(isSensical(_))) s else nonSensicalStartTag + s + nonSensicalEndTag
142+
if (args.forall(isSensical(_))) s
143+
else Diagnostic.nonSensicalStartTag + s + Diagnostic.nonSensicalEndTag
143144
}
144145
}
145-
146-
object DiagnosticString {
147-
final val nonSensicalStartTag = "<nonsensical>"
148-
final val nonSensicalEndTag = "</nonsensical>"
149-
}
150-
151146
}

src/dotty/tools/dotc/typer/Inferencing.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import util.{Stats, SimpleMap}
1515
import util.common._
1616
import Decorators._
1717
import Uniques._
18-
import ErrorReporting.{errorType, DiagnosticString}
1918
import config.Printers._
2019
import annotation.tailrec
2120
import collection.mutable

0 commit comments

Comments
 (0)