Skip to content

Refactor Diagnostic #1100

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

Closed
wants to merge 1 commit into from
Closed
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
5 changes: 4 additions & 1 deletion src/dotty/tools/dotc/reporting/ConsoleReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ class ConsoleReporter(
}

override def doReport(d: Diagnostic)(implicit ctx: Context): Boolean = {
val issue = !(d.isSuppressed && hasErrors)
val issue =
!d.isNonSensical ||
!hasErrors || // if there are no errors yet, report even if diagnostic is non-sensical
ctx.settings.YshowSuppressedErrors.value
if (issue) d match {
case d: Error =>
printMessageAndPos(s"error: ${d.msg}", d.pos)
Expand Down
47 changes: 47 additions & 0 deletions src/dotty/tools/dotc/reporting/Diagnostic.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package dotty.tools
package dotc
package reporting

import util.SourcePosition

object Diagnostic {

// Error levels
val ERROR = 2
val WARNING = 1
val INFO = 0

val nonSensicalStartTag = "<nonsensical>"
val nonSensicalEndTag = "</nonsensical>"
}

class Diagnostic(msgFn: => String, val pos: SourcePosition, val level: Int) extends Exception {
import Diagnostic._
private var myMsg: String = null
private var myIsNonSensical: Boolean = false

/** The message to report */
def msg: String = {
if (myMsg == null) {
myMsg = msgFn
if (myMsg.contains(nonSensicalStartTag)) {
myIsNonSensical = true
// myMsg might be composed of several d"..." invocations -> nested nonsensical tags possible
myMsg = myMsg.replaceAllLiterally(nonSensicalStartTag, "").replaceAllLiterally(nonSensicalEndTag, "")
}
}
myMsg
}

/** A message is non-sensical if it contains references to <nonsensical> tags.
* Such tags are inserted by the error diagnostic framework if a message
* contains references to internally generated error types. Normally we
* want to suppress error messages referring to types like this because
* they look weird and are normally follow-up errors to something that
* was diagnosed before.
*/
def isNonSensical = { msg; myIsNonSensical }

override def toString = s"$getClass at $pos: $msg"
override def getMessage() = msg
}
37 changes: 1 addition & 36 deletions src/dotty/tools/dotc/reporting/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,44 +12,9 @@ import config.Printers
import java.lang.System.currentTimeMillis
import typer.ErrorReporting.DiagnosticString
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the weird test errors are because DiagnosticString does not exist anymore.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good analysis. Can you take it over and integrate the PR request directly
into your work?

On Wed, Feb 17, 2016 at 6:53 PM, Guillaume Martres <[email protected]

wrote:

In src/dotty/tools/dotc/reporting/Reporter.scala
#1100 (comment):

@@ -12,44 +12,9 @@ import config.Printers
import java.lang.System.currentTimeMillis
import typer.ErrorReporting.DiagnosticString

I think the weird test errors are because DiagnosticString does not exist
anymore.


Reply to this email directly or view it on GitHub
https://github.com/lampepfl/dotty/pull/1100/files#r53202952.

Martin Odersky
EPFL

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure.

import typer.Mode
import Diagnostic.{ERROR, WARNING, INFO}

object Reporter {

private val ERROR = 2
private val WARNING = 1
private val INFO = 0

class Diagnostic(msgFn: => String, val pos: SourcePosition, val level: Int) extends Exception {
import DiagnosticString._

private var myMsg: String = null
private var myIsNonSensical: Boolean = false

/** The message to report */
def msg: String = {
if (myMsg == null) {
myMsg = msgFn
if (myMsg.contains(nonSensicalStartTag)) {
myIsNonSensical = true
// myMsg might be composed of several d"..." invocations -> nested nonsensical tags possible
myMsg = myMsg.replaceAllLiterally(nonSensicalStartTag, "").replaceAllLiterally(nonSensicalEndTag, "")
}
}
myMsg
}

/** Report in current reporter */
def report(implicit ctx: Context) = ctx.reporter.report(this)

def isNonSensical = { msg; myIsNonSensical }
def isSuppressed(implicit ctx: Context): Boolean = !ctx.settings.YshowSuppressedErrors.value && isNonSensical

override def toString = s"$getClass at $pos: $msg"
override def getMessage() = msg

def checkingStr: String = msgFn
}

class Error(msgFn: => String, pos: SourcePosition) extends Diagnostic(msgFn, pos, ERROR)
class Warning(msgFn: => String, pos: SourcePosition) extends Diagnostic(msgFn, pos, WARNING)
class Info(msgFn: => String, pos: SourcePosition) extends Diagnostic(msgFn, pos, INFO)
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/reporting/StoreReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ package reporting

import core.Contexts.Context
import collection.mutable
import Reporter.{Diagnostic, Error, Warning}
import Reporter.{Error, Warning}
import config.Printers._

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ package reporting

import scala.collection.mutable
import util.{SourcePosition, SourceFile}
import Reporter.Diagnostic
import core.Contexts.Context

/**
Expand Down
10 changes: 3 additions & 7 deletions src/dotty/tools/dotc/typer/ErrorReporting.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Trees._
import Types._, ProtoTypes._, Contexts._, Decorators._, Denotations._, Symbols._
import Applications._, Implicits._, Flags._
import util.Positions._
import reporting.Diagnostic
import printing.Showable
import printing.Disambiguation.disambiguated

Expand Down Expand Up @@ -139,13 +140,8 @@ object ErrorReporting {
}

val s = new StringInterpolators(sc).i(args : _*)
if (args.forall(isSensical(_))) s else nonSensicalStartTag + s + nonSensicalEndTag
if (args.forall(isSensical(_))) s
else Diagnostic.nonSensicalStartTag + s + Diagnostic.nonSensicalEndTag
}
}

object DiagnosticString {
final val nonSensicalStartTag = "<nonsensical>"
final val nonSensicalEndTag = "</nonsensical>"
}

}