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 @@ -6695,10 +6696,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 TopLevelDeclWithMembers and not ValuetypeDecl)) {
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
32 changes: 32 additions & 0 deletions Test/git-issues/git-issue-4394.dfy
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// RUN: %baredafny verify %args "%s" > "%t"
// RUN: %diff "%s.expect" "%t"

datatype T = T(
f:map<nat,nat>
) {
const Keys := f.Keys // Used to be an error
}

codatatype codatatypeT = CT(
f:map<nat,nat>
) {
const Keys := f.Keys // Used to be an error
}

type opaqueT {
const f: map<nat, nat>
const Keys := f.Keys
}

newtype newT = x: int | true {
const f: map<nat, nat> := map[]
const Keys := f.Keys
}

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 1 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