Skip to content

Commit e24289a

Browse files
committed
Make relevant parts of compiler conform to new error handling
1 parent 628b7f3 commit e24289a

20 files changed

+317
-338
lines changed

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

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,8 @@ import rewrite.Rewrites.patch
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

@@ -100,17 +99,17 @@ object Parsers {
10099
/** Issue an error at given offset if beyond last error offset
101100
* and update lastErrorOffset.
102101
*/
103-
def syntaxError(expl: MessageCreator, offset: Int = in.offset): Unit =
102+
def syntaxError(msg: Message, offset: Int = in.offset): Unit =
104103
if (offset > lastErrorOffset) {
105-
syntaxError(expl, Position(offset))
104+
syntaxError(msg, Position(offset))
106105
lastErrorOffset = in.offset
107106
}
108107

109108
/** Unconditionally issue an error at given position, without
110109
* updating lastErrorOffset.
111110
*/
112-
def syntaxError(expl: MessageCreator, pos: Position): Unit =
113-
ctx.explainError(expl, source atPos pos)
111+
def syntaxError(msg: Message, pos: Position): Unit =
112+
ctx.error(msg, source atPos pos)
114113

115114
}
116115

@@ -216,20 +215,20 @@ object Parsers {
216215
}
217216
}
218217

219-
def warning(msg: MessageCreator, offset: Int = in.offset) =
220-
ctx.explainWarning(msg, source atPos Position(offset))
218+
def warning(msg: Message, offset: Int = in.offset) =
219+
ctx.warning(msg, source atPos Position(offset))
221220

222-
def deprecationWarning(msg: String, offset: Int = in.offset) =
221+
def deprecationWarning(msg: Message, offset: Int = in.offset) =
223222
ctx.deprecationWarning(msg, source atPos Position(offset))
224223

225224
/** Issue an error at current offset taht input is incomplete */
226-
def incompleteInputError(msg: String) =
225+
def incompleteInputError(msg: Message) =
227226
ctx.incompleteInputError(msg, source atPos Position(in.offset))
228227

229228
/** If at end of file, issue an incompleteInputError.
230229
* Otherwise issue a syntax error and skip to next safe point.
231230
*/
232-
def syntaxErrorOrIncomplete(msg: String) =
231+
def syntaxErrorOrIncomplete(msg: Message) =
233232
if (in.token == EOF) incompleteInputError(msg)
234233
else {
235234
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
@@ -73,15 +73,15 @@ class FancyConsoleReporter(
7373
}).show else ""
7474

7575
/** Prints the message with the given position indication. */
76-
override def printMessageAndPos(msg: String, pos: SourcePosition, kind: String = "")(implicit ctx: Context): Unit = {
76+
override def printMessageAndPos(msg: Message, pos: SourcePosition, kind: String)(implicit ctx: Context): Unit = {
7777
printMessage(posStr(pos, kind))
7878
if (pos.exists) {
7979
val (src, offset) = sourceLine(pos)
8080
val marker = columnMarker(pos, offset)
81-
val err = errorMsg(pos, msg, offset)
81+
val err = errorMsg(pos, msg.msg, offset)
8282

8383
printMessage(List(src, marker, err).mkString("\n"))
84-
} else printMessage(msg)
84+
} else printMessage(msg.msg)
8585
}
8686

8787
override def printExplanation(m: Message)(implicit ctx: Context): Unit = {
@@ -90,16 +90,4 @@ class FancyConsoleReporter(
9090
|${Blue("===========")}""".stripMargin)
9191
printMessage(m.explanation)
9292
}
93-
94-
95-
//override def summary(implicit ctx: Context): String = {
96-
// val b = new mutable.ListBuffer[String]
97-
// if (warningCount > 0)
98-
// b += countString(warningCount, Yellow("warning").show) + " found"
99-
// if (errorCount > 0)
100-
// b += countString(errorCount, Red("error").show) + " found"
101-
// for ((settingName, count) <- unreportedWarnings)
102-
// b += s"there were $count ${settingName.tail} ${Yellow("warning(s)").show}; re-run with $settingName for details"
103-
// b.mkString("\n")
104-
//}
10593
}

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
@@ -184,7 +176,7 @@ trait Reporting { this: Context =>
184176
abstract class Reporter extends interfaces.ReporterResult {
185177

186178
/** Report a diagnostic */
187-
def doReport(d: Message)(implicit ctx: Context): Unit
179+
def doReport(d: MessageContainer)(implicit ctx: Context): Unit
188180

189181
/** Whether very long lines can be truncated. This exists so important
190182
* debugging information (like printing the classpath) is not rendered
@@ -199,7 +191,7 @@ abstract class Reporter extends interfaces.ReporterResult {
199191
finally _truncationOK = saved
200192
}
201193

202-
type ErrorHandler = Message => Context => Unit
194+
type ErrorHandler = MessageContainer => Context => Unit
203195
private var incompleteHandler: ErrorHandler = d => c => report(d)(c)
204196
def withIncompleteHandler[T](handler: ErrorHandler)(op: => T): T = {
205197
val saved = incompleteHandler
@@ -228,7 +220,7 @@ abstract class Reporter extends interfaces.ReporterResult {
228220
override def default(key: String) = 0
229221
}
230222

231-
def report(d: Message)(implicit ctx: Context): Unit =
223+
def report(d: MessageContainer)(implicit ctx: Context): Unit =
232224
if (!isHidden(d)) {
233225
doReport(d)(ctx.addMode(Mode.Printing))
234226
d match {
@@ -242,12 +234,11 @@ abstract class Reporter extends interfaces.ReporterResult {
242234
}
243235
}
244236

245-
def incomplete(d: Message)(implicit ctx: Context): Unit =
237+
def incomplete(d: MessageContainer)(implicit ctx: Context): Unit =
246238
incompleteHandler(d)(ctx)
247239

248-
249240
/** Summary of warnings and errors */
250-
def summary/*(implicit ctx: Context)*/: String = {
241+
def summary: String = {
251242
val b = new mutable.ListBuffer[String]
252243
if (warningCount > 0)
253244
b += countString(warningCount, "warning") + " found"
@@ -275,7 +266,7 @@ abstract class Reporter extends interfaces.ReporterResult {
275266
}
276267

277268
/** Should this diagnostic not be reported at all? */
278-
def isHidden(m: Message)(implicit ctx: Context): Boolean = ctx.mode.is(Mode.Printing)
269+
def isHidden(m: MessageContainer)(implicit ctx: Context): Boolean = ctx.mode.is(Mode.Printing)
279270

280271
/** Does this reporter contain not yet reported errors or warnings? */
281272
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
@@ -6,17 +6,17 @@ import core.Contexts.Context
66
import collection.mutable
77
import Reporter.{Error, Warning}
88
import config.Printers.typr
9-
import diagnostic.Message
9+
import diagnostic.MessageContainer
1010
import diagnostic.messages._
1111

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

17-
private var infos: mutable.ListBuffer[Message] = null
17+
private var infos: mutable.ListBuffer[MessageContainer] = null
1818

19-
def doReport(m: Message)(implicit ctx: Context): Unit = {
19+
def doReport(m: MessageContainer)(implicit ctx: Context): Unit = {
2020
typr.println(s">>>> StoredError: ${m.message}") // !!! DEBUG
2121
if (infos == null) infos = new mutable.ListBuffer
2222
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)