-
Notifications
You must be signed in to change notification settings - Fork 125
Provide Specs for the Standard Library #1249
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
juliand665
wants to merge
50
commits into
viperproject:master
Choose a base branch
from
juliand665:prusti-std
base: master
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 7 commits
Commits
Show all changes
50 commits
Select commit
Hold shift + click to select a range
204d267
initial specs
juliand665 33cb190
add specs for try trait
juliand665 2e2bc12
Merge remote-tracking branch 'upstream/master' into prusti-std
juliand665 b9700a8
specify some known sizes & alignments
juliand665 1a0a6db
specify swap
juliand665 453ebc0
formatting
juliand665 7499fac
remove invalid extern spec body
juliand665 daa381f
remove unnecessary specs on pure functions
juliand665 e15681b
remove resolved TODO
juliand665 4f34396
attribute formatting
juliand665 81ae3ae
add shorthand for when size = alignment
juliand665 99afaad
specify size of usize/isize
juliand665 40f5c38
ever more formatting
juliand665 fa59ee0
Merge branch 'master' into prusti-std
juliand665 0f86c87
update specs to use merged improvements
juliand665 fe7467d
include try trait specs
juliand665 875cff7
specify binop reference forwarding
juliand665 a39354f
add compiletests for built-in specs
juliand665 9202b5d
include generic params in ghost constraint evaluation substs
juliand665 4a2a241
update tests to use included specs
juliand665 b2078f2
more spec removal
juliand665 9b3c7a1
add specs for convert::From and convert::Into
juliand665 ed66937
specify Default behavior for tuples
juliand665 ca67140
rename std tests folder
juliand665 a5558a6
minor tweaks
juliand665 0d4866d
basic slice/vec/str/string spec framework
juliand665 ac3c120
allow declaring alloc as extern crate
juliand665 2ee4286
move alloc specs to prusti-std crate
juliand665 7ae6a06
minor fixes
juliand665 4b58fae
update test to use core spec
juliand665 921c6a2
add future spec
juliand665 ae5cbc8
Merge remote-tracking branch 'upstream/master' into prusti-std
juliand665 7ad493b
minor tweaks
juliand665 252b46a
option flatten & option/result transpose
juliand665 19b6023
specify size of arrays
juliand665 d7ce8c7
conditionalize std specs
juliand665 9ca4729
add missing import
juliand665 f6df16a
add tests for clone specs
juliand665 5ddc36b
use snapshot equality for Default specs
juliand665 d23dbd5
update smir checks to allow what prusti-std needs
juliand665 077c0e0
add missing "supertrait" bounds
juliand665 0b2a7d8
comment out all #1221 specs for now
juliand665 67e96eb
fix warning
juliand665 4716233
Merge branch 'master' into prusti-std
Aurel300 b9497e3
Comment out failing test.
vakaras e9ba64e
bump versions
Aurel300 cfba140
Merge remote-tracking branch 'upstream/master' into prusti-std
Aurel300 e49dd1a
Merge remote-tracking branch 'upstream/master' into prusti-std
Aurel300 c5ba9e6
account for prusti-std in user guide
Aurel300 a1a8d22
Merge remote-tracking branch 'upstream/master' into prusti-std
Aurel300 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 was deleted.
Oops, something went wrong.
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,12 @@ | ||
| use crate::*; | ||
|
|
||
| #[extern_spec] | ||
| trait Clone { | ||
| #[ghost_constraint(Self: SnapshotEqualClone, [ | ||
| ensures(result === self), | ||
| ])] | ||
| fn clone(&self) -> Self; | ||
| } | ||
|
|
||
| /// Specifies that `Clone::clone`, if implemented, preserves snapshot equality (`===`). | ||
| pub auto trait SnapshotEqualClone {} | ||
45 changes: 45 additions & 0 deletions
45
prusti-contracts/prusti-contracts/src/core_spec/default.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,45 @@ | ||
| use crate::*; | ||
|
|
||
| #[extern_spec] | ||
| trait Default { | ||
| #[ghost_constraint(Self: Copy + PureDefault, [pure])] | ||
| fn default() -> Self; | ||
| } | ||
|
|
||
| /// Specifies that `Default::default`, if implemented, is a pure method, allowing its usage in specs. | ||
| /// | ||
| /// Does not apply to types that do not implement `Copy`, since pure methods can only involve `Copy` types. | ||
| pub auto trait PureDefault {} | ||
|
juliand665 marked this conversation as resolved.
|
||
|
|
||
| // analogous to https://github.com/rust-lang/rust/blob/872631d0f0fadffe3220ab1bd9c8f1f2342341e2/library/core/src/default.rs#L190-L202 | ||
| macro_rules! default_spec { | ||
| ($t:ty, $v:expr) => { | ||
| #[extern_spec] | ||
| impl Default for $t { | ||
| #[pure] | ||
| #[ensures(result == $v)] | ||
| fn default() -> Self; | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| default_spec! { (), () } | ||
|
juliand665 marked this conversation as resolved.
|
||
| default_spec! { bool, false } | ||
| default_spec! { char, '\x00' } | ||
|
|
||
| default_spec! { usize, 0 } | ||
| default_spec! { u8, 0 } | ||
| default_spec! { u16, 0 } | ||
| default_spec! { u32, 0 } | ||
| default_spec! { u64, 0 } | ||
| default_spec! { u128, 0 } | ||
|
|
||
| default_spec! { isize, 0 } | ||
| default_spec! { i8, 0 } | ||
| default_spec! { i16, 0 } | ||
| default_spec! { i32, 0 } | ||
| default_spec! { i64, 0 } | ||
| default_spec! { i128, 0 } | ||
|
|
||
| default_spec! { f32, 0.0f32 } | ||
| default_spec! { f64, 0.0f64 } | ||
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,65 @@ | ||
| use crate::*; | ||
|
|
||
| #[extern_spec] | ||
| mod core { | ||
| mod mem { | ||
| use crate::*; | ||
|
|
||
| #[pure] | ||
| #[ghost_constraint(T: core_spec::mem::KnownSize, [ensures(result == T::size())])] | ||
| fn size_of<T>() -> usize; | ||
|
|
||
| #[pure] | ||
| #[ghost_constraint(T: core_spec::mem::KnownSize, [ensures(result == T::align())])] | ||
| fn align_of<T>() -> usize; | ||
|
|
||
| #[ensures(*x === old(snap(y)) && *y === old(snap(x)))] | ||
| fn swap<T>(x: &mut T, y: &mut T); | ||
| } | ||
| } | ||
|
|
||
| pub trait KnownSize { | ||
| #[pure] | ||
| fn size() -> usize; | ||
|
|
||
| #[pure] | ||
| fn align() -> usize; | ||
| } | ||
|
|
||
| macro_rules! known_size_spec { | ||
| ($t:ty, $size:expr, $align:expr) => { | ||
| #[refine_trait_spec] | ||
| impl KnownSize for $t { | ||
| #[pure] | ||
| #[ensures(result == $size)] | ||
|
Aurel300 marked this conversation as resolved.
Outdated
|
||
| fn size() -> usize { | ||
| $size | ||
| } | ||
|
|
||
| #[pure] | ||
| #[ensures(result == $align)] | ||
| fn align() -> usize { | ||
| $align | ||
| } | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| known_size_spec!(bool, 1, 1); | ||
|
|
||
| known_size_spec!(i8, 1, 1); | ||
| known_size_spec!(i16, 2, 2); | ||
| known_size_spec!(i32, 4, 4); | ||
| known_size_spec!(i64, 8, 8); | ||
| known_size_spec!(i128, 16, 16); | ||
|
|
||
| known_size_spec!(u8, 1, 1); | ||
| known_size_spec!(u16, 2, 2); | ||
| known_size_spec!(u32, 4, 4); | ||
| known_size_spec!(u64, 8, 8); | ||
| known_size_spec!(u128, 16, 16); | ||
|
|
||
| known_size_spec!(f32, 4, 4); | ||
| known_size_spec!(f64, 8, 8); | ||
|
|
||
| // usize/isize are not specified exactly because they are platform-dependent and programmers should not depend on their specific values anyway. | ||
|
Aurel300 marked this conversation as resolved.
Outdated
|
||
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,11 @@ | ||
| pub mod default; | ||
| pub mod option; | ||
| pub mod result; | ||
| pub mod clone; | ||
| pub mod mem; | ||
|
|
||
| // NOTE: specs marked with FUTURE are not fully expressible yet (in a clean way). | ||
| // They are due to be revised later as features are added. | ||
|
|
||
| pub use clone::SnapshotEqualClone; | ||
| pub use default::PureDefault; |
34 changes: 34 additions & 0 deletions
34
prusti-contracts/prusti-contracts/src/core_spec/ops/try.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 @@ | ||
| use crate::*; | ||
|
|
||
| #[allow(unused_imports)] | ||
| use core::ops::*; | ||
| #[allow(unused_imports)] | ||
| use std::convert::Infallible; | ||
|
|
||
| #[extern_spec] | ||
| impl<T, E> Try for Result<T, E> { | ||
| #[ensures(result === Ok(output))] | ||
| fn from_output(output: T) -> Self; | ||
|
|
||
| #[ensures(match old(self) { | ||
| Ok(output) => result === ControlFlow::Continue(output), | ||
| Err(error) => result === ControlFlow::Break(Err(error)), | ||
| })] | ||
| fn branch(self) -> ControlFlow<Result<Infallible, E>, T>; | ||
| } | ||
|
|
||
| #[extern_spec] | ||
| impl<T> Try for Option<T> { | ||
| #[ensures(result === Some(output))] | ||
| fn from_output(output: T) -> Self; | ||
|
|
||
| #[ensures(match old(self) { | ||
| Some(output) => result === ControlFlow::Continue(output), | ||
| //None => result === ControlFlow::Break(None), | ||
| None => match result { | ||
| ControlFlow::Break(residual) => residual.is_none(), | ||
| _ => false, | ||
| }, | ||
| })] | ||
| fn branch(self) -> ControlFlow<Option<Infallible>, T>; | ||
| } |
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.