Skip to content

Commit

Permalink
report: Trim down enriched error messages
Browse files Browse the repository at this point in the history
The implementation here comes from porting from nsc.  But I think
things were done differently enough with its Global state, that make it
not translate nicely here.  So I'm paring it down to what I know works
(at least for me): versions, settings, compilation unit.  Not the
tree/symbol/site stuff.
  • Loading branch information
dwijnand committed Oct 17, 2023
1 parent 8c1cdc2 commit b120977
Showing 1 changed file with 3 additions and 36 deletions.
39 changes: 3 additions & 36 deletions compiler/src/dotty/tools/dotc/report.scala
Original file line number Diff line number Diff line change
Expand Up @@ -136,46 +136,16 @@ object report:
def formatExplain(pairs: List[(String, Any)]) = pairs.map((k, v) => f"$k%20s: $v").mkString("\n")

val settings = ctx.settings.userSetSettings(ctx.settingsState).sortBy(_.name)
val tree = ctx.tree
val sym = tree.symbol
val pos = tree.sourcePos
val path = pos.source.path
val site = ctx.outersIterator.map(_.owner).filter(sym => !sym.exists || sym.isClass || sym.is(Method)).next()

import untpd.*
extension (tree: Tree) def summaryString: String = tree match
case Literal(const) => s"Literal($const)"
case Ident(name) => s"Ident(${name.decode})"
case Select(qual, name) => s"Select(${qual.summaryString}, ${name.decode})"
case tree: NameTree => (if tree.isType then "type " else "") + tree.name.decode
case tree => s"${tree.className}${if tree.symbol.exists then s"(${tree.symbol})" else ""}"
def showSetting(s: Setting[?]): String = if s.value == "" then s"${s.name} \"\"" else s"${s.name} ${s.value}"

val info1 = formatExplain(List(
"while compiling" -> ctx.compilationUnit,
"during phase" -> ctx.phase.megaPhase,
"mode" -> ctx.mode,
"library version" -> scala.util.Properties.versionString,
"compiler version" -> dotty.tools.dotc.config.Properties.versionString,
"settings" -> settings.map(s => if s.value == "" then s"${s.name} \"\"" else s"${s.name} ${s.value}").mkString(" "),
"settings" -> settings.map(showSetting).mkString(" "),
))
val symbolInfos = if sym eq NoSymbol then List("symbol" -> sym) else List(
"symbol" -> sym.showLocated,
"symbol definition" -> s"${sym.showDcl} (a ${sym.className})",
"symbol package" -> sym.enclosingPackageClass.fullName,
"symbol owners" -> sym.showExtendedLocation,
)
val info2 = formatExplain(List(
"tree" -> tree.summaryString,
"tree position" -> (if pos.exists then s"$path:${pos.line + 1}:${pos.column}" else s"$path:<unknown>"),
"tree type" -> tree.typeOpt.show,
) ::: symbolInfos ::: List(
"call site" -> s"${site.showLocated} in ${site.enclosingPackageClass}"
))
val context_s = try
s""" == Source file context for tree position ==
|
|${messageRendering.messageAndPos(Diagnostic.Error("", pos))}""".stripMargin
catch case _: Exception => "<Cannot read source file>"
s"""
| $errorMessage
|
Expand All @@ -184,9 +154,6 @@ object report:
| https://github.com/lampepfl/dotty/issues/new/choose
|
|$info1
|
|$info2
|
|$context_s""".stripMargin
|""".stripMargin
} catch case _: Throwable => errorMessage // don't introduce new errors trying to report errors, so swallow exceptions
end report

0 comments on commit b120977

Please sign in to comment.