From 1af6201aaf8f0544a8c2f086ec3582ec0410dcf2 Mon Sep 17 00:00:00 2001 From: Valentin Schneeberger Date: Tue, 26 Mar 2024 15:55:45 +0100 Subject: [PATCH] Catch stackoverflow errors in the highlighter (#19836) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Matt Bovel Co-authored-by: Sébastien Doeraene [Cherry-picked ff6ef73f2bcf2c997bef5937e0522ae0e44a27f3] --- .../dotc/printing/SyntaxHighlighting.scala | 38 +++++++++++-------- .../dotty/tools/repl/ReplCompilerTests.scala | 13 +++++++ 2 files changed, 35 insertions(+), 16 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala index 6f65320d2c8e..ce925e336b53 100644 --- a/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala +++ b/compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala @@ -124,24 +124,30 @@ object SyntaxHighlighting { } } - val parser = new Parser(source) - val trees = parser.blockStatSeq() - TreeHighlighter.highlight(trees) - - val highlighted = new StringBuilder() - - for (idx <- colorAt.indices) { - val prev = if (idx == 0) NoColor else colorAt(idx - 1) - val curr = colorAt(idx) - if (curr != prev) - highlighted.append(curr) - highlighted.append(in(idx)) - } + try + val parser = new Parser(source) + val trees = parser.blockStatSeq() + TreeHighlighter.highlight(trees) + + + val highlighted = new StringBuilder() - if (colorAt.last != NoColor) - highlighted.append(NoColor) + for (idx <- colorAt.indices) { + val prev = if (idx == 0) NoColor else colorAt(idx - 1) + val curr = colorAt(idx) + if (curr != prev) + highlighted.append(curr) + highlighted.append(in(idx)) + } + + if (colorAt.last != NoColor) + highlighted.append(NoColor) - highlighted.toString + highlighted.toString + catch + case e: StackOverflowError => + in } } + } diff --git a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala index c193cd476d44..8c6c6b567ad1 100644 --- a/compiler/test/dotty/tools/repl/ReplCompilerTests.scala +++ b/compiler/test/dotty/tools/repl/ReplCompilerTests.scala @@ -413,3 +413,16 @@ class ReplVerboseTests extends ReplTest(ReplTest.defaultOptions :+ "-verbose"): } end ReplVerboseTests + +class ReplHighlightTests extends ReplTest(ReplTest.defaultOptions.filterNot(_.startsWith("-color")) :+ "-color:always"): + @Test def i18596: Unit = initially: + run("""(1 to 500).foldRight("x") { case (_, n) => s"$n" }""") + + @Test def i16904: Unit = initially: + run(""""works not fine"* 10000""") + + run(""" + case class Tree(left: Tree, right: Tree) + def deepTree(depth: Int): Tree + deepTree(300)""") +