Skip to content

Commit

Permalink
Rename MessageContainer -> Diagnostic
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Mar 25, 2020
1 parent d0679b0 commit 540cdec
Show file tree
Hide file tree
Showing 22 changed files with 136 additions and 137 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import ast.{Trees, tpd}
import core._, core.Decorators._
import Contexts._, Names._, NameOps._, Symbols._, SymDenotations._, Trees._, Types._
import classpath._
import reporting._, reporting.diagnostic.MessageContainer
import reporting._, reporting.diagnostic.Diagnostic
import util._

/** A Driver subclass designed to be used from IDEs */
Expand Down Expand Up @@ -138,9 +138,9 @@ class InteractiveDriver(val settings: List[String]) extends Driver {
(fromSource ++ fromClassPath).distinct
}

def run(uri: URI, sourceCode: String): List[MessageContainer] = run(uri, toSource(uri, sourceCode))
def run(uri: URI, sourceCode: String): List[Diagnostic] = run(uri, toSource(uri, sourceCode))

def run(uri: URI, source: SourceFile): List[MessageContainer] = {
def run(uri: URI, source: SourceFile): List[Diagnostic] = {
val previousCtx = myCtx
try {
val reporter =
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/printing/Formatting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Texts._, Types._, Flags._, Symbols._, Contexts._
import collection.mutable
import Decorators._
import scala.util.control.NonFatal
import reporting.diagnostic.MessageContainer
import reporting.diagnostic.Diagnostic
import util.DiffUtil
import Highlighting._

Expand Down Expand Up @@ -88,7 +88,7 @@ object Formatting {
}

private def wrapNonSensical(arg: Any, str: String)(implicit ctx: Context): String = {
import MessageContainer._
import Diagnostic._
def isSensical(arg: Any): Boolean = arg match {
case tpe: Type =>
tpe.exists && !tpe.isErroneous
Expand Down
24 changes: 12 additions & 12 deletions compiler/src/dotty/tools/dotc/reporting/ConsoleReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package reporting

import core.Contexts._
import java.io.{ BufferedReader, PrintWriter }
import diagnostic.MessageContainer
import diagnostic.Diagnostic
import diagnostic.messages.{ Error, ConditionalWarning }

/**
Expand All @@ -15,28 +15,28 @@ class ConsoleReporter(
writer: PrintWriter = new PrintWriter(Console.err, true)
) extends AbstractReporter {

import MessageContainer._
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(m: MessageContainer)(implicit ctx: Context): Unit = {
val didPrint = m match {
case m: Error =>
printMessage(messageAndPos(m.contained, m.pos, diagnosticLevel(m)))
def doReport(dia: Diagnostic)(implicit ctx: Context): Unit = {
val didPrint = dia match {
case dia: Error =>
printMessage(messageAndPos(dia.contained, dia.pos, diagnosticLevel(dia)))
if (ctx.settings.Xprompt.value) Reporter.displayPrompt(reader, writer)
true
case m: ConditionalWarning if !m.enablingOption.value =>
case dia: ConditionalWarning if !dia.enablingOption.value =>
false
case m =>
printMessage(messageAndPos(m.contained, m.pos, diagnosticLevel(m)))
case dia =>
printMessage(messageAndPos(dia.contained, dia.pos, diagnosticLevel(dia)))
true
}

if (didPrint && ctx.shouldExplain(m))
printMessage(explanation(m.contained))
else if (didPrint && m.contained.explanation.nonEmpty)
if (didPrint && ctx.shouldExplain(dia))
printMessage(explanation(dia.contained))
else if (didPrint && dia.contained.explanation.nonEmpty)
printMessage("\nlonger explanation available when compiling with `-explain`")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package dotc
package reporting

import core.Contexts.Context
import diagnostic.MessageContainer
import diagnostic.Diagnostic

/**
* This trait implements `isHidden` so that we avoid reporting non-sensical messages.
Expand All @@ -12,9 +12,9 @@ trait HideNonSensicalMessages extends Reporter {
/** Hides non-sensical messages, unless we haven't reported any error yet or
* `-Yshow-suppressed-errors` is set.
*/
override def isHidden(m: MessageContainer)(implicit ctx: Context): Boolean =
super.isHidden(m) || {
m.isNonSensical &&
override def isHidden(dia: Diagnostic)(implicit ctx: Context): Boolean =
super.isHidden(dia) || {
dia.isNonSensical &&
hasErrors && // if there are no errors yet, report even if diagnostic is non-sensical
!ctx.settings.YshowSuppressedErrors.value
}
Expand Down
20 changes: 10 additions & 10 deletions compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import core.Contexts.Context
import core.Decorators._
import printing.Highlighting.{Blue, Red, Yellow}
import printing.SyntaxHighlighting
import diagnostic.{ErrorMessageID, Message, MessageContainer}
import diagnostic.{ErrorMessageID, Message, Diagnostic}
import diagnostic.messages._
import util.SourcePosition
import scala.internal.Chars.{ LF, CR, FF, SU }
Expand Down Expand Up @@ -167,14 +167,14 @@ trait MessageRendering {
Yellow(str).show
}

def diagnosticLevel(cont: MessageContainer): String =
cont match {
case m: Error => "Error"
case m: FeatureWarning => "Feature Warning"
case m: DeprecationWarning => "Deprecation Warning"
case m: UncheckedWarning => "Unchecked Warning"
case m: MigrationWarning => "Migration Warning"
case m: Warning => "Warning"
case m: Info => "Info"
def diagnosticLevel(dia: Diagnostic): String =
dia match {
case dia: Error => "Error"
case dia: FeatureWarning => "Feature Warning"
case dia: DeprecationWarning => "Deprecation Warning"
case dia: UncheckedWarning => "Unchecked Warning"
case dia: MigrationWarning => "Migration Warning"
case dia: Warning => "Warning"
case dia: Info => "Info"
}
}
44 changes: 22 additions & 22 deletions compiler/src/dotty/tools/dotc/reporting/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ object Reporter {
/** Convert a SimpleReporter into a real Reporter */
def fromSimpleReporter(simple: interfaces.SimpleReporter): Reporter =
new Reporter with UniqueMessagePositions with HideNonSensicalMessages {
override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = m match {
case m: ConditionalWarning if !m.enablingOption.value =>
override def doReport(dia: Diagnostic)(implicit ctx: Context): Unit = dia match {
case dia: ConditionalWarning if !dia.enablingOption.value =>
case _ =>
simple.report(m)
simple.report(dia)
}
}

/** A reporter that ignores reports, and doesn't record errors */
@sharable object NoReporter extends Reporter {
def doReport(m: MessageContainer)(implicit ctx: Context): Unit = ()
override def report(m: MessageContainer)(implicit ctx: Context): Unit = ()
def doReport(dia: Diagnostic)(implicit ctx: Context): Unit = ()
override def report(dia: Diagnostic)(implicit ctx: Context): Unit = ()
}

type ErrorHandler = (MessageContainer, Context) => Unit
type ErrorHandler = (Diagnostic, Context) => Unit

private val defaultIncompleteHandler: ErrorHandler =
(mc, ctx) => ctx.reporter.report(mc)(ctx)
Expand Down Expand Up @@ -192,7 +192,7 @@ abstract class Reporter extends interfaces.ReporterResult {
import Reporter._

/** Report a diagnostic */
def doReport(m: MessageContainer)(implicit ctx: Context): Unit
def doReport(dia: Diagnostic)(implicit ctx: Context): Unit

/** Whether very long lines can be truncated. This exists so important
* debugging information (like printing the classpath) is not rendered
Expand Down Expand Up @@ -261,25 +261,25 @@ abstract class Reporter extends interfaces.ReporterResult {

var unreportedWarnings: Map[String, Int] = Map.empty

def report(m: MessageContainer)(implicit ctx: Context): Unit =
if (!isHidden(m)) {
doReport(m)(ctx.addMode(Mode.Printing))
m match {
case m: ConditionalWarning if !m.enablingOption.value =>
val key = m.enablingOption.name
def report(dia: Diagnostic)(implicit ctx: Context): Unit =
if (!isHidden(dia)) {
doReport(dia)(ctx.addMode(Mode.Printing))
dia match {
case dia: ConditionalWarning if !dia.enablingOption.value =>
val key = dia.enablingOption.name
unreportedWarnings =
unreportedWarnings.updated(key, unreportedWarnings.getOrElse(key, 0) + 1)
case m: Warning => _warningCount += 1
case m: Error =>
errors = m :: errors
case dia: Warning => _warningCount += 1
case dia: Error =>
errors = dia :: errors
_errorCount += 1
case m: Info => // nothing to do here
case dia: Info => // nothing to do here
// match error if d is something else
}
}

def incomplete(m: MessageContainer)(implicit ctx: Context): Unit =
incompleteHandler(m, ctx)
def incomplete(dia: Diagnostic)(implicit ctx: Context): Unit =
incompleteHandler(dia, ctx)

/** Summary of warnings and errors */
def summary: String = {
Expand Down Expand Up @@ -307,7 +307,7 @@ abstract class Reporter extends interfaces.ReporterResult {
}

/** Should this diagnostic not be reported at all? */
def isHidden(m: MessageContainer)(implicit ctx: Context): Boolean =
def isHidden(dia: Diagnostic)(implicit ctx: Context): Boolean =
ctx.mode.is(Mode.Printing)

/** Does this reporter contain errors that have yet to be reported by its outer reporter ?
Expand All @@ -316,12 +316,12 @@ abstract class Reporter extends interfaces.ReporterResult {
def hasUnreportedErrors: Boolean = false

/** If this reporter buffers messages, remove and return all buffered messages. */
def removeBufferedMessages(implicit ctx: Context): List[MessageContainer] = Nil
def removeBufferedMessages(implicit ctx: Context): List[Diagnostic] = Nil

/** Issue all error messages in this reporter to next outer one, or make sure they are written. */
def flush()(implicit ctx: Context): Unit =
removeBufferedMessages.foreach(ctx.reporter.report)

/** If this reporter buffers messages, all buffered messages, otherwise Nil */
def pendingMessages(using Context): List[MessageContainer] = Nil
def pendingMessages(using Context): List[Diagnostic] = Nil
}
14 changes: 7 additions & 7 deletions compiler/src/dotty/tools/dotc/reporting/StoreReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package reporting
import core.Contexts.Context
import collection.mutable
import config.Printers.typr
import diagnostic.MessageContainer
import diagnostic.Diagnostic
import diagnostic.messages._

/** This class implements a Reporter that stores all messages
Expand All @@ -20,12 +20,12 @@ import diagnostic.messages._
*/
class StoreReporter(outer: Reporter) extends Reporter {

protected var infos: mutable.ListBuffer[MessageContainer] = null
protected var infos: mutable.ListBuffer[Diagnostic] = null

def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
typr.println(s">>>> StoredError: ${m.message}") // !!! DEBUG
def doReport(dia: Diagnostic)(implicit ctx: Context): Unit = {
typr.println(s">>>> StoredError: ${dia.message}") // !!! DEBUG
if (infos == null) infos = new mutable.ListBuffer
infos += m
infos += dia
}

override def hasUnreportedErrors: Boolean =
Expand All @@ -34,11 +34,11 @@ class StoreReporter(outer: Reporter) extends Reporter {
override def hasStickyErrors: Boolean =
infos != null && infos.exists(_.isInstanceOf[StickyError])

override def removeBufferedMessages(implicit ctx: Context): List[MessageContainer] =
override def removeBufferedMessages(implicit ctx: Context): List[Diagnostic] =
if (infos != null) try infos.toList finally infos = null
else Nil

override def pendingMessages(using Context): List[MessageContainer] = infos.toList
override def pendingMessages(using Context): List[Diagnostic] = infos.toList

override def errorsReported: Boolean = hasErrors || (outer != null && outer.errorsReported)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ package dotc
package reporting

import core.Contexts.Context
import diagnostic.MessageContainer
import diagnostic.Diagnostic
import diagnostic.messages.Error

/**
* This class implements a Reporter that throws all errors and sends warnings and other
* info to the underlying reporter.
*/
class ThrowingReporter(reportInfo: Reporter) extends Reporter {
def doReport(m: MessageContainer)(implicit ctx: Context): Unit = m match {
case _: Error => throw m
case _ => reportInfo.doReport(m)
def doReport(dia: Diagnostic)(implicit ctx: Context): Unit = dia match {
case _: Error => throw dia
case _ => reportInfo.doReport(dia)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ package reporting
import scala.collection.mutable
import util.SourceFile
import core.Contexts.Context
import diagnostic.MessageContainer
import diagnostic.Diagnostic

/** This trait implements `isHidden` so that multiple messages per position
* are suppressed, unless they are of increasing severity. */
Expand All @@ -16,14 +16,14 @@ trait UniqueMessagePositions extends Reporter {
/** Logs a position and returns true if it was already logged.
* @note Two positions are considered identical for logging if they have the same point.
*/
override def isHidden(m: MessageContainer)(implicit ctx: Context): Boolean =
super.isHidden(m) || {
m.pos.exists && !ctx.settings.YshowSuppressedErrors.value && {
override def isHidden(dia: Diagnostic)(implicit ctx: Context): Boolean =
super.isHidden(dia) || {
dia.pos.exists && !ctx.settings.YshowSuppressedErrors.value && {
var shouldHide = false
for (pos <- m.pos.start to m.pos.end)
for (pos <- dia.pos.start to dia.pos.end)
positions get (ctx.source, pos) match {
case Some(level) if level >= m.level => shouldHide = true
case _ => positions((ctx.source, pos)) = m.level
case Some(level) if level >= dia.level => shouldHide = true
case _ => positions((ctx.source, pos)) = dia.level
}
shouldHide
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,27 @@ import core.Contexts.Context

import java.util.Optional

object MessageContainer {
object Diagnostic {
val nonSensicalStartTag: String = "<nonsensical>"
val nonSensicalEndTag: String = "</nonsensical>"

implicit class MessageContext(val c: Context) extends AnyVal {
def shouldExplain(cont: MessageContainer): Boolean = {
def shouldExplain(dia: Diagnostic): Boolean = {
implicit val ctx = c
cont.contained.explanation match {
dia.contained.explanation match {
case "" => false
case _ => ctx.settings.explain.value
}
}
}
}

class MessageContainer(
class Diagnostic(
val contained: Message,
val pos: SourcePosition,
val level: Int
) extends Exception with interfaces.Diagnostic {
import MessageContainer._
import Diagnostic._
private var myMsg: String = null
private var myIsNonSensical: Boolean = false
private var myContained: Message = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ object Message {
* see where old errors still exist
*/
implicit def toNoExplanation(str: => String): Message =
new NoExplanation(str)
NoExplanation(str)
}

/** A `Message` contains all semantic information necessary to easily
* comprehend what caused the message to be logged. Each message can be turned
* into a `MessageContainer` which contains the log level and can later be
* into a `Diagnostic` which contains the log level and can later be
* consumed by a subclass of `Reporter`. However, the error position is only
* part of `MessageContainer`, not `Message`.
* part of `Diagnostic`, not `Message`.
*
* NOTE: you should not be persisting Most messages take an implicit
* `Context` and these contexts weigh in at about 4mb per instance, as such
Expand All @@ -40,7 +40,7 @@ abstract class Message(val errorId: ErrorMessageID) { self =>
* > found: Int
*
* This message will be placed underneath the position given by the enclosing
* `MessageContainer`
* `Diagnostic`
*/
def msg: String

Expand Down
Loading

0 comments on commit 540cdec

Please sign in to comment.