Skip to content

Commit ff6ef73

Browse files
Valentin889mbovelsjrd
authored
Catch stackoverflow errors in the highlighter (#19836)
Co-authored-by: Matt Bovel <[email protected]> Co-authored-by: Sébastien Doeraene <[email protected]>
1 parent 5c6524a commit ff6ef73

File tree

2 files changed

+35
-16
lines changed

2 files changed

+35
-16
lines changed

compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala

+22-16
Original file line numberDiff line numberDiff line change
@@ -124,24 +124,30 @@ object SyntaxHighlighting {
124124
}
125125
}
126126

127-
val parser = new Parser(source)
128-
val trees = parser.blockStatSeq()
129-
TreeHighlighter.highlight(trees)
130-
131-
val highlighted = new StringBuilder()
132-
133-
for (idx <- colorAt.indices) {
134-
val prev = if (idx == 0) NoColor else colorAt(idx - 1)
135-
val curr = colorAt(idx)
136-
if (curr != prev)
137-
highlighted.append(curr)
138-
highlighted.append(in(idx))
139-
}
127+
try
128+
val parser = new Parser(source)
129+
val trees = parser.blockStatSeq()
130+
TreeHighlighter.highlight(trees)
131+
132+
133+
val highlighted = new StringBuilder()
140134

141-
if (colorAt.last != NoColor)
142-
highlighted.append(NoColor)
135+
for (idx <- colorAt.indices) {
136+
val prev = if (idx == 0) NoColor else colorAt(idx - 1)
137+
val curr = colorAt(idx)
138+
if (curr != prev)
139+
highlighted.append(curr)
140+
highlighted.append(in(idx))
141+
}
142+
143+
if (colorAt.last != NoColor)
144+
highlighted.append(NoColor)
143145

144-
highlighted.toString
146+
highlighted.toString
147+
catch
148+
case e: StackOverflowError =>
149+
in
145150
}
146151
}
152+
147153
}

compiler/test/dotty/tools/repl/ReplCompilerTests.scala

+13
Original file line numberDiff line numberDiff line change
@@ -497,3 +497,16 @@ class ReplVerboseTests extends ReplTest(ReplTest.defaultOptions :+ "-verbose"):
497497
}
498498

499499
end ReplVerboseTests
500+
501+
class ReplHighlightTests extends ReplTest(ReplTest.defaultOptions.filterNot(_.startsWith("-color")) :+ "-color:always"):
502+
@Test def i18596: Unit = initially:
503+
run("""(1 to 500).foldRight("x") { case (_, n) => s"<x>$n</x>" }""")
504+
505+
@Test def i16904: Unit = initially:
506+
run(""""works not fine"* 10000""")
507+
508+
run("""
509+
case class Tree(left: Tree, right: Tree)
510+
def deepTree(depth: Int): Tree
511+
deepTree(300)""")
512+

0 commit comments

Comments
 (0)