Skip to content

Commit

Permalink
fix #491
Browse files Browse the repository at this point in the history
  • Loading branch information
Felalolf committed Jul 20, 2022
1 parent d50b823 commit 081cd2a
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ object DefaultErrorBackTranslator {

val transformVerificationErrorReason: VerificationErrorReason => VerificationErrorReason = {
case AssertionFalseError(info / OverflowCheckAnnotation) => OverflowErrorReason(info)
case AssertionFalseError(info / ReceiverNotNilCheckAnnotation) => InterfaceReceiverIsNilReason(info)
case AssertionFalseError(info / ReceiverNotNilCheckAnnotation) => ReceiverIsNilReason(info)
case x => x
}

Expand Down
7 changes: 6 additions & 1 deletion src/main/scala/viper/gobra/reporting/VerifierError.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,11 @@ case class CallError(info: Source.Verifier.Info) extends VerificationError {
override def localMessage: String = "Call might fail"
}

case class DerefError(info: Source.Verifier.Info) extends VerificationError {
override def localId: String = "dereference_error"
override def localMessage: String = "Dereference might fail"
}

case class PostconditionError(info: Source.Verifier.Info) extends VerificationError {
override def localId: String = "postcondition_error"
override def localMessage: String = "Postcondition might not hold"
Expand Down Expand Up @@ -381,7 +386,7 @@ case class OverflowErrorReason(node: Source.Verifier.Info) extends VerificationE
override def message: String = s"Expression ${node.origin.tag.trim} might cause integer overflow."
}

case class InterfaceReceiverIsNilReason(node: Source.Verifier.Info) extends VerificationErrorReason {
case class ReceiverIsNilReason(node: Source.Verifier.Info) extends VerificationErrorReason {
override def id: String = "receiver_is_nil_error"
override def message: String = s"The receiver might be nil"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@ package viper.gobra.translator.encodings

import org.bitbucket.inkytonik.kiama.==>
import viper.gobra.ast.{internal => in}
import viper.gobra.reporting.{DerefError, ReceiverIsNilReason, Source}
import viper.gobra.theory.Addressability.{Exclusive, Shared}
import viper.gobra.translator.encodings.combinators.LeafTypeEncoding
import viper.gobra.translator.context.Context
import viper.gobra.translator.util.ViperWriter.CodeWriter
import viper.silver.verifier.ErrorReason
import viper.silver.{ast => vpr}

class PointerEncoding extends LeafTypeEncoding {

import viper.gobra.translator.util.ViperWriter.CodeLevel._
import viper.gobra.translator.util.TypePatterns._

/**
Expand Down Expand Up @@ -69,10 +72,16 @@ class PointerEncoding extends LeafTypeEncoding {
* To avoid conflicts with other encodings, an encoding for type T should be defined at shared operations on type T.
* Super implements shared variables with [[variable]].
*
* Ref[*e] -> [e]
* Ref[*e] -> assert [e != nil]; [e]
*/
override def reference(ctx: Context): in.Location ==> CodeWriter[vpr.Exp] = default(super.reference(ctx)){
case (loc: in.Deref) :: _ / Shared =>
ctx.expression(loc.exp)
val errorT = (x: Source.Verifier.Info, _: ErrorReason) =>
DerefError(x).dueTo(ReceiverIsNilReason(x))
for {
e <- ctx.expression(loc.exp)
cond <- ctx.expression(in.UneqCmp(loc.exp, in.NilLit(loc.exp.typ)(loc.info))(loc.info))
res <- assert(cond, e, errorT)(ctx)
} yield res
}
}
20 changes: 20 additions & 0 deletions src/test/resources/regressions/issues/000491.gobra
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/

package main

type A struct {
x int
}

func foo() {
var x *A
//:: ExpectedOutput(dereference_error:receiver_is_nil_error)
var y *A = &(*x)
}

func bar() {
var x *[3]int
//:: ExpectedOutput(dereference_error:receiver_is_nil_error)
var y []int = (*x)[:]
}

0 comments on commit 081cd2a

Please sign in to comment.