Skip to content
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

Provide Specs for the Standard Library #1249

Open
wants to merge 50 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
204d267
initial specs
juliand665 Nov 24, 2022
33cb190
add specs for try trait
juliand665 Nov 24, 2022
2e2bc12
Merge remote-tracking branch 'upstream/master' into prusti-std
juliand665 Nov 24, 2022
b9700a8
specify some known sizes & alignments
juliand665 Nov 24, 2022
1a0a6db
specify swap
juliand665 Nov 24, 2022
453ebc0
formatting
juliand665 Nov 24, 2022
7499fac
remove invalid extern spec body
juliand665 Nov 24, 2022
daa381f
remove unnecessary specs on pure functions
juliand665 Nov 24, 2022
e15681b
remove resolved TODO
juliand665 Nov 24, 2022
4f34396
attribute formatting
juliand665 Nov 25, 2022
81ae3ae
add shorthand for when size = alignment
juliand665 Nov 25, 2022
99afaad
specify size of usize/isize
juliand665 Nov 25, 2022
40f5c38
ever more formatting
juliand665 Nov 25, 2022
fa59ee0
Merge branch 'master' into prusti-std
juliand665 Jan 16, 2023
0f86c87
update specs to use merged improvements
juliand665 Jan 16, 2023
fe7467d
include try trait specs
juliand665 Jan 16, 2023
875cff7
specify binop reference forwarding
juliand665 Jan 16, 2023
a39354f
add compiletests for built-in specs
juliand665 Jan 16, 2023
9202b5d
include generic params in ghost constraint evaluation substs
juliand665 Jan 21, 2023
4a2a241
update tests to use included specs
juliand665 Jan 22, 2023
b2078f2
more spec removal
juliand665 Jan 22, 2023
9b3c7a1
add specs for convert::From and convert::Into
juliand665 Jan 23, 2023
ed66937
specify Default behavior for tuples
juliand665 Jan 23, 2023
ca67140
rename std tests folder
juliand665 Jan 23, 2023
a5558a6
minor tweaks
juliand665 Jan 23, 2023
0d4866d
basic slice/vec/str/string spec framework
juliand665 Jan 25, 2023
ac3c120
allow declaring alloc as extern crate
juliand665 Jan 25, 2023
2ee4286
move alloc specs to prusti-std crate
juliand665 Jan 26, 2023
7ae6a06
minor fixes
juliand665 Jan 26, 2023
4b58fae
update test to use core spec
juliand665 Jan 26, 2023
921c6a2
add future spec
juliand665 Jan 26, 2023
ae5cbc8
Merge remote-tracking branch 'upstream/master' into prusti-std
juliand665 Jan 26, 2023
7ad493b
minor tweaks
juliand665 Jan 30, 2023
252b46a
option flatten & option/result transpose
juliand665 Jan 30, 2023
19b6023
specify size of arrays
juliand665 Feb 2, 2023
d7ce8c7
conditionalize std specs
juliand665 Feb 3, 2023
9ca4729
add missing import
juliand665 Feb 4, 2023
f6df16a
add tests for clone specs
juliand665 Feb 4, 2023
5ddc36b
use snapshot equality for Default specs
juliand665 Feb 4, 2023
d23dbd5
update smir checks to allow what prusti-std needs
juliand665 Feb 4, 2023
077c0e0
add missing "supertrait" bounds
juliand665 Feb 15, 2023
0b2a7d8
comment out all #1221 specs for now
juliand665 Feb 15, 2023
67e96eb
fix warning
juliand665 Mar 6, 2023
4716233
Merge branch 'master' into prusti-std
Aurel300 Apr 3, 2023
b9497e3
Comment out failing test.
vakaras May 4, 2023
e9ba64e
bump versions
Aurel300 May 5, 2023
cfba140
Merge remote-tracking branch 'upstream/master' into prusti-std
Aurel300 May 5, 2023
e49dd1a
Merge remote-tracking branch 'upstream/master' into prusti-std
Aurel300 Aug 18, 2023
c5ba9e6
account for prusti-std in user guide
Aurel300 Sep 4, 2023
a1a8d22
Merge remote-tracking branch 'upstream/master' into prusti-std
Aurel300 Sep 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 0 additions & 18 deletions prusti-contracts/prusti-contracts/src/core_spec.rs

This file was deleted.

12 changes: 12 additions & 0 deletions prusti-contracts/prusti-contracts/src/core_spec/clone.rs
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),
])]
Aurel300 marked this conversation as resolved.
Show resolved Hide resolved
fn clone(&self) -> Self;
}

/// Specifies that `Clone::clone`, if implemented, preserves snapshot equality (`===`).
pub auto trait SnapshotEqualClone {}
47 changes: 47 additions & 0 deletions prusti-contracts/prusti-contracts/src/core_spec/default.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
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.
Show resolved Hide 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 {
$v
}
Aurel300 marked this conversation as resolved.
Show resolved Hide resolved
}
};
}

default_spec! { (), () }
juliand665 marked this conversation as resolved.
Show resolved Hide 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 }
65 changes: 65 additions & 0 deletions prusti-contracts/prusti-contracts/src/core_spec/mem.rs
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);
Aurel300 marked this conversation as resolved.
Show resolved Hide resolved
}
}

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.
Show resolved Hide resolved
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.
Show resolved Hide resolved
11 changes: 11 additions & 0 deletions prusti-contracts/prusti-contracts/src/core_spec/mod.rs
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 prusti-contracts/prusti-contracts/src/core_spec/ops/try.rs
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>;
}
Loading