Repository navigation
Conversation
Use the same loop parser for invariants, modifies, and decreases. Consume a match-arm comma without retaining it in the generated statement, reject mixed terminators and non-loop inputs, and report malformed predicates without panic. Cover loop/while/for syntax, attribute ordering, a failing decreases check, and invalid-input diagnostics.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the loop-contract parsing and diagnostic changes.
No open issues 🎉
What changed in this PR
Fixes Kani’s loop contract macros when a loop appears directly in a match arm, without changing loop transformation or modifies-clause behavior.
Changes:
- Adds a shared parser for loop expressions with an optional semicolon or trailing comma, used by all three loop contract attributes.
- Replaces parsing panics with normal compile errors.
- Adds parser, match-arm, attribute-order, diagnostic, and negative termination-check regressions.
| File | Description |
|---|---|
tests/ui/loop-contract-attributes/main.rs |
Tests invalid attribute placement and malformed invariant input. |
tests/ui/loop-contract-attributes/expected |
Records expected compile diagnostics. |
tests/expected/loop-contract/match_decreases_stale.rs |
Tests rejection of a nondecreasing termination measure. |
tests/expected/loop-contract/match_decreases_stale.expected |
Requires the termination check to fail. |
tests/expected/loop-contract/loop_match_expression.rs |
Covers match-arm loop forms and different attribute orders. |
tests/expected/loop-contract/loop_match_expression.expected |
Requires all four harnesses to verify successfully. |
library/kani_macros/src/sysroot/loop_contracts/mod.rs |
Introduces the shared parser, integrates diagnostics, and adds parser unit tests. |
Review effort: Balanced
This branch has not been deployed
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Fix loop contract attribute parsing for loop expressions used directly as match arms.
Previously, the token stream passed to
#[kani::loop_invariant],#[kani::loop_modifies], or#[kani::loop_decreases]could include the match arm's trailing comma. Parsing it as asyn::Stmtfailed, and the macro could panic instead of expanding the loop or reporting a useful diagnostic.Changes
LoopInputparser forloop,while, andforexpressions, supporting an optional statement semicolon or match-arm trailing comma.Testing
loop-contractexpected tests pass, including positive and negative decreases checks.kani_macrosunit tests pass.Related work
This fixes match-arm attribute parsing, independently of the loop transformation and modifies-clause fixes in #4930, #4947, #5068, and #5071.
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 and MIT licenses.