Repository navigation
Keep loop attributes and labels when rewriting while let and for loops with a loop invariant
#4930
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
feliperodri
merged 4 commits into
model-checking:main
from
Talha-Dmr:fix-while-let-loop-modifies
Oct 2, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
b6d9974
Keep loop attributes when rewriting `while let` and `for` loops with …
Talha-Dmr eb4ac5e
Keep the label when rewriting `while let` and `for` loops with a loop…
Talha-Dmr 36ea4cf
Keep only the label when rewriting for loops with a loop invariant
Talha-Dmr d2fadb8
Explain why the for loop rewrite does not carry over attributes yet
Talha-Dmr 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
1 change: 1 addition & 0 deletions
1
tests/expected/loop-contract/loop_contract_labeled_loops.expected
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 @@ | ||
| Complete - 2 successfully verified harnesses, 0 failures, 2 total. |
38 changes: 38 additions & 0 deletions
38
tests/expected/loop-contract/loop_contract_labeled_loops.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,38 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // kani-flags: -Z loop-contracts | ||
|
|
||
| //! Check that a loop invariant can be attached to labeled `while let` and `for` loops. | ||
| //! The rewrites of these loops used to drop the label, so a `break` or `continue` that names | ||
| //! the label failed to compile. | ||
|
|
||
| #![feature(proc_macro_hygiene)] | ||
| #![feature(stmt_expr_attributes)] | ||
|
|
||
| #[kani::proof] | ||
| fn labeled_while_let() { | ||
| let mut k: u8 = kani::any(); | ||
| #[kani::loop_invariant(true)] | ||
| 'outer: while let Some(n) = k.checked_sub(1) { | ||
| k = n; | ||
| if k == 3 { | ||
| break 'outer; | ||
| } | ||
| } | ||
| assert!(k == 0 || k == 3); | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn labeled_for() { | ||
| let a: [u8; 5] = kani::any(); | ||
| let mut sum: u32 = 0; | ||
| #[kani::loop_invariant(sum <= (kani::index as u32 * u8::MAX as u32))] | ||
| 'outer: for x in a { | ||
| if x == 0 { | ||
| continue 'outer; | ||
| } | ||
| sum = sum + (x as u32); | ||
| } | ||
| assert!(sum <= 5 * 255); | ||
| } |
9 changes: 9 additions & 0 deletions
9
tests/expected/loop-contract/while_let_loop_modifies_fail.expected
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,9 @@ | ||
| - Status: FAILURE\ | ||
| - Description: "Check that j is assignable"\ | ||
| in function invariant_then_modifies | ||
|
|
||
| - Status: FAILURE\ | ||
| - Description: "Check that j is assignable"\ | ||
| in function modifies_then_invariant | ||
|
|
||
| VERIFICATION:- FAILED |
42 changes: 42 additions & 0 deletions
42
tests/expected/loop-contract/while_let_loop_modifies_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,42 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // kani-flags: -Z loop-contracts | ||
|
|
||
| //! Check that `#[kani::loop_modifies]` is applied to a `while let` loop whether it is written | ||
| //! before or after `#[kani::loop_invariant]`. | ||
| //! Both loops write `j`, which is not in the modifies clause, so the assigns check for `j` must | ||
| //! fail in both harnesses. The `while let` rewrite used to drop the attributes that follow | ||
| //! `#[kani::loop_invariant]`, so `invariant_then_modifies` verified successfully with an | ||
| //! inferred assigns clause. | ||
|
|
||
| #![feature(proc_macro_hygiene)] | ||
| #![feature(stmt_expr_attributes)] | ||
|
|
||
| #[kani::proof] | ||
| fn invariant_then_modifies() { | ||
| let mut k: u8 = kani::any(); | ||
| let mut i: u8 = 0; | ||
| let mut j: u8 = 0; | ||
| #[kani::loop_invariant(i == j)] | ||
| #[kani::loop_modifies(&k, &i)] | ||
| while let Some(n) = k.checked_sub(1) { | ||
| k = n; | ||
| i = i.wrapping_add(1); | ||
| j = j.wrapping_add(1); | ||
| } | ||
| } | ||
|
|
||
| #[kani::proof] | ||
| fn modifies_then_invariant() { | ||
| let mut k: u8 = kani::any(); | ||
| let mut i: u8 = 0; | ||
| let mut j: u8 = 0; | ||
| #[kani::loop_modifies(&k, &i)] | ||
| #[kani::loop_invariant(i == j)] | ||
| while let Some(n) = k.checked_sub(1) { | ||
| k = n; | ||
| i = i.wrapping_add(1); | ||
| j = j.wrapping_add(1); | ||
| } | ||
| } |
Oops, something went wrong.
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.