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

Feat: Rust operators and immutable collections #5081

Merged
merged 103 commits into from
Apr 26, 2024
Merged

Conversation

MikaelMayer
Copy link
Member

@MikaelMayer MikaelMayer commented Feb 13, 2024

Description

This PR implements most immutable types in Dafny (Sequence, Set, Multiset, Map) and implements most if not all operators on them. It also sets an architecture that can now be relied on.
List of features in this PR:

  • More comprehensive Rust AST, precedence and associativity to render parentheses
  • The Rust compiler no longer crash when not in /runAllTests, but emits invalid code. It makes it possible to see which features are missing by searching for <i> in the generated code.
  • Made verification of GenExpr less brittle
  • Previous PR review comments
  • Ability to use shadowed identifiers to avoid numbers in names. Rust has the same rules as Dafny for shadowing, and I will continue to monitor cases where semantics might differ.

How has this been tested?

I added an integration test for compiling Dafny to Rust, and I added unit tests for the runtime in Rust
All tests for each compiler now have a .rs.check if they fail..

By submitting this pull request, I confirm that my contribution is made under the terms of the MIT license.

Dafny clone more generic
Test for native array pointers
Ensure formatting only consider the opened file
Added Rust Fun AST
# Conflicts:
#	Source/DafnyCore/AST/Grammar/IndentationFormatter.cs
#	Source/DafnyCore/AST/Grammar/TokenNewIndentCollector.cs
Pretty-print the ! (==) and all the other operators are now supported
Rust AST for many expressions
Detection of unimplemented features
Code review
Rewriting of a few Rust AST to make them more appealing
Support for operators, it compiles
Copy link
Member

@robin-aws robin-aws left a comment

Choose a reason for hiding this comment

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

Okay I'm ready to ship, with just two blocking comments:

  1. The remaining compileName += $"({why})"; without failing compilation (and any others I didn't catch), since that means emitting bad code without failing the pipeline by default.
  2. The point about using options.Get instead of adding another flag to the legacy options implementation (weaker but should be a quick fix).

I still don't feel like I've give the core code generation logic in Dafny enough of a review, but I think that can be backfilled before marking the backend stable.

Comment on lines 496 to 497
if (!isTpSupported(typeArg.Formal, out var why)) {
compileName += $"({why})";
Copy link
Member

Choose a reason for hiding this comment

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

Another case of emitting bad code without emitUncompilableCode.

I'd strongly suggest using compiler.AddUnsupported instead now that you have it, so it's harder to make this mistake.

cargo.toml
```

With `dafny translate`, only `program-rust/src/program.rs` is emitted.
Copy link
Member

Choose a reason for hiding this comment

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

Just FYI, note this is debatably not correct: #5203

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep indeed, will have to fix it separately.

docs/DafnyRef/integration- rust/IntegrationRust.md Outdated Show resolved Hide resolved
- Function parameters always take primitive types without borrowing them (`bool`, `u32`, etc.)
- Function parameters always borrows variables of other types (`DafnyString`, structs, etc.), because
borrowing is always cheaper than cloning for non-Copy types.
Open question: There are at least two more alternatives make
Copy link
Member

Choose a reason for hiding this comment

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

Fragment?

Source/TestDafny/MultiBackendTest.cs Show resolved Hide resolved
@@ -11,14 +11,17 @@ namespace Microsoft.Dafny.Compilers;

public abstract class DafnyExecutableBackend : ExecutableBackend {

protected virtual bool PreventShadowing => true;
protected virtual bool SupportsUncompilableCode => true;
Copy link
Member

Choose a reason for hiding this comment

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

nit: reads like "can compile uncompilable code" but I think we want a shorter version of "supports outputting partial code for programs that have uncompilable code"

Copy link
Member Author

Choose a reason for hiding this comment

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

Renamed to CanEmitUncompilableCode

@@ -360,6 +360,7 @@ public enum ContractTestingMode {
public int DeprecationNoise = 1;
public bool VerifyAllModules = false;
public bool SeparateModuleOutput = false;
public bool EmitUncompilableCode { get; set; }
Copy link
Member

Choose a reason for hiding this comment

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

options.Get(CommonOptionBag.EmitUncompilableCode) is the incantation you're looking for. :)

robin-aws
robin-aws previously approved these changes Apr 17, 2024
Copy link
Member

@robin-aws robin-aws left a comment

Choose a reason for hiding this comment

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

🚀 🦀

Rocket craaaaaaaaab, burning out his fumes out here alone...

@MikaelMayer MikaelMayer enabled auto-merge (squash) April 19, 2024 15:19
@MikaelMayer MikaelMayer merged commit 3e38580 into master Apr 26, 2024
20 checks passed
@MikaelMayer MikaelMayer deleted the feat-rust-operators branch April 26, 2024 17:09
keyboardDrummer added a commit to keyboardDrummer/dafny that referenced this pull request Apr 29, 2024
fabiomadge added a commit that referenced this pull request Apr 29, 2024
This reverts commit 3e38580 to fix
failing nightly:
https://github.com/dafny-lang/dafny/actions/runs/8868313153

---------

Co-authored-by: Fabio Madge <fmadge@amazon.com>
MikaelMayer added a commit that referenced this pull request Apr 29, 2024
### Description
This PR implements most immutable types in Dafny (Sequence, Set,
Multiset, Map) and implements most if not all operators on them. It also
sets an architecture that can now be relied on.
List of features in this PR:
- More comprehensive Rust AST, precedence and associativity to render
parentheses
- The Rust compiler no longer crash when not in /runAllTests, but emits
invalid code. It makes it possible to see which features are missing by
searching for `<i>` in the generated code.
- Made verification of GenExpr less brittle
- Previous PR review comments
- Ability to use shadowed identifiers to avoid numbers in names. Rust
has the same rules as Dafny for shadowing, and I will continue to
monitor cases where semantics might differ.

### How has this been tested?
I added an integration test for compiling Dafny to Rust, and I added
unit tests for the runtime in Rust
*All tests for each compiler now have a `.rs.check` if they fail.*.

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

---------

Co-authored-by: Robin Salkeld <salkeldr@amazon.com>
@MikaelMayer MikaelMayer mentioned this pull request Apr 29, 2024
MikaelMayer added a commit that referenced this pull request Apr 29, 2024
This PR is a replay of #5081
after fixing the issue with the paths too long for Windows, so it should
no longer break the CI.

### Description
This PR implements most immutable types in Dafny (Sequence, Set,
Multiset, Map) and implements most if not all operators on them. It also
sets an architecture that can now be relied on.
List of features in this PR:
- More comprehensive Rust AST, precedence and associativity to render
parentheses
- The Rust compiler no longer crash when not in /runAllTests, but emits
invalid code. It makes it possible to see which features are missing by
searching for `<i>` in the generated code.
- Made verification of GenExpr less brittle
- Previous PR review comments
- Ability to use shadowed identifiers to avoid numbers in names. Rust
has the same rules as Dafny for shadowing, and I will continue to
monitor cases where semantics might differ.

### How has this been tested?
I added an integration test for compiling Dafny to Rust, and I added
unit tests for the runtime in Rust
*All tests for each compiler now have a `.rs.check` if they fail.*.

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

---------

Co-authored-by: Robin Salkeld <salkeldr@amazon.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants