Skip to content

Commit 575e4a7

Browse files
committed
Complete better structure to diagnostic messages
1 parent 556c762 commit 575e4a7

15 files changed

+201
-150
lines changed

src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class ScalaSettings extends Settings.SettingGroup {
2323
val migration = BooleanSetting("-migration", "Emit warning and location for migration issues from Scala 2.")
2424
val encoding = StringSetting("-encoding", "encoding", "Specify character encoding used by source files.", Properties.sourceEncoding)
2525
val explaintypes = BooleanSetting("-explaintypes", "Explain type errors in more detail.")
26-
val explainerrors = BooleanSetting("-explain", "Explain errors in more detail.")
26+
val explain = BooleanSetting("-explain", "Explain errors in more detail.")
2727
val feature = BooleanSetting("-feature", "Emit warning and location for usages of features that should be imported explicitly.")
2828
val g = ChoiceSetting("-g", "level", "Set level of generated debugging info.", List("none", "source", "line", "vars", "notailcalls"), "vars")
2929
val help = BooleanSetting("-help", "Print a synopsis of standard options")

src/dotty/tools/dotc/parsing/JavaParsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import scala.reflect.internal.util.Collections._
2929
object JavaParsers {
3030

3131
import ast.untpd._
32-
import reporting.ErrorMessages.Syntax._
32+
import reporting.diagnostic.syntax._
3333

3434
class JavaParser(source: SourceFile)(implicit ctx: Context) extends ParserCommon(source) {
3535

src/dotty/tools/dotc/parsing/MarkupParsers.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import Utility._
3232
object MarkupParsers {
3333

3434
import ast.untpd._
35-
import reporting.ErrorMessages.Syntax._
35+
import reporting.diagnostic.syntax._
3636

3737
case object MissingEndTagControl extends ControlThrowable {
3838
override def getMessage = "start tag was here: "

src/dotty/tools/dotc/parsing/Parsers.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ import ast.Trees._
1717
import Decorators._
1818
import StdNames._
1919
import util.Positions._
20-
import reporting.ErrorMessages._
2120
import Constants._
2221
import ScriptParsers._
2322
import scala.annotation.{tailrec, switch}
@@ -28,7 +27,9 @@ import Scanners.Comment
2827
object Parsers {
2928

3029
import ast.untpd._
31-
import reporting.ErrorMessages.Syntax._
30+
import reporting.diagnostic.MessageCreator
31+
import MessageCreator._
32+
import reporting.diagnostic.syntax._
3233

3334
case class OpInfo(operand: Tree, operator: Name, offset: Offset)
3435

@@ -88,7 +89,7 @@ object Parsers {
8889
/** Issue an error at given offset if beyond last error offset
8990
* and update lastErrorOffset.
9091
*/
91-
def syntaxError(expl: ErrorMessage, offset: Int = in.offset): Unit =
92+
def syntaxError(expl: MessageCreator, offset: Int = in.offset): Unit =
9293
if (offset > lastErrorOffset) {
9394
syntaxError(expl, Position(offset))
9495
lastErrorOffset = in.offset
@@ -97,7 +98,7 @@ object Parsers {
9798
/** Unconditionally issue an error at given position, without
9899
* updating lastErrorOffset.
99100
*/
100-
def syntaxError(expl: ErrorMessage, pos: Position): Unit =
101+
def syntaxError(expl: MessageCreator, pos: Position): Unit =
101102
ctx.explainError(expl, source atPos pos)
102103

103104
}
@@ -204,7 +205,7 @@ object Parsers {
204205
}
205206
}
206207

207-
def warning(msg: ErrorMessage, offset: Int = in.offset) =
208+
def warning(msg: MessageCreator, offset: Int = in.offset) =
208209
ctx.explainWarning(msg, source atPos Position(offset))
209210

210211
def deprecationWarning(msg: String, offset: Int = in.offset) =
@@ -1014,7 +1015,7 @@ object Parsers {
10141015

10151016
handler match {
10161017
case Block(Nil, EmptyTree) =>
1017-
syntaxError(EmptyCatchBlock(body), handler.pos)
1018+
syntaxError(new EmptyCatchBlock(body), handler.pos)
10181019
case _ =>
10191020
}
10201021

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +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._
12+
import diagnostic.messages._
1313

1414
/**
1515
* This class implements a Reporter that displays messages on a text

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

Lines changed: 0 additions & 117 deletions
This file was deleted.

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ import config.Printers
1010
import java.lang.System.currentTimeMillis
1111
import core.Mode
1212
import dotty.tools.dotc.core.Symbols.Symbol
13-
import diagnostic.Message
14-
import ErrorMessages._
15-
import diagnostic.basic._
13+
import diagnostic.messages._
14+
import diagnostic._
15+
import MessageCreator._
1616

1717
object Reporter {
1818
/** Convert a SimpleReporter into a real Reporter */
@@ -75,10 +75,10 @@ trait Reporting { this: Context =>
7575
def warning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
7676
reporter.report(new Warning(msg, pos))
7777

78-
def explainWarning(err: => ErrorMessage, pos: SourcePosition = NoSourcePosition): Unit = {
79-
reporter.report(new Warning(err.msg, pos, s"${err.kind} warning"))
80-
if (this.shouldExplain(err))
81-
reporter.report(new Info(err.explanation, NoSourcePosition))
78+
def explainWarning(msg: => MessageCreator, pos: SourcePosition = NoSourcePosition): Unit = {
79+
reporter.report(msg.warning(pos))
80+
if (this.shouldExplain(msg))
81+
reporter.report(new Info(msg.explanation, NoSourcePosition))
8282
}
8383

8484
def strictWarning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
@@ -90,10 +90,10 @@ trait Reporting { this: Context =>
9090
reporter.report(new Error(msg, pos))
9191
}
9292

93-
def explainError(err: => ErrorMessage, pos: SourcePosition = NoSourcePosition): Unit = {
94-
reporter.report(new Error(err.msg, pos, s"${err.kind} error"))
95-
if (this.shouldExplain(err))
96-
reporter.report(new Info(err.explanation, NoSourcePosition))
93+
def explainError(msg: => MessageCreator, pos: SourcePosition = NoSourcePosition): Unit = {
94+
reporter.report(msg.error(pos))
95+
if (this.shouldExplain(msg))
96+
reporter.report(new Info(msg.explanation, NoSourcePosition))
9797
}
9898

9999
def errorOrMigrationWarning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import core.Contexts.Context
66
import collection.mutable
77
import config.Printers._
88
import diagnostic.Message
9-
import diagnostic.basic._
9+
import diagnostic.messages._
1010

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

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

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

1111
/**

src/dotty/tools/dotc/reporting/diagnostic/Message.scala

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ package reporting
44
package diagnostic
55

66
import util.SourcePosition
7+
import core.Contexts.Context
78

89
import java.util.Optional
910

@@ -55,9 +56,3 @@ class Message(
5556
override def toString = s"$getClass at $pos: $message"
5657
override def getMessage() = message
5758
}
58-
59-
object NoExplanation {
60-
def unapply(m: Message): Option[Message] =
61-
if (m.explanation == "") Some(m)
62-
else None
63-
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package dotty.tools
2+
package dotc
3+
package reporting
4+
package diagnostic
5+
6+
import util.{SourcePosition, NoSourcePosition}
7+
import core.Contexts.Context
8+
9+
object MessageCreator {
10+
implicit class DiagnosticContext(val c: Context) extends AnyVal {
11+
def shouldExplain(msg: MessageCreator): Boolean = {
12+
implicit val ctx: Context = c
13+
msg match {
14+
case NoExplanation(_) => false
15+
case _ => ctx.settings.explain.value
16+
}
17+
}
18+
}
19+
20+
implicit def toNoExplanation(str: String) =
21+
new NoExplanation(str)
22+
}
23+
24+
trait MessageCreator {
25+
import messages._
26+
27+
val msgFn: String = msg
28+
def msg: String
29+
def kind: String
30+
def explanation: String
31+
32+
def error(pos: SourcePosition) =
33+
new Error(msgFn, pos, kind, explanation)
34+
35+
def warning(pos: SourcePosition) =
36+
new Warning(msgFn, pos, kind, explanation)
37+
38+
def info(pos: SourcePosition) =
39+
new Info(msgFn, pos, kind, explanation)
40+
41+
def featureWarnign(pos: SourcePosition) =
42+
new FeatureWarning(msgFn, pos, kind, explanation)
43+
44+
def uncheckedWarning(pos: SourcePosition) =
45+
new UncheckedWarning(msgFn, pos, kind, explanation)
46+
47+
def deprecationWarning(pos: SourcePosition) =
48+
new DeprecationWarning(msgFn, pos, kind, explanation)
49+
50+
def migrationWarning(pos: SourcePosition) =
51+
new MigrationWarning(msgFn, pos, kind, explanation)
52+
}
53+
54+
class NoExplanation(val msg: String) extends MessageCreator {
55+
val explanation = ""
56+
val kind = ""
57+
}
58+
59+
object NoExplanation {
60+
def unapply(m: MessageCreator): Option[MessageCreator] =
61+
if (m.explanation == "") Some(m)
62+
else None
63+
}

src/dotty/tools/dotc/reporting/diagnostic/basic.scala renamed to src/dotty/tools/dotc/reporting/diagnostic/messages.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import util.{SourcePosition, NoSourcePosition}
99
import config.Settings.Setting
1010
import interfaces.Diagnostic.{ERROR, WARNING, INFO}
1111

12-
object basic {
12+
object messages {
1313

1414
class Error(
1515
msgFn: => String,

0 commit comments

Comments
 (0)