Skip to content

Commit a535680

Browse files
committed
Turn defined messages and explanations into protected defs
Caching is done at a central point in class `Message` where also `<nonsensical>` tags are dropped.
1 parent 3d21fc5 commit a535680

File tree

3 files changed

+351
-355
lines changed

3 files changed

+351
-355
lines changed

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

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,36 +48,39 @@ abstract class Message(val errorId: ErrorMessageID) { self =>
4848
*/
4949
protected def msg: String
5050

51-
/** The kind of the error message is something like "Syntax" or "Type
52-
* Mismatch"
51+
/** The kind of the error message, e.g. "Syntax" or "Type Mismatch".
52+
* This will be printed as "$kind Error", "$kind Warning", etc, on the first
53+
* line of the message.
5354
*/
5455
def kind: String
5556

5657
/** The explanation should provide a detailed description of why the error
5758
* occurred and use examples from the user's own code to illustrate how to
58-
* avoid these errors.
59+
* avoid these errors. It might contain embedded <nonsensical> tags.
5960
*/
60-
def explanation: String
61+
protected def explain: String
6162

6263
private var myMsg: String | Null = null
6364
private var myIsNonSensical: Boolean = false
6465

66+
private def dropNonSensical(msg: String): String =
67+
if msg.contains(nonSensicalStartTag) then
68+
myIsNonSensical = true
69+
// myMsg might be composed of several d"..." invocations -> nested
70+
// nonsensical tags possible
71+
msg
72+
.replaceAllLiterally(nonSensicalStartTag, "")
73+
.replaceAllLiterally(nonSensicalEndTag, "")
74+
else msg
75+
6576
/** The message with potential embedded <nonsensical> tags */
6677
def rawMessage = message
6778

6879
/** The message to report. <nonsensical> tags are filtered out */
69-
def message: String =
70-
if myMsg == null then
71-
myMsg =
72-
if msg.contains(nonSensicalStartTag) then
73-
myIsNonSensical = true
74-
// myMsg might be composed of several d"..." invocations -> nested
75-
// nonsensical tags possible
76-
msg
77-
.replaceAllLiterally(nonSensicalStartTag, "")
78-
.replaceAllLiterally(nonSensicalEndTag, "")
79-
else msg
80-
myMsg
80+
lazy val message: String = dropNonSensical(msg)
81+
82+
/** The explanation to report. <nonsensical> tags are filtered out */
83+
lazy val explanation: String = dropNonSensical(explain)
8184

8285
/** A message is non-sensical if it contains references to <nonsensical>
8386
* tags. Such tags are inserted by the error diagnostic framework if a
@@ -94,30 +97,30 @@ abstract class Message(val errorId: ErrorMessageID) { self =>
9497
* that was captured in the original message.
9598
*/
9699
def persist: Message = new Message(errorId) {
97-
val kind = self.kind
98-
val msg = self.msg
99-
val explanation = self.explanation
100+
val kind = self.kind
101+
val msg = self.msg
102+
val explain = self.explain
100103
}
101104

102105
def append(suffix: => String): Message = mapMsg(_ ++ suffix)
103106

104107
def mapMsg(f: String => String): Message = new Message(errorId):
105-
val kind = self.kind
106-
lazy val msg = f(self.msg)
107-
lazy val explanation = self.explanation
108+
val kind = self.kind
109+
def msg = f(self.msg)
110+
def explain = self.explain
108111

109112
def appendExplanation(suffix: => String): Message = new Message(errorId):
110-
val kind = self.kind
111-
lazy val msg = self.msg
112-
lazy val explanation = self.explanation ++ suffix
113+
val kind = self.kind
114+
def msg = self.msg
115+
def explain = self.explain ++ suffix
113116

114117
override def toString = msg
115118
}
116119

117120
/** The fallback `Message` containing no explanation and having no `kind` */
118121
class NoExplanation(msgFn: => String) extends Message(ErrorMessageID.NoExplanationID) {
119-
lazy val msg: String = msgFn
120-
val explanation: String = ""
122+
def msg: String = msgFn
123+
def explain: String = ""
121124
val kind: String = ""
122125

123126
override def toString(): String = msg

0 commit comments

Comments
 (0)