Skip to content

Commit d15c6ba

Browse files
committed
Harden display logic
Displaying stuff should never report an exception or cause more messages to be displayed that relate to the displaying.
1 parent 3bc3d87 commit d15c6ba

File tree

3 files changed

+12
-3
lines changed

3 files changed

+12
-3
lines changed

src/dotty/tools/dotc/printing/Showable.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import core._
55

66
import Contexts._, Texts._, Decorators._
77
import config.Config.summarizeDepth
8+
import scala.util.control.NonFatal
89

910
trait Showable extends Any {
1011

@@ -20,7 +21,11 @@ trait Showable extends Any {
2021
def fallbackToText(printer: Printer): Text = toString
2122

2223
/** The string representation of this showable element. */
23-
def show(implicit ctx: Context): String = toText(ctx.printer).show
24+
def show(implicit ctx: Context): String =
25+
try toText(ctx.printer).show
26+
catch {
27+
case NonFatal(ex) => s"[cannot display due to $ex, raw string = $toString]"
28+
}
2429

2530
/** The summarized string representation of this showable element.
2631
* Recursion depth is limited to some smallish value. Default is

src/dotty/tools/dotc/reporting/Reporter.scala

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import config.Settings.Setting
1111
import config.Printers
1212
import java.lang.System.currentTimeMillis
1313
import typer.ErrorReporting.DiagnosticString
14+
import typer.Mode
1415

1516
object Reporter {
1617

@@ -215,7 +216,7 @@ abstract class Reporter {
215216
}
216217

217218
def report(d: Diagnostic)(implicit ctx: Context): Unit = if (!isHidden(d)) {
218-
doReport(d)
219+
doReport(d)(ctx.addMode(Mode.Printing))
219220
d match {
220221
case d: ConditionalWarning if !d.enablingOption.value => unreportedWarnings(d.enablingOption.name) += 1
221222
case d: Warning => warningCount += 1
@@ -248,7 +249,7 @@ abstract class Reporter {
248249
}
249250

250251
/** Should this diagnostic not be reported at all? */
251-
def isHidden(d: Diagnostic)(implicit ctx: Context): Boolean = false
252+
def isHidden(d: Diagnostic)(implicit ctx: Context): Boolean = ctx.mode.is(Mode.Printing)
252253

253254
/** Does this reporter contain not yet reported errors or warnings? */
254255
def hasPending: Boolean = false

src/dotty/tools/dotc/typer/Mode.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ object Mode {
6363
*/
6464
val AllowDependentFunctions = newMode(9, "AllowDependentFunctions")
6565

66+
/** We are currently printing something: avoid to produce more logs about
67+
* the printing
68+
*/
6669
val Printing = newMode(10, "Printing")
6770

6871
val PatternOrType = Pattern | Type

0 commit comments

Comments
 (0)