Skip to content

Commit

Permalink
Catch stackoverflow errors in the highlighter (#19836)
Browse files Browse the repository at this point in the history
Co-authored-by: Matt Bovel <matthieu@bovel.net>
Co-authored-by: Sébastien Doeraene <sjrdoeraene@gmail.com>
  • Loading branch information
3 people committed Mar 26, 2024
1 parent 5c6524a commit ff6ef73
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 16 deletions.
38 changes: 22 additions & 16 deletions compiler/src/dotty/tools/dotc/printing/SyntaxHighlighting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

}
13 changes: 13 additions & 0 deletions compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -497,3 +497,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"<x>$n</x>" }""")

@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)""")

0 comments on commit ff6ef73

Please sign in to comment.