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

Fix: Ability to use .Key as a constant name in datatypes and classes #4423

Merged
merged 10 commits into from
Aug 29, 2023
8 changes: 5 additions & 3 deletions Source/DafnyCore/Verifier/Translator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
using static Microsoft.Dafny.Util;
using Core;
using Microsoft.BaseTypes;
using Microsoft.Dafny.Compilers;
using Microsoft.Dafny.Triggers;
using Action = System.Action;
using PODesc = Microsoft.Dafny.ProofObligationDescription;
Expand Down Expand Up @@ -6686,10 +6687,11 @@ Bpl.Function GetReadonlyField(Field f) {
return predef.ArrayLength;
} else if (f.EnclosingClass is ValuetypeDecl { Name: "real" } && f.Name == "Floor") {
return predef.RealFloor;
} else if (f is SpecialField && !(f is DatatypeDestructor)) {
} else if (f is SpecialField && !(f is DatatypeDestructor || f.EnclosingClass is IndDatatypeDecl or ClassLikeDecl)) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're checking IndDatatypeDecl instead of DatatypeDecl. The latter also includes codatatype declarations. Did you intend to make this distinction? I don't quite understand the test, but perhaps you even want TopLevelDeclWithMembers instead of IndDatatypeDecl or ClassLikeDecl, which would then also include abstract types and newtypes. I suggest adding test cases for these, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting remark. Testing it with co-datatype did not yield any error, but opaque types and newtypes did yield errors.
However, map itself being a ValueTypeDecl, which is a TopLevelDeclWithMembers, I had to make an exclusion check, but it ended up working.

if (f.Name is "Keys" or "Values" or "Items") {
Contract.Assert(f.Type is SetType);
var setType = (SetType)f.Type;
var fType = f.Type.NormalizeExpand();
Contract.Assert(fType is SetType);
var setType = (SetType)fType;
return f.Name switch {
"Keys" => setType.Finite ? predef.MapDomain : predef.IMapDomain,
"Values" => setType.Finite ? predef.MapValues : predef.IMapValues,
Expand Down
16 changes: 16 additions & 0 deletions Test/git-issues/git-issue-4394.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %baredafny verify %args "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

datatype T = T(
f:map<nat,nat>
) {
const Keys := f.Keys // no problem if another name is used for the constant
}

class C {
const Keys: set<nat>

constructor(f:map<nat,nat>) {
this.Keys := f.Keys;
}
}
2 changes: 2 additions & 0 deletions Test/git-issues/git-issue-4394.dfy.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

Dafny program verifier finished with 0 verified, 0 errors
1 change: 1 addition & 0 deletions docs/dev/news/4394.fix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Ability to use .Key as a constant name in datatypes and classes
Loading