Skip to content

Commit 654ca85

Browse files
committed
Fix scala#6371: Make error message rendering resilient to missing sources
1 parent fbc9d0a commit 654ca85

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ trait MessageRendering {
4343
* @return (lines before error, lines after error, line numbers offset)
4444
*/
4545
def sourceLines(pos: SourcePosition, diagnosticLevel: String)(implicit ctx: Context): (List[String], List[String], Int) = {
46+
assert(pos.exists && pos.source.file.exists)
4647
var maxLen = Int.MinValue
4748
def render(offsetAndLine: (Int, String)): String = {
4849
val (offset, line) = offsetAndLine
@@ -113,7 +114,9 @@ trait MessageRendering {
113114
*/
114115
def posStr(pos: SourcePosition, diagnosticLevel: String, message: Message)(implicit ctx: Context): String =
115116
if (pos.exists) hl(diagnosticLevel)({
116-
val file = s"${pos.source.file.toString}:${pos.line + 1}:${pos.column}"
117+
val file =
118+
if (pos.source.file.exists) s"${pos.source.file.toString}:${pos.line + 1}:${pos.column}"
119+
else s"${pos.source.file.toString}: offset ${pos.start} (missing source file)"
117120
val errId =
118121
if (message.errorId ne ErrorMessageID.NoExplanationID) {
119122
val errorNumber = message.errorId.errorNumber()
@@ -145,7 +148,7 @@ trait MessageRendering {
145148
val sb = mutable.StringBuilder.newBuilder
146149
val posString = posStr(pos, diagnosticLevel, msg)
147150
if (posString.nonEmpty) sb.append(posString).append(EOL)
148-
if (pos.exists) {
151+
if (pos.exists && pos.source.file.exists) {
149152
val (srcBefore, srcAfter, offset) = sourceLines(pos, diagnosticLevel)
150153
val marker = columnMarker(pos, offset, diagnosticLevel)
151154
val err = errorMsg(pos, msg.msg, offset)

0 commit comments

Comments
 (0)