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

Basic specs for strings and str #988

Merged
merged 1 commit into from
Apr 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
22 changes: 22 additions & 0 deletions creusot-contracts/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,17 @@ impl<T> ShallowModel for Rc<T> {
}
}

impl ShallowModel for str {
type ShallowModelTy = Seq<char>;

#[logic]
#[open]
#[trusted]
fn shallow_model(self) -> Self::ShallowModelTy {
pearlite! { absurd }
}
}

impl<T: DeepModel> DeepModel for Arc<T> {
type DeepModelTy = T::DeepModelTy;
#[logic]
Expand Down Expand Up @@ -107,3 +118,14 @@ impl DeepModel for bool {
self
}
}

impl ShallowModel for String {
type ShallowModelTy = Seq<char>;

#[logic]
#[open(self)]
#[trusted]
fn shallow_model(self) -> Self::ShallowModelTy {
pearlite! { absurd }
}
}
1 change: 1 addition & 0 deletions creusot-contracts/src/std.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pub mod ops;
pub mod option;
pub mod result;
pub mod slice;
pub mod string;
pub mod time;
mod tuples;
pub mod vec;
31 changes: 31 additions & 0 deletions creusot-contracts/src/std/string.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
use crate::*;
use ::std::ops::Deref;

extern_spec! {
mod std {
mod string {
impl Deref for String {
#[ensures(result@ == self@)]
fn deref(&self) -> &str;
}

impl String {
#[ensures(result@ == [email protected]())]
fn len(&self) -> usize;

}
}
}
}

extern_spec! {
impl str {
#[ensures(result@ == self@)]
fn to_string(&self) -> String;

#[requires(ix@ < [email protected]())]
#[ensures([email protected](result.1@) == self@)]
#[ensures([email protected]() == ix@)]
fn split_at(&self, ix : usize) -> (&str, &str);
}
}
2 changes: 1 addition & 1 deletion creusot/tests/should_fail/bug/01_resolve_unsoundness.mlcfg
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ module C01ResolveUnsoundness_MakeVecOfSize
function shallow_model2 (self : borrowed (Alloc_Vec_Vec_Type.t_vec bool (Alloc_Alloc_Global_Type.t_global))) : Seq.seq bool

=
[#"../../../../../creusot-contracts/src/model.rs" 97 8 97 31] shallow_model0 ( * self)
[#"../../../../../creusot-contracts/src/model.rs" 108 8 108 31] shallow_model0 ( * self)
val shallow_model2 (self : borrowed (Alloc_Vec_Vec_Type.t_vec bool (Alloc_Alloc_Global_Type.t_global))) : Seq.seq bool
ensures { result = shallow_model2 self }

Expand Down
2 changes: 1 addition & 1 deletion creusot/tests/should_fail/final_borrows.mlcfg
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ module FinalBorrows_Indexing
ensures { result = index_logic1 self ix }

function shallow_model0 (self : borrowed (slice t)) : Seq.seq t =
[#"../../../../creusot-contracts/src/model.rs" 97 8 97 31] shallow_model1 ( * self)
[#"../../../../creusot-contracts/src/model.rs" 108 8 108 31] shallow_model1 ( * self)
val shallow_model0 (self : borrowed (slice t)) : Seq.seq t
ensures { result = shallow_model0 self }

Expand Down
4 changes: 2 additions & 2 deletions creusot/tests/should_succeed/100doors.mlcfg
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ module C100doors_F
function shallow_model3 (self : borrowed (Alloc_Vec_Vec_Type.t_vec bool (Alloc_Alloc_Global_Type.t_global))) : Seq.seq bool

=
[#"../../../../creusot-contracts/src/model.rs" 97 8 97 31] shallow_model0 ( * self)
[#"../../../../creusot-contracts/src/model.rs" 108 8 108 31] shallow_model0 ( * self)
val shallow_model3 (self : borrowed (Alloc_Vec_Vec_Type.t_vec bool (Alloc_Alloc_Global_Type.t_global))) : Seq.seq bool
ensures { result = shallow_model3 self }

Expand All @@ -314,7 +314,7 @@ module C100doors_F
ensures { inv10 result }

function shallow_model2 (self : Alloc_Vec_Vec_Type.t_vec bool (Alloc_Alloc_Global_Type.t_global)) : Seq.seq bool =
[#"../../../../creusot-contracts/src/model.rs" 79 8 79 31] shallow_model0 self
[#"../../../../creusot-contracts/src/model.rs" 90 8 90 31] shallow_model0 self
val shallow_model2 (self : Alloc_Vec_Vec_Type.t_vec bool (Alloc_Alloc_Global_Type.t_global)) : Seq.seq bool
ensures { result = shallow_model2 self }

Expand Down
Loading
Loading