Skip to content

Make sure messages are lazily evaluated until report in Reporter #1696

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 3 commits into from
Nov 17, 2016
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
14 changes: 7 additions & 7 deletions src/dotty/tools/dotc/parsing/Parsers.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ object Parsers {
/** Issue an error at given offset if beyond last error offset
* and update lastErrorOffset.
*/
def syntaxError(msg: Message, offset: Int = in.offset): Unit =
def syntaxError(msg: => Message, offset: Int = in.offset): Unit =
Copy link
Contributor

Choose a reason for hiding this comment

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

👍 to this whole commit (without having rechecked other occurrences of Message).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks! I discussed with @odersky and we agreed that my other approach was a tad over-engineered...hehe

WDYT @odersky? LGTY?

Copy link
Contributor

Choose a reason for hiding this comment

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

(Note that questions on StoreReporter and so on are still probably on, but all that commit does makes sense).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, the store reporter will force if debugging - but I think we can live with that :)

Copy link
Contributor

Choose a reason for hiding this comment

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

FWIW, IMHO we can live with sharp edges as long as we document them when we notice them, and you already added some comments in this direction so 👍.
I'm just asking for a warning comment on the store reporter lest people use it in other scenarios, as I think Murphy's law applies to software evolution. Copy-pasting this would be enough, if drafting the text is the problem:

/** Beware this can leak memory, see https://github.com/lampepfl/dotty/pull/1696#issuecomment-259739205 */

Copy link
Contributor Author

@felixmulder felixmulder Nov 14, 2016

Choose a reason for hiding this comment

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

As soon as the restructuring repo PR is merged - I wanted to make the StoreReporter private to the compiler - WDYT?

But sure, maybe it could use another comment :)

Copy link
Contributor

Choose a reason for hiding this comment

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

But sure, maybe it could use another comment :)

👍 . "Private to the compiler" doesn't help compiler contributors, neither core ones nor contributors outside EPFL. I also had no clue StoreReporter is for debugging only.

I'm also confused by the pushback — ideally one documents more pitfalls, not fewer (or even better, makes the comments unnecessary by removing them).

I'm aware there are bad comments, and double-checked checklists of comment downsides (on http://wiki.c2.com/?CommentCostsAndBenefits)—and I'm not sure I see a downside here (other than "time to write", which is why I offered text).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, didn't mean to push back - but at home right now ^^. The PR has been updated to document this in a2354dd 👍

Copy link
Contributor

Choose a reason for hiding this comment

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

but at home right now ^^

Oh sorry didn't mean to interrupt. Only time bound I care for: comments are best addressed before the issue/PR is closed, or they should be moved to a new issue lest they're forgot.

if (offset > lastErrorOffset) {
syntaxError(msg, Position(offset))
lastErrorOffset = in.offset
Expand All @@ -108,7 +108,7 @@ object Parsers {
/** Unconditionally issue an error at given position, without
* updating lastErrorOffset.
*/
def syntaxError(msg: Message, pos: Position): Unit =
def syntaxError(msg: => Message, pos: Position): Unit =
ctx.error(msg, source atPos pos)

}
Expand Down Expand Up @@ -215,23 +215,23 @@ object Parsers {
}
}

def warning(msg: Message, sourcePos: SourcePosition) =
def warning(msg: => Message, sourcePos: SourcePosition) =
ctx.warning(msg, sourcePos)

def warning(msg: Message, offset: Int = in.offset) =
def warning(msg: => Message, offset: Int = in.offset) =
ctx.warning(msg, source atPos Position(offset))

def deprecationWarning(msg: Message, offset: Int = in.offset) =
def deprecationWarning(msg: => Message, offset: Int = in.offset) =
ctx.deprecationWarning(msg, source atPos Position(offset))

/** Issue an error at current offset taht input is incomplete */
def incompleteInputError(msg: Message) =
def incompleteInputError(msg: => Message) =
ctx.incompleteInputError(msg, source atPos Position(in.offset))

/** If at end of file, issue an incompleteInputError.
* Otherwise issue a syntax error and skip to next safe point.
*/
def syntaxErrorOrIncomplete(msg: Message) =
def syntaxErrorOrIncomplete(msg: => Message) =
if (in.token == EOF) incompleteInputError(msg)
else {
syntaxError(msg)
Expand Down
48 changes: 26 additions & 22 deletions src/dotty/tools/dotc/reporting/Reporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,16 @@ trait Reporting { this: Context =>
reporter.report(new Info(msg, pos))

def deprecationWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
reporter.report(msg.deprecationWarning(pos))
reporter.report(new DeprecationWarning(msg, pos))

def migrationWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
reporter.report(msg.migrationWarning(pos))
reporter.report(new MigrationWarning(msg, pos))

def uncheckedWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
reporter.report(msg.uncheckedWarning(pos))
reporter.report(new UncheckedWarning(msg, pos))

def featureWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
reporter.report(msg.featureWarning(pos))
reporter.report(new FeatureWarning(msg, pos))

def featureWarning(feature: String, featureDescription: String, isScala2Feature: Boolean,
featureUseSite: Symbol, required: Boolean, pos: SourcePosition): Unit = {
Expand All @@ -73,23 +73,27 @@ trait Reporting { this: Context =>
}

def warning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
reporter.report(msg.warning(pos))
reporter.report(new Warning(msg, pos))

def strictWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
if (this.settings.strict.value) error(msg, pos)
else warning(msg.mapMsg(_ + "\n(This would be an error under strict mode)"), pos)
else reporter.report {
new ExtendMessage(() => msg)(_ + "\n(This would be an error under strict mode)").warning(pos)
}

def error(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
reporter.report(msg.error(pos))
reporter.report(new Error(msg, pos))

def errorOrMigrationWarning(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
if (ctx.scala2Mode) migrationWarning(msg, pos) else error(msg, pos)

def restrictionError(msg: => Message, pos: SourcePosition = NoSourcePosition): Unit =
error(msg.mapMsg(m => s"Implementation restriction: $m"), pos)
reporter.report {
new ExtendMessage(() => msg)(m => s"Implementation restriction: $m").error(pos)
}

def incompleteInputError(msg: Message, pos: SourcePosition = NoSourcePosition)(implicit ctx: Context): Unit =
reporter.incomplete(msg.error(pos))(ctx)
def incompleteInputError(msg: => Message, pos: SourcePosition = NoSourcePosition)(implicit ctx: Context): Unit =
reporter.incomplete(new Error(msg, pos))(ctx)

/** Log msg if settings.log contains the current phase.
* See [[config.CompilerCommand#explainAdvanced]] for the exact meaning of
Expand Down Expand Up @@ -192,7 +196,7 @@ trait Reporting { this: Context =>
abstract class Reporter extends interfaces.ReporterResult {

/** Report a diagnostic */
def doReport(d: MessageContainer)(implicit ctx: Context): Unit
def doReport(m: MessageContainer)(implicit ctx: Context): Unit

/** Whether very long lines can be truncated. This exists so important
* debugging information (like printing the classpath) is not rendered
Expand Down Expand Up @@ -236,22 +240,22 @@ abstract class Reporter extends interfaces.ReporterResult {
override def default(key: String) = 0
}

def report(d: MessageContainer)(implicit ctx: Context): Unit =
if (!isHidden(d)) {
doReport(d)(ctx.addMode(Mode.Printing))
d match {
case d: ConditionalWarning if !d.enablingOption.value => unreportedWarnings(d.enablingOption.name) += 1
case d: Warning => warningCount += 1
case d: Error =>
errors = d :: errors
def report(m: MessageContainer)(implicit ctx: Context): Unit =
if (!isHidden(m)) {
doReport(m)(ctx.addMode(Mode.Printing))
m match {
case m: ConditionalWarning if !m.enablingOption.value => unreportedWarnings(m.enablingOption.name) += 1
case m: Warning => warningCount += 1
case m: Error =>
errors = m :: errors
errorCount += 1
case d: Info => // nothing to do here
case m: Info => // nothing to do here
// match error if d is something else
}
}

def incomplete(d: MessageContainer)(implicit ctx: Context): Unit =
incompleteHandler(d)(ctx)
def incomplete(m: MessageContainer)(implicit ctx: Context): Unit =
incompleteHandler(m)(ctx)

/** Summary of warnings and errors */
def summary: String = {
Expand Down
15 changes: 11 additions & 4 deletions src/dotty/tools/dotc/reporting/StoreReporter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,16 @@ import config.Printers.typr
import diagnostic.MessageContainer
import diagnostic.messages._

/**
* This class implements a Reporter that stores all messages
*/
/** This class implements a Reporter that stores all messages
*
* Beware that this reporter can leak memory, and force messages in two
* scenarios:
*
* - During debugging `config.Printers.typr` is set from `noPrinter` to `new
* Printer`, which forces the message
* - The reporter is not flushed and the message containers capture a
* `Context` (about 4MB)
*/
class StoreReporter(outer: Reporter) extends Reporter {

private var infos: mutable.ListBuffer[MessageContainer] = null
Expand All @@ -31,7 +38,7 @@ class StoreReporter(outer: Reporter) extends Reporter {

override def flush()(implicit ctx: Context) =
if (infos != null) {
infos foreach ctx.reporter.report
infos.foreach(ctx.reporter.report(_))
infos = null
}

Expand Down
54 changes: 40 additions & 14 deletions src/dotty/tools/dotc/reporting/diagnostic/Message.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ package diagnostic
import util.SourcePosition
import core.Contexts.Context

import messages._

object Message {
/** This implicit conversion provides a fallback for error messages that have
* not yet been ported to the new scheme. Comment out this `implicit def` to
Expand All @@ -20,6 +22,13 @@ object Message {
* into a `MessageContainer` which contains the log level and can later be
* consumed by a subclass of `Reporter`.
*
* NOTE: you should not be persisting messages. Most messages take an implicit
* `Context` and these contexts weigh in at about 4mb per instance, as such
* persisting these will result in a memory leak.
*
* Instead use the `persist` method to create an instance that does not keep a
* reference to these contexts.
*
* @param errorId a unique number identifying the message, this will later be
* used to reference documentation online
*/
Expand Down Expand Up @@ -47,45 +56,62 @@ abstract class Message(val errorId: Int) { self =>
*/
def explanation: String

/** It is possible to map `msg` to add details, this is at the loss of
* precision since the type of the resulting `Message` won't be original
* extending class
*
* @return a `Message` with the mapped message
/** The implicit `Context` in messages is a large thing that we don't want
* persisted. This method gets around that by duplicating the message
* without the implicit context being passed along.
*/
def mapMsg(f: String => String) = new Message(errorId) {
val msg = f(self.msg)
def persist: Message = new Message (errorId) {
val msg = self.msg
val kind = self.kind
val explanation = self.explanation
}
}

/** An extended message keeps the contained message from being evaluated, while
* allowing for extension for the `msg` string
*
* This is useful when we need to add additional information to an existing
* message.
*/
class ExtendMessage(_msg: () => Message)(f: String => String) { self =>
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe MappedMessage or TransformedMessage would be even clearer.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure :)

lazy val msg = f(_msg().msg)
lazy val kind = _msg().kind
lazy val explanation = _msg().explanation
lazy val errorId = _msg().errorId

private def toMessage = new Message(errorId) {
val msg = self.msg
val kind = self.kind
val explanation = self.explanation
}

/** Enclose this message in an `Error` container */
def error(pos: SourcePosition) =
new Error(self, pos)
new Error(toMessage, pos)

/** Enclose this message in an `Warning` container */
def warning(pos: SourcePosition) =
new Warning(self, pos)
new Warning(toMessage, pos)

/** Enclose this message in an `Info` container */
def info(pos: SourcePosition) =
new Info(self, pos)
new Info(toMessage, pos)

/** Enclose this message in an `FeatureWarning` container */
def featureWarning(pos: SourcePosition) =
new FeatureWarning(self, pos)
new FeatureWarning(toMessage, pos)

/** Enclose this message in an `UncheckedWarning` container */
def uncheckedWarning(pos: SourcePosition) =
new UncheckedWarning(self, pos)
new UncheckedWarning(toMessage, pos)

/** Enclose this message in an `DeprecationWarning` container */
def deprecationWarning(pos: SourcePosition) =
new DeprecationWarning(self, pos)
new DeprecationWarning(toMessage, pos)

/** Enclose this message in an `MigrationWarning` container */
def migrationWarning(pos: SourcePosition) =
new MigrationWarning(self, pos)
new MigrationWarning(toMessage, pos)
}

/** The fallback `Message` containing no explanation and having no `kind` */
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/reporting/diagnostic/messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ object messages {
val explanation = ""
}

case class EarlyDefinitionsNotSupported()(implicit ctx:Context)
case class EarlyDefinitionsNotSupported()(implicit ctx: Context)
extends Message(9) {
val kind = "Syntax"
val msg = "early definitions are not supported; use trait parameters instead"
Expand Down
37 changes: 37 additions & 0 deletions test/dotty/tools/dotc/reporting/TestMessageLaziness.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package dotty.tools
package dotc
package reporting

import org.junit.Assert._
import org.junit.Test

import core.Contexts._

import test.DottyTest

import diagnostic.{ Message, MessageContainer, ExtendMessage }

class TestMessageLaziness extends DottyTest {
ctx = ctx.fresh.setReporter(new NonchalantReporter)

class NonchalantReporter(implicit ctx: Context) extends Reporter
with UniqueMessagePositions with HideNonSensicalMessages {
def doReport(m: MessageContainer)(implicit ctx: Context) = ???

override def report(m: MessageContainer)(implicit ctx: Context) = ()
}

case class LazyError() extends Message(1000) {
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

throw new Error("Didn't stay lazy.")

val kind = "Test"
val msg = "Please don't blow up"
val explanation = ""
}

@Test def assureLazy =
ctx.error(LazyError())

@Test def assureLazyExtendMessage =
ctx.strictWarning(LazyError())
}