How to give formal proofs by hand? Is there a language specification, as formal as formal logic, that we can build our proof on?
For example, how do I know if this module panic
s?
mod my_mod {
use std::marker::PhantomData;
#[derive(Clone, Copy)]
pub struct A<'id> {
p: PhantomData<*mut &'id u8>,
data: u32,
}
pub fn scoped<T>(data: u32, f: impl for<'id> FnOnce(A<'id>) -> T) -> T {
f(A {p: PhantomData::default(), data})
}
impl<'id> PartialEq for A<'id> {
fn eq(&self, other: &Self) -> bool {
// Can we prove that this assertion will never failed whatever code is using this mod?
assert_eq!(self.data, other.data);
// some UB stuff if the assertion failed here
true
}
}
}
The intuition behind this very specific example is to give type level marks (as a lifetime generic) on the variables, so that we may guarantee two A<'id>
's are always copied from a same source.