Skip to content

Commit f7b8980

Browse files
committed
Improve documentation for message framework
1 parent 18d63fe commit f7b8980

File tree

3 files changed

+48
-10
lines changed

3 files changed

+48
-10
lines changed

src/dotty/tools/dotc/ast/Trees.scala

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

55
import core._
6-
import Types._, Names._, Flags._, util.Positions._, Contexts._, Constants._, SymDenotations._, Symbols._
7-
import Denotations._, StdNames._, Comments._
6+
import Types._, Names._, Flags._, util.Positions._, Contexts._, Constants._
7+
import SymDenotations._, Symbols._, Denotations._, StdNames._, Comments._
88
import annotation.tailrec
99
import language.higherKinds
1010
import collection.IndexedSeqOptimized

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

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,92 @@ package dotc
33
package reporting
44
package diagnostic
55

6-
import util.{SourcePosition, NoSourcePosition}
6+
import util.SourcePosition
77
import core.Contexts.Context
88

99
object Message {
10+
/** This implicit conversion provides a fallback for error messages that have
11+
* not yet been ported to the new scheme. Comment out this `implicit def` to
12+
* see where old errors still exist
13+
*/
1014
implicit def toNoExplanation(str: String): Message =
1115
new NoExplanation(str)
1216
}
1317

1418
abstract class Message(val errorId: String) { self =>
1519
import messages._
1620

21+
/** The `msg` contains the diagnostic message e.g:
22+
*
23+
* > expected: String
24+
* > found: Int
25+
*
26+
* This message wil be placed underneath the position given by the enclosing
27+
* `MessageContainer`
28+
*/
1729
def msg: String
30+
31+
/** The kind of the error message is something like "Syntax" or "Type
32+
* Mismatch"
33+
*/
1834
def kind: String
35+
36+
/** The explanation should provide a detailed description of why the error
37+
* occurred and use examples from the user's own code to illustrate how to
38+
* avoid these errors.
39+
*/
1940
def explanation: String
2041

42+
/** It is possible to map `msg` to add details, this is at the loss of
43+
* precision since the type of the resulting `Message` won't be original
44+
* extending class
45+
*
46+
* @return a `Message` with the mapped message
47+
*/
2148
def mapMsg(f: String => String) = new Message(errorId) {
2249
val msg = f(self.msg)
2350
val kind = self.kind
2451
val explanation = self.explanation
2552
}
2653

54+
/** Enclose this message in an `Error` container */
2755
def error(pos: SourcePosition) =
2856
new Error(self, pos, explanation)
2957

58+
/** Enclose this message in an `Warning` container */
3059
def warning(pos: SourcePosition) =
3160
new Warning(self, pos, explanation)
3261

62+
/** Enclose this message in an `Info` container */
3363
def info(pos: SourcePosition) =
3464
new Info(self, pos, explanation)
3565

66+
/** Enclose this message in an `FeatureWarning` container */
3667
def featureWarning(pos: SourcePosition) =
3768
new FeatureWarning(self, pos, explanation)
3869

70+
/** Enclose this message in an `UncheckedWarning` container */
3971
def uncheckedWarning(pos: SourcePosition) =
4072
new UncheckedWarning(self, pos, explanation)
4173

74+
/** Enclose this message in an `DeprecationWarning` container */
4275
def deprecationWarning(pos: SourcePosition) =
4376
new DeprecationWarning(self, pos, explanation)
4477

78+
/** Enclose this message in an `MigrationWarning` container */
4579
def migrationWarning(pos: SourcePosition) =
4680
new MigrationWarning(self, pos, explanation)
4781
}
4882

83+
/** The fallback `Message` containing no explanation and having no `kind` */
4984
class NoExplanation(val msg: String) extends Message("") {
5085
val explanation = ""
5186
val kind = ""
5287
}
5388

89+
/** The extractor for `NoExplanation` can be used to check whether any error
90+
* lacks an explanation
91+
*/
5492
object NoExplanation {
5593
def unapply(m: Message): Option[Message] =
5694
if (m.explanation == "") Some(m)

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import printing.Formatting
1414

1515
object messages {
1616

17-
/** Message container to be consumed by the reporter ---------------------- */
17+
// `MessageContainer`s to be consumed by `Reporter` ---------------------- //
1818
class Error(
1919
msgFn: => Message,
2020
pos: SourcePosition,
@@ -73,22 +73,22 @@ object messages {
7373
def enablingOption(implicit ctx: Context) = ctx.settings.migration
7474
}
7575

76-
/** Messages ----------------------------------------------------------------
77-
*
76+
/** Messages
77+
* ========
7878
* The role of messages is to provide the necessary details for a simple to
7979
* understand diagnostic event. Each message can be turned into a message
8080
* container (one of the above) by calling the appropriate method on them.
8181
* For instance:
8282
*
8383
* ```scala
84-
* EmptyCatchBlock(tree).error // res: Error
85-
* EmptyCatchBlock(tree).warning // res: Warning
84+
* EmptyCatchBlock(tree).error(pos) // res: Error
85+
* EmptyCatchBlock(tree).warning(pos) // res: Warning
8686
* ```
8787
*/
8888
import dotc.ast.Trees._
8989
import dotc.ast.untpd
9090

91-
/** Syntax Errors --------------------------------------------------------- */
91+
// Syntax Errors ---------------------------------------------------------- //
9292
abstract class EmptyCatchOrFinallyBlock(tryBody: untpd.Tree, errNo: String)(implicit ctx: Context)
9393
extends Message(errNo) {
9494
val explanation = {
@@ -166,7 +166,7 @@ object messages {
166166
}
167167
}
168168

169-
/** Type Errors ----------------------------------------------------------- */
169+
// Type Errors ------------------------------------------------------------ //
170170
case class DuplicateBind(bind: untpd.Bind, tree: untpd.CaseDef)(implicit ctx: Context)
171171
extends Message("E004") {
172172
val kind = "Naming"

0 commit comments

Comments
 (0)