diff options
Diffstat (limited to 'src/dotty/tools/dotc/reporting/diagnostic/Message.scala')
-rw-r--r-- | src/dotty/tools/dotc/reporting/diagnostic/Message.scala | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/src/dotty/tools/dotc/reporting/diagnostic/Message.scala b/src/dotty/tools/dotc/reporting/diagnostic/Message.scala new file mode 100644 index 000000000..90ebf11a2 --- /dev/null +++ b/src/dotty/tools/dotc/reporting/diagnostic/Message.scala @@ -0,0 +1,63 @@ +package dotty.tools +package dotc +package reporting +package diagnostic + +import util.SourcePosition + +import java.util.Optional + +object Message { + val nonSensicalStartTag = "<nonsensical>" + val nonSensicalEndTag = "</nonsensical>" +} + +class Message( + msgFn: => String, + val pos: SourcePosition, + val level: Int, + val kind: String, + val explanation: 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 <nonsensical> + * 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 +} + +object NoExplanation { + def unapply(m: Message): Option[Message] = + if (m.explanation == "") Some(m) + else None +} |