Skip to content

Commit edcb837

Browse files
committed
Fix rustc build and test parallel_add example
1 parent 3059031 commit edcb837

5 files changed

Lines changed: 29 additions & 15 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ creusot-std = "0.9.0"
88

99
[features]
1010
solutions = []
11+
nightly = []
1112

1213
[lints.rust]
1314
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(creusot)'] }

src/ex0_examples.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ pub fn sum_slice(xs: &[u64]) -> u64 {
7676
sum_slice_lemma(xs);
7777
let _ = xs
7878
.iter()
79-
.map_inv(|x, produced| {
80-
proof_assert! { sum@ + x@ == sum_seq(xs@[0..produced.len() + 1]) };
79+
.map_inv(|x, _produced| {
80+
proof_assert! { sum@ + x@ == sum_seq(xs@[0.._produced.len() + 1]) };
8181
sum += *x;
8282
})
8383
.collect::<()>();
@@ -104,6 +104,7 @@ pub fn sum_seq(xs: Seq<u64>) -> Int {
104104
#[ensures(forall<i> 0 <= i && i < xs@.len() ==> xs@[0..i+1][0..i] == xs@[0..i])]
105105
#[ensures(xs@[0..xs@.len()] == xs@)]
106106
pub fn sum_slice_lemma(xs: &[u64]) {
107+
_ = xs;
107108
let _ = snapshot! { sum_seq_sub(xs@) };
108109
}
109110

@@ -210,8 +211,8 @@ pub fn interior_mut() {
210211
let cell = UnsafeCell::new(0); // `PermCell::new` will return a cell and a permission
211212
let (b1, b2) = (&cell, &cell); // Share the cell (this line won't change)
212213
*&mut *b1.get() = 1; // Replace this with `PermCell::set` or `PermCell::borrow_mut` to write to it
213-
let result = *&*b2.get(); // Replace this with `PermCell::get` or `PermCell::borrow` to read from it
214-
proof_assert! { result == 1i32 };
214+
let _result = *&*b2.get(); // Replace this with `PermCell::get` or `PermCell::borrow` to read from it
215+
proof_assert! { _result == 1i32 };
215216
}
216217
}
217218

src/ex3_parallel_add.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ use std::thread;
2626
// Spawn two threads that add 2 to a shared atomic variable.
2727
// Prove that the final value is 4.
2828
#[trusted]
29-
pub fn parallel_add() {
29+
#[ensures(result == 4i32)]
30+
pub fn parallel_add() -> i32 {
3031
let atomic = AtomicI32::new(0);
3132

3233
thread::scope(|s| {
@@ -44,6 +45,10 @@ pub fn parallel_add() {
4445
let _ = t2.join().unwrap();
4546
});
4647

47-
let n = atomic.into_inner(); // Non-atomically read the atomic
48-
proof_assert!(n == 4i32)
48+
atomic.into_inner() // Non-atomically read the atomic
49+
}
50+
51+
#[test]
52+
fn test() {
53+
assert! { parallel_add() == 4 }
4954
}

src/solutions/ex0_examples.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,8 @@ pub fn sum_slice(xs: &[u64]) -> u64 {
7373
sum_slice_lemma(xs);
7474
let _ = xs
7575
.iter()
76-
.map_inv(|x, produced| {
77-
proof_assert! { sum@ + x@ == sum_seq(xs@[0..produced.len() + 1]) };
76+
.map_inv(|x, _produced| {
77+
proof_assert! { sum@ + x@ == sum_seq(xs@[0.._produced.len() + 1]) };
7878
sum += *x;
7979
})
8080
.collect::<()>();
@@ -86,6 +86,7 @@ pub fn sum_slice(xs: &[u64]) -> u64 {
8686
#[ensures(forall<i> 0 <= i && i < xs@.len() ==> xs@[0..i+1][0..i] == xs@[0..i])]
8787
#[ensures(xs@[0..xs@.len()] == xs@)]
8888
pub fn sum_slice_lemma(xs: &[u64]) {
89+
_ = xs;
8990
let _ = snapshot! { sum_seq_sub(xs@) };
9091
}
9192

@@ -182,8 +183,8 @@ pub fn interior_mut() {
182183
let (cell, mut perm) = PermCell::new(0);
183184
let (b1, b2) = (&cell, &cell);
184185
b1.set(ghost! { &mut **perm }, 1);
185-
let result = b2.take(ghost! { &mut **perm });
186-
proof_assert! { result == 1i32 };
186+
let _result = b2.take(ghost! { &mut **perm });
187+
proof_assert! { _result == 1i32 };
187188
}
188189
}
189190

src/solutions/ex3_parallel_add.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,9 @@ impl Protocol for ParallelAddAtomicInv {
3535
}
3636
}
3737

38-
#[requires(tokens.contains(PARALLEL_ADD()))]
39-
pub fn parallel_add(mut tokens: Ghost<Tokens>) {
38+
#[cfg(feature = "nightly")]
39+
#[ensures(result == 4i32)]
40+
pub fn parallel_add() -> i32 {
4041
let (atomic, own) = AtomicI32::new(0);
4142

4243
// Create our ghost state
@@ -104,6 +105,11 @@ pub fn parallel_add(mut tokens: Ghost<Tokens>) {
104105
inv.auth2.frag_lemma(&frag2);
105106
inv.own
106107
};
107-
let n = atomic.into_inner(own); // Non-atomically read the atomic
108-
proof_assert!(n == 4i32)
108+
atomic.into_inner(own) // Non-atomically read the atomic
109+
}
110+
111+
#[cfg(feature = "nightly")]
112+
#[test]
113+
fn test() {
114+
assert! { parallel_add() == 4 }
109115
}

0 commit comments

Comments
 (0)