Skip to content

Commit e7e3718

Browse files
committed
Add -Ydebug-type-error to print the stack when a TypeError is caught
1 parent 16184eb commit e7e3718

File tree

11 files changed

+30
-14
lines changed

11 files changed

+30
-14
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ class TreeMapWithImplicits extends tpd.TreeMap {
119119
}
120120
catch {
121121
case ex: TypeError =>
122-
ctx.error(ex.toMessage, tree.sourcePos)
122+
ctx.error(ex, tree.sourcePos)
123123
tree
124124
}
125125
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ class ScalaSettings extends Settings.SettingGroup {
103103
val YdebugNames: Setting[Boolean] = BooleanSetting("-Ydebug-names", "Show internal representation of names")
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)
106+
val YdebugTypeError: Setting[Boolean] = BooleanSetting("-Ydebug-type-error", "Print the stack trace when a TypeError is caught", false)
106107
val YtermConflict: Setting[String] = ChoiceSetting("-Yresolve-term-conflict", "strategy", "Resolve term conflicts", List("package", "object", "error"), "error")
107108
val Ylog: Setting[List[String]] = PhasesSetting("-Ylog", "Log operations during")
108109
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package reporting
55
import scala.annotation.internal.sharable
66

77
import core.Contexts._
8+
import core.TypeError
89
import util.{SourcePosition, NoSourcePosition}
910
import core.Decorators.PhaseListDecorator
1011
import collection.mutable
@@ -136,6 +137,12 @@ trait Reporting { this: Context =>
136137
reporter.report(if (sticky) new StickyError(msg, fullPos) else new Error(msg, fullPos))
137138
}
138139

140+
def error(ex: TypeError, pos: SourcePosition): Unit = {
141+
error(ex.toMessage, pos, sticky = true)
142+
if (ctx.settings.YdebugTypeError.value)
143+
ex.printStackTrace
144+
}
145+
139146
def errorOrMigrationWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
140147
if (ctx.scala2Mode) migrationWarning(msg, pos) else error(msg, pos)
141148

compiler/src/dotty/tools/dotc/sbt/ExtractAPI.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ private class ExtractAPICollector(implicit val ctx: Context) extends ThunkHolder
247247
case ex: TypeError =>
248248
// See neg/i1750a for an example where a cyclic error can arise.
249249
// The root cause in this example is an illegal "override" of an inner trait
250-
ctx.error(ex.toMessage, csym.sourcePos, sticky = true)
250+
ctx.error(ex, csym.sourcePos)
251251
defn.ObjectType :: Nil
252252
}
253253
if (ValueClasses.isDerivedValueClass(csym)) {

compiler/src/dotty/tools/dotc/transform/MacroTransform.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ abstract class MacroTransform extends Phase {
6464
}
6565
catch {
6666
case ex: TypeError =>
67-
ctx.error(ex.toMessage, tree.sourcePos, sticky = true)
67+
ctx.error(ex, tree.sourcePos)
6868
tree
6969
}
7070

compiler/src/dotty/tools/dotc/transform/MegaPhase.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ class MegaPhase(val miniPhases: Array[MiniPhase]) extends Phase {
170170
}
171171
catch {
172172
case ex: TypeError =>
173-
ctx.error(ex.toMessage, tree.sourcePos)
173+
ctx.error(ex, tree.sourcePos)
174174
tree
175175
}
176176
def goUnnamed(tree: Tree, start: Int) =
@@ -205,7 +205,7 @@ class MegaPhase(val miniPhases: Array[MiniPhase]) extends Phase {
205205
}
206206
catch {
207207
case ex: TypeError =>
208-
ctx.error(ex.toMessage, tree.sourcePos, sticky = true)
208+
ctx.error(ex, tree.sourcePos)
209209
tree
210210
}
211211
if (tree.isInstanceOf[NameTree]) goNamed(tree, start) else goUnnamed(tree, start)

compiler/src/dotty/tools/dotc/transform/OverridingPairs.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ object OverridingPairs {
134134
case ex: TypeError =>
135135
// See neg/i1750a for an example where a cyclic error can arise.
136136
// The root cause in this example is an illegal "override" of an inner trait
137-
ctx.error(ex.toMessage, base.sourcePos, sticky = true)
137+
ctx.error(ex, base.sourcePos)
138138
}
139139
} else {
140140
curEntry = curEntry.prev

compiler/src/dotty/tools/dotc/typer/ErrorReporting.scala

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,25 @@ object ErrorReporting {
1616

1717
import tpd._
1818

19-
def errorTree(tree: untpd.Tree, msg: => Message, pos: SourcePosition, sticky: Boolean = false)(implicit ctx: Context): tpd.Tree =
20-
tree.withType(errorType(msg, pos, sticky))
19+
def errorTree(tree: untpd.Tree, msg: => Message, pos: SourcePosition)(implicit ctx: Context): tpd.Tree =
20+
tree.withType(errorType(msg, pos))
2121

2222
def errorTree(tree: untpd.Tree, msg: => Message)(implicit ctx: Context): tpd.Tree =
2323
errorTree(tree, msg, tree.sourcePos)
2424

25-
def errorType(msg: => Message, pos: SourcePosition, sticky: Boolean = false)(implicit ctx: Context): ErrorType = {
26-
ctx.error(msg, pos, sticky)
25+
def errorTree(tree: untpd.Tree, msg: TypeError, pos: SourcePosition)(implicit ctx: Context): tpd.Tree =
26+
tree.withType(errorType(msg, pos))
27+
28+
def errorType(msg: => Message, pos: SourcePosition)(implicit ctx: Context): ErrorType = {
29+
ctx.error(msg, pos)
2730
ErrorType(msg)
2831
}
2932

33+
def errorType(ex: TypeError, pos: SourcePosition)(implicit ctx: Context): ErrorType = {
34+
ctx.error(ex, pos)
35+
ErrorType(ex.toMessage)
36+
}
37+
3038
def wrongNumberOfTypeArgs(fntpe: Type, expectedArgs: List[ParamInfo], actual: List[untpd.Tree], pos: SourcePosition)(implicit ctx: Context): ErrorType =
3139
errorType(WrongNumberOfTypeArgs(fntpe, expectedArgs, actual)(ctx), pos)
3240

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -971,7 +971,7 @@ class RefChecks extends MiniPhase { thisPhase =>
971971
tree
972972
} catch {
973973
case ex: TypeError =>
974-
ctx.error(ex.toMessage, tree.sourcePos, sticky = true)
974+
ctx.error(ex, tree.sourcePos)
975975
tree
976976
}
977977

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2419,7 +2419,7 @@ class Typer extends Namer
24192419
try adapt(typedUnadapted(tree, pt, locked), pt, locked)
24202420
catch {
24212421
case ex: TypeError =>
2422-
errorTree(tree, ex.toMessage, tree.sourcePos.focus, sticky = true)
2422+
errorTree(tree, ex, tree.sourcePos.focus)
24232423
// This uses tree.span.focus instead of the default tree.span, because:
24242424
// - since tree can be a top-level definition, tree.span can point to the whole definition
24252425
// - that would in turn hide all other type errors inside tree.
@@ -3195,7 +3195,7 @@ class Typer extends Namer
31953195
}
31963196
}
31973197
catch {
3198-
case ex: TypeError => errorTree(tree, ex.toMessage, tree.sourcePos, sticky = true)
3198+
case ex: TypeError => errorTree(tree, ex, tree.sourcePos)
31993199
}
32003200
val nestedCtx = ctx.fresh.setNewTyperState()
32013201
val app = tryExtension(nestedCtx)

0 commit comments

Comments
 (0)