aboutsummaryrefslogtreecommitdiff
path: root/src/dotty/tools/dotc/reporting/diagnostic/Message.scala
blob: b3c19820fa08737b91f0217b34bcd5572d623b4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package dotty.tools
package dotc
package reporting
package diagnostic

import util.SourcePosition
import core.Contexts.Context

import java.util.Optional

object Message {
  val nonSensicalStartTag = "<nonsensical>"
  val nonSensicalEndTag = "</nonsensical>"

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

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
}