Something went wrong. Try again.
forked from verus-lang/verus
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373// rust_verify/tests/example.rs expect-warnings#[allow(unused_imports)]use verus_builtin::*;#[allow(unused_imports)]use verus_builtin_macros::*;
verus! {
// ANCHOR: fun_modesspec fn f1(x: int) -> int { x / 2}
proof fn f2(x: int) -> int { x / 2}
// "exec" is optional, and is usually omittedexec fn f3(x: u64) -> u64 { x / 2}
// ANCHOR_END: fun_modes/*// ANCHOR: fun_modes2fn f3(x: u64) -> u64 { x / 2 } // exec function// ANCHOR_END: fun_modes2*/
/*// ANCHOR: spec_fun1spec fn min(x: int, y: int) -> int { if x <= y { x } else { y }}
fn test() { assert(min(10, 20) == 10); // succeeds assert(min(100, 200) == 100); // succeeds}// ANCHOR_END: spec_fun1*/
// ANCHOR: spec_fun3spec fn min(x: int, y: int) -> int { if x <= y { x } else { y }}
spec fn min3(x: int, y: int, z: int) -> int { min(x, min(y, z))}
fn compute_min3(x: u64, y: u64, z: u64) -> (m: u64) ensures m == min3(x as int, y as int, z as int),{ let mut m = x; if y < m { m = y; } if z < m { m = z; } m}
fn test() { let m = compute_min3(10, 20, 30); assert(m == 10);}
// ANCHOR_END: spec_fun3/*// ANCHOR: spec_fun_mod1mod M1 { use verus_builtin::*;
pub open spec fn min(x: int, y: int) -> int { if x <= y { x } else { y } }}
mod M2 { use verus_builtin::*; use crate::M1::*;
fn test() { assert(min(10, 20) == 10); // succeeds }}// ANCHOR_END: spec_fun_mod1
// ANCHOR: spec_fun_mod2mod M1 { use verus_builtin::*;
pub closed spec fn min(x: int, y: int) -> int { if x <= y { x } else { y } }
pub proof fn lemma_min(x: int, y: int) ensures min(x,y) <= x && min(x,y) <= y, {}}
mod M2 { use verus_builtin::*; use crate::M1::*;
fn test() { assert(min(10, 20) == min(10, 20)); // succeeds assert(min(10, 20) == 10); // FAILS proof { lemma_min(10,20); } assert(min(10, 20) <= 10); // succeeds }}// ANCHOR_END: spec_fun_mod2*/
/*// ANCHOR: spec_fun_proofmod M1 { use verus_builtin::*;
pub closed spec fn min(x: int, y: int) -> int { if x <= y { x } else { y } }
pub proof fn lemma_min(x: int, y: int) ensures min(x, y) <= x, min(x, y) <= y, min(x, y) == x || min(x, y) == y, { }}
mod M2 { use verus_builtin::*; use crate::M1::*;
proof fn test() { lemma_min(10, 20); assert(min(10, 20) == 10); // succeeds assert(min(100, 200) == 100); // FAILS }}// ANCHOR_END: spec_fun_proof*/
// ANCHOR: spec_fun_proof_block1fn test_consts_infer() { let u: u8 = 1; proof { let i: int = 2; let n: nat = 3; assert(0 <= u < i < n < 4); }}
// ANCHOR_END: spec_fun_proof_block1// ANCHOR: spec_fun_proof_block2mod M1 { use verus_builtin::*;
pub closed spec fn min(x: int, y: int) -> int { if x <= y { x } else { y } }
pub proof fn lemma_min(x: int, y: int) ensures min(x, y) <= x, min(x, y) <= y, min(x, y) == x || min(x, y) == y, { }
}
mod M2 { use verus_builtin::*; use crate::M1::*;
fn test() { proof { lemma_min(10, 20); lemma_min(100, 200); } assert(min(10, 20) == 10); // succeeds assert(min(100, 200) == 100); // succeeds }
}
// ANCHOR_END: spec_fun_proof_block2/*// ANCHOR: assert_bymod M1 { use verus_builtin::*;
pub closed spec fn min(x: int, y: int) -> int { if x <= y { x } else { y } }
pub proof fn lemma_min(x: int, y: int) ensures min(x, y) <= x, min(x, y) <= y, min(x, y) == x || min(x, y) == y, { }}
mod M2 { use verus_builtin::*; use crate::M1::*;
fn test() { assert(min(10, 20) == 10) by { lemma_min(10, 20); lemma_min(100, 200); } assert(min(10, 20) == 10); // succeeds assert(min(100, 200) == 100); // FAILS }}// ANCHOR_END: assert_by*/
/*// ANCHOR: determinismmod M1 { use verus_builtin::*;
pub closed spec fn s(i: int) -> int { i + 1 }
pub proof fn p(i: int) -> int { i + 1 }}
mod M2 { use verus_builtin::*; use crate::M1::*;
proof fn test_determinism() { let s1 = s(10); let s2 = s(10); assert(s1 == s2); // succeeds
let p1 = p(10); let p2 = p(10); assert(p1 == p2); // FAILS }}// ANCHOR_END: determinism*/
// ANCHOR: recommends1spec fn f(i: nat) -> nat recommends i > 0,{ (i - 1) as nat}
proof fn test1() { assert(f(0) == f(0)); // succeeds}
// ANCHOR_END: recommends1/*// ANCHOR: recommends2proof fn test2() { assert(f(0) <= f(1)); // FAILS}// ANCHOR_END: recommends2*/
// ANCHOR: recommends3spec fn caller1() -> nat { f(0) // no note, warning, or error generated
}
// ANCHOR_END: recommends3// ANCHOR: recommends4spec(checked) fn caller2() -> nat { f(0) // generates a warning because of "(checked)"
}
// ANCHOR_END: recommends4/*// ANCHOR: ghost_abilities0fn divide_by_zero() { let x: u8 = 1; assert(x / 0 == x / 0); // succeeds in ghost code let y = x / 0; // FAILS in exec code}// ANCHOR_END: ghost_abilities0*/
// ANCHOR: ghost_abilities1mod MA { // does not implement Copy // does not allow construction by other modules pub struct S { private_field: u8, }
}
mod MB { use verus_builtin::*; use crate::MA::*;
// construct a ghost S spec fn make_S() -> S;
// duplicate an S spec fn duplicate_S(s: S) -> (S, S) { (s, s) }
}
// ANCHOR_END: ghost_abilities1/*// ANCHOR: ghost_abilities2fn test(s: S) { let pair = duplicate_S(s); // FAILS}// ANCHOR_END: ghost_abilities2*/
fn main() {}
} // verus!