generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 133
Add a #[derive(Invariant)] macro
#3250
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f7cb4a1
Add a `#[derive(Invariant)]` macro
adpaco-aws 1720300
Format fix
adpaco-aws 64d146d
Fix empty struct test
adpaco-aws 9301495
Merge branch 'main' into derive-invariant
adpaco-aws dbe03cd
Add test in which the invariant check fails
adpaco-aws 4795210
Newline
adpaco-aws 9a8dbb2
Add comment and link to issue
adpaco-aws File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
tests/expected/derive-invariant/empty_struct/empty_struct.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! Check that Kani can automatically derive `Invariant` for empty structs. | ||
|
|
||
| extern crate kani; | ||
| use kani::Invariant; | ||
|
|
||
| #[derive(kani::Arbitrary)] | ||
| #[derive(kani::Invariant)] | ||
| struct Void; | ||
|
|
||
| #[derive(kani::Arbitrary)] | ||
| #[derive(kani::Invariant)] | ||
| struct Void2(()); | ||
|
|
||
| #[derive(kani::Arbitrary)] | ||
| #[derive(kani::Invariant)] | ||
| struct VoidOfVoid(Void, Void2); | ||
|
|
||
| #[kani::proof] | ||
| fn check_empty_struct_invariant_1() { | ||
| let void1: Void = kani::any(); | ||
| assert!(void1.is_safe()); | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_empty_struct_invariant_2() { | ||
| let void2: Void2 = kani::any(); | ||
| assert!(void2.is_safe()); | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_empty_struct_invariant_3() { | ||
| let void3: VoidOfVoid = kani::any(); | ||
| assert!(void3.is_safe()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| - Status: SUCCESS\ | ||
| - Description: "assertion failed: void1.is_safe()" | ||
|
|
||
| - Status: SUCCESS\ | ||
| - Description: "assertion failed: void2.is_safe()" | ||
|
|
||
| - Status: SUCCESS\ | ||
| - Description: "assertion failed: void3.is_safe()" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - Status: SUCCESS\ | ||
| - Description: "assertion failed: point.is_safe()" |
20 changes: 20 additions & 0 deletions
20
tests/expected/derive-invariant/generic_struct/generic_struct.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! Check that Kani can automatically derive `Invariant` for structs with generics. | ||
|
|
||
| extern crate kani; | ||
| use kani::Invariant; | ||
|
|
||
| #[derive(kani::Arbitrary)] | ||
| #[derive(kani::Invariant)] | ||
| struct Point<X, Y> { | ||
| x: X, | ||
| y: Y, | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_generic_struct_invariant() { | ||
| let point: Point<i32, i8> = kani::any(); | ||
| assert!(point.is_safe()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| - Status: FAILURE\ | ||
| - Description: "assertion failed: wrapper.is_safe()" | ||
|
|
||
| Verification failed for - check_invariant_fail |
33 changes: 33 additions & 0 deletions
33
tests/expected/derive-invariant/invariant_fail/invariant_fail.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! Check that a verification failure is triggered when the derived `Invariant` | ||
| //! method is checked but not satisfied. | ||
|
|
||
| extern crate kani; | ||
| use kani::Invariant; | ||
| // Note: This represents an incorrect usage of `Arbitrary` and `Invariant`. | ||
| // | ||
| // The `Arbitrary` implementation should respect the type invariant, | ||
| // but Kani does not enforce this in any way at the moment. | ||
| // <https://github.com/model-checking/kani/issues/3265> | ||
| #[derive(kani::Arbitrary)] | ||
| struct NotNegative(i32); | ||
adpaco-aws marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| impl kani::Invariant for NotNegative { | ||
| fn is_safe(&self) -> bool { | ||
| self.0 >= 0 | ||
| } | ||
| } | ||
|
|
||
| #[derive(kani::Arbitrary)] | ||
| #[derive(kani::Invariant)] | ||
| struct NotNegativeWrapper { | ||
| x: NotNegative, | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_invariant_fail() { | ||
| let wrapper: NotNegativeWrapper = kani::any(); | ||
| assert!(wrapper.is_safe()); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - Status: SUCCESS\ | ||
| - Description: "assertion failed: point.is_safe()" |
20 changes: 20 additions & 0 deletions
20
tests/expected/derive-invariant/named_struct/named_struct.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! Check that Kani can automatically derive `Invariant` for structs with named fields. | ||
|
|
||
| extern crate kani; | ||
| use kani::Invariant; | ||
|
|
||
| #[derive(kani::Arbitrary)] | ||
| #[derive(kani::Invariant)] | ||
| struct Point { | ||
| x: i32, | ||
| y: i32, | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn check_generic_struct_invariant() { | ||
| let point: Point = kani::any(); | ||
| assert!(point.is_safe()); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| - Status: SUCCESS\ | ||
| - Description: "assertion failed: point.is_safe()" |
17 changes: 17 additions & 0 deletions
17
tests/expected/derive-invariant/unnamed_struct/unnamed_struct.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| //! Check that Kani can automatically derive `Invariant` for structs with unnamed fields. | ||
|
|
||
| extern crate kani; | ||
| use kani::Invariant; | ||
|
|
||
| #[derive(kani::Arbitrary)] | ||
| #[derive(kani::Invariant)] | ||
| struct Point(i32, i32); | ||
|
|
||
| #[kani::proof] | ||
| fn check_generic_struct_invariant() { | ||
| let point: Point = kani::any(); | ||
| assert!(point.is_safe()); | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.