Skip to content

Commit 86dd647

Browse files
Close lampepfl/dotty-knowledge#15: Add a flag to print traces of compile errors
1 parent 746653e commit 86dd647

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class ScalaSettings extends Settings.SettingGroup {
104104
val YdebugPos: Setting[Boolean] = BooleanSetting("-Ydebug-pos", "Show full source positions including spans")
105105
val YdebugTreeWithId: Setting[Int] = IntSetting("-Ydebug-tree-with-id", "Print the stack trace when the tree with the given id is created", Int.MinValue)
106106
val YdebugTypeError: Setting[Boolean] = BooleanSetting("-Ydebug-type-error", "Print the stack trace when a TypeError is caught", false)
107+
val YdebugError: Setting[Boolean] = BooleanSetting("-Ydebug-error", "Print the stack trace when any error is caught", false)
107108
val YtermConflict: Setting[String] = ChoiceSetting("-Yresolve-term-conflict", "strategy", "Resolve term conflicts", List("package", "object", "error"), "error")
108109
val Ylog: Setting[List[String]] = PhasesSetting("-Ylog", "Log operations during")
109110
val YemitTastyInClass: Setting[Boolean] = BooleanSetting("-Yemit-tasty-in-class", "Generate tasty in the .class file and add an empty *.hasTasty file.")

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,8 @@ trait Reporting { this: Context =>
135135
def error(msg: => Message, pos: SourcePosition = NoSourcePosition, sticky: Boolean = false): Unit = {
136136
val fullPos = addInlineds(pos)
137137
reporter.report(if (sticky) new StickyError(msg, fullPos) else new Error(msg, fullPos))
138+
if (ctx.settings.YdebugError.value)
139+
Thread.dumpStack()
138140
}
139141

140142
def error(ex: TypeError, pos: SourcePosition): Unit = {

docs/docs/contributing/debugging.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ title: Debugging Techniques
99
- [How to disable color](#how-to-disable-color)
1010
- [Reporting as a non-intrusive println](#reporting-as-a-non-intrusive-println)
1111
- [Printing out trees after phases](#printing-out-trees-after-phases)
12+
- [Printing out stack traces of compile time errors](#printing-out-stack-traces-of-compile-time-errors)
1213
- [Configuring the printer output](#configuring-the-printer-output)
1314
- [Figuring out an object creation site](#figuring-out-an-object-creation-site)
1415
* [Via ID](#via-id)
@@ -110,6 +111,34 @@ dotc -Xprint:all ../issues/Playground.scala
110111

111112
To find out the list of all the phases and their names, check out [this](https://github.com/lampepfl/dotty/blob/10526a7d0aa8910729b6036ee51942e05b71abf6/compiler/src/dotty/tools/dotc/Compiler.scala#L34) line in `Compiler.scala`. Each `Phase` object has `phaseName` defined on it, this is the phase name.
112113

114+
## Printing out stack traces of compile time errors
115+
You can use the flag `-Ydebug-error` to get the stack trace of all the compile-time errors. Consider the following file:
116+
117+
```scala
118+
object Foo
119+
object Foo
120+
```
121+
122+
Clearly we cannot define an object `Foo` twice. Now compile it as follows: `dotc -Ydebug-error ../issues/Playground.scala` (use whatever path you saved it under). The result will be as follows:
123+
124+
```scala
125+
-- Error: ../issues/Playground.scala:2:0 ---------------------------------------
126+
2 |object Foo
127+
|^
128+
|object Foo has already been compiled once during this run
129+
java.lang.Thread.getStackTrace(Thread.java:1552)
130+
dotty.tools.dotc.reporting.Reporting.error(Reporter.scala:139)
131+
dotty.tools.dotc.core.Contexts$Context.error(Contexts.scala:71)
132+
dotty.tools.dotc.typer.Namer.errorName$2(Namer.scala:300)
133+
dotty.tools.dotc.typer.Namer.checkNoConflict$1(Namer.scala:306)
134+
dotty.tools.dotc.typer.Namer.createSymbol(Namer.scala:353)
135+
dotty.tools.dotc.typer.Namer.recur$1(Namer.scala:490)
136+
dotty.tools.dotc.typer.Namer.recur$3$$anonfun$2(Namer.scala:495)
137+
...
138+
```
139+
140+
So, the error happened in the Namer's `checkNoConflict` method (after which all the stack frames represent the mechanics of issuing an error, not an intent that produced the error in the first place).
141+
113142
## Configuring the printer output
114143
Printing from the `show` and `-Xprint` is done from the Printers framework (discussed in more details below). The following settings influence the output of the printers:
115144

0 commit comments

Comments
 (0)