-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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 | ||
*/ | ||
|
@@ -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 => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe There was a problem hiding this comment. Choose a reason for hiding this commentThe 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` */ | ||
|
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(d: MessageContainer)(implicit ctx: Context) = ??? | ||
|
||
override def report(d: => MessageContainer)(implicit ctx: Context) = () | ||
} | ||
|
||
case class LazyError() extends Message(1000) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
MessageContainer
still takes messages by-name so I doubt it needs to be by-name itself.MessageContainer
would have to also become by-name—starting fromisHidden
. A test might be harder, so one would have to grep for uses ofMessageContainer
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I'll investigate this with...more tests! 😮