-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Diagnostic.scala
106 lines (89 loc) · 3.24 KB
/
Diagnostic.scala
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
package dotty.tools
package dotc
package reporting
import scala.language.unsafeNulls
import dotty.tools.dotc.config.Settings.Setting
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.interfaces.Diagnostic.{ERROR, INFO, WARNING}
import dotty.tools.dotc.util.SourcePosition
import java.util.Optional
import scala.util.chaining._
import core.Decorators.toMessage
object Diagnostic:
def shouldExplain(dia: Diagnostic)(using Context): Boolean =
ctx.settings.explain.value && dia.msg.canExplain
|| ctx.settings.explainTypes.value && dia.msg.isInstanceOf[TypeMismatchMsg]
// keep old explain-types behavior for backwards compatibility and cross-compilation
// `Diagnostics to be consumed by `Reporter` ---------------------- //
class Error(
msg: Message,
pos: SourcePosition
) extends Diagnostic(msg, pos, ERROR):
def this(str: => String, pos: SourcePosition) = this(str.toMessage, pos)
/** A sticky error is an error that should not be hidden by backtracking and
* trying some alternative path. Typically, errors issued after catching
* a TypeError exception are sticky.
*/
class StickyError(
msg: Message,
pos: SourcePosition
) extends Error(msg, pos)
class Warning(
msg: Message,
pos: SourcePosition
) extends Diagnostic(msg, pos, WARNING) {
def toError: Error = new Error(msg, pos).tap(e => if isVerbose then e.setVerbose())
def toInfo: Info = new Info(msg, pos).tap(e => if isVerbose then e.setVerbose())
def isSummarizedConditional(using Context): Boolean = false
}
class Info(
msg: Message,
pos: SourcePosition
) extends Diagnostic(msg, pos, INFO):
def this(str: => String, pos: SourcePosition) = this(str.toMessage, pos)
abstract class ConditionalWarning(
msg: Message,
pos: SourcePosition
) extends Warning(msg, pos) {
def enablingOption(using Context): Setting[Boolean]
override def isSummarizedConditional(using Context): Boolean = !enablingOption.value
}
class FeatureWarning(
msg: Message,
pos: SourcePosition
) extends ConditionalWarning(msg, pos) {
def enablingOption(using Context): Setting[Boolean] = ctx.settings.feature
}
class UncheckedWarning(
msg: Message,
pos: SourcePosition
) extends ConditionalWarning(msg, pos) {
def enablingOption(using Context): Setting[Boolean] = ctx.settings.unchecked
}
class DeprecationWarning(
msg: Message,
pos: SourcePosition
) extends ConditionalWarning(msg, pos) {
def enablingOption(using Context): Setting[Boolean] = ctx.settings.deprecation
}
class MigrationWarning(
msg: Message,
pos: SourcePosition
) extends Warning(msg, pos)
class Diagnostic(
val msg: Message,
val pos: SourcePosition,
val level: Int
) extends Exception with interfaces.Diagnostic:
private var verbose: Boolean = false
def isVerbose: Boolean = verbose
def setVerbose(): this.type =
verbose = true
this
override def position: Optional[interfaces.SourcePosition] =
if (pos.exists && pos.source.exists) Optional.of(pos) else Optional.empty()
override def message: String =
msg.message.replaceAll("\u001B\\[[;\\d]*m", "")
override def toString: String = s"$getClass at $pos: $message"
override def getMessage(): String = message
end Diagnostic