Skip to content

Commit

Permalink
Merge pull request #988 from creusot-rs/string-specs
Browse files Browse the repository at this point in the history
Basic specs for strings and str
  • Loading branch information
xldenis committed Apr 7, 2024
2 parents 1357cc9 + b3221d2 commit 6a569c9
Show file tree
Hide file tree
Showing 57 changed files with 342 additions and 288 deletions.
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@ == self@.len())]
fn len(&self) -> usize;

}
}
}
}

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

#[requires(ix@ < self@.len())]
#[ensures(result.0@.concat(result.1@) == self@)]
#[ensures(result.0@.len() == 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

0 comments on commit 6a569c9

Please sign in to comment.