While run-time contract programming is sometimes nice to have, the ability to prove at compile-time the correctness of contracts and specifications is in my opinion a game changer, despite you may want to use it only for important library code (standard library, foundational crates) or critical user functions (important infrastructure software, the kind of code Rust was meant for).
Having ways to prove some indexes can’t go past array bounds or can’t overflow, or some unwraps can’t fail is going to also improve the performance (as long as rustc uses the proof results to optimize better) and not just correctness.
For the correctness part the rustc plugin (like Clippy) discussed here is promising, “Leveraging rust types for modular specification and verification” by Vytautas Astrauskas et al:
https://www.research-collection.ethz.ch/handle/20.500.11850/311092
The syntax shown in that paper currently is:
#[ensures="p.x == old(p.x) + s"]
#[ensures="p.y == old(p.y)"]
fn shift_x(p: &mut Point, s: i32) {
p.x = p.x + s
}
A more complex example:
#[requires="0 <= n && n < length(r)"]
#[ensures="length(r) == old(length(r))"]
#[ensures="get_nth_x(r, n) == old(get_nth_x(r, n)) + s"]
#[ensures="forall i: i32 ::
(0<=i && i<length(r) && i != n) ==>
get_nth_x(r, i) == old(get_nth_x(r, i))"]
fn shift_nth_x(r: &mut Route, n: i32, s: i32) {
let p = borrow_nth(r, n);
shift_x(p,s);
}
To improve the readability, improve code aesthetics, and allow syntax highlighting of the contracts and proofs, I’d like a syntax like (like in Why3 language):
fn shift_x(p: &mut Point, s: i32) {
ensures { p.x == old(p.x) + s }
ensures { p.y == old(p.y) }
p.x = p.x + s
}
To allow that we can add some new syntax to Rust:
requires {}
ensures {}
old()
forall i: T :: p1 ==> p2
And little more for explicit proofs, integral intervals, etc.
They have added such syntax to Ada 2012 for similar purposes. Given the correctness purposes of Rust I think this added complexity is worth having (but not right away, putting the contracts and proofs in #[] is OK at the beginning. Ada did the same thing, the contracts were written inside comments before Ada2012).