forked from scala/scala3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDiagnostic.scala
116 lines (97 loc) · 3.89 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
107
108
109
110
111
112
113
114
115
116
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 dotty.tools.dotc.util.chaining.*
import java.util.{Collections, Optional, List => JList}
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)
/** A Warning with an origin. The semantics of `origin` depend on the warning.
* For example, an unused import warning has an origin that specifies the unused selector.
* The origin of a deprecation is the deprecated element.
*/
trait OriginWarning(val origin: String):
self: Warning =>
/** Lints are likely to be filtered. Provide extra axes for filtering by `-Wconf`.
*/
class LintWarning(msg: Message, pos: SourcePosition, origin: String)
extends Warning(msg, pos), OriginWarning(origin)
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, origin: String)
extends ConditionalWarning(msg, pos), OriginWarning(origin):
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 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 diagnosticRelatedInformation: JList[interfaces.DiagnosticRelatedInformation] =
Collections.emptyList()
override def toString: String = s"$getClass at $pos L${pos.line+1}: $message"
end Diagnostic