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

Chore: Dafny to Rust refactorings #5513

Merged
merged 29 commits into from
Jun 22, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
0f9f1a1
chore-rust-refactorings
MikaelMayer May 2, 2024
45fd9ee
Removed enforce determinism extra option. More error checks
MikaelMayer May 31, 2024
136a72b
Ensure right handling of tuples and printing, and no extra rs.check file
MikaelMayer Jun 1, 2024
2da9647
Merge branch 'master' into chore-rust-refactorings
MikaelMayer Jun 4, 2024
6a5dcdb
Merge branch 'master' into chore-rust-refactorings
MikaelMayer Jun 4, 2024
77df75f
Fixed formatting
MikaelMayer Jun 4, 2024
a9084e5
Updated all .rs.check files
MikaelMayer Jun 4, 2024
2afbd5b
Merge branch 'chore-rust-refactorings' of https://github.com/dafny-la…
MikaelMayer Jun 4, 2024
530dfd5
Merge branch 'refs/heads/master' into chore-rust-refactorings
MikaelMayer Jun 5, 2024
f991053
Merge branch 'refs/heads/master' into chore-rust-refactorings
MikaelMayer Jun 5, 2024
1facdae
WIP need to remove these check files
MikaelMayer Jun 5, 2024
1692ddf
Updated test and check files
MikaelMayer Jun 6, 2024
1b29dda
Merge branch 'refs/heads/master' into chore-rust-refactorings
MikaelMayer Jun 6, 2024
6b65030
Merge branch 'master' into chore-rust-refactorings
MikaelMayer Jun 6, 2024
ad72a8e
Merge branch 'refs/heads/master' into chore-rust-refactorings
MikaelMayer Jun 10, 2024
6c6a423
Merge branch 'master' into chore-rust-refactorings
MikaelMayer Jun 11, 2024
6c6e37e
Fixed the core files for the rust tests
MikaelMayer Jun 12, 2024
4853061
Fixed typôs
MikaelMayer Jun 13, 2024
ccbe323
Merge branch 'refs/heads/master' into chore-rust-refactorings
MikaelMayer Jun 13, 2024
b29ffd0
Updated check files again
MikaelMayer Jun 13, 2024
0a13114
More fixups
MikaelMayer Jun 13, 2024
40721bb
Reverted ownership and included a change in the message
MikaelMayer Jun 13, 2024
1f9824e
One last test fixed
MikaelMayer Jun 13, 2024
af055f4
New attempt at fixing CI
MikaelMayer Jun 14, 2024
4fda132
Merge branch 'master' into chore-rust-refactorings
MikaelMayer Jun 19, 2024
6e11aca
Merge branch 'master' into chore-rust-refactorings
MikaelMayer Jun 21, 2024
c5f22f1
latest improvements
MikaelMayer Jun 21, 2024
1832a90
Fixed one last test
MikaelMayer Jun 21, 2024
123ef92
Changed the printing so that it's deterministic
MikaelMayer Jun 21, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
chore-rust-refactorings
Chore: Dafny to Rust refactorings
- extern module as attribute
- Type arguments with type bounds (can be tested)
- Attributes handling
- Type on Select field and TupleSelect
- Name refactoring
- every In
- NoStatementBuffer
- GenFormals
- EmitNameAndActualTypeArgs (for classes)
- ParseAttributes
- GetExtractOverrideName (to be able to model Rust options)
Dafny
- StructBuild first argument to be an expression instead of a string
- Apply and ApplyType
- Type to TypeParamDecl
- Formals to Fields, NamelessFormals to NamelessFields
- Visibility to string
- Self to SelfBorrowed
- SeltMut to SelfBorrowedMut
- Box/BoxNew (for attribute annotations)
- bool
- ToOwned (used in tail recursion)
- IsImmutableConversion
- Formal.self becomes Formal.selfBorrowed
- Formal.selfMut becomes Formal.selfBorrowedMut
- AssignMember and all new variants ExtractTuple and Index
- ExprFromType
- Lambda (makes code prettier)
- Error handling
- Debug for datatypes
- No more system object, just any
- New char encoding
- int!
- Refactoring of DafatypeValue's first argument
- constant
- GenIdent refactoring
  • Loading branch information
MikaelMayer committed May 31, 2024
commit 0f9f1a1f7dbef18144513dfc67c901241fd22353

Large diffs are not rendered by default.

82 changes: 59 additions & 23 deletions Source/DafnyCore/Backends/Dafny/AST.dfy
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,27 @@ module {:extern "DAST.Format"} DAST.Format
| ImpliesFormat
| EquivalenceFormat
| ReverseFormat

function SeqToHeight<T>(s: seq<T>, f: T --> nat): (r: nat)
requires forall t <- s :: f.requires(t)
ensures forall t <- s :: f(t) <= r
{
if |s| == 0 then 0 else
var i := f(s[0]);
var j := SeqToHeight(s[1..], f);
if i < j then j else i
}
}

module {:extern "DAST"} DAST {
import opened Std.Wrappers

datatype Module = Module(name: string, isExtern: bool, body: seq<ModuleItem>)
// Prevents Dafny names to being direclty integrated into Rust without explicit conversion
// Make it a newtype once newtypes are compatible with standard library
// See issue https://github.com/dafny-lang/dafny/issues/5345
datatype Name = Name(dafny_name: string)

datatype Module = Module(name: Name, attributes: seq<Attribute>, body: Option<seq<ModuleItem>>)

datatype ModuleItem =
| Module(Module)
Expand All @@ -44,48 +59,69 @@ module {:extern "DAST"} DAST {
Primitive(Primitive) | Passthrough(string) |
TypeArg(Ident)

datatype TypeArgDecl = TypeArgDecl(name: Ident, bounds: seq<TypeArgBound>)

datatype TypeArgBound =
| SupportsEquality
| SupportsDefault

datatype Primitive = Int | Real | String | Bool | Char

datatype NewtypeRange =
| U8 | I8 | U16 | I16 | U32 | I32 | U64 | I64 | U128 | I128 | BigInt
| NoRange

datatype Attribute = Attribute(name: string, args: seq<string>)

datatype DatatypeType = DatatypeType(path: seq<Ident>, attributes: seq<Attribute>)
Copy link
Contributor

Choose a reason for hiding this comment

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

Side note: we should eventually give this a better name

Copy link
Member Author

Choose a reason for hiding this comment

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

Any suggestion? DataType? ResolvedDatatype ?


datatype ResolvedType =
| Datatype(path: seq<Ident>)
| Trait(path: seq<Ident>)
| Newtype(baseType: Type, range: NewtypeRange, erase: bool)
| Datatype(datatypeType: DatatypeType)
| Trait(path: seq<Ident>, attributes: seq<Attribute>)
| Newtype(baseType: Type, range: NewtypeRange, erase: bool, attributes: seq<Attribute>)

datatype Ident = Ident(id: string)
datatype Ident = Ident(id: Name)

datatype Class = Class(name: string, enclosingModule: Ident, typeParams: seq<Type>, superClasses: seq<Type>, fields: seq<Field>, body: seq<ClassItem>)
datatype Class = Class(name: Name, enclosingModule: Ident, typeParams: seq<TypeArgDecl>, superClasses: seq<Type>, fields: seq<Field>, body: seq<ClassItem>, attributes: seq<Attribute>)

datatype Trait = Trait(name: string, typeParams: seq<Type>, body: seq<ClassItem>)
datatype Trait = Trait(name: Name, typeParams: seq<TypeArgDecl>, body: seq<ClassItem>, attributes: seq<Attribute>)

datatype Datatype = Datatype(name: string, enclosingModule: Ident, typeParams: seq<Type>, ctors: seq<DatatypeCtor>, body: seq<ClassItem>, isCo: bool)
datatype Datatype = Datatype(name: Name, enclosingModule: Ident, typeParams: seq<TypeArgDecl>, ctors: seq<DatatypeCtor>, body: seq<ClassItem>, isCo: bool, attributes: seq<Attribute>)

datatype DatatypeCtor = DatatypeCtor(name: string, args: seq<Formal>, hasAnyArgs: bool /* includes ghost */)
datatype DatatypeCtor = DatatypeCtor(name: Name, args: seq<Formal>, hasAnyArgs: bool /* includes ghost */)

datatype Newtype = Newtype(name: string, typeParams: seq<Type>, base: Type, range: NewtypeRange, witnessStmts: seq<Statement>, witnessExpr: Option<Expression>)
datatype Newtype = Newtype(name: Name, typeParams: seq<TypeArgDecl>, base: Type, range: NewtypeRange, witnessStmts: seq<Statement>, witnessExpr: Option<Expression>, attributes: seq<Attribute>)

datatype ClassItem = Method(Method)

datatype Field = Field(formal: Formal, defaultValue: Option<Expression>)

datatype Formal = Formal(name: string, typ: Type)
datatype Formal = Formal(name: Name, typ: Type, attributes: seq<Attribute>)

datatype Method = Method(
isStatic: bool,
hasBody: bool,
overridingPath: Option<seq<Ident>>,
name: Name,
typeParams: seq<TypeArgDecl>,
params: seq<Formal>,
body: seq<Statement>,
outTypes: seq<Type>,
outVars: Option<seq<Ident>>)

datatype Method = Method(isStatic: bool, hasBody: bool, overridingPath: Option<seq<Ident>>, name: string, typeParams: seq<Type>, params: seq<Formal>, body: seq<Statement>, outTypes: seq<Type>, outVars: Option<seq<Ident>>)
datatype CallSignature = CallSignature(parameters: seq<Formal>)

datatype CallName =
Name(name: string) |
CallName(name: Name, onType: Option<Type>, signature: CallSignature) |
MapBuilderAdd | MapBuilderBuild | SetBuilderAdd | SetBuilderBuild

datatype Statement =
DeclareVar(name: string, typ: Type, maybeValue: Option<Expression>) |
DeclareVar(name: Name, typ: Type, maybeValue: Option<Expression>) |
Assign(lhs: AssignLhs, value: Expression) |
If(cond: Expression, thn: seq<Statement>, els: seq<Statement>) |
Labeled(lbl: string, body: seq<Statement>) |
While(cond: Expression, body: seq<Statement>) |
Foreach(boundName: string, boundType: Type, over: Expression, body: seq<Statement>) |
Foreach(boundName: Name, boundType: Type, over: Expression, body: seq<Statement>) |
Call(on: Expression, callName: CallName, typeArgs: seq<Type>, args: seq<Expression>, outs: Option<seq<Ident>>) |
Return(expr: Expression) |
EarlyReturn() |
Expand All @@ -96,8 +132,8 @@ module {:extern "DAST"} DAST {
Print(Expression)

datatype AssignLhs =
Ident(Ident) |
Select(expr: Expression, field: string) |
Ident(ident: Ident) |
Select(expr: Expression, field: Name) |
Index(expr: Expression, indices: seq<Expression>)

datatype CollKind = Seq | Array | Map
Expand All @@ -124,12 +160,12 @@ module {:extern "DAST"} DAST {

datatype Expression =
Literal(Literal) |
Ident(string) |
Ident(Name) |
Companion(seq<Ident>) |
Tuple(seq<Expression>) |
New(path: seq<Ident>, typeArgs: seq<Type>, args: seq<Expression>) |
NewArray(dims: seq<Expression>, typ: Type) |
DatatypeValue(path: seq<Ident>, typeArgs: seq<Type>, variant: string, isCo: bool, contents: seq<(string, Expression)>) |
DatatypeValue(datatypeType: DatatypeType, typeArgs: seq<Type>, variant: Name, isCo: bool, contents: seq<(string, Expression)>) |
Convert(value: Expression, from: Type, typ: Type) |
SeqConstruct(length: Expression, elem: Expression) |
SeqValue(elements: seq<Expression>, typ: Type) |
Expand All @@ -148,17 +184,17 @@ module {:extern "DAST"} DAST {
ArrayLen(expr: Expression, dim: nat) |
MapKeys(expr: Expression) |
MapValues(expr: Expression) |
Select(expr: Expression, field: string, isConstant: bool, onDatatype: bool) |
SelectFn(expr: Expression, field: string, onDatatype: bool, isStatic: bool, arity: nat) |
Select(expr: Expression, field: Name, isConstant: bool, onDatatype: bool, fieldType: Type) |
SelectFn(expr: Expression, field: Name, onDatatype: bool, isStatic: bool, arity: nat) |
Index(expr: Expression, collKind: CollKind, indices: seq<Expression>) |
IndexRange(expr: Expression, isArray: bool, low: Option<Expression>, high: Option<Expression>) |
TupleSelect(expr: Expression, index: nat) |
TupleSelect(expr: Expression, index: nat, fieldType: Type) |
Call(on: Expression, callName: CallName, typeArgs: seq<Type>, args: seq<Expression>) |
Lambda(params: seq<Formal>, retType: Type, body: seq<Statement>) |
BetaRedex(values: seq<(Formal, Expression)>, retType: Type, expr: Expression) |
IIFE(name: Ident, typ: Type, value: Expression, iifeBody: Expression) |
Apply(expr: Expression, args: seq<Expression>) |
TypeTest(on: Expression, dType: seq<Ident>, variant: string) |
TypeTest(on: Expression, dType: seq<Ident>, variant: Name) |
InitializationValue(typ: Type) |
BoolBoundedPool() |
SetBoundedPool(of: Expression) |
Expand Down
Loading
Loading