-
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 all commits
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(m: MessageContainer)(implicit ctx: Context) = ??? | ||
|
||
override def report(m: 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.
👍 to this whole commit (without having rechecked other occurrences of
Message
).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.
Thanks! I discussed with @odersky and we agreed that my other approach was a tad over-engineered...hehe
WDYT @odersky? LGTY?
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.
(Note that questions on
StoreReporter
and so on are still probably on, but all that commit does makes sense).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.
Yes, the store reporter will force if debugging - but I think we can live with that :)
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.
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 */
Uh oh!
There was an error while loading. Please reload this page.
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.
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 :)
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.
👍 . "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).
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.
Yeah, didn't mean to push back - but at home right now ^^. The PR has been updated to document this in a2354dd 👍
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.
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.