Skip to content

Commit 2b87b6a

Browse files
Backport "Catch stackoverflow errors in the highlighter" to LTS (#21021)
Backports #19836 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 89fa365 + 1af6201 commit 2b87b6a

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
@@ -413,3 +413,16 @@ class ReplVerboseTests extends ReplTest(ReplTest.defaultOptions :+ "-verbose"):
413413
}
414414

415415
end ReplVerboseTests
416+
417+
class ReplHighlightTests extends ReplTest(ReplTest.defaultOptions.filterNot(_.startsWith("-color")) :+ "-color:always"):
418+
@Test def i18596: Unit = initially:
419+
run("""(1 to 500).foldRight("x") { case (_, n) => s"<x>$n</x>" }""")
420+
421+
@Test def i16904: Unit = initially:
422+
run(""""works not fine"* 10000""")
423+
424+
run("""
425+
case class Tree(left: Tree, right: Tree)
426+
def deepTree(depth: Int): Tree
427+
deepTree(300)""")
428+

0 commit comments

Comments
 (0)