From 18a69f7bd230bc06696e41be53a6735aa6e94ccc Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Fri, 16 Sep 2016 13:34:57 +0200 Subject: Rename Diagnostic to diagnostic.Message --- .../tools/dotc/reporting/ConsoleReporter.scala | 19 +++++---- src/dotty/tools/dotc/reporting/Diagnostic.scala | 47 --------------------- .../dotc/reporting/HideNonSensicalMessages.scala | 7 ++-- src/dotty/tools/dotc/reporting/Reporter.scala | 43 +++++++++++-------- src/dotty/tools/dotc/reporting/StoreReporter.scala | 13 +++--- .../tools/dotc/reporting/ThrowingReporter.scala | 7 ++-- .../dotc/reporting/UniqueMessagePositions.scala | 13 +++--- .../dotc/reporting/diagnostic/Diagnostic.scala | 48 ++++++++++++++++++++++ 8 files changed, 107 insertions(+), 90 deletions(-) delete mode 100644 src/dotty/tools/dotc/reporting/Diagnostic.scala create mode 100644 src/dotty/tools/dotc/reporting/diagnostic/Diagnostic.scala (limited to 'src/dotty/tools/dotc/reporting') diff --git a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala index 1d9423b70..1ed889683 100644 --- a/src/dotty/tools/dotc/reporting/ConsoleReporter.scala +++ b/src/dotty/tools/dotc/reporting/ConsoleReporter.scala @@ -8,6 +8,7 @@ import core.Contexts._ import Reporter._ import java.io.{ BufferedReader, IOException, PrintWriter } import scala.reflect.internal.util._ +import diagnostic.Message /** * This class implements a Reporter that displays messages on a text @@ -40,17 +41,17 @@ class ConsoleReporter( } } - override def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match { - case d: Error => - printMessageAndPos(d.message, d.pos, d.kind) + override def doReport(m: Message)(implicit ctx: Context): Unit = m match { + case m: Error => + printMessageAndPos(m.message, m.pos, m.kind) if (ctx.settings.prompt.value) displayPrompt() - case d: ConditionalWarning if !d.enablingOption.value => - case d: MigrationWarning => - printMessageAndPos(d.message, d.pos, d.kind) - case d: Warning => - printMessageAndPos(d.message, d.pos, d.kind) + case m: ConditionalWarning if !m.enablingOption.value => + case m: MigrationWarning => + printMessageAndPos(m.message, m.pos, m.kind) + case m: Warning => + printMessageAndPos(m.message, m.pos, m.kind) case _ => - printMessageAndPos(d.message, d.pos, d.kind) + printMessageAndPos(m.message, m.pos, m.kind) } def displayPrompt(): Unit = { diff --git a/src/dotty/tools/dotc/reporting/Diagnostic.scala b/src/dotty/tools/dotc/reporting/Diagnostic.scala deleted file mode 100644 index d08636802..000000000 --- a/src/dotty/tools/dotc/reporting/Diagnostic.scala +++ /dev/null @@ -1,47 +0,0 @@ -package dotty.tools -package dotc -package reporting - -import util.SourcePosition - -import java.util.Optional - -object Diagnostic { - val nonSensicalStartTag = "" - val nonSensicalEndTag = "" -} - -class Diagnostic(msgFn: => String, val pos: SourcePosition, val level: Int, val kind: String) - extends Exception with interfaces.Diagnostic { - import Diagnostic._ - private var myMsg: String = null - private var myIsNonSensical: Boolean = false - - override def position: Optional[interfaces.SourcePosition] = - if (pos.exists && pos.source.exists) Optional.of(pos) else Optional.empty() - - /** The message to report */ - def message: String = { - if (myMsg == null) { - myMsg = msgFn - if (myMsg.contains(nonSensicalStartTag)) { - myIsNonSensical = true - // myMsg might be composed of several d"..." invocations -> nested nonsensical tags possible - myMsg = myMsg.replaceAllLiterally(nonSensicalStartTag, "").replaceAllLiterally(nonSensicalEndTag, "") - } - } - myMsg - } - - /** A message is non-sensical if it contains references to tags. - * Such tags are inserted by the error diagnostic framework if a message - * contains references to internally generated error types. Normally we - * want to suppress error messages referring to types like this because - * they look weird and are normally follow-up errors to something that - * was diagnosed before. - */ - def isNonSensical = { message; myIsNonSensical } - - override def toString = s"$getClass at $pos: $message" - override def getMessage() = message -} diff --git a/src/dotty/tools/dotc/reporting/HideNonSensicalMessages.scala b/src/dotty/tools/dotc/reporting/HideNonSensicalMessages.scala index a325fe9f7..140777275 100644 --- a/src/dotty/tools/dotc/reporting/HideNonSensicalMessages.scala +++ b/src/dotty/tools/dotc/reporting/HideNonSensicalMessages.scala @@ -3,6 +3,7 @@ package dotc package reporting import core.Contexts.Context +import diagnostic.Message /** * This trait implements `isHidden` so that we avoid reporting non-sensical messages. @@ -11,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(d: Diagnostic)(implicit ctx: Context): Boolean = - super.isHidden(d) || { - d.isNonSensical && + override def isHidden(m: Message)(implicit ctx: Context): Boolean = + super.isHidden(m) || { + m.isNonSensical && hasErrors && // if there are no errors yet, report even if diagnostic is non-sensical !ctx.settings.YshowSuppressedErrors.value } diff --git a/src/dotty/tools/dotc/reporting/Reporter.scala b/src/dotty/tools/dotc/reporting/Reporter.scala index db0922b2e..8c9308ba5 100644 --- a/src/dotty/tools/dotc/reporting/Reporter.scala +++ b/src/dotty/tools/dotc/reporting/Reporter.scala @@ -13,36 +13,47 @@ import java.lang.System.currentTimeMillis import core.Mode import interfaces.Diagnostic.{ERROR, WARNING, INFO} import dotty.tools.dotc.core.Symbols.Symbol +import diagnostic.Message import ErrorMessages._ object Reporter { - class Error(msgFn: => String, pos: SourcePosition, kind: String = "Error") extends Diagnostic(msgFn, pos, ERROR, kind) - class Warning(msgFn: => String, pos: SourcePosition, kind: String = "Warning") extends Diagnostic(msgFn, pos, WARNING, kind) - class Info(msgFn: => String, pos: SourcePosition, kind: String = "Info") extends Diagnostic(msgFn, pos, INFO, kind) + class Error(msgFn: => String, pos: SourcePosition, kind: String = "Error") + extends Message(msgFn, pos, ERROR, kind) - abstract class ConditionalWarning(msgFn: => String, pos: SourcePosition, kind: String) extends Warning(msgFn, pos, kind) { + class Warning(msgFn: => String, pos: SourcePosition, kind: String = "Warning") + extends Message(msgFn, pos, WARNING, kind) + + class Info(msgFn: => String, pos: SourcePosition, kind: String = "Info") + extends Message(msgFn, pos, INFO, kind) + + abstract class ConditionalWarning(msgFn: => String, pos: SourcePosition, kind: String) + extends Warning(msgFn, pos, kind) { def enablingOption(implicit ctx: Context): Setting[Boolean] } - class FeatureWarning(msgFn: => String, pos: SourcePosition, kind: String = "Feature Warning") extends ConditionalWarning(msgFn, pos, kind) { + class FeatureWarning(msgFn: => String, pos: SourcePosition, kind: String = "Feature Warning") + extends ConditionalWarning(msgFn, pos, kind) { def enablingOption(implicit ctx: Context) = ctx.settings.feature } - class UncheckedWarning(msgFn: => String, pos: SourcePosition, kind: String = "Unchecked Warning") extends ConditionalWarning(msgFn, pos, kind) { + class UncheckedWarning(msgFn: => String, pos: SourcePosition, kind: String = "Unchecked Warning") + extends ConditionalWarning(msgFn, pos, kind) { def enablingOption(implicit ctx: Context) = ctx.settings.unchecked } - class DeprecationWarning(msgFn: => String, pos: SourcePosition, kind: String = "Deprecation Warning") extends ConditionalWarning(msgFn, pos, kind) { + class DeprecationWarning(msgFn: => String, pos: SourcePosition, kind: String = "Deprecation Warning") + extends ConditionalWarning(msgFn, pos, kind) { def enablingOption(implicit ctx: Context) = ctx.settings.deprecation } - class MigrationWarning(msgFn: => String, pos: SourcePosition, kind: String = "Migration Warning") extends ConditionalWarning(msgFn, pos, kind) { + class MigrationWarning(msgFn: => String, pos: SourcePosition, kind: String = "Migration Warning") extends + ConditionalWarning(msgFn, pos, kind) { def enablingOption(implicit ctx: Context) = ctx.settings.migration } /** Convert a SimpleReporter into a real Reporter */ def fromSimpleReporter(simple: interfaces.SimpleReporter): Reporter = new Reporter with UniqueMessagePositions with HideNonSensicalMessages { - override def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match { - case d: ConditionalWarning if !d.enablingOption.value => + override def doReport(m: Message)(implicit ctx: Context): Unit = m match { + case m: ConditionalWarning if !m.enablingOption.value => case _ => - simple.report(d) + simple.report(m) } } } @@ -211,7 +222,7 @@ trait Reporting { this: Context => abstract class Reporter extends interfaces.ReporterResult { /** Report a diagnostic */ - def doReport(d: Diagnostic)(implicit ctx: Context): Unit + def doReport(d: Message)(implicit ctx: Context): Unit /** Whether very long lines can be truncated. This exists so important * debugging information (like printing the classpath) is not rendered @@ -226,7 +237,7 @@ abstract class Reporter extends interfaces.ReporterResult { finally _truncationOK = saved } - type ErrorHandler = Diagnostic => Context => Unit + type ErrorHandler = Message => Context => Unit private var incompleteHandler: ErrorHandler = d => c => report(d)(c) def withIncompleteHandler[T](handler: ErrorHandler)(op: => T): T = { val saved = incompleteHandler @@ -255,7 +266,7 @@ abstract class Reporter extends interfaces.ReporterResult { override def default(key: String) = 0 } - def report(d: Diagnostic)(implicit ctx: Context): Unit = + def report(d: Message)(implicit ctx: Context): Unit = if (!isHidden(d)) { doReport(d)(ctx.addMode(Mode.Printing)) d match { @@ -269,7 +280,7 @@ abstract class Reporter extends interfaces.ReporterResult { } } - def incomplete(d: Diagnostic)(implicit ctx: Context): Unit = + def incomplete(d: Message)(implicit ctx: Context): Unit = incompleteHandler(d)(ctx) @@ -302,7 +313,7 @@ abstract class Reporter extends interfaces.ReporterResult { } /** Should this diagnostic not be reported at all? */ - def isHidden(d: Diagnostic)(implicit ctx: Context): Boolean = ctx.mode.is(Mode.Printing) + def isHidden(m: Message)(implicit ctx: Context): Boolean = ctx.mode.is(Mode.Printing) /** Does this reporter contain not yet reported errors or warnings? */ def hasPending: Boolean = false diff --git a/src/dotty/tools/dotc/reporting/StoreReporter.scala b/src/dotty/tools/dotc/reporting/StoreReporter.scala index b7b7c1af0..dd5935f4c 100644 --- a/src/dotty/tools/dotc/reporting/StoreReporter.scala +++ b/src/dotty/tools/dotc/reporting/StoreReporter.scala @@ -6,24 +6,25 @@ import core.Contexts.Context import collection.mutable import Reporter.{Error, Warning} import config.Printers.typr +import diagnostic.Message /** * This class implements a Reporter that stores all messages */ class StoreReporter(outer: Reporter) extends Reporter { - private var infos: mutable.ListBuffer[Diagnostic] = null + private var infos: mutable.ListBuffer[Message] = null - def doReport(d: Diagnostic)(implicit ctx: Context): Unit = { - typr.println(s">>>> StoredError: ${d.message}") // !!! DEBUG + def doReport(m: Message)(implicit ctx: Context): Unit = { + typr.println(s">>>> StoredError: ${m.message}") // !!! DEBUG if (infos == null) infos = new mutable.ListBuffer - infos += d + infos += m } override def hasPending: Boolean = infos != null && { infos exists { - case d: Error => true - case d: Warning => true + case _: Error => true + case _: Warning => true case _ => false } } diff --git a/src/dotty/tools/dotc/reporting/ThrowingReporter.scala b/src/dotty/tools/dotc/reporting/ThrowingReporter.scala index 026453036..25adc6a41 100644 --- a/src/dotty/tools/dotc/reporting/ThrowingReporter.scala +++ b/src/dotty/tools/dotc/reporting/ThrowingReporter.scala @@ -4,6 +4,7 @@ package reporting import core.Contexts.Context import collection.mutable +import diagnostic.Message import Reporter._ /** @@ -11,8 +12,8 @@ import Reporter._ * info to the underlying reporter. */ class ThrowingReporter(reportInfo: Reporter) extends Reporter { - def doReport(d: Diagnostic)(implicit ctx: Context): Unit = d match { - case _: Error => throw d - case _ => reportInfo.doReport(d) + def doReport(m: Message)(implicit ctx: Context): Unit = m match { + case _: Error => throw m + case _ => reportInfo.doReport(m) } } diff --git a/src/dotty/tools/dotc/reporting/UniqueMessagePositions.scala b/src/dotty/tools/dotc/reporting/UniqueMessagePositions.scala index 32554e6b6..1ef8b3447 100644 --- a/src/dotty/tools/dotc/reporting/UniqueMessagePositions.scala +++ b/src/dotty/tools/dotc/reporting/UniqueMessagePositions.scala @@ -5,6 +5,7 @@ package reporting import scala.collection.mutable import util.{SourcePosition, SourceFile} import core.Contexts.Context +import diagnostic.Message /** * This trait implements `isHidden` so that multiple messages per position @@ -17,12 +18,12 @@ 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(d: Diagnostic)(implicit ctx: Context): Boolean = - super.isHidden(d) || { - d.pos.exists && { - positions get (ctx.source, d.pos.point) match { - case Some(level) if level >= d.level => true - case _ => positions((ctx.source, d.pos.point)) = d.level; false + override def isHidden(m: Message)(implicit ctx: Context): Boolean = + super.isHidden(m) || { + m.pos.exists && { + positions get (ctx.source, m.pos.point) match { + case Some(level) if level >= m.level => true + case _ => positions((ctx.source, m.pos.point)) = m.level; false } } } diff --git a/src/dotty/tools/dotc/reporting/diagnostic/Diagnostic.scala b/src/dotty/tools/dotc/reporting/diagnostic/Diagnostic.scala new file mode 100644 index 000000000..230c1edc0 --- /dev/null +++ b/src/dotty/tools/dotc/reporting/diagnostic/Diagnostic.scala @@ -0,0 +1,48 @@ +package dotty.tools +package dotc +package reporting +package diagnostic + +import util.SourcePosition + +import java.util.Optional + +object Message { + val nonSensicalStartTag = "" + val nonSensicalEndTag = "" +} + +class Message(msgFn: => String, val pos: SourcePosition, val level: Int, val kind: String) +extends Exception with interfaces.Diagnostic { + import Message._ + private var myMsg: String = null + private var myIsNonSensical: Boolean = false + + override def position: Optional[interfaces.SourcePosition] = + if (pos.exists && pos.source.exists) Optional.of(pos) else Optional.empty() + + /** The message to report */ + def message: String = { + if (myMsg == null) { + myMsg = msgFn + if (myMsg.contains(nonSensicalStartTag)) { + myIsNonSensical = true + // myMsg might be composed of several d"..." invocations -> nested nonsensical tags possible + myMsg = myMsg.replaceAllLiterally(nonSensicalStartTag, "").replaceAllLiterally(nonSensicalEndTag, "") + } + } + myMsg + } + + /** A message is non-sensical if it contains references to tags. + * Such tags are inserted by the error diagnostic framework if a message + * contains references to internally generated error types. Normally we + * want to suppress error messages referring to types like this because + * they look weird and are normally follow-up errors to something that + * was diagnosed before. + */ + def isNonSensical = { message; myIsNonSensical } + + override def toString = s"$getClass at $pos: $message" + override def getMessage() = message +} -- cgit v1.2.3