Skip to content

Commit 656f3d7

Browse files
committed
Make relevant parts of compiler conform to new error handling
1 parent d4b23e0 commit 656f3d7

20 files changed

+316
-337
lines changed

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

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ import Scanners.Comment
2727
object Parsers {
2828

2929
import ast.untpd._
30-
import reporting.diagnostic.MessageCreator
31-
import MessageCreator._
32-
import reporting.diagnostic.syntax._
30+
import reporting.diagnostic.Message
31+
import reporting.diagnostic.messages._
3332

3433
case class OpInfo(operand: Tree, operator: Name, offset: Offset)
3534

@@ -89,7 +88,7 @@ object Parsers {
8988
/** Issue an error at given offset if beyond last error offset
9089
* and update lastErrorOffset.
9190
*/
92-
def syntaxError(expl: MessageCreator, offset: Int = in.offset): Unit =
91+
def syntaxError(expl: Message, offset: Int = in.offset): Unit =
9392
if (offset > lastErrorOffset) {
9493
syntaxError(expl, Position(offset))
9594
lastErrorOffset = in.offset
@@ -98,8 +97,8 @@ object Parsers {
9897
/** Unconditionally issue an error at given position, without
9998
* updating lastErrorOffset.
10099
*/
101-
def syntaxError(expl: MessageCreator, pos: Position): Unit =
102-
ctx.explainError(expl, source atPos pos)
100+
def syntaxError(expl: Message, pos: Position): Unit =
101+
ctx.error(expl, source atPos pos)
103102

104103
}
105104

@@ -205,20 +204,20 @@ object Parsers {
205204
}
206205
}
207206

208-
def warning(msg: MessageCreator, offset: Int = in.offset) =
209-
ctx.explainWarning(msg, source atPos Position(offset))
207+
def warning(msg: Message, offset: Int = in.offset) =
208+
ctx.warning(msg, source atPos Position(offset))
210209

211-
def deprecationWarning(msg: String, offset: Int = in.offset) =
210+
def deprecationWarning(msg: Message, offset: Int = in.offset) =
212211
ctx.deprecationWarning(msg, source atPos Position(offset))
213212

214213
/** Issue an error at current offset taht input is incomplete */
215-
def incompleteInputError(msg: String) =
214+
def incompleteInputError(msg: Message) =
216215
ctx.incompleteInputError(msg, source atPos Position(in.offset))
217216

218217
/** If at end of file, issue an incompleteInputError.
219218
* Otherwise issue a syntax error and skip to next safe point.
220219
*/
221-
def syntaxErrorOrIncomplete(msg: String) =
220+
def syntaxErrorOrIncomplete(msg: Message) =
222221
if (in.token == EOF) incompleteInputError(msg)
223222
else {
224223
syntaxError(msg)

src/dotty/tools/dotc/printing/Formatting.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import collection.Map
88
import Decorators._
99
import scala.annotation.switch
1010
import scala.util.control.NonFatal
11-
import reporting.diagnostic.Message
11+
import reporting.diagnostic.MessageContainer
1212

1313
object Formatting {
1414

@@ -67,6 +67,7 @@ object Formatting {
6767
*/
6868
class ErrorMessageFormatter(sc: StringContext) extends StringFormatter(sc) {
6969
override protected def showArg(arg: Any)(implicit ctx: Context): String = {
70+
import MessageContainer._
7071
def isSensical(arg: Any): Boolean = arg match {
7172
case tpe: Type =>
7273
tpe.exists && !tpe.isErroneous
@@ -76,7 +77,7 @@ object Formatting {
7677
}
7778
val str = super.showArg(arg)
7879
if (isSensical(arg)) str
79-
else Message.nonSensicalStartTag + str + Message.nonSensicalEndTag
80+
else nonSensicalStartTag + str + nonSensicalEndTag
8081
}
8182
}
8283

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,19 @@ import Reporter._
99
import java.io.{ BufferedReader, IOException, PrintWriter }
1010
import scala.reflect.internal.util._
1111
import diagnostic.Message
12+
import diagnostic.MessageContainer
1213
import diagnostic.messages._
1314

1415
/**
1516
* This class implements a Reporter that displays messages on a text
1617
* console.
1718
*/
1819
class ConsoleReporter(
19-
reader: BufferedReader = Console.in,
20-
writer: PrintWriter = new PrintWriter(Console.err, true))
21-
extends Reporter with UniqueMessagePositions with HideNonSensicalMessages {
20+
reader: BufferedReader = Console.in,
21+
writer: PrintWriter = new PrintWriter(Console.err, true)
22+
) extends Reporter with UniqueMessagePositions with HideNonSensicalMessages {
2223

23-
import Message._
24+
import MessageContainer._
2425

2526
/** maximal number of error messages to be printed */
2627
protected def ErrorLimit = 100
@@ -35,9 +36,9 @@ class ConsoleReporter(
3536
def printMessage(msg: String): Unit = { writer.print(msg + "\n"); writer.flush() }
3637

3738
/** Prints the message with the given position indication. */
38-
def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
39+
def printMessageAndPos(msg: Message, pos: SourcePosition, kind: String)(implicit ctx: Context): Unit = {
3940
val posStr = if (pos.exists) s"$pos: " else ""
40-
printMessage(s"${posStr}$kind: $msg")
41+
printMessage(s"${posStr}${kind}: ${msg.msg}")
4142
if (pos.exists) {
4243
printSourceLine(pos)
4344
printColumnMarker(pos)
@@ -52,21 +53,21 @@ class ConsoleReporter(
5253
|${m.explanation}""".stripMargin
5354
)
5455

55-
override def doReport(m: Message)(implicit ctx: Context): Unit = {
56+
override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
5657
m match {
5758
case m: Error =>
58-
printMessageAndPos(m.message, m.pos, m.kind)
59+
printMessageAndPos(m.contained, m.pos, m.kind)
5960
if (ctx.settings.prompt.value) displayPrompt()
6061
case m: ConditionalWarning if !m.enablingOption.value =>
6162
case m: MigrationWarning =>
62-
printMessageAndPos(m.message, m.pos, m.kind)
63+
printMessageAndPos(m.contained, m.pos, m.kind)
6364
case m: Warning =>
64-
printMessageAndPos(m.message, m.pos, m.kind)
65+
printMessageAndPos(m.contained, m.pos, m.kind)
6566
case _ =>
66-
printMessageAndPos(m.message, m.pos, m.kind)
67+
printMessageAndPos(m.contained, m.pos, m.kind)
6768
}
6869

69-
if (ctx.shouldExplain(m)) printExplanation(m)
70+
if (ctx.shouldExplain(m)) printExplanation(m.contained)
7071
}
7172

7273
def displayPrompt(): Unit = {

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

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,15 +66,15 @@ class FancyConsoleReporter(
6666
}).show else ""
6767

6868
/** Prints the message with the given position indication. */
69-
override def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
69+
override def printMessageAndPos(msg: Message, pos: SourcePosition, kind: String)(implicit ctx: Context): Unit = {
7070
printMessage(posStr(pos, kind))
7171
if (pos.exists) {
7272
val (src, offset) = sourceLine(pos)
7373
val marker = columnMarker(pos, offset)
74-
val err = errorMsg(pos, msg, offset)
74+
val err = errorMsg(pos, msg.msg, offset)
7575

7676
printMessage(List(src, marker, err).mkString("\n"))
77-
} else printMessage(msg)
77+
} else printMessage(msg.msg)
7878
}
7979

8080
override def printExplanation(m: Message)(implicit ctx: Context): Unit = {
@@ -83,16 +83,4 @@ class FancyConsoleReporter(
8383
|${Blue("===========")}""".stripMargin)
8484
printMessage(m.explanation)
8585
}
86-
87-
88-
//override def summary(implicit ctx: Context): String = {
89-
// val b = new mutable.ListBuffer[String]
90-
// if (warningCount > 0)
91-
// b += countString(warningCount, Yellow("warning").show) + " found"
92-
// if (errorCount > 0)
93-
// b += countString(errorCount, Red("error").show) + " found"
94-
// for ((settingName, count) <- unreportedWarnings)
95-
// b += s"there were $count ${settingName.tail} ${Yellow("warning(s)").show}; re-run with $settingName for details"
96-
// b.mkString("\n")
97-
//}
9886
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package dotc
33
package reporting
44

55
import core.Contexts.Context
6-
import diagnostic.Message
6+
import diagnostic.MessageContainer
77

88
/**
99
* This trait implements `isHidden` so that we avoid reporting non-sensical messages.
@@ -12,7 +12,7 @@ trait HideNonSensicalMessages extends Reporter {
1212
/** Hides non-sensical messages, unless we haven't reported any error yet or
1313
* `-Yshow-suppressed-errors` is set.
1414
*/
15-
override def isHidden(m: Message)(implicit ctx: Context): Boolean =
15+
override def isHidden(m: MessageContainer)(implicit ctx: Context): Boolean =
1616
super.isHidden(m) || {
1717
m.isNonSensical &&
1818
hasErrors && // if there are no errors yet, report even if diagnostic is non-sensical

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

Lines changed: 25 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ import core.Mode
1212
import dotty.tools.dotc.core.Symbols.Symbol
1313
import diagnostic.messages._
1414
import diagnostic._
15-
import MessageCreator._
15+
import Message._
1616

1717
object Reporter {
1818
/** Convert a SimpleReporter into a real Reporter */
1919
def fromSimpleReporter(simple: interfaces.SimpleReporter): Reporter =
2020
new Reporter with UniqueMessagePositions with HideNonSensicalMessages {
21-
override def doReport(m: Message)(implicit ctx: Context): Unit = m match {
21+
override def doReport(m: MessageContainer)(implicit ctx: Context): Unit = m match {
2222
case m: ConditionalWarning if !m.enablingOption.value =>
2323
case _ =>
2424
simple.report(m)
@@ -37,17 +37,17 @@ trait Reporting { this: Context =>
3737
def echo(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
3838
reporter.report(new Info(msg, pos, "Info"))
3939

40-
def deprecationWarning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
41-
reporter.report(new DeprecationWarning(msg, pos, "Deprecation Warning"))
40+
def deprecationWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
41+
reporter.report(msg.deprecationWarning(pos))
4242

43-
def migrationWarning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
44-
reporter.report(new MigrationWarning(msg, pos, "Migration Warning"))
43+
def migrationWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
44+
reporter.report(msg.migrationWarning(pos))
4545

46-
def uncheckedWarning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
47-
reporter.report(new UncheckedWarning(msg, pos, "Unchecked Warning"))
46+
def uncheckedWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
47+
reporter.report(msg.uncheckedWarning(pos))
4848

49-
def featureWarning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
50-
reporter.report(new FeatureWarning(msg, pos, "Feature Warning"))
49+
def featureWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
50+
reporter.report(msg.featureWarning(pos))
5151

5252
def featureWarning(feature: String, featureDescription: String, isScala2Feature: Boolean,
5353
featureUseSite: Symbol, required: Boolean, pos: SourcePosition): Unit = {
@@ -72,32 +72,24 @@ trait Reporting { this: Context =>
7272
else reporter.report(new FeatureWarning(msg, pos))
7373
}
7474

75-
def warning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
76-
reporter.report(new Warning(msg, pos))
77-
78-
def explainWarning(msg: => MessageCreator, pos: SourcePosition = NoSourcePosition): Unit =
75+
def warning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
7976
reporter.report(msg.warning(pos))
8077

81-
def strictWarning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
78+
def strictWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
8279
if (this.settings.strict.value) error(msg, pos)
83-
else warning(msg + "\n(This would be an error under strict mode)", pos)
84-
85-
def error(msg: => String, pos: SourcePosition = NoSourcePosition): Unit = {
86-
// println("*** ERROR: " + msg) // !!! DEBUG
87-
reporter.report(new Error(msg, pos))
88-
}
80+
else warning(msg.mapMsg(_ + "\n(This would be an error under strict mode)"), pos)
8981

90-
def explainError(msg: => MessageCreator, pos: SourcePosition = NoSourcePosition): Unit =
82+
def error(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
9183
reporter.report(msg.error(pos))
9284

93-
def errorOrMigrationWarning(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
85+
def errorOrMigrationWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
9486
if (ctx.scala2Mode) migrationWarning(msg, pos) else error(msg, pos)
9587

96-
def restrictionError(msg: => String, pos: SourcePosition = NoSourcePosition): Unit =
97-
error(s"Implementation restriction: $msg", pos)
88+
def restrictionError(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
89+
error(msg.mapMsg(m => s"Implementation restriction: $m"), pos)
9890

99-
def incompleteInputError(msg: String, pos: SourcePosition = NoSourcePosition)(implicit ctx: Context): Unit =
100-
reporter.incomplete(new Error(msg, pos))(ctx)
91+
def incompleteInputError(msg: Message, pos: SourcePosition = NoSourcePosition)(implicit ctx: Context): Unit =
92+
reporter.incomplete(msg.error(pos))(ctx)
10193

10294
/** Log msg if settings.log contains the current phase.
10395
* See [[config.CompilerCommand#explainAdvanced]] for the exact meaning of
@@ -180,7 +172,7 @@ trait Reporting { this: Context =>
180172
abstract class Reporter extends interfaces.ReporterResult {
181173

182174
/** Report a diagnostic */
183-
def doReport(d: Message)(implicit ctx: Context): Unit
175+
def doReport(d: MessageContainer)(implicit ctx: Context): Unit
184176

185177
/** Whether very long lines can be truncated. This exists so important
186178
* debugging information (like printing the classpath) is not rendered
@@ -195,7 +187,7 @@ abstract class Reporter extends interfaces.ReporterResult {
195187
finally _truncationOK = saved
196188
}
197189

198-
type ErrorHandler = Message => Context => Unit
190+
type ErrorHandler = MessageContainer => Context => Unit
199191
private var incompleteHandler: ErrorHandler = d => c => report(d)(c)
200192
def withIncompleteHandler[T](handler: ErrorHandler)(op: => T): T = {
201193
val saved = incompleteHandler
@@ -224,7 +216,7 @@ abstract class Reporter extends interfaces.ReporterResult {
224216
override def default(key: String) = 0
225217
}
226218

227-
def report(d: Message)(implicit ctx: Context): Unit =
219+
def report(d: MessageContainer)(implicit ctx: Context): Unit =
228220
if (!isHidden(d)) {
229221
doReport(d)(ctx.addMode(Mode.Printing))
230222
d match {
@@ -238,12 +230,11 @@ abstract class Reporter extends interfaces.ReporterResult {
238230
}
239231
}
240232

241-
def incomplete(d: Message)(implicit ctx: Context): Unit =
233+
def incomplete(d: MessageContainer)(implicit ctx: Context): Unit =
242234
incompleteHandler(d)(ctx)
243235

244-
245236
/** Summary of warnings and errors */
246-
def summary/*(implicit ctx: Context)*/: String = {
237+
def summary: String = {
247238
val b = new mutable.ListBuffer[String]
248239
if (warningCount > 0)
249240
b += countString(warningCount, "warning") + " found"
@@ -271,7 +262,7 @@ abstract class Reporter extends interfaces.ReporterResult {
271262
}
272263

273264
/** Should this diagnostic not be reported at all? */
274-
def isHidden(m: Message)(implicit ctx: Context): Boolean = ctx.mode.is(Mode.Printing)
265+
def isHidden(m: MessageContainer)(implicit ctx: Context): Boolean = ctx.mode.is(Mode.Printing)
275266

276267
/** Does this reporter contain not yet reported errors or warnings? */
277268
def hasPending: Boolean = false

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@ package reporting
55
import core.Contexts.Context
66
import collection.mutable
77
import config.Printers._
8-
import diagnostic.Message
8+
import diagnostic.MessageContainer
99
import diagnostic.messages._
1010

1111
/**
1212
* This class implements a Reporter that stores all messages
1313
*/
1414
class StoreReporter(outer: Reporter) extends Reporter {
1515

16-
private var infos: mutable.ListBuffer[Message] = null
16+
private var infos: mutable.ListBuffer[MessageContainer] = null
1717

18-
def doReport(m: Message)(implicit ctx: Context): Unit = {
18+
def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
1919
typr.println(s">>>> StoredError: ${m.message}") // !!! DEBUG
2020
if (infos == null) infos = new mutable.ListBuffer
2121
infos += m

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ package reporting
44

55
import core.Contexts.Context
66
import collection.mutable
7-
import diagnostic.Message
7+
import diagnostic.MessageContainer
88
import diagnostic.messages.Error
99
import Reporter._
1010

@@ -13,7 +13,7 @@ import Reporter._
1313
* info to the underlying reporter.
1414
*/
1515
class ThrowingReporter(reportInfo: Reporter) extends Reporter {
16-
def doReport(m: Message)(implicit ctx: Context): Unit = m match {
16+
def doReport(m: MessageContainer)(implicit ctx: Context): Unit = m match {
1717
case _: Error => throw m
1818
case _ => reportInfo.doReport(m)
1919
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ package reporting
55
import scala.collection.mutable
66
import util.{SourcePosition, SourceFile}
77
import core.Contexts.Context
8-
import diagnostic.Message
8+
import diagnostic.MessageContainer
99

1010
/**
1111
* This trait implements `isHidden` so that multiple messages per position
@@ -18,7 +18,7 @@ trait UniqueMessagePositions extends Reporter {
1818
/** Logs a position and returns true if it was already logged.
1919
* @note Two positions are considered identical for logging if they have the same point.
2020
*/
21-
override def isHidden(m: Message)(implicit ctx: Context): Boolean =
21+
override def isHidden(m: MessageContainer)(implicit ctx: Context): Boolean =
2222
super.isHidden(m) || {
2323
m.pos.exists && {
2424
positions get (ctx.source, m.pos.point) match {

0 commit comments

Comments
 (0)