Skip to content

Commit

Permalink
Fix: Ability to cast a datatype to its trait when overriding functions (
Browse files Browse the repository at this point in the history
#4824)

This PR fixes #4823
There were two issues there:
- The function override check was casting to ClassLikeDecl, when it
should now be TopLevelDeclWithMembers as IndDatatypeDecl can also
override trait declarations.
- The generated axiom used to rely on types being unboxed, but since now
we might compare a trait with a datatype value, we need to ensure
correct boxing for Boogie to type-check.
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 authored Apr 1, 2024
1 parent 5babf7c commit 10b59f5
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 4 deletions.
8 changes: 5 additions & 3 deletions Source/DafnyCore/DooFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public static ManifestData Read(TextReader reader) {
}

public void Write(TextWriter writer) {
writer.Write(Toml.FromModel(this, new TomlModelOptions()));
writer.Write(Toml.FromModel(this, new TomlModelOptions()).Replace("\r\n", "\n"));
}
}

Expand Down Expand Up @@ -103,7 +103,9 @@ private static DooFile Read(ZipArchive archive) {
}

public DooFile(Program dafnyProgram) {
var tw = new StringWriter();
var tw = new StringWriter {
NewLine = "\n"
};
var pr = new Printer(tw, ProgramSerializationOptions, PrintModes.Serialization);
// afterResolver is false because we don't yet have a way to safely skip resolution
// when reading the program back into memory.
Expand Down Expand Up @@ -191,7 +193,7 @@ public void Write(ConcreteSyntaxTree wr) {
var manifestWr = wr.NewFile(ManifestFileEntry);
using var manifestWriter = new StringWriter();
Manifest.Write(manifestWriter);
manifestWr.Write(manifestWriter.ToString());
manifestWr.Write(manifestWriter.ToString().Replace("\r\n", "\n"));

var programTextWr = wr.NewFile(ProgramFileEntry);
programTextWr.Write(ProgramText);
Expand Down
Binary file modified Source/DafnyStandardLibraries/binaries/DafnyStandardLibraries.doo
Binary file not shown.
2 changes: 1 addition & 1 deletion Source/DafnyStandardLibraries/src/Std/Collections/Seq.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ module Std.Collections.Seq {
Some((s[..i], s[(i + 1)..]))
}

lemma WillSplitOnDelim<T>(s: seq<T>, delim: T, prefix: seq<T>)
lemma {:rlimit 1000} {:vcs_split_on_every_assert} WillSplitOnDelim<T>(s: seq<T>, delim: T, prefix: seq<T>)
requires |prefix| < |s|
requires forall i :: 0 <= i < |prefix| ==> prefix[i] == s[i]
requires delim !in prefix && s[|prefix|] == delim
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// RUN: %baredafny verify %args --type-system-refresh --general-traits=datatype "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

trait Test<T> {
function Cast(t: T): Test<T>
}

datatype Impl extends Test<Impl> = ImplConstructor()
{
function Cast(t: Impl): Test<Impl> { t }
}
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/4823.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ability to cast a datatype to its trait when overriding functions

0 comments on commit 10b59f5

Please sign in to comment.