Skip to content

Commit 20e7948

Browse files
authored
Merge pull request #6582 from dotty-staging/debug-pos
Add documentation to debug missing position crash
2 parents 63f62b7 + 483a0e6 commit 20e7948

File tree

5 files changed

+69
-31
lines changed

5 files changed

+69
-31
lines changed

compiler/src/dotty/tools/dotc/ast/Positioned.scala

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ import core.Flags.{JavaDefined, Extension}
1010
import core.StdNames.nme
1111
import annotation.constructorOnly
1212
import annotation.internal.sharable
13+
import reporting.Reporter
14+
15+
import java.io.{ PrintWriter }
1316

1417
/** A base class for things that have positions (currently: modifiers and trees)
1518
*/
@@ -24,13 +27,11 @@ abstract class Positioned(implicit @constructorOnly src: SourceFile) extends Pro
2427
def uniqueId: Int = myUniqueId
2528

2629
def uniqueId_=(id: Int): Unit = {
27-
if (Positioned.debugId == id) {
28-
def printTrace() = {
29-
val stack = Thread.currentThread().getStackTrace().map("> " + _)
30-
System.err.println(stack.mkString(s"> Debug tree (id=${Positioned.debugId}) creation \n> $this\n", "\n", "\n"))
31-
}
32-
printTrace()
30+
def printTrace() = {
31+
println(s"Debug tree (id=${Positioned.debugId}) creation \n$this\n")
32+
Reporter.displayPrompt(Console.in, new PrintWriter(Console.err, true))
3333
}
34+
if (Positioned.debugId == id) printTrace()
3435
myUniqueId = id
3536
}
3637

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

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class ConsoleReporter(
2525
val didPrint = m match {
2626
case m: Error =>
2727
printMessage(messageAndPos(m.contained(), m.pos, diagnosticLevel(m)))
28-
if (ctx.settings.Xprompt.value) displayPrompt()
28+
if (ctx.settings.Xprompt.value) Reporter.displayPrompt(reader, writer)
2929
true
3030
case m: ConditionalWarning if !m.enablingOption.value =>
3131
false
@@ -40,28 +40,5 @@ class ConsoleReporter(
4040
printMessage("\nlonger explanation available when compiling with `-explain`")
4141
}
4242

43-
/** Show prompt if `-Xprompt` is passed as a flag to the compiler */
44-
def displayPrompt(): Unit = {
45-
writer.println()
46-
writer.print("a)bort, s)tack, r)esume: ")
47-
writer.flush()
48-
if (reader != null) {
49-
def loop(): Unit = reader.read match {
50-
case 'a' | 'A' =>
51-
new Throwable().printStackTrace(writer)
52-
System.exit(1)
53-
case 's' | 'S' =>
54-
new Throwable().printStackTrace(writer)
55-
writer.println()
56-
writer.flush()
57-
case 'r' | 'R' =>
58-
()
59-
case _ =>
60-
loop()
61-
}
62-
loop()
63-
}
64-
}
65-
6643
override def flush()(implicit ctx: Context): Unit = { writer.flush() }
6744
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,17 @@ import core.Contexts._
88
import util.{SourcePosition, NoSourcePosition}
99
import core.Decorators.PhaseListDecorator
1010
import collection.mutable
11-
import java.lang.System.currentTimeMillis
1211
import core.Mode
1312
import dotty.tools.dotc.core.Symbols.{Symbol, NoSymbol}
1413
import diagnostic.messages._
1514
import diagnostic._
1615
import ast.{tpd, Trees}
1716
import Message._
1817

18+
import java.lang.System.currentTimeMillis
19+
import java.io.{ BufferedReader, PrintWriter }
20+
21+
1922
object Reporter {
2023
/** Convert a SimpleReporter into a real Reporter */
2124
def fromSimpleReporter(simple: interfaces.SimpleReporter): Reporter =
@@ -37,6 +40,29 @@ object Reporter {
3740

3841
private val defaultIncompleteHandler: ErrorHandler =
3942
(mc, ctx) => ctx.reporter.report(mc)(ctx)
43+
44+
/** Show prompt if `-Xprompt` is passed as a flag to the compiler */
45+
def displayPrompt(reader: BufferedReader, writer: PrintWriter): Unit = {
46+
writer.println()
47+
writer.print("a)bort, s)tack, r)esume: ")
48+
writer.flush()
49+
if (reader != null) {
50+
def loop(): Unit = reader.read match {
51+
case 'a' | 'A' =>
52+
new Throwable().printStackTrace(writer)
53+
System.exit(1)
54+
case 's' | 'S' =>
55+
new Throwable().printStackTrace(writer)
56+
writer.println()
57+
writer.flush()
58+
case 'r' | 'R' =>
59+
()
60+
case _ =>
61+
loop()
62+
}
63+
loop()
64+
}
65+
}
4066
}
4167

4268
trait Reporting { this: Context =>

docs/docs/internals/debug-macros.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: doc-page
3+
title: "Debug Macros"
4+
---
5+
6+
Complex macros may break invariants of the compiler, which leads to compiler crashes.
7+
Here we lists common compiler crashes and how to deal with them.
8+
9+
## position not set
10+
11+
For this problem, here is the log that is usually shown:
12+
13+
```
14+
[error] assertion failed: position not set for org.scalactic.anyvals.PosZInt.+$extension3(SizeParam.this.minSize)(
15+
[error] org.scalactic.anyvals.PosZInt.widenToInt(SizeParam.this.sizeRange)
16+
[error] ) # 2326942 of class dotty.tools.dotc.ast.Trees$Apply in library/src-bootstrapped/scala/tasty/reflect/utils/TreeUtils.scala
17+
```
18+
19+
To debug why the position is not set, note the tree id `2326942`, and enable
20+
the following compiler option:
21+
22+
```
23+
-Ydebug-tree-with-id 2326942
24+
```
25+
26+
With the option above, the compiler will crash when the tree is created. From
27+
the stack trace, we will be able to figure out where the tree is created.
28+
29+
If the position is in the compiler, then either report a compiler bug or
30+
fix the problem with `.withSpan(tree.span)`. The following fix is an example:
31+
32+
- https://github.com/lampepfl/dotty/pull/6581

docs/sidebar.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,8 @@ sidebar:
185185
url: docs/internals/type-system.html
186186
- title: "Dotty Internals 1: Trees & Symbols (Meeting Notes)"
187187
url: docs/internals/dotty-internals-1-notes.html
188+
- title: Debug Macros
189+
url: docs/internals/debug-macros.html
188190
- title: Resources
189191
subsection:
190192
- title: Talks

0 commit comments

Comments
 (0)