Skip to content

Commit 73f6f1b

Browse files
add d string interpolator (marks nonsensical error messages)
1 parent 094d8c5 commit 73f6f1b

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,4 +105,32 @@ object ErrorReporting {
105105

106106
def err(implicit ctx: Context): Errors = new Errors
107107

108+
/** The d string interpolator works like the i string interpolator, but marks nonsensical errors
109+
* using `<nonsensical>...</nonsensical>` tags.
110+
* Note: Instead of these tags, it would be nicer to return a data structure containing the message string
111+
* and a boolean indicating whether the message is sensical, but then we cannot use string operations
112+
* like concatenation, stripMargin etc on the values returned by d"...", and in the current error
113+
* message composition methods, this is crucial.
114+
*/
115+
implicit class DiagnosticString(val sc: StringContext) extends AnyVal {
116+
import DiagnosticString._
117+
def d(args: Any*)(implicit ctx: Context): String = {
118+
def isSensical(arg: Any): Boolean = arg match {
119+
case tpe: Type if tpe.isErroneous => false
120+
case NoType => false
121+
case sym: Symbol if sym.isCompleted =>
122+
sym.info != ErrorType && sym.info != TypeAlias(ErrorType) && sym.info != NoType
123+
case _ => true
124+
}
125+
126+
val s = new InfoString(sc).i(args)
127+
if (args.forall(isSensical(_))) s else nonSensicalStartTag + s + nonSensicalEndTag
128+
}
129+
}
130+
131+
object DiagnosticString {
132+
final val nonSensicalStartTag = "<nonsensical>"
133+
final val nonSensicalEndTag = "</nonsensical>"
134+
}
135+
108136
}

0 commit comments

Comments
 (0)