-
Notifications
You must be signed in to change notification settings - Fork 176
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
Open
Tianshu-Huang
wants to merge
6
commits into
model-checking:main
Choose a base branch
from
Tianshu-Huang:autoharness-pattern-type
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1362,6 +1362,31 @@ fn call_kani_any_for_ty( | |
| } else { | ||
| ptr_lcl | ||
| } | ||
| } else if let TyKind::RigidTy(RigidTy::Pat(base_ty, _)) = ty.kind() { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is stale on the current toolchain. rustc_public converts PatternKind::NotNull since nightly-2026-08-21, and the tests go through this path without panicking. |
||
| // A pattern type (e.g. `pattern_type!(u8 is 1..=12)`) is layout-compatible with its base | ||
| // type. Generate an arbitrary value of the base type, constrain it to the pattern's | ||
| // validity range via `assume_scalar_niche`, then transmute it to the pattern type. The | ||
| // assumption must come first: with `-Z valid-value-checks` the transmute itself is | ||
| // checked, so the value has to be valid before the pattern-typed local ever exists. | ||
| let base_lcl = call_kani_any_for_ty( | ||
| tcx, | ||
| models, | ||
| body, | ||
| base_ty, | ||
| mutability, | ||
| source, | ||
| invariant_cache, | ||
| mined_cache, | ||
| ); | ||
| assume_scalar_niche(tcx, models.kani_assume, body, source, base_lcl, ty); | ||
| let pat_lcl = body.new_local(ty, source.span(body.blocks()), mutability); | ||
| body.assign_to( | ||
| Place::from(pat_lcl), | ||
| Rvalue::Cast(CastKind::Transmute, Operand::Move(Place::from(base_lcl)), ty), | ||
| source, | ||
| InsertPosition::Before, | ||
| ); | ||
| pat_lcl | ||
| } else { | ||
| // Prefer an unbounded nondeterministic value via (implemented or compiler-derived) | ||
| // Arbitrary; fall back to a smart-pointer model (`Box`/`Rc`/`Arc` of a derivable pointee) | ||
|
|
||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!