Skip to content

Commit

Permalink
Fix: Handling of opaque methods in traits overriding check (#4207)
Browse files Browse the repository at this point in the history
This PR fixes #4205
I added the corresponding test.

<small>By submitting this pull request, I confirm that my contribution
is made under the terms of the [MIT
license](https://github.com/dafny-lang/dafny/blob/master/LICENSE.txt).</small>
  • Loading branch information
MikaelMayer committed Jul 12, 2023
1 parent d4203a4 commit 4df64dd
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 0 deletions.
17 changes: 17 additions & 0 deletions Source/DafnyCore/Verifier/Translator.ClassMembers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1058,6 +1058,7 @@ public partial class Translator {

var moreArgsCF = new List<Boogie.Expr>();
Expr layer = null;
Expr reveal = null;

// Add the fuel argument
if (f.IsFuelAware()) {
Expand All @@ -1072,6 +1073,18 @@ public partial class Translator {
layer = new Boogie.IdentifierExpr(f.tok, "$LZ", predef.LayerType); // $LZ
}

if (f.IsOpaque) {
Contract.Assert(overridingFunction.IsOpaque);
var revealVar = new Boogie.BoundVariable(f.tok, new Boogie.TypedIdent(f.tok, "reveal", Boogie.Type.Bool));
forallFormals.Add(revealVar);
reveal = new Boogie.IdentifierExpr(f.tok, revealVar);
argsJF.Add(reveal);
} else if (overridingFunction.IsOpaque) {
// We can't use a bound variable $fuel, because then one of the triggers won't be mentioning this $fuel.
// Instead, we do the next best thing: use the literal false.
reveal = new Boogie.LiteralExpr(f.tok, false);
}

// Add heap arguments
if (f is TwoStateFunction) {
Contract.Assert(bvPrevHeap != null);
Expand Down Expand Up @@ -1127,6 +1140,10 @@ public partial class Translator {
argsCF.Add(layer);
}

if (reveal != null) {
argsCF.Add(reveal);
}

argsCF = Concat(argsCF, moreArgsCF);

// ante := useViaCanCall || (useViaContext && this != null && $Is(this, C))
Expand Down
4 changes: 4 additions & 0 deletions Source/DafnyCore/Verifier/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3580,6 +3580,10 @@ public enum StmtType { NONE, ASSERT, ASSUME };
argsC.Add(etran.layerInterCluster.GetFunctionFuel(f));
}

if (f.IsOpaque) {
argsC.Add(GetRevealConstant(f));
}

// add heap arguments
if (f is TwoStateFunction) {
argsC.Add(etran.Old.HeapExpr);
Expand Down
12 changes: 12 additions & 0 deletions Test/git-issues/git-issue-4205.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// RUN: %baredafny verify %args "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

trait T {
opaque function bar(): int
}

class F extends T {
opaque function bar(): int {
1
}
}
2 changes: 2 additions & 0 deletions Test/git-issues/git-issue-4205.dfy.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Dafny program verifier finished with 1 verified, 0 errors
13 changes: 13 additions & 0 deletions Test/git-issues/git-issue-4205a.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// RUN: %baredafny verify %args "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

trait T {
opaque function bar(): (r: int)
}

class F extends T {
constructor() {}
opaque function bar(): (r: int) {
1
}
}
2 changes: 2 additions & 0 deletions Test/git-issues/git-issue-4205a.dfy.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Dafny program verifier finished with 1 verified, 0 errors
1 change: 1 addition & 0 deletions docs/dev/news/4205.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Traits with opaque functions can now be extended without errors

0 comments on commit 4df64dd

Please sign in to comment.