Skip to content

Commit d0f3cae

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

File tree

4 files changed

+26
-2
lines changed

4 files changed

+26
-2
lines changed

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

+5-2
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)

project/scripts/cmdTests

+12
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,18 @@ clear_out "$OUT"
3232
"$SBT" ";dotc -d $OUT/out.jar $SOURCE; dotc -decompile -classpath $OUT/out.jar -color:never $MAIN" > "$tmp"
3333
grep -qe "def main(args: scala.Array\[scala.Predef.String\]): scala.Unit =" "$tmp"
3434

35+
# check that missing source file does not crash message rendering
36+
echo "testing that missing source file does not crash message rendering"
37+
clear_out "$OUT"
38+
clear_out "$OUT1"
39+
cp tests/neg/i6371/A_1.scala $OUT/A.scala
40+
cp tests/neg/i6371/B_2.scala $OUT/B.scala
41+
"$SBT" "dotc $OUT/A.scala -d $OUT1"
42+
rm $OUT/A.scala
43+
"$SBT" "dotc -classpath $OUT1 -d $OUT1 $OUT/B.scala" > "$tmp"
44+
grep -qe "Error: A.scala: offset" "$tmp"
45+
46+
3547
## Disabled because of flakeyness, should be changed to not depend on sbt
3648
# echo "running Vulpix meta test"
3749
# tmp=$(mktemp)

tests/neg/i6371/A_1.scala

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
object A {
2+
inline def foo(a: Any): Unit = a match {
3+
case _: Int => // error
4+
case _ =>
5+
}
6+
}

tests/neg/i6371/B_2.scala

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
object B {
2+
A.foo("aa")
3+
}

0 commit comments

Comments
 (0)