Skip to content

Commit 26abe45

Browse files
committed
Refactor common error messages to diagnostic.basic
1 parent a4070dd commit 26abe45

File tree

8 files changed

+120
-44
lines changed

8 files changed

+120
-44
lines changed

interfaces/src/main/java/dotty/tools/dotc/interfaces/Diagnostic.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ public interface Diagnostic {
2020
/** @return The message to report */
2121
String message();
2222

23+
/** @return The explanation behind the message */
24+
String explanation();
25+
2326
/** @return Level of the diagnostic, can be either ERROR, WARNING or INFO */
2427
int level();
2528

src/dotty/tools/dotc/reporting/ConsoleReporter.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Reporter._
99
import java.io.{ BufferedReader, IOException, PrintWriter }
1010
import scala.reflect.internal.util._
1111
import diagnostic.Message
12+
import diagnostic.basic._
1213

1314
/**
1415
* This class implements a Reporter that displays messages on a text

src/dotty/tools/dotc/reporting/Reporter.scala

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4,49 +4,17 @@ package reporting
44

55
import core.Contexts._
66
import util.{SourcePosition, NoSourcePosition}
7-
import util.{SourceFile, NoSource}
87
import core.Decorators.PhaseListDecorator
98
import collection.mutable
10-
import config.Settings.Setting
119
import config.Printers
1210
import java.lang.System.currentTimeMillis
1311
import core.Mode
14-
import interfaces.Diagnostic.{ERROR, WARNING, INFO}
1512
import dotty.tools.dotc.core.Symbols.Symbol
1613
import diagnostic.Message
1714
import ErrorMessages._
15+
import diagnostic.basic._
1816

1917
object Reporter {
20-
class Error(msgFn: => String, pos: SourcePosition, kind: String = "Error")
21-
extends Message(msgFn, pos, ERROR, kind)
22-
23-
class Warning(msgFn: => String, pos: SourcePosition, kind: String = "Warning")
24-
extends Message(msgFn, pos, WARNING, kind)
25-
26-
class Info(msgFn: => String, pos: SourcePosition, kind: String = "Info")
27-
extends Message(msgFn, pos, INFO, kind)
28-
29-
abstract class ConditionalWarning(msgFn: => String, pos: SourcePosition, kind: String)
30-
extends Warning(msgFn, pos, kind) {
31-
def enablingOption(implicit ctx: Context): Setting[Boolean]
32-
}
33-
class FeatureWarning(msgFn: => String, pos: SourcePosition, kind: String = "Feature Warning")
34-
extends ConditionalWarning(msgFn, pos, kind) {
35-
def enablingOption(implicit ctx: Context) = ctx.settings.feature
36-
}
37-
class UncheckedWarning(msgFn: => String, pos: SourcePosition, kind: String = "Unchecked Warning")
38-
extends ConditionalWarning(msgFn, pos, kind) {
39-
def enablingOption(implicit ctx: Context) = ctx.settings.unchecked
40-
}
41-
class DeprecationWarning(msgFn: => String, pos: SourcePosition, kind: String = "Deprecation Warning")
42-
extends ConditionalWarning(msgFn, pos, kind) {
43-
def enablingOption(implicit ctx: Context) = ctx.settings.deprecation
44-
}
45-
class MigrationWarning(msgFn: => String, pos: SourcePosition, kind: String = "Migration Warning") extends
46-
ConditionalWarning(msgFn, pos, kind) {
47-
def enablingOption(implicit ctx: Context) = ctx.settings.migration
48-
}
49-
5018
/** Convert a SimpleReporter into a real Reporter */
5119
def fromSimpleReporter(simple: interfaces.SimpleReporter): Reporter =
5220
new Reporter with UniqueMessagePositions with HideNonSensicalMessages {

src/dotty/tools/dotc/reporting/StoreReporter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package reporting
44

55
import core.Contexts.Context
66
import collection.mutable
7-
import Reporter.{Error, Warning}
87
import config.Printers._
98
import diagnostic.Message
9+
import diagnostic.basic._
1010

1111
/**
1212
* This class implements a Reporter that stores all messages

src/dotty/tools/dotc/reporting/ThrowingReporter.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package reporting
55
import core.Contexts.Context
66
import collection.mutable
77
import diagnostic.Message
8+
import diagnostic.basic.Error
89
import Reporter._
910

1011
/**

src/dotty/tools/dotc/reporting/diagnostic/Diagnostic.scala renamed to src/dotty/tools/dotc/reporting/diagnostic/Message.scala

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,13 @@ object Message {
1212
val nonSensicalEndTag = "</nonsensical>"
1313
}
1414

15-
class Message(msgFn: => String, val pos: SourcePosition, val level: Int, val kind: String)
16-
extends Exception with interfaces.Diagnostic {
15+
class Message(
16+
msgFn: => String,
17+
val pos: SourcePosition,
18+
val level: Int,
19+
val kind: String,
20+
val explanation: String
21+
) extends Exception with interfaces.Diagnostic {
1722
import Message._
1823
private var myMsg: String = null
1924
private var myIsNonSensical: Boolean = false
@@ -27,22 +32,32 @@ extends Exception with interfaces.Diagnostic {
2732
myMsg = msgFn
2833
if (myMsg.contains(nonSensicalStartTag)) {
2934
myIsNonSensical = true
30-
// myMsg might be composed of several d"..." invocations -> nested nonsensical tags possible
31-
myMsg = myMsg.replaceAllLiterally(nonSensicalStartTag, "").replaceAllLiterally(nonSensicalEndTag, "")
35+
// myMsg might be composed of several d"..." invocations -> nested
36+
// nonsensical tags possible
37+
myMsg =
38+
myMsg
39+
.replaceAllLiterally(nonSensicalStartTag, "")
40+
.replaceAllLiterally(nonSensicalEndTag, "")
3241
}
3342
}
3443
myMsg
3544
}
3645

37-
/** A message is non-sensical if it contains references to <nonsensical> tags.
38-
* Such tags are inserted by the error diagnostic framework if a message
39-
* contains references to internally generated error types. Normally we
40-
* want to suppress error messages referring to types like this because
41-
* they look weird and are normally follow-up errors to something that
42-
* was diagnosed before.
46+
/** A message is non-sensical if it contains references to <nonsensical>
47+
* tags. Such tags are inserted by the error diagnostic framework if a
48+
* message contains references to internally generated error types. Normally
49+
* we want to suppress error messages referring to types like this because
50+
* they look weird and are normally follow-up errors to something that was
51+
* diagnosed before.
4352
*/
4453
def isNonSensical = { message; myIsNonSensical }
4554

4655
override def toString = s"$getClass at $pos: $message"
4756
override def getMessage() = message
4857
}
58+
59+
object NoExplanation {
60+
def unapply(m: Message): Option[Message] =
61+
if (m.explanation == "") Some(m)
62+
else None
63+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package dotty.tools
2+
package dotc
3+
package reporting
4+
package diagnostic
5+
6+
import dotc.core.Contexts.Context
7+
import util.{SourceFile, NoSource}
8+
import util.{SourcePosition, NoSourcePosition}
9+
import config.Settings.Setting
10+
import interfaces.Diagnostic.{ERROR, WARNING, INFO}
11+
12+
object basic {
13+
14+
class Error(
15+
msgFn: => String,
16+
pos: SourcePosition,
17+
kind: String = "Error",
18+
explanation: String = ""
19+
) extends Message(msgFn, pos, ERROR, kind, explanation)
20+
21+
class Warning(
22+
msgFn: => String,
23+
pos: SourcePosition,
24+
kind: String = "Warning",
25+
explanation: String = ""
26+
) extends Message(msgFn, pos, WARNING, kind, explanation)
27+
28+
class Info(
29+
msgFn: => String,
30+
pos: SourcePosition,
31+
kind: String = "Info",
32+
explanation: String = ""
33+
) extends Message(msgFn, pos, INFO, kind, explanation)
34+
35+
abstract class ConditionalWarning(
36+
msgFn: => String,
37+
pos: SourcePosition,
38+
kind: String,
39+
explanation: String = ""
40+
) extends Warning(msgFn, pos, kind, explanation) {
41+
def enablingOption(implicit ctx: Context): Setting[Boolean]
42+
}
43+
44+
class FeatureWarning(
45+
msgFn: => String,
46+
pos: SourcePosition,
47+
kind: String = "Feature Warning",
48+
explanation: String = ""
49+
) extends ConditionalWarning(msgFn, pos, kind, explanation) {
50+
def enablingOption(implicit ctx: Context) = ctx.settings.feature
51+
}
52+
53+
class UncheckedWarning(
54+
msgFn: => String,
55+
pos: SourcePosition,
56+
kind: String = "Unchecked Warning",
57+
explanation: String = ""
58+
) extends ConditionalWarning(msgFn, pos, kind, explanation) {
59+
def enablingOption(implicit ctx: Context) = ctx.settings.unchecked
60+
}
61+
62+
class DeprecationWarning(
63+
msgFn: => String,
64+
pos: SourcePosition,
65+
kind: String = "Deprecation Warning",
66+
explanation: String = ""
67+
) extends ConditionalWarning(msgFn, pos, kind, explanation) {
68+
def enablingOption(implicit ctx: Context) = ctx.settings.deprecation
69+
}
70+
71+
class MigrationWarning(
72+
msgFn: => String,
73+
pos: SourcePosition,
74+
kind: String = "Migration Warning",
75+
explanation: String = ""
76+
) extends ConditionalWarning(msgFn, pos, kind, explanation) {
77+
def enablingOption(implicit ctx: Context) = ctx.settings.migration
78+
}
79+
80+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package dotty.tools
2+
package dotc
3+
package reporting
4+
package diagnostic
5+
6+
object parser {
7+
8+
}

0 commit comments

Comments
 (0)