Skip to content

Add a flag to print traces of compile errors #6942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class ScalaSettings extends Settings.SettingGroup {
val YdebugPos: Setting[Boolean] = BooleanSetting("-Ydebug-pos", "Show full source positions including spans")
val YdebugTreeWithId: Setting[Int] = IntSetting("-Ydebug-tree-with-id", "Print the stack trace when the tree with the given id is created", Int.MinValue)
val YdebugTypeError: Setting[Boolean] = BooleanSetting("-Ydebug-type-error", "Print the stack trace when a TypeError is caught", false)
val YdebugError: Setting[Boolean] = BooleanSetting("-Ydebug-error", "Print the stack trace when any error is caught", false)
val YtermConflict: Setting[String] = ChoiceSetting("-Yresolve-term-conflict", "strategy", "Resolve term conflicts", List("package", "object", "error"), "error")
val Ylog: Setting[List[String]] = PhasesSetting("-Ylog", "Log operations during")
val YemitTastyInClass: Setting[Boolean] = BooleanSetting("-Yemit-tasty-in-class", "Generate tasty in the .class file and add an empty *.hasTasty file.")
Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/reporting/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@ trait Reporting { this: Context =>
def error(msg: => Message, pos: SourcePosition = NoSourcePosition, sticky: Boolean = false): Unit = {
val fullPos = addInlineds(pos)
reporter.report(if (sticky) new StickyError(msg, fullPos) else new Error(msg, fullPos))
if (ctx.settings.YdebugError.value)
Thread.dumpStack()
}

def error(ex: TypeError, pos: SourcePosition): Unit = {
Expand Down
29 changes: 29 additions & 0 deletions docs/docs/contributing/debugging.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ title: Debugging Techniques
- [How to disable color](#how-to-disable-color)
- [Reporting as a non-intrusive println](#reporting-as-a-non-intrusive-println)
- [Printing out trees after phases](#printing-out-trees-after-phases)
- [Printing out stack traces of compile time errors](#printing-out-stack-traces-of-compile-time-errors)
- [Configuring the printer output](#configuring-the-printer-output)
- [Figuring out an object creation site](#figuring-out-an-object-creation-site)
* [Via ID](#via-id)
Expand Down Expand Up @@ -110,6 +111,34 @@ dotc -Xprint:all ../issues/Playground.scala

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.

## Printing out stack traces of compile time errors
You can use the flag `-Ydebug-error` to get the stack trace of all the compile-time errors. Consider the following file:

```scala
object Foo
object Foo
```

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:

```scala
-- Error: ../issues/Playground.scala:2:0 ---------------------------------------
2 |object Foo
|^
|object Foo has already been compiled once during this run
java.lang.Thread.getStackTrace(Thread.java:1552)
dotty.tools.dotc.reporting.Reporting.error(Reporter.scala:139)
dotty.tools.dotc.core.Contexts$Context.error(Contexts.scala:71)
dotty.tools.dotc.typer.Namer.errorName$2(Namer.scala:300)
dotty.tools.dotc.typer.Namer.checkNoConflict$1(Namer.scala:306)
dotty.tools.dotc.typer.Namer.createSymbol(Namer.scala:353)
dotty.tools.dotc.typer.Namer.recur$1(Namer.scala:490)
dotty.tools.dotc.typer.Namer.recur$3$$anonfun$2(Namer.scala:495)
...
```

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).

## Configuring the printer output
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:

Expand Down