Skip to content
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

Allow custom error message for assertion failures via @msg annotation #815

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
34 changes: 33 additions & 1 deletion src/main/scala/viper/silver/verifier/VerificationError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,23 @@ sealed abstract class AbstractVerificationError extends VerificationError {

def pos = offendingNode.pos

def readableMessage(withId: Boolean, withPosition: Boolean) = {
val customMessageAnnotation = "msg"

def customErrorMessage = offendingNode match {
case i: Infoed => i.info.getUniqueInfo[AnnotationInfo] match {
case Some(ai) if ai.values.contains(customMessageAnnotation) && ai.values(customMessageAnnotation).nonEmpty =>
Some(ai.values("msg").head)
case _ => None
}
case _ => None
}

def readableMessage(withId: Boolean, withPosition: Boolean) = customErrorMessage match {
case Some(msg) => msg
case _ => defaultReadableMessage(withId, withPosition)
}

def defaultReadableMessage(withId: Boolean, withPosition: Boolean) = {
val idStr = if (withId) s"[$fullId] " else ""
val posStr = if (withPosition) s" ($pos)" else ""

Expand Down Expand Up @@ -399,6 +415,22 @@ object errors {
val id = "assert.failed"
val text = "Assert might fail."

def customAssertErrorMessage = offendingNode.info.getUniqueInfo[AnnotationInfo] match {
case Some(ai) if ai.values.contains(customMessageAnnotation) && ai.values(customMessageAnnotation).nonEmpty =>
Some(ai.values("msg").head)
case _ => None
}

override def readableMessage: String = customAssertErrorMessage match {
case Some(msg) => msg
case None => super.readableMessage
}

override def readableMessage(withId: Boolean, withPosition: Boolean): String = customAssertErrorMessage match {
case Some(msg) => msg
case None => super.readableMessage(withId, withPosition)
}

override def pos = offendingNode.exp.pos
def withNode(offendingNode: errors.ErrorNode = this.offendingNode) = AssertFailed(offendingNode.asInstanceOf[Assert], this.reason, this.cached)
def withReason(r: ErrorReason) = AssertFailed(offendingNode, r, cached)
Expand Down
Loading