-
Notifications
You must be signed in to change notification settings - Fork 181
Autoharness: support pattern types (RigidTy::Pat) #4780
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 6 commits into
model-checking:main
from
Tianshu-Huang:autoharness-pattern-type
Sep 24, 2026
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
db8ac88
Autoharness: support pattern types (RigidTy::Pat)
Tianshu-Huang 60a17cc
fix rustfmt formatting
Tianshu-Huang e54cefe
Fix scalar_niche for pointer pattern types; add assert for NonNull
Tianshu-Huang b0770eb
Merge branch 'main' into autoharness-pattern-type
feliperodri 52a97d8
Reject raw-pointer pattern bases; assume niche before transmute; test…
Tianshu-Huang 4b78457
Merge branch 'main' into autoharness-pattern-type
Tianshu-Huang 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
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 |
|---|---|---|
| @@ -1,5 +1,9 @@ | ||
| | niche_probe | days_left_in_year | Missing Arbitrary implementation for argument(s) s: Schedule | | ||
| | niche_probe | signed_niche | Missing Arbitrary implementation for argument(s) p: PosI8 | | ||
| | niche_probe | duration_nanos | #[kani::proof] | Success | | ||
| | niche_probe | nonzero | #[kani::proof] | Success | | ||
| Complete - 2 successfully verified functions, 0 failures, 2 total. | ||
| Status: SATISFIED | ||
| Status: SATISFIED | ||
| | niche_probe | check_monthly::<Month> | #[kani::proof] | Success | | ||
| | niche_probe | cover_extremes | #[kani::proof] | Success | | ||
| | niche_probe | days_left_in_year | #[kani::proof] | Success | | ||
| | niche_probe | duration_nanos | #[kani::proof] | Success | | ||
| | niche_probe | nonzero | #[kani::proof] | Success | | ||
| | niche_probe | signed_niche | #[kani::proof] | Success | | ||
| Complete - 10 successfully verified functions, 0 failures, 10 total. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| # Copyright Kani Contributors | ||
| # SPDX-License-Identifier: Apache-2.0 OR MIT | ||
| script: run.sh | ||
| expected: 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,4 @@ | ||
| Status: SATISFIED | ||
| | pattern_type_probe | nonzero_arg | #[kani::proof] | Success | | ||
| | pattern_type_probe | percent_in_range | #[kani::proof] | Success | | ||
| Complete - 2 successfully verified functions, 0 failures, 2 total. |
34 changes: 34 additions & 0 deletions
34
tests/script-based-pre/autoharness_pattern_type/pattern_type_probe.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,34 @@ | ||
| // Copyright Kani Contributors | ||
| // SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| // Pattern types (`RigidTy::Pat`) wrap a base type with a validity constraint, e.g. | ||
| // `pattern_type!(u8 is 0..=100)`. std builds its niche types on them (`NonZero`'s inner | ||
| // type, `Duration`'s nanoseconds, wtf8 code points, ...). Autoharness must derive | ||
| // Arbitrary for integer-based pattern types -- both as struct fields and as top-level | ||
| // arguments -- and constrain the generated values to the pattern's range. | ||
| #![feature(pattern_types, pattern_type_macro)] | ||
| #![allow(internal_features)] | ||
| use std::pat::pattern_type; | ||
|
|
||
| pub struct Percent { | ||
| value: pattern_type!(u8 is 0..=100), | ||
| tag: u8, | ||
| } | ||
|
|
||
| // Field position: `Percent` is derived through a synthesized `any()`. The generated | ||
| // value must respect the range (assert) and both bounds must be reachable (cover). | ||
| pub fn percent_in_range(p: Percent) { | ||
| // SAFETY: a pattern type is layout-compatible with its base type. | ||
| let v: u8 = unsafe { std::mem::transmute(p.value) }; | ||
| kani::assert(v <= 100, "generated value must be within the pattern's range"); | ||
| kani::cover!(v == 0 && p.tag == 0, "lower bound reachable"); | ||
| kani::cover!(v == 100, "upper bound reachable"); | ||
| } | ||
|
|
||
| // Top-level argument position. | ||
| pub fn nonzero_arg(x: pattern_type!(u8 is 1..)) { | ||
| // SAFETY: as above. | ||
| let v: u8 = unsafe { std::mem::transmute(x) }; | ||
| kani::assert(v != 0, "generated value must be within the pattern's range"); | ||
| kani::cover!(v == 255, "upper bound reachable"); | ||
| } |
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,10 @@ | ||
| #!/usr/bin/env bash | ||
| # Copyright Kani Contributors | ||
| # SPDX-License-Identifier: Apache-2.0 OR MIT | ||
|
|
||
| # Pattern types (`RigidTy::Pat`) wrap a base scalar type with a validity constraint | ||
| # (e.g. `pattern_type!(u8 is 0..=100)`; std's niche types are built on them). Autoharness | ||
| # must recognize them as derivable and constrain generated values to the pattern's range. | ||
| # `-Z valid-value-checks` verifies the range is assumed *before* the value is transmuted to | ||
| # the pattern type (the transmute itself is validity-checked under that flag). | ||
| kani autoharness -Z autoharness -Z valid-value-checks --output-format=regular pattern_type_probe.rs |
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.