Skip to content

Commit bda8ae9

Browse files
committed
Fix a potential StackOverflow in recursion detection code
1 parent 0918e04 commit bda8ae9

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

compiler/src/dotty/tools/dotc/core/TypeErrors.scala

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,21 @@ class MissingType(pre: Type, name: Name) extends TypeError {
4040
}
4141
}
4242

43-
class RecursionOverflow(val op: String, details: => String, previous: Throwable, val weight: Int) extends TypeError {
43+
class RecursionOverflow(val op: String, details: => String, val previous: Throwable, val weight: Int) extends TypeError {
4444

4545
def explanation: String = s"$op $details"
4646

4747
private def recursions: List[RecursionOverflow] = {
48-
val nested = previous match {
49-
case previous: RecursionOverflow => previous.recursions
50-
case _ => Nil
48+
import scala.collection.mutable.ListBuffer
49+
val result = ListBuffer.empty[RecursionOverflow]
50+
@annotation.tailrec def loop(throwable: Throwable): List[RecursionOverflow] = throwable match {
51+
case ro: RecursionOverflow =>
52+
result += ro
53+
loop(ro.previous)
54+
case _ => result.toList
55+
5156
}
52-
this :: nested
57+
loop(this)
5358
}
5459

5560
def opsString(rs: List[RecursionOverflow])(implicit ctx: Context): String = {

0 commit comments

Comments
 (0)