Skip to content

Commit ec0c3e3

Browse files
committed
refactor: change Message kind into an enum instead of a String
While digging into Diagnostics there wasn't an easy way to tell all the available `kind`s that there could be since they were just a string value. So this small changes just makes the `kind` field in `Message` a `MessageKind` which is an enum of all the types of message kinds that exist.
1 parent 6f3fe05 commit ec0c3e3

File tree

6 files changed

+64
-23
lines changed

6 files changed

+64
-23
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ abstract class Message(val errorId: ErrorMessageID) { self =>
5151
* This will be printed as "$kind Error", "$kind Warning", etc, on the first
5252
* line of the message.
5353
*/
54-
def kind: String
54+
def kind: MessageKind
5555

5656
/** The explanation should provide a detailed description of why the error
5757
* occurred and use examples from the user's own code to illustrate how to
@@ -140,7 +140,7 @@ abstract class Message(val errorId: ErrorMessageID) { self =>
140140
class NoExplanation(msgFn: => String) extends Message(ErrorMessageID.NoExplanationID) {
141141
def msg: String = msgFn
142142
def explain: String = ""
143-
val kind: String = ""
143+
val kind: MessageKind = MessageKind.NoKind
144144

145145
override def toString(): String = msg
146146
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package dotty.tools.dotc.reporting
2+
3+
/** Message kinds that can be used in a Message.
4+
* NOTE: Keep in mind that if you have a new message or a new ErrorMessageID
5+
* that doesn't fit well into an existing kind, create a new one.
6+
*/
7+
enum MessageKind:
8+
case NoKind
9+
case Syntax
10+
case Type
11+
case TypeMismatch
12+
case Naming
13+
case Declaration
14+
case NotFound
15+
case PatternMatch
16+
case Cyclic
17+
case Reference
18+
case DocComment
19+
case LossyConversion
20+
case PatternMatchExhaustivity
21+
case MatchCaseUnreachable
22+
case Compatibility
23+
case PotentialIssue
24+
25+
/** Human readable message that will end up being shown to the user.
26+
* NOTE: This is only used in the situation where you have multiple words
27+
* and don't want to rely on the default toString of the enum.
28+
*/
29+
def message: String =
30+
this match
31+
case NoKind => "No Kind"
32+
case TypeMismatch => "Type Mismatch"
33+
case NotFound => "Not Found"
34+
case PatternMatch => "Pattern Match"
35+
case DocComment => "Doc Comment"
36+
case LossyConversion => "Lossy Conversion"
37+
case PatternMatchExhaustivity => "Pattern Match Exhaustivity"
38+
case MatchCaseUnreachable => "Match case Unreachable"
39+
case PotentialIssue => "Potential Issue"
40+
case kind => kind.toString
41+
end MessageKind

compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,13 +172,13 @@ trait MessageRendering {
172172
val realPos = pos.nonInlined
173173
val fileAndPos = posFileStr(realPos)
174174
val errId =
175-
if (message.errorId ne ErrorMessageID.NoExplanationID) {
175+
if (message.errorId ne ErrorMessageID.NoExplanationID) then
176176
val errorNumber = message.errorId.errorNumber
177177
s"[E${"0" * (3 - errorNumber.toString.length) + errorNumber}] "
178-
} else ""
178+
else ""
179179
val kind =
180-
if (message.kind == "") diagnosticString
181-
else s"${message.kind} $diagnosticString"
180+
if (message.kind == MessageKind.NoKind) diagnosticString
181+
else s"${message.kind.message} $diagnosticString"
182182
val title =
183183
if fileAndPos.isEmpty then s"$errId$kind:" // this happens in dotty.tools.repl.ScriptedTests // TODO add name of source or remove `:` (and update test files)
184184
else s"$errId$kind: $fileAndPos"

compiler/src/dotty/tools/dotc/reporting/messages.scala

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,42 +40,42 @@ import transform.SymUtils._
4040
*/
4141

4242
abstract class SyntaxMsg(errorId: ErrorMessageID) extends Message(errorId):
43-
def kind = "Syntax"
43+
def kind = MessageKind.Syntax
4444

4545
abstract class TypeMsg(errorId: ErrorMessageID) extends Message(errorId):
46-
def kind = "Type"
46+
def kind = MessageKind.Type
4747

4848
trait ShowMatchTrace(tps: Type*)(using Context) extends Message:
4949
override def msgSuffix: String = matchReductionAddendum(tps*)
5050

5151
abstract class TypeMismatchMsg(found: Type, expected: Type)(errorId: ErrorMessageID)(using Context)
5252
extends Message(errorId), ShowMatchTrace(found, expected):
53-
def kind = "Type Mismatch"
53+
def kind = MessageKind.TypeMismatch
5454
def explain = err.whyNoMatchStr(found, expected)
5555
override def canExplain = true
5656

5757
abstract class NamingMsg(errorId: ErrorMessageID) extends Message(errorId):
58-
def kind = "Naming"
58+
def kind = MessageKind.Naming
5959

6060
abstract class DeclarationMsg(errorId: ErrorMessageID) extends Message(errorId):
61-
def kind = "Declaration"
61+
def kind = MessageKind.Declaration
6262

6363
/** A simple not found message (either for idents, or member selection.
6464
* Messages of this class are sometimes dropped in favor of other, more
6565
* specific messages.
6666
*/
6767
abstract class NotFoundMsg(errorId: ErrorMessageID) extends Message(errorId):
68-
def kind = "Not Found"
68+
def kind = MessageKind.NotFound
6969
def name: Name
7070

7171
abstract class PatternMatchMsg(errorId: ErrorMessageID) extends Message(errorId):
72-
def kind = "Pattern Match"
72+
def kind = MessageKind.PatternMatch
7373

7474
abstract class CyclicMsg(errorId: ErrorMessageID) extends Message(errorId):
75-
def kind = "Cyclic"
75+
def kind = MessageKind.Cyclic
7676

7777
abstract class ReferenceMsg(errorId: ErrorMessageID) extends Message(errorId):
78-
def kind = "Reference"
78+
def kind = MessageKind.Reference
7979

8080
abstract class EmptyCatchOrFinallyBlock(tryBody: untpd.Tree, errNo: ErrorMessageID)(using Context)
8181
extends SyntaxMsg(EmptyCatchOrFinallyBlockID) {
@@ -638,7 +638,7 @@ import transform.SymUtils._
638638

639639
class ProperDefinitionNotFound()(using Context)
640640
extends Message(ProperDefinitionNotFoundID) {
641-
def kind: String = "Doc Comment"
641+
def kind = MessageKind.DocComment
642642
def msg = em"""Proper definition was not found in ${hl("@usecase")}"""
643643

644644
def explain = {
@@ -818,14 +818,14 @@ import transform.SymUtils._
818818

819819
class LossyWideningConstantConversion(sourceType: Type, targetType: Type)(using Context)
820820
extends Message(LossyWideningConstantConversionID):
821-
def kind = "Lossy Conversion"
821+
def kind = MessageKind.LossyConversion
822822
def msg = em"""|Widening conversion from $sourceType to $targetType loses precision.
823823
|Write `.to$targetType` instead.""".stripMargin
824824
def explain = ""
825825

826826
class PatternMatchExhaustivity(uncoveredFn: => String, hasMore: Boolean)(using Context)
827827
extends Message(PatternMatchExhaustivityID) {
828-
def kind = "Pattern Match Exhaustivity"
828+
def kind = MessageKind.PatternMatchExhaustivity
829829
lazy val uncovered = uncoveredFn
830830
def msg =
831831
val addendum = if hasMore then "(More unmatched cases are elided)" else ""
@@ -856,7 +856,7 @@ import transform.SymUtils._
856856

857857
class MatchCaseUnreachable()(using Context)
858858
extends Message(MatchCaseUnreachableID) {
859-
def kind = "Match case Unreachable"
859+
def kind = MessageKind.MatchCaseUnreachable
860860
def msg = "Unreachable case"
861861
def explain = ""
862862
}
@@ -1783,7 +1783,7 @@ import transform.SymUtils._
17831783

17841784
class FailureToEliminateExistential(tp: Type, tp1: Type, tp2: Type, boundSyms: List[Symbol], classRoot: Symbol)(using Context)
17851785
extends Message(FailureToEliminateExistentialID) {
1786-
def kind: String = "Compatibility"
1786+
def kind = MessageKind.Compatibility
17871787
def msg =
17881788
val originalType = ctx.printer.dclsText(boundSyms, "; ").show
17891789
em"""An existential type that came from a Scala-2 classfile for $classRoot
@@ -2228,7 +2228,7 @@ import transform.SymUtils._
22282228

22292229
class PureExpressionInStatementPosition(stat: untpd.Tree, val exprOwner: Symbol)(using Context)
22302230
extends Message(PureExpressionInStatementPositionID) {
2231-
def kind = "Potential Issue"
2231+
def kind = MessageKind.PotentialIssue
22322232
def msg = "A pure expression does nothing in statement position; you may be omitting necessary parentheses"
22332233
def explain =
22342234
em"""The pure expression $stat doesn't have any side effect and its result is not assigned elsewhere.

compiler/test/dotty/tools/dotc/reporting/TestMessageLaziness.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class TestMessageLaziness extends DottyTest {
1616
}
1717

1818
case class LazyError() extends Message(ErrorMessageID.LazyErrorId) {
19-
val kind = "Test"
19+
val kind = MessageKind.NoKind
2020
def msg = throw new Error("Didn't stay lazy.")
2121
def explain = ""
2222
}

compiler/test/dotty/tools/dotc/reporting/TestReporter.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ object TestReporter {
120120
/** Prints the message with the given position indication in a simplified manner */
121121
override def printMessageAndPos(dia: Diagnostic, extra: String)(using Context): Unit = {
122122
def report() = {
123-
val msg = s"${dia.pos.line + 1}: " + dia.msg.kind + extra
123+
val msg = s"${dia.pos.line + 1}: " + dia.msg.kind.message + extra
124124
val extraInfo = inlineInfo(dia.pos)
125125

126126
writer.println(msg)

0 commit comments

Comments
 (0)